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 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) | ndx; 47 deUint32 rcp = (int)((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 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 DE_TEST_ASSERT(deClz32(0) == 32); 80 DE_TEST_ASSERT(deClz32(1) == 31); 81 DE_TEST_ASSERT(deClz32(0xF1) == 24); 82 DE_TEST_ASSERT(deClz32(0xBC12) == 16); 83 DE_TEST_ASSERT(deClz32(0xABBACD) == 8); 84 DE_TEST_ASSERT(deClz32(0x10000000) == 3); 85 DE_TEST_ASSERT(deClz32(0x20000000) == 2); 86 DE_TEST_ASSERT(deClz32(0x40000000) == 1); 87 DE_TEST_ASSERT(deClz32(0x80000000) == 0); 88 89 /* Test simple inputs for dePop32(). */ 90 DE_TEST_ASSERT(dePop32(0) == 0); 91 DE_TEST_ASSERT(dePop32(~0) == 32); 92 DE_TEST_ASSERT(dePop32(0xFF) == 8); 93 DE_TEST_ASSERT(dePop32(0xFF00FF) == 16); 94 DE_TEST_ASSERT(dePop32(0x3333333) == 14); 95 DE_TEST_ASSERT(dePop32(0x33333333) == 16); 96 97 /* dePop32(): Check exp2(N) values and inverses. */ 98 for (numBits = 0; numBits < 32; numBits++) 99 { 100 DE_TEST_ASSERT(dePop32(1<<numBits) == 1); 101 DE_TEST_ASSERT(dePop32(~(1<<numBits)) == 31); 102 } 103 104 /* Check exp2(N) values. */ 105 for (numBits = 0; numBits < 32; numBits++) 106 { 107 deUint32 val = (1u<<numBits); 108 deRcp32(val, &rcp, &exp); 109 110 DE_TEST_ASSERT(rcp == (1u<<DE_RCP_FRAC_BITS)); 111 DE_TEST_ASSERT(exp == numBits); 112 } 113 114 /* Check random values. */ 115 for (numBits = 0; numBits < 32; numBits++) 116 { 117 int NUM_ITERS = deMax32(16, 1 << (numBits/2)); 118 int iter; 119 120 for (iter = 0; iter < NUM_ITERS; iter++) 121 { 122 const int EPS = 1 << (DE_RCP_FRAC_BITS - NUM_ACCURATE_BITS); 123 124 deUint32 val = (deRandom_getUint32(&rnd) & ((1u<<numBits)-1)) | (1u<<numBits); 125 deUint32 ref = (deUint32)(((1.0f / (double)val) * (double)(1<<DE_RCP_FRAC_BITS)) * (double)(1u<<numBits)); 126 127 deRcp32(val, &rcp, &exp); 128 129 DE_TEST_ASSERT(rcp >= ref-EPS && rcp < ref+EPS); 130 DE_TEST_ASSERT(exp == numBits); 131 } 132 } 133 } 134 135 DE_END_EXTERN_C 136