blob: 103e4e96255b33fbd6ebe6097b43f0b1a13f67f2 [file] [log] [blame]
Ashok Bhat658f89d2013-02-28 18:32:03 +00001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <stdio.h>
30#include <stdint.h>
31
32#include "private/pixelflinger/ggl_fixed.h"
33
34// gglClampx() tests
35struct gglClampx_test_t
36{
37 GGLfixed input;
38 GGLfixed output;
39};
40
41gglClampx_test_t gglClampx_tests[] =
42{
43 {FIXED_ONE + 1, FIXED_ONE},
44 {FIXED_ONE, FIXED_ONE},
45 {FIXED_ONE - 1, FIXED_ONE - 1},
46 {1, 1},
47 {0, 0},
48 {FIXED_MIN,0}
49};
50
51void gglClampx_test()
52{
53 uint32_t i;
54
55 printf("Testing gglClampx\n");
56 for(i = 0; i < sizeof(gglClampx_tests)/sizeof(gglClampx_test_t); ++i)
57 {
58 gglClampx_test_t *test = &gglClampx_tests[i];
59 printf("Test input=0x%08x output=0x%08x :",
60 test->input, test->output);
61 if(gglClampx(test->input) == test->output)
62 printf("Passed\n");
63 else
64 printf("Failed\n");
65 }
66}
67
68// gglClz() tests
69struct gglClz_test_t
70{
71 GGLfixed input;
72 GGLfixed output;
73};
74
75gglClz_test_t gglClz_tests[] =
76{
77 {0, 32},
78 {1, 31},
79 {-1,0}
80};
81
82void gglClz_test()
83{
84 uint32_t i;
85
86 printf("Testing gglClz\n");
87 for(i = 0; i < sizeof(gglClz_tests)/sizeof(gglClz_test_t); ++i)
88 {
89 gglClz_test_t *test = &gglClz_tests[i];
90 printf("Test input=0x%08x output=%2d :", test->input, test->output);
91 if(gglClz(test->input) == test->output)
92 printf("Passed\n");
93 else
94 printf("Failed\n");
95 }
96}
97
98// gglMulx() tests
99struct gglMulx_test_t
100{
101 GGLfixed x;
102 GGLfixed y;
103 int shift;
104};
105
106gglMulx_test_t gglMulx_tests[] =
107{
108 {1,1,1},
109 {0,1,1},
110 {FIXED_ONE,FIXED_ONE,16},
111 {FIXED_MIN,FIXED_MAX,16},
112 {FIXED_MAX,FIXED_MAX,16},
113 {FIXED_MIN,FIXED_MIN,16},
114 {FIXED_HALF,FIXED_ONE,16},
115 {FIXED_MAX,FIXED_MAX,31},
116 {FIXED_ONE,FIXED_MAX,31}
117};
118
119void gglMulx_test()
120{
121 uint32_t i;
122 GGLfixed actual, expected;
123
124 printf("Testing gglMulx\n");
125 for(i = 0; i < sizeof(gglMulx_tests)/sizeof(gglMulx_test_t); ++i)
126 {
127 gglMulx_test_t *test = &gglMulx_tests[i];
128 printf("Test x=0x%08x y=0x%08x shift=%2d :",
129 test->x, test->y, test->shift);
130 actual = gglMulx(test->x, test->y, test->shift);
131 expected =
132 ((int64_t)test->x * test->y + (1 << (test->shift-1))) >> test->shift;
133 if(actual == expected)
134 printf(" Passed\n");
135 else
136 printf(" Failed Actual(0x%08x) Expected(0x%08x)\n",
137 actual, expected);
138 }
139}
140// gglMulAddx() tests
141struct gglMulAddx_test_t
142{
143 GGLfixed x;
144 GGLfixed y;
145 int shift;
146 GGLfixed a;
147};
148
149gglMulAddx_test_t gglMulAddx_tests[] =
150{
151 {1,2,1,1},
152 {0,1,1,1},
153 {FIXED_ONE,FIXED_ONE,16, 0},
154 {FIXED_MIN,FIXED_MAX,16, FIXED_HALF},
155 {FIXED_MAX,FIXED_MAX,16, FIXED_MIN},
156 {FIXED_MIN,FIXED_MIN,16, FIXED_MAX},
157 {FIXED_HALF,FIXED_ONE,16,FIXED_ONE},
158 {FIXED_MAX,FIXED_MAX,31, FIXED_HALF},
159 {FIXED_ONE,FIXED_MAX,31, FIXED_HALF}
160};
161
162void gglMulAddx_test()
163{
164 uint32_t i;
165 GGLfixed actual, expected;
166
167 printf("Testing gglMulAddx\n");
168 for(i = 0; i < sizeof(gglMulAddx_tests)/sizeof(gglMulAddx_test_t); ++i)
169 {
170 gglMulAddx_test_t *test = &gglMulAddx_tests[i];
171 printf("Test x=0x%08x y=0x%08x shift=%2d a=0x%08x :",
172 test->x, test->y, test->shift, test->a);
173 actual = gglMulAddx(test->x, test->y,test->a, test->shift);
174 expected = (((int64_t)test->x * test->y) >> test->shift) + test->a;
175
176 if(actual == expected)
177 printf(" Passed\n");
178 else
179 printf(" Failed Actual(0x%08x) Expected(0x%08x)\n",
180 actual, expected);
181 }
182}
183// gglMulSubx() tests
184struct gglMulSubx_test_t
185{
186 GGLfixed x;
187 GGLfixed y;
188 int shift;
189 GGLfixed a;
190};
191
192gglMulSubx_test_t gglMulSubx_tests[] =
193{
194 {1,2,1,1},
195 {0,1,1,1},
196 {FIXED_ONE,FIXED_ONE,16, 0},
197 {FIXED_MIN,FIXED_MAX,16, FIXED_HALF},
198 {FIXED_MAX,FIXED_MAX,16, FIXED_MIN},
199 {FIXED_MIN,FIXED_MIN,16, FIXED_MAX},
200 {FIXED_HALF,FIXED_ONE,16,FIXED_ONE},
201 {FIXED_MAX,FIXED_MAX,31, FIXED_HALF},
202 {FIXED_ONE,FIXED_MAX,31, FIXED_HALF}
203};
204
205void gglMulSubx_test()
206{
207 uint32_t i;
208 GGLfixed actual, expected;
209
210 printf("Testing gglMulSubx\n");
211 for(i = 0; i < sizeof(gglMulSubx_tests)/sizeof(gglMulSubx_test_t); ++i)
212 {
213 gglMulSubx_test_t *test = &gglMulSubx_tests[i];
214 printf("Test x=0x%08x y=0x%08x shift=%2d a=0x%08x :",
215 test->x, test->y, test->shift, test->a);
216 actual = gglMulSubx(test->x, test->y, test->a, test->shift);
217 expected = (((int64_t)test->x * test->y) >> test->shift) - test->a;
218
219 if(actual == expected)
220 printf(" Passed\n");
221 else
222 printf(" Failed Actual(0x%08x) Expected(0x%08x)\n",
223 actual, expected);
224 }
225}
226
227// gglMulii() tests
228const int32_t INT32_MAX = 0x7FFFFFFF;
229const int32_t INT32_MIN = 0x80000000;
230
231struct gglMulii_test_t
232{
233 int32_t x;
234 int32_t y;
235};
236
237gglMulii_test_t gglMulii_tests[] =
238{
239 {1,INT32_MIN},
240 {1,INT32_MAX},
241 {0,INT32_MIN},
242 {0,INT32_MAX},
243 {INT32_MIN, INT32_MAX},
244 {INT32_MAX, INT32_MIN},
245 {INT32_MIN, INT32_MIN},
246 {INT32_MAX, INT32_MAX}
247};
248
249void gglMulii_test()
250{
251 uint32_t i;
252 int64_t actual, expected;
253
254 printf("Testing gglMulii\n");
255 for(i = 0; i < sizeof(gglMulii_tests)/sizeof(gglMulii_test_t); ++i)
256 {
257 gglMulii_test_t *test = &gglMulii_tests[i];
258 printf("Test x=0x%08x y=0x%08x :", test->x, test->y);
259 actual = gglMulii(test->x, test->y);
260 expected = ((int64_t)test->x * test->y);
261
262 if(actual == expected)
263 printf(" Passed\n");
264 else
265 printf(" Failed Actual(%ld) Expected(%ld)\n",
266 actual, expected);
267 }
268}
269
270int main(int argc, char** argv)
271{
272 gglClampx_test();
273 gglClz_test();
274 gglMulx_test();
275 gglMulAddx_test();
276 gglMulSubx_test();
277 gglMulii_test();
278 return 0;
279}