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 int32_t 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 uint32_t s_rcpLUT[1<<RCP_LUT_BITS] =\n");
42 printf("{\n");
43
44 for (ndx = 0; ndx < (1 << RCP_LUT_BITS); ndx++)
45 {
46 uint32_t val = (1u << RCP_LUT_BITS) | (uint32_t)ndx;
47 uint32_t rcp = (uint32_t)((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 uint32_t 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 uint32_t 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 uint32_t EPS = 1u << (DE_RCP_FRAC_BITS - NUM_ACCURATE_BITS);
148
149 uint32_t val = (deRandom_getUint32(&rnd) & ((1u << numBits) - 1)) | (1u << numBits);
150 uint32_t ref =
151 (uint32_t)(((1.0f / (double)val) * (double)(1 << DE_RCP_FRAC_BITS)) * (double)(1u << numBits));
152
153 deRcp32(val, &rcp, &exp);
154
155 DE_TEST_ASSERT(rcp >= ref - EPS && rcp < ref + EPS);
156 DE_TEST_ASSERT(exp == numBits);
157 }
158 }
159
160 DE_TEST_ASSERT(deBitMask32(0, 0) == 0);
161 DE_TEST_ASSERT(deBitMask32(8, 0) == 0);
162 DE_TEST_ASSERT(deBitMask32(16, 0) == 0);
163 DE_TEST_ASSERT(deBitMask32(31, 0) == 0);
164 DE_TEST_ASSERT(deBitMask32(32, 0) == 0);
165
166 DE_TEST_ASSERT(deBitMask32(0, 2) == 3);
167 DE_TEST_ASSERT(deBitMask32(0, 32) == 0xFFFFFFFFu);
168
169 DE_TEST_ASSERT(deBitMask32(16, 16) == 0xFFFF0000u);
170 DE_TEST_ASSERT(deBitMask32(31, 1) == 0x80000000u);
171 DE_TEST_ASSERT(deBitMask32(8, 4) == 0xF00u);
172
173 DE_TEST_ASSERT(deUintMaxValue32(1) == 1);
174 DE_TEST_ASSERT(deUintMaxValue32(2) == 3);
175 DE_TEST_ASSERT(deUintMaxValue32(32) == 0xFFFFFFFFu);
176
177 DE_TEST_ASSERT(deIntMaxValue32(1) == 0);
178 DE_TEST_ASSERT(deIntMaxValue32(2) == 1);
179 DE_TEST_ASSERT(deIntMaxValue32(32) == 0x7FFFFFFF);
180
181 DE_TEST_ASSERT(deIntMinValue32(1) == -1);
182 DE_TEST_ASSERT(deIntMinValue32(2) == -2);
183 DE_TEST_ASSERT(deIntMinValue32(32) == -0x7FFFFFFF - 1);
184
185 DE_TEST_ASSERT(deSignExtendTo32((int)0x0, 1) == 0);
186 DE_TEST_ASSERT(deSignExtendTo32((int)0x1, 1) == (int)0xFFFFFFFF);
187 DE_TEST_ASSERT(deSignExtendTo32((int)0x3, 3) == 3);
188 DE_TEST_ASSERT(deSignExtendTo32((int)0x6, 3) == (int)0xFFFFFFFE);
189 DE_TEST_ASSERT(deSignExtendTo32((int)0x3, 4) == 3);
190 DE_TEST_ASSERT(deSignExtendTo32((int)0xC, 4) == (int)0xFFFFFFFC);
191 DE_TEST_ASSERT(deSignExtendTo32((int)0x7FC3, 16) == (int)0x7FC3);
192 DE_TEST_ASSERT(deSignExtendTo32((int)0x84A0, 16) == (int)0xFFFF84A0);
193 DE_TEST_ASSERT(deSignExtendTo32((int)0xFFC3, 17) == (int)0xFFC3);
194 DE_TEST_ASSERT(deSignExtendTo32((int)0x184A0, 17) == (int)0xFFFF84A0);
195 DE_TEST_ASSERT(deSignExtendTo32((int)0x7A016601, 32) == (int)0x7A016601);
196 DE_TEST_ASSERT(deSignExtendTo32((int)0x8A016601, 32) == (int)0x8A016601);
197
198 DE_TEST_ASSERT(deReverseBytes32(0x11223344) == 0x44332211);
199 DE_TEST_ASSERT(deReverseBytes32(0xfecddeef) == 0xefdecdfe);
200 DE_TEST_ASSERT(deReverseBytes16(0x1122) == 0x2211);
201 DE_TEST_ASSERT(deReverseBytes16(0xdeef) == 0xefde);
202
203 DE_TEST_ASSERT(deInt64InInt32Range((int64_t)0x7FFFFFF));
204 DE_TEST_ASSERT(deInt64InInt32Range(0));
205 DE_TEST_ASSERT(deInt64InInt32Range(1));
206 DE_TEST_ASSERT(deInt64InInt32Range(-1));
207 DE_TEST_ASSERT(deInt64InInt32Range(-((int64_t)0x7FFFFFF)));
208 DE_TEST_ASSERT(deInt64InInt32Range(-((int64_t)0x8000 << 16)));
209 DE_TEST_ASSERT(deInt64InInt32Range((int64_t)deIntMinValue32(32)));
210
211 DE_TEST_ASSERT(!deInt64InInt32Range((((int64_t)0x7FFFFFF) << 32) | (int64_t)0xFFFFFFFF));
212 DE_TEST_ASSERT(!deInt64InInt32Range((int64_t)0x7FFFFFFF + 1));
213 DE_TEST_ASSERT(!deInt64InInt32Range(-((int64_t)0x7FFFFFFF + 2)));
214 DE_TEST_ASSERT(!deInt64InInt32Range(-((((int64_t)0x7FFFFFF) << 32) | (int64_t)0xFFFFFFFF)));
215 DE_TEST_ASSERT(!deInt64InInt32Range((int64_t)deIntMinValue32(32) - 1));
216 }
217
218 DE_END_EXTERN_C
219