comparison test/callback_suite_aggrs/globals.c @ 523:cd46e111bc4c

- new test/callback_suite_aggrs (currently ahead of checked-in dyncall code and won't compile, though, as with call_suite_aggrs; dc code will be checked in, soon)
author Tassilo Philipp
date Wed, 13 Apr 2022 14:59:57 +0200
parents
children a6d00ee46731
comparison
equal deleted inserted replaced
522:f7fec6699e21 523:cd46e111bc4c
1 /*
2
3 Package: dyncall
4 Library: test
5 File: test/callback_suite_aggrs/globals.c
6 Description:
7 License:
8
9 Copyright (c) 2022 Tassilo Philipp <tphilipp@potion-studios.com>
10
11 Permission to use, copy, modify, and distribute this software for any
12 purpose with or without fee is hereby granted, provided that the above
13 copyright notice and this permission notice appear in all copies.
14
15 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23 */
24
25 #include <stdlib.h>
26 #include "globals.h"
27 #include <float.h>
28 #include <string.h>
29
30 #define X(CH,T) T *V_##CH; T *K_##CH;
31 DEF_TYPES
32 #undef X
33
34 #define AGGR_MISALIGN 1
35
36 static double rand_d() { return ( ( (double) rand() ) / ( (double) RAND_MAX ) ); }
37
38 /* fill mem with random values, make sure no float aligned memory location
39 * results in a NaN, as they always compare to false; so avaid all ones in
40 * exporent (for simplicity we just look at first 7 exponent bits and make sure
41 * they aren't all set, which would work for all IEEE754 precision formats) */
42 static void rand_mem__fp_friendly(void* p, size_t s)
43 {
44 int i;
45 for(i = 0; i<s; ++i) {
46 char* c = (char*)p;
47 c[i] = (char)rand(); /* slowish, byte by byte, but whatev */
48 if((c[i]&0x7f) == 0x7f)
49 c[i] ^= 1;
50 }
51 }
52
53 int get_max_aggr_size()
54 {
55 static int s = 0;
56 int i;
57 if(s == 0) {
58 for(i=0; i<G_naggs; ++i)
59 if(G_agg_sizes[i] > s)
60 s = G_agg_sizes[i];
61 }
62 return s;
63 }
64
65 void init_test_data()
66 {
67 int i;
68 int maxaggrsize = get_max_aggr_size();
69 #define X(CH,T) V_##CH = (T*) malloc(sizeof(T)*(G_maxargs+1)); K_##CH = (T*) malloc(sizeof(T)*(G_maxargs+1));
70 DEF_TYPES
71 #undef X
72
73 for(i=0;i<G_maxargs+1;++i) {
74 K_B[i] = (DCbool) ((int)rand_d() & 1);
75 K_c[i] = (char) (((rand_d()-0.5)*2) * (1<<7));
76 K_s[i] = (short) (((rand_d()-0.5)*2) * (1<<(sizeof(short)*8-1)));
77 K_i[i] = (int) (((rand_d()-0.5)*2) * (1<<(sizeof(int)*8-2)));
78 K_j[i] = (long) (((rand_d()-0.5)*2) * (1L<<(sizeof(long)*8-2)));
79 K_l[i] = (long long) (((rand_d()-0.5)*2) * (1LL<<(sizeof(long long)*8-2)));
80 K_C[i] = (unsigned char) (((rand_d()-0.5)*2) * (1<<7));
81 K_S[i] = (unsigned short) (((rand_d()-0.5)*2) * (1<<(sizeof(short)*8-1)));
82 K_I[i] = (unsigned int) (((rand_d()-0.5)*2) * (1<<(sizeof(int)*8-2)));
83 K_J[i] = (unsigned long) (((rand_d()-0.5)*2) * (1L<<(sizeof(long)*8-2)));
84 K_L[i] = (unsigned long long)(((rand_d()-0.5)*2) * (1LL<<(sizeof(long long)*8-2)));
85 K_p[i] = (void*)(long) (((rand_d()-0.5)*2) * (1LL<<(sizeof(void*)*8-1)));
86 K_f[i] = (float) (rand_d() * FLT_MAX);
87 K_d[i] = (double) (((rand_d()-0.5)*2) * DBL_MAX);
88 K_a[i] = malloc(maxaggrsize+AGGR_MISALIGN);
89 rand_mem__fp_friendly(K_a[i], maxaggrsize+AGGR_MISALIGN);
90 K_a[i] = (char*)K_a[i]+AGGR_MISALIGN;
91 }
92 }
93
94 void clear_V()
95 {
96 static int aggr_init = 0;
97 int maxaggrsize = get_max_aggr_size();
98
99 int i;
100 for(i=0;i<G_maxargs+1;++i) {
101 if(aggr_init)
102 free((char*)V_a[i]-AGGR_MISALIGN);
103 #define X(CH,T) V_##CH[i] = (T) 0;
104 DEF_TYPES
105 #undef X
106 V_a[i] = malloc(maxaggrsize+AGGR_MISALIGN);
107 memset(V_a[i], 0, maxaggrsize+AGGR_MISALIGN);
108 V_a[i] = (char*)V_a[i]+AGGR_MISALIGN;
109 }
110 aggr_init = 1;
111 }
112
113 void deinit_test_data()
114 {
115 int i;
116 for(i=0;i<G_maxargs+1;++i) {
117 free((char*)V_a[i]-AGGR_MISALIGN);
118 free((char*)K_a[i]-AGGR_MISALIGN);
119 }
120
121 #define X(CH,T) free(V_##CH); free(K_##CH);
122 DEF_TYPES
123 #undef X
124 }
125