1 /*-------------------------------------------------------------------------
2  * drawElements Base Portability Library
3  * -------------------------------------
4  *
5  * Copyright 2014 The Android Open Source Project
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *//*!
20  * \file
21  * \brief Testing of deInt32 functions.
22  *//*--------------------------------------------------------------------*/
23 
24 #include "deInt32.h"
25 #include "deRandom.h"
26 
27 #include <stdio.h> /* printf() */
28 
29 DE_BEGIN_EXTERN_C
30 
deInt32_computeLUTs(void)31 void deInt32_computeLUTs (void)
32 {
33 	enum
34 	{
35 		RCP_LUT_BITS = 8
36 	};
37 
38 	int ndx;
39 
40 	printf("enum { RCP_LUT_BITS = %d };\n", RCP_LUT_BITS);
41 	printf("static const deUint32 s_rcpLUT[1<<RCP_LUT_BITS] =\n");
42 	printf("{\n");
43 
44 	for (ndx = 0; ndx < (1<<RCP_LUT_BITS); ndx++)
45 	{
46 		deUint32	val = (1u << RCP_LUT_BITS) | (deUint32)ndx;
47 		deUint32	rcp = (deUint32)((1u << DE_RCP_FRAC_BITS) / ((double)val / (1<<RCP_LUT_BITS)));
48 
49 		if ((ndx & 3) == 0)
50 			printf("\t");
51 
52 		printf("0x%08x", rcp);
53 
54 		if ((ndx & 3) == 3)
55 		{
56 			if (ndx != (1<<RCP_LUT_BITS)-1)
57 				printf(",");
58 			printf("\n");
59 		}
60 		else
61 			printf(", ");
62 	}
63 
64 	printf("};\n");
65 }
66 
deInt32_selfTest(void)67 void deInt32_selfTest (void)
68 {
69 	const int	NUM_ACCURATE_BITS	= 29;
70 
71 	deRandom	rnd;
72 	deUint32	rcp;
73 	int			exp;
74 	int			numBits;
75 
76 	deRandom_init(&rnd, 0xdeadbeefu-1);
77 
78 	/* Test deClz32(). */
79 	{
80 		int i;
81 		for (i = 0; i < 32; i++)
82 		{
83 			DE_TEST_ASSERT(deClz32(1u << i) == 31-i);
84 			DE_TEST_ASSERT(deClz32((1u << i) | ((1u << i)-1u)) == 31-i);
85 		}
86 	}
87 
88 	DE_TEST_ASSERT(deClz32(0) == 32);
89 	DE_TEST_ASSERT(deClz32(1) == 31);
90 	DE_TEST_ASSERT(deClz32(0xF1) == 24);
91 	DE_TEST_ASSERT(deClz32(0xBC12) == 16);
92 	DE_TEST_ASSERT(deClz32(0xABBACD) == 8);
93 	DE_TEST_ASSERT(deClz32(0x10000000) == 3);
94 	DE_TEST_ASSERT(deClz32(0x20000000) == 2);
95 	DE_TEST_ASSERT(deClz32(0x40000000) == 1);
96 	DE_TEST_ASSERT(deClz32(0x80000000) == 0);
97 
98 	/* Test deCtz32(). */
99 	{
100 		int i;
101 		for (i = 0; i < 32; i++)
102 		{
103 			DE_TEST_ASSERT(deCtz32(1u << i) == i);
104 			DE_TEST_ASSERT(deCtz32(~((1u << i)-1u)) == i);
105 		}
106 	}
107 
108 	DE_TEST_ASSERT(deCtz32(0) == 32);
109 	DE_TEST_ASSERT(deCtz32(1) == 0);
110 	DE_TEST_ASSERT(deCtz32(0x3F4) == 2);
111 	DE_TEST_ASSERT(deCtz32(0x3F40) == 6);
112 	DE_TEST_ASSERT(deCtz32(0xFFFFFFFF) == 0);
113 
114 	/* Test simple inputs for dePop32(). */
115 	DE_TEST_ASSERT(dePop32(0u) == 0);
116 	DE_TEST_ASSERT(dePop32(~0u) == 32);
117 	DE_TEST_ASSERT(dePop32(0xFF) == 8);
118 	DE_TEST_ASSERT(dePop32(0xFF00FF) == 16);
119 	DE_TEST_ASSERT(dePop32(0x3333333) == 14);
120 	DE_TEST_ASSERT(dePop32(0x33333333) == 16);
121 
122 	/* dePop32(): Check exp2(N) values and inverses. */
123 	for (numBits = 0; numBits < 32; numBits++)
124 	{
125 		DE_TEST_ASSERT(dePop32(1u<<numBits) == 1);
126 		DE_TEST_ASSERT(dePop32(~(1u<<numBits)) == 31);
127 	}
128 
129 	/* Check exp2(N) values. */
130 	for (numBits = 0; numBits < 32; numBits++)
131 	{
132 		deUint32 val = (1u<<numBits);
133 		deRcp32(val, &rcp, &exp);
134 
135 		DE_TEST_ASSERT(rcp == (1u<<DE_RCP_FRAC_BITS));
136 		DE_TEST_ASSERT(exp == numBits);
137 	}
138 
139 	/* Check random values. */
140 	for (numBits = 0; numBits < 32; numBits++)
141 	{
142 		int NUM_ITERS = deMax32(16, 1 << (numBits/2));
143 		int iter;
144 
145 		for (iter = 0; iter < NUM_ITERS; iter++)
146 		{
147 			const deUint32	EPS = 1u << (DE_RCP_FRAC_BITS - NUM_ACCURATE_BITS);
148 
149 			deUint32		val = (deRandom_getUint32(&rnd) & ((1u<<numBits)-1)) | (1u<<numBits);
150 			deUint32		ref = (deUint32)(((1.0f / (double)val) * (double)(1<<DE_RCP_FRAC_BITS)) * (double)(1u<<numBits));
151 
152 			deRcp32(val, &rcp, &exp);
153 
154 			DE_TEST_ASSERT(rcp >= ref-EPS && rcp < ref+EPS);
155 			DE_TEST_ASSERT(exp == numBits);
156 		}
157 	}
158 
159 	DE_TEST_ASSERT(deBitMask32(0, 0) == 0);
160 	DE_TEST_ASSERT(deBitMask32(8, 0) == 0);
161 	DE_TEST_ASSERT(deBitMask32(16, 0) == 0);
162 	DE_TEST_ASSERT(deBitMask32(31, 0) == 0);
163 	DE_TEST_ASSERT(deBitMask32(32, 0) == 0);
164 
165 	DE_TEST_ASSERT(deBitMask32(0, 2) == 3);
166 	DE_TEST_ASSERT(deBitMask32(0, 32) == 0xFFFFFFFFu);
167 
168 	DE_TEST_ASSERT(deBitMask32(16, 16) == 0xFFFF0000u);
169 	DE_TEST_ASSERT(deBitMask32(31, 1) == 0x80000000u);
170 	DE_TEST_ASSERT(deBitMask32(8, 4) == 0xF00u);
171 
172 	DE_TEST_ASSERT(deUintMaxValue32(1) == 1);
173 	DE_TEST_ASSERT(deUintMaxValue32(2) == 3);
174 	DE_TEST_ASSERT(deUintMaxValue32(32) == 0xFFFFFFFFu);
175 
176 	DE_TEST_ASSERT(deIntMaxValue32(1) == 0);
177 	DE_TEST_ASSERT(deIntMaxValue32(2) == 1);
178 	DE_TEST_ASSERT(deIntMaxValue32(32) == 0x7FFFFFFF);
179 
180 	DE_TEST_ASSERT(deIntMinValue32(1) == -1);
181 	DE_TEST_ASSERT(deIntMinValue32(2) == -2);
182 	DE_TEST_ASSERT(deIntMinValue32(32) == -0x7FFFFFFF - 1);
183 
184 	DE_TEST_ASSERT(deSignExtendTo32((int)0x0, 1) == 0);
185 	DE_TEST_ASSERT(deSignExtendTo32((int)0x1, 1) == (int)0xFFFFFFFF);
186 	DE_TEST_ASSERT(deSignExtendTo32((int)0x3, 3) == 3);
187 	DE_TEST_ASSERT(deSignExtendTo32((int)0x6, 3) == (int)0xFFFFFFFE);
188 	DE_TEST_ASSERT(deSignExtendTo32((int)0x3, 4) == 3);
189 	DE_TEST_ASSERT(deSignExtendTo32((int)0xC, 4) == (int)0xFFFFFFFC);
190 	DE_TEST_ASSERT(deSignExtendTo32((int)0x7FC3, 16) == (int)0x7FC3);
191 	DE_TEST_ASSERT(deSignExtendTo32((int)0x84A0, 16) == (int)0xFFFF84A0);
192 	DE_TEST_ASSERT(deSignExtendTo32((int)0xFFC3, 17) == (int)0xFFC3);
193 	DE_TEST_ASSERT(deSignExtendTo32((int)0x184A0, 17) == (int)0xFFFF84A0);
194 	DE_TEST_ASSERT(deSignExtendTo32((int)0x7A016601, 32) == (int)0x7A016601);
195 	DE_TEST_ASSERT(deSignExtendTo32((int)0x8A016601, 32) == (int)0x8A016601);
196 
197 	DE_TEST_ASSERT(deReverseBytes32(0x11223344) == 0x44332211);
198 	DE_TEST_ASSERT(deReverseBytes32(0xfecddeef) == 0xefdecdfe);
199 	DE_TEST_ASSERT(deReverseBytes16(0x1122) == 0x2211);
200 	DE_TEST_ASSERT(deReverseBytes16(0xdeef) == 0xefde);
201 
202 	DE_TEST_ASSERT(deInt64InInt32Range((deInt64)0x7FFFFFF));
203 	DE_TEST_ASSERT(deInt64InInt32Range(0));
204 	DE_TEST_ASSERT(deInt64InInt32Range(1));
205 	DE_TEST_ASSERT(deInt64InInt32Range(-1));
206 	DE_TEST_ASSERT(deInt64InInt32Range(-((deInt64)0x7FFFFFF)));
207 	DE_TEST_ASSERT(deInt64InInt32Range(-((deInt64)0x8000 << 16)));
208 	DE_TEST_ASSERT(deInt64InInt32Range((deInt64)deIntMinValue32(32)));
209 
210 	DE_TEST_ASSERT(!deInt64InInt32Range((((deInt64)0x7FFFFFF) << 32) | (deInt64)0xFFFFFFFF));
211 	DE_TEST_ASSERT(!deInt64InInt32Range((deInt64)0x7FFFFFFF + 1));
212 	DE_TEST_ASSERT(!deInt64InInt32Range(-((deInt64)0x7FFFFFFF + 2)));
213 	DE_TEST_ASSERT(!deInt64InInt32Range(-((((deInt64)0x7FFFFFF) << 32) | (deInt64)0xFFFFFFFF)));
214 	DE_TEST_ASSERT(!deInt64InInt32Range((deInt64)deIntMinValue32(32) - 1));
215 }
216 
217 DE_END_EXTERN_C
218