Add qemu 2.4.0
[kvmfornfv.git] / qemu / pixman / 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 #include "utils-prng.h"
8
9 #if defined(_MSC_VER)
10 #define snprintf _snprintf
11 #define strcasecmp _stricmp
12 #endif
13
14 #define ARRAY_LENGTH(A) ((int) (sizeof (A) / sizeof ((A) [0])))
15
16 /* A primitive pseudorandom number generator,
17  * taken from POSIX.1-2001 example
18  */
19
20 extern prng_t prng_state_data;
21 extern prng_t *prng_state;
22 #ifdef USE_OPENMP
23 #pragma omp threadprivate(prng_state_data)
24 #pragma omp threadprivate(prng_state)
25 #endif
26
27 static inline uint32_t
28 prng_rand (void)
29 {
30     return prng_rand_r (prng_state);
31 }
32
33 static inline void
34 prng_srand (uint32_t seed)
35 {
36     if (!prng_state)
37     {
38         /* Without setting a seed, PRNG does not work properly (is just
39          * returning zeros). So we only initialize the pointer here to
40          * make sure that 'prng_srand' is always called before any
41          * other 'prng_*' function. The wrongdoers violating this order
42          * will get a segfault. */
43         prng_state = &prng_state_data;
44     }
45     prng_srand_r (prng_state, seed);
46 }
47
48 static inline uint32_t
49 prng_rand_n (int max)
50 {
51     return prng_rand () % max;
52 }
53
54 static inline void
55 prng_randmemset (void *buffer, size_t size, prng_randmemset_flags_t flags)
56 {
57     prng_randmemset_r (prng_state, buffer, size, flags);
58 }
59
60 /* CRC 32 computation
61  */
62 uint32_t
63 compute_crc32 (uint32_t    in_crc32,
64                const void *buf,
65                size_t      buf_len);
66
67 uint32_t
68 compute_crc32_for_image (uint32_t        in_crc32,
69                          pixman_image_t *image);
70
71 /* Print the image in hexadecimal */
72 void
73 print_image (pixman_image_t *image);
74
75 /* Returns TRUE if running on a little endian system
76  */
77 static force_inline pixman_bool_t
78 is_little_endian (void)
79 {
80     unsigned long endian_check_var = 1;
81     return *(unsigned char *)&endian_check_var == 1;
82 }
83
84 /* perform endian conversion of pixel data
85  */
86 void
87 image_endian_swap (pixman_image_t *img);
88
89 /* Allocate memory that is bounded by protected pages,
90  * so that out-of-bounds access will cause segfaults
91  */
92 void *
93 fence_malloc (int64_t len);
94
95 void
96 fence_free (void *data);
97
98 /* Generate n_bytes random bytes in fence_malloced memory */
99 uint8_t *
100 make_random_bytes (int n_bytes);
101
102 /* Return current time in seconds */
103 double
104 gettime (void);
105
106 uint32_t
107 get_random_seed (void);
108
109 /* main body of the fuzzer test */
110 int
111 fuzzer_test_main (const char *test_name,
112                   int         default_number_of_iterations,
113                   uint32_t    expected_checksum,
114                   uint32_t    (*test_function)(int testnum, int verbose),
115                   int         argc,
116                   const char *argv[]);
117
118 void
119 fail_after (int seconds, const char *msg);
120
121 /* If possible, enable traps for floating point exceptions */
122 void enable_divbyzero_exceptions(void);
123
124 /* Converts a8r8g8b8 pixels to pixels that
125  *  - are not premultiplied,
126  *  - are stored in this order in memory: R, G, B, A, regardless of
127  *    the endianness of the computer.
128  * It is allowed for @src and @dst to point to the same memory buffer.
129  */
130 void
131 a8r8g8b8_to_rgba_np (uint32_t *dst, uint32_t *src, int n_pixels);
132
133 pixman_bool_t
134 write_png (pixman_image_t *image, const char *filename);
135
136 void
137 draw_checkerboard (pixman_image_t *image,
138                    int check_size,
139                    uint32_t color1, uint32_t color2);
140
141 /* A pair of macros which can help to detect corruption of
142  * floating point registers after a function call. This may
143  * happen if _mm_empty() call is forgotten in MMX/SSE2 fast
144  * path code, or ARM NEON assembly optimized function forgets
145  * to save/restore d8-d15 registers before use.
146  */
147
148 #define FLOAT_REGS_CORRUPTION_DETECTOR_START()                 \
149     static volatile double frcd_volatile_constant1 = 123451;   \
150     static volatile double frcd_volatile_constant2 = 123452;   \
151     static volatile double frcd_volatile_constant3 = 123453;   \
152     static volatile double frcd_volatile_constant4 = 123454;   \
153     static volatile double frcd_volatile_constant5 = 123455;   \
154     static volatile double frcd_volatile_constant6 = 123456;   \
155     static volatile double frcd_volatile_constant7 = 123457;   \
156     static volatile double frcd_volatile_constant8 = 123458;   \
157     double frcd_canary_variable1 = frcd_volatile_constant1;    \
158     double frcd_canary_variable2 = frcd_volatile_constant2;    \
159     double frcd_canary_variable3 = frcd_volatile_constant3;    \
160     double frcd_canary_variable4 = frcd_volatile_constant4;    \
161     double frcd_canary_variable5 = frcd_volatile_constant5;    \
162     double frcd_canary_variable6 = frcd_volatile_constant6;    \
163     double frcd_canary_variable7 = frcd_volatile_constant7;    \
164     double frcd_canary_variable8 = frcd_volatile_constant8;
165
166 #define FLOAT_REGS_CORRUPTION_DETECTOR_FINISH()                \
167     assert (frcd_canary_variable1 == frcd_volatile_constant1); \
168     assert (frcd_canary_variable2 == frcd_volatile_constant2); \
169     assert (frcd_canary_variable3 == frcd_volatile_constant3); \
170     assert (frcd_canary_variable4 == frcd_volatile_constant4); \
171     assert (frcd_canary_variable5 == frcd_volatile_constant5); \
172     assert (frcd_canary_variable6 == frcd_volatile_constant6); \
173     assert (frcd_canary_variable7 == frcd_volatile_constant7); \
174     assert (frcd_canary_variable8 == frcd_volatile_constant8);
175
176 /* Try to get an aligned memory chunk */
177 void *
178 aligned_malloc (size_t align, size_t size);
179
180 double
181 convert_srgb_to_linear (double component);
182
183 double
184 convert_linear_to_srgb (double component);
185
186 void
187 initialize_palette (pixman_indexed_t *palette, uint32_t depth, int is_rgb);
188
189 const char *
190 operator_name (pixman_op_t op);
191
192 const char *
193 format_name (pixman_format_code_t format);
194
195 typedef struct
196 {
197     double r, g, b, a;
198 } color_t;
199
200 void
201 do_composite (pixman_op_t op,
202               const color_t *src,
203               const color_t *mask,
204               const color_t *dst,
205               color_t *result,
206               pixman_bool_t component_alpha);
207
208 void
209 round_color (pixman_format_code_t format, color_t *color);
210
211 typedef struct
212 {
213     pixman_format_code_t format;
214     uint32_t am, rm, gm, bm;
215     uint32_t as, rs, gs, bs;
216     uint32_t aw, rw, gw, bw;
217 } pixel_checker_t;
218
219 void
220 pixel_checker_init (pixel_checker_t *checker, pixman_format_code_t format);
221
222 void
223 pixel_checker_split_pixel (const pixel_checker_t *checker, uint32_t pixel,
224                            int *a, int *r, int *g, int *b);
225
226 void
227 pixel_checker_get_max (const pixel_checker_t *checker, color_t *color,
228                        int *a, int *r, int *g, int *b);
229
230 void
231 pixel_checker_get_min (const pixel_checker_t *checker, color_t *color,
232                        int *a, int *r, int *g, int *b);
233
234 pixman_bool_t
235 pixel_checker_check (const pixel_checker_t *checker,
236                      uint32_t pixel, color_t *color);
237
238 void
239 pixel_checker_convert_pixel_to_color (const pixel_checker_t *checker,
240                                       uint32_t pixel, color_t *color);
241
242 void
243 pixel_checker_get_masks (const pixel_checker_t *checker,
244                          uint32_t              *am,
245                          uint32_t              *rm,
246                          uint32_t              *gm,
247                          uint32_t              *bm);