pixman-accessors.h: Delete unused macros
[profile/ivi/pixman.git] / test / utils.h
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4
5 #include <assert.h>
6 #include "pixman-private.h" /* For 'inline' definition */
7
8 #define ARRAY_LENGTH(A) ((int) (sizeof (A) / sizeof ((A) [0])))
9
10 /* A primitive pseudorandom number generator,
11  * taken from POSIX.1-2001 example
12  */
13
14 extern uint32_t lcg_seed;
15 #ifdef USE_OPENMP
16 #pragma omp threadprivate(lcg_seed)
17 #endif
18
19 static inline uint32_t
20 lcg_rand (void)
21 {
22     lcg_seed = lcg_seed * 1103515245 + 12345;
23     return ((uint32_t)(lcg_seed / 65536) % 32768);
24 }
25
26 static inline void
27 lcg_srand (uint32_t seed)
28 {
29     lcg_seed = seed;
30 }
31
32 static inline uint32_t
33 lcg_rand_n (int max)
34 {
35     return lcg_rand () % max;
36 }
37
38 static inline uint32_t
39 lcg_rand_N (int max)
40 {
41     uint32_t lo = lcg_rand ();
42     uint32_t hi = lcg_rand () << 15;
43     return (lo | hi) % max;
44 }
45
46 static inline uint32_t
47 lcg_rand_u32 (void)
48 {
49     /* This uses the 10/11 most significant bits from the 3 lcg results
50      * (and mixes them with the low from the adjacent one).
51      */
52     uint32_t lo = lcg_rand() >> -(32 - 15 - 11 * 2);
53     uint32_t mid = lcg_rand() << (32 - 15 - 11 * 1);
54     uint32_t hi = lcg_rand() << (32 - 15 - 11 * 0);
55
56     return (hi ^ mid ^ lo);
57 }
58
59 /* CRC 32 computation
60  */
61 uint32_t
62 compute_crc32 (uint32_t    in_crc32,
63                const void *buf,
64                size_t      buf_len);
65
66 /* Returns TRUE if running on a little endian system */
67 pixman_bool_t
68 is_little_endian (void);
69
70 /* perform endian conversion of pixel data
71  */
72 void
73 image_endian_swap (pixman_image_t *img);
74
75 /* Allocate memory that is bounded by protected pages,
76  * so that out-of-bounds access will cause segfaults
77  */
78 void *
79 fence_malloc (int64_t len);
80
81 void
82 fence_free (void *data);
83
84 /* Generate n_bytes random bytes in fence_malloced memory */
85 uint8_t *
86 make_random_bytes (int n_bytes);
87
88 /* Return current time in seconds */
89 double
90 gettime (void);
91
92 uint32_t
93 get_random_seed (void);
94
95 /* main body of the fuzzer test */
96 int
97 fuzzer_test_main (const char *test_name,
98                   int         default_number_of_iterations,
99                   uint32_t    expected_checksum,
100                   uint32_t    (*test_function)(int testnum, int verbose),
101                   int         argc,
102                   const char *argv[]);
103
104 void
105 fail_after (int seconds, const char *msg);
106
107 /* If possible, enable traps for floating point exceptions */
108 void enable_fp_exceptions(void);
109
110 pixman_bool_t
111 write_png (pixman_image_t *image, const char *filename);
112
113 /* A pair of macros which can help to detect corruption of
114  * floating point registers after a function call. This may
115  * happen if _mm_empty() call is forgotten in MMX/SSE2 fast
116  * path code, or ARM NEON assembly optimized function forgets
117  * to save/restore d8-d15 registers before use.
118  */
119
120 #define FLOAT_REGS_CORRUPTION_DETECTOR_START()                 \
121     static volatile double frcd_volatile_constant1 = 123451;   \
122     static volatile double frcd_volatile_constant2 = 123452;   \
123     static volatile double frcd_volatile_constant3 = 123453;   \
124     static volatile double frcd_volatile_constant4 = 123454;   \
125     static volatile double frcd_volatile_constant5 = 123455;   \
126     static volatile double frcd_volatile_constant6 = 123456;   \
127     static volatile double frcd_volatile_constant7 = 123457;   \
128     static volatile double frcd_volatile_constant8 = 123458;   \
129     double frcd_canary_variable1 = frcd_volatile_constant1;    \
130     double frcd_canary_variable2 = frcd_volatile_constant2;    \
131     double frcd_canary_variable3 = frcd_volatile_constant3;    \
132     double frcd_canary_variable4 = frcd_volatile_constant4;    \
133     double frcd_canary_variable5 = frcd_volatile_constant5;    \
134     double frcd_canary_variable6 = frcd_volatile_constant6;    \
135     double frcd_canary_variable7 = frcd_volatile_constant7;    \
136     double frcd_canary_variable8 = frcd_volatile_constant8;
137
138 #define FLOAT_REGS_CORRUPTION_DETECTOR_FINISH()                \
139     assert (frcd_canary_variable1 == frcd_volatile_constant1); \
140     assert (frcd_canary_variable2 == frcd_volatile_constant2); \
141     assert (frcd_canary_variable3 == frcd_volatile_constant3); \
142     assert (frcd_canary_variable4 == frcd_volatile_constant4); \
143     assert (frcd_canary_variable5 == frcd_volatile_constant5); \
144     assert (frcd_canary_variable6 == frcd_volatile_constant6); \
145     assert (frcd_canary_variable7 == frcd_volatile_constant7); \
146     assert (frcd_canary_variable8 == frcd_volatile_constant8);
147
148 /* Try to get an aligned memory chunk */
149 void *
150 aligned_malloc (size_t align, size_t size);
151
152 void
153 initialize_palette (pixman_indexed_t *palette, uint32_t depth, int is_rgb);
154
155 typedef struct
156 {
157     double r, g, b, a;
158 } color_t;
159
160 void
161 round_color (pixman_format_code_t format, color_t *color);
162
163 typedef struct
164 {
165     pixman_format_code_t format;
166     uint32_t am, rm, gm, bm;
167     uint32_t as, rs, gs, bs;
168     uint32_t aw, rw, gw, bw;
169 } pixel_checker_t;
170
171 void
172 pixel_checker_init (pixel_checker_t *checker, pixman_format_code_t format);
173
174 void
175 pixel_checker_split_pixel (const pixel_checker_t *checker, uint32_t pixel,
176                            int *a, int *r, int *g, int *b);
177
178 void
179 pixel_checker_get_max (const pixel_checker_t *checker, color_t *color,
180                        int *a, int *r, int *g, int *b);
181
182 void
183 pixel_checker_get_min (const pixel_checker_t *checker, color_t *color,
184                        int *a, int *r, int *g, int *b);
185
186 pixman_bool_t
187 pixel_checker_check (const pixel_checker_t *checker,
188                      uint32_t pixel, color_t *color);