mmx: add x8f8g8b8 fetcher
[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 /* Converts a8r8g8b8 pixels to pixels that
111  *  - are not premultiplied,
112  *  - are stored in this order in memory: R, G, B, A, regardless of
113  *    the endianness of the computer.
114  * It is allowed for @src and @dst to point to the same memory buffer.
115  */
116 void
117 a8r8g8b8_to_rgba_np (uint32_t *dst, uint32_t *src, int n_pixels);
118
119 pixman_bool_t
120 write_png (pixman_image_t *image, const char *filename);
121
122 /* A pair of macros which can help to detect corruption of
123  * floating point registers after a function call. This may
124  * happen if _mm_empty() call is forgotten in MMX/SSE2 fast
125  * path code, or ARM NEON assembly optimized function forgets
126  * to save/restore d8-d15 registers before use.
127  */
128
129 #define FLOAT_REGS_CORRUPTION_DETECTOR_START()                 \
130     static volatile double frcd_volatile_constant1 = 123451;   \
131     static volatile double frcd_volatile_constant2 = 123452;   \
132     static volatile double frcd_volatile_constant3 = 123453;   \
133     static volatile double frcd_volatile_constant4 = 123454;   \
134     static volatile double frcd_volatile_constant5 = 123455;   \
135     static volatile double frcd_volatile_constant6 = 123456;   \
136     static volatile double frcd_volatile_constant7 = 123457;   \
137     static volatile double frcd_volatile_constant8 = 123458;   \
138     double frcd_canary_variable1 = frcd_volatile_constant1;    \
139     double frcd_canary_variable2 = frcd_volatile_constant2;    \
140     double frcd_canary_variable3 = frcd_volatile_constant3;    \
141     double frcd_canary_variable4 = frcd_volatile_constant4;    \
142     double frcd_canary_variable5 = frcd_volatile_constant5;    \
143     double frcd_canary_variable6 = frcd_volatile_constant6;    \
144     double frcd_canary_variable7 = frcd_volatile_constant7;    \
145     double frcd_canary_variable8 = frcd_volatile_constant8;
146
147 #define FLOAT_REGS_CORRUPTION_DETECTOR_FINISH()                \
148     assert (frcd_canary_variable1 == frcd_volatile_constant1); \
149     assert (frcd_canary_variable2 == frcd_volatile_constant2); \
150     assert (frcd_canary_variable3 == frcd_volatile_constant3); \
151     assert (frcd_canary_variable4 == frcd_volatile_constant4); \
152     assert (frcd_canary_variable5 == frcd_volatile_constant5); \
153     assert (frcd_canary_variable6 == frcd_volatile_constant6); \
154     assert (frcd_canary_variable7 == frcd_volatile_constant7); \
155     assert (frcd_canary_variable8 == frcd_volatile_constant8);
156
157 /* Try to get an aligned memory chunk */
158 void *
159 aligned_malloc (size_t align, size_t size);
160
161 void
162 initialize_palette (pixman_indexed_t *palette, uint32_t depth, int is_rgb);
163
164 typedef struct
165 {
166     double r, g, b, a;
167 } color_t;
168
169 void
170 round_color (pixman_format_code_t format, color_t *color);
171
172 typedef struct
173 {
174     pixman_format_code_t format;
175     uint32_t am, rm, gm, bm;
176     uint32_t as, rs, gs, bs;
177     uint32_t aw, rw, gw, bw;
178 } pixel_checker_t;
179
180 void
181 pixel_checker_init (pixel_checker_t *checker, pixman_format_code_t format);
182
183 void
184 pixel_checker_split_pixel (const pixel_checker_t *checker, uint32_t pixel,
185                            int *a, int *r, int *g, int *b);
186
187 void
188 pixel_checker_get_max (const pixel_checker_t *checker, color_t *color,
189                        int *a, int *r, int *g, int *b);
190
191 void
192 pixel_checker_get_min (const pixel_checker_t *checker, color_t *color,
193                        int *a, int *r, int *g, int *b);
194
195 pixman_bool_t
196 pixel_checker_check (const pixel_checker_t *checker,
197                      uint32_t pixel, color_t *color);