1 /*
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 #include <inttypes.h>
32 
33 #include "private/pixelflinger/ggl_fixed.h"
34 
35 // gglClampx() tests
36 struct gglClampx_test_t
37 {
38     GGLfixed input;
39     GGLfixed output;
40 };
41 
42 gglClampx_test_t gglClampx_tests[] =
43 {
44     {FIXED_ONE + 1, FIXED_ONE},
45     {FIXED_ONE, FIXED_ONE},
46     {FIXED_ONE - 1, FIXED_ONE - 1},
47     {1, 1},
48     {0, 0},
49     {FIXED_MIN,0}
50 };
51 
gglClampx_test()52 void gglClampx_test()
53 {
54     uint32_t i;
55 
56     printf("Testing gglClampx\n");
57     for(i = 0; i < sizeof(gglClampx_tests)/sizeof(gglClampx_test_t); ++i)
58     {
59         gglClampx_test_t *test = &gglClampx_tests[i];
60         printf("Test input=0x%08x output=0x%08x :",
61                 test->input, test->output);
62         if(gglClampx(test->input) == test->output)
63             printf("Passed\n");
64         else
65             printf("Failed\n");
66     }
67 }
68 
69 // gglClz() tests
70 struct gglClz_test_t
71 {
72     GGLfixed input;
73     GGLfixed output;
74 };
75 
76 gglClz_test_t gglClz_tests[] =
77 {
78     {0, 32},
79     {1, 31},
80     {-1,0}
81 };
82 
gglClz_test()83 void gglClz_test()
84 {
85     uint32_t i;
86 
87     printf("Testing gglClz\n");
88     for(i = 0; i < sizeof(gglClz_tests)/sizeof(gglClz_test_t); ++i)
89     {
90         gglClz_test_t *test = &gglClz_tests[i];
91         printf("Test input=0x%08x output=%2d :", test->input, test->output);
92         if(gglClz(test->input) == test->output)
93             printf("Passed\n");
94         else
95             printf("Failed\n");
96     }
97 }
98 
99 // gglMulx() tests
100 struct gglMulx_test_t
101 {
102     GGLfixed x;
103     GGLfixed y;
104     int      shift;
105 };
106 
107 gglMulx_test_t gglMulx_tests[] =
108 {
109     {1,1,1},
110     {0,1,1},
111     {FIXED_ONE,FIXED_ONE,16},
112     {FIXED_MIN,FIXED_MAX,16},
113     {FIXED_MAX,FIXED_MAX,16},
114     {FIXED_MIN,FIXED_MIN,16},
115     {FIXED_HALF,FIXED_ONE,16},
116     {FIXED_MAX,FIXED_MAX,31},
117     {FIXED_ONE,FIXED_MAX,31}
118 };
119 
gglMulx_test()120 void gglMulx_test()
121 {
122     uint32_t i;
123     GGLfixed actual, expected;
124 
125     printf("Testing gglMulx\n");
126     for(i = 0; i < sizeof(gglMulx_tests)/sizeof(gglMulx_test_t); ++i)
127     {
128         gglMulx_test_t *test = &gglMulx_tests[i];
129         printf("Test x=0x%08x y=0x%08x shift=%2d :",
130                 test->x, test->y, test->shift);
131         actual = gglMulx(test->x, test->y, test->shift);
132         expected =
133           ((int64_t)test->x * test->y + (1 << (test->shift-1))) >> test->shift;
134     if(actual == expected)
135         printf(" Passed\n");
136     else
137         printf(" Failed Actual(0x%08x) Expected(0x%08x)\n",
138                actual, expected);
139     }
140 }
141 // gglMulAddx() tests
142 struct gglMulAddx_test_t
143 {
144     GGLfixed x;
145     GGLfixed y;
146     int      shift;
147     GGLfixed a;
148 };
149 
150 gglMulAddx_test_t gglMulAddx_tests[] =
151 {
152     {1,2,1,1},
153     {0,1,1,1},
154     {FIXED_ONE,FIXED_ONE,16, 0},
155     {FIXED_MIN,FIXED_MAX,16, FIXED_HALF},
156     {FIXED_MAX,FIXED_MAX,16, FIXED_MIN},
157     {FIXED_MIN,FIXED_MIN,16, FIXED_MAX},
158     {FIXED_HALF,FIXED_ONE,16,FIXED_ONE},
159     {FIXED_MAX,FIXED_MAX,31, FIXED_HALF},
160     {FIXED_ONE,FIXED_MAX,31, FIXED_HALF}
161 };
162 
gglMulAddx_test()163 void gglMulAddx_test()
164 {
165     uint32_t i;
166     GGLfixed actual, expected;
167 
168     printf("Testing gglMulAddx\n");
169     for(i = 0; i < sizeof(gglMulAddx_tests)/sizeof(gglMulAddx_test_t); ++i)
170     {
171         gglMulAddx_test_t *test = &gglMulAddx_tests[i];
172         printf("Test x=0x%08x y=0x%08x shift=%2d a=0x%08x :",
173                 test->x, test->y, test->shift, test->a);
174         actual = gglMulAddx(test->x, test->y,test->a, test->shift);
175         expected = (((int64_t)test->x * test->y) >> test->shift) + test->a;
176 
177         if(actual == expected)
178             printf(" Passed\n");
179         else
180             printf(" Failed Actual(0x%08x) Expected(0x%08x)\n",
181                     actual, expected);
182     }
183 }
184 // gglMulSubx() tests
185 struct gglMulSubx_test_t
186 {
187     GGLfixed x;
188     GGLfixed y;
189     int      shift;
190     GGLfixed a;
191 };
192 
193 gglMulSubx_test_t gglMulSubx_tests[] =
194 {
195     {1,2,1,1},
196     {0,1,1,1},
197     {FIXED_ONE,FIXED_ONE,16, 0},
198     {FIXED_MIN,FIXED_MAX,16, FIXED_HALF},
199     {FIXED_MAX,FIXED_MAX,16, FIXED_MIN},
200     {FIXED_MIN,FIXED_MIN,16, FIXED_MAX},
201     {FIXED_HALF,FIXED_ONE,16,FIXED_ONE},
202     {FIXED_MAX,FIXED_MAX,31, FIXED_HALF},
203     {FIXED_ONE,FIXED_MAX,31, FIXED_HALF}
204 };
205 
gglMulSubx_test()206 void gglMulSubx_test()
207 {
208     uint32_t i;
209     GGLfixed actual, expected;
210 
211     printf("Testing gglMulSubx\n");
212     for(i = 0; i < sizeof(gglMulSubx_tests)/sizeof(gglMulSubx_test_t); ++i)
213     {
214         gglMulSubx_test_t *test = &gglMulSubx_tests[i];
215         printf("Test x=0x%08x y=0x%08x shift=%2d a=0x%08x :",
216                 test->x, test->y, test->shift, test->a);
217         actual = gglMulSubx(test->x, test->y, test->a, test->shift);
218         expected = (((int64_t)test->x * test->y) >> test->shift) - test->a;
219 
220         if(actual == expected)
221             printf(" Passed\n");
222         else
223             printf(" Failed Actual(0x%08x) Expected(0x%08x)\n",
224                 actual, expected);
225     }
226 }
227 
228 // gglMulii() tests
229 
230 struct gglMulii_test_t
231 {
232     int32_t x;
233     int32_t y;
234 };
235 
236 gglMulii_test_t gglMulii_tests[] =
237 {
238     {1,INT32_MIN},
239     {1,INT32_MAX},
240     {0,INT32_MIN},
241     {0,INT32_MAX},
242     {INT32_MIN, INT32_MAX},
243     {INT32_MAX, INT32_MIN},
244     {INT32_MIN, INT32_MIN},
245     {INT32_MAX, INT32_MAX}
246 };
247 
gglMulii_test()248 void gglMulii_test()
249 {
250     uint32_t i;
251     int64_t actual, expected;
252 
253     printf("Testing gglMulii\n");
254     for(i = 0; i < sizeof(gglMulii_tests)/sizeof(gglMulii_test_t); ++i)
255     {
256         gglMulii_test_t *test = &gglMulii_tests[i];
257         printf("Test x=0x%08x y=0x%08x :", test->x, test->y);
258         actual = gglMulii(test->x, test->y);
259         expected = ((int64_t)test->x * test->y);
260 
261         if(actual == expected)
262             printf(" Passed\n");
263         else
264             printf(" Failed Actual(%" PRId64 ") Expected(%" PRId64 ")\n",
265                     actual, expected);
266     }
267 }
268 
main(int,char **)269 int main(int /*argc*/, char** /*argv*/)
270 {
271     gglClampx_test();
272     gglClz_test();
273     gglMulx_test();
274     gglMulAddx_test();
275     gglMulSubx_test();
276     gglMulii_test();
277     return 0;
278 }
279