1 1.5 jmcneill /* $NetBSD: dtv_math.c,v 1.5 2011/08/09 01:42:24 jmcneill Exp $ */ 2 1.1 jmcneill 3 1.1 jmcneill /*- 4 1.1 jmcneill * Copyright (c) 2011 Alan Barrett <apb (at) NetBSD.org> 5 1.1 jmcneill * All rights reserved. 6 1.1 jmcneill * 7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without 8 1.1 jmcneill * modification, are permitted provided that the following conditions 9 1.1 jmcneill * are met: 10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright 11 1.1 jmcneill * notice, this list of conditions and the following disclaimer. 12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the 14 1.1 jmcneill * documentation and/or other materials provided with the distribution. 15 1.1 jmcneill * 16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 1.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE. 27 1.1 jmcneill */ 28 1.1 jmcneill 29 1.1 jmcneill #include <sys/cdefs.h> 30 1.5 jmcneill __KERNEL_RCSID(0, "$NetBSD: dtv_math.c,v 1.5 2011/08/09 01:42:24 jmcneill Exp $"); 31 1.1 jmcneill 32 1.2 jmcneill #include <sys/types.h> 33 1.1 jmcneill #include <sys/bitops.h> 34 1.5 jmcneill #include <sys/module.h> 35 1.1 jmcneill 36 1.5 jmcneill #include <dev/dtv/dtv_math.h> 37 1.4 apb 38 1.1 jmcneill /* 39 1.1 jmcneill * dtv_intlog10 -- return an approximation to log10(x) * 1<<24, 40 1.1 jmcneill * using integer arithmetic. 41 1.1 jmcneill * 42 1.3 apb * As a special case, returns 0 when x == 0. The mathematical 43 1.3 apb * result is -infinity. 44 1.3 apb * 45 1.3 apb * This function uses 0.5 + x/2 - 1/x as an approximation to 46 1.3 apb * log2(x) for x in the range [1.0, 2.0], and scales the input value 47 1.3 apb * to fit this range. The resulting error is always better than 48 1.3 apb * 0.2%. 49 1.1 jmcneill * 50 1.3 apb * Here's a table of the desired and actual results, as well 51 1.3 apb * as the absolute and relative errors, for several values of x. 52 1.3 apb * 53 1.3 apb * x desired actual err_abs err_rel 54 1.3 apb * 0 0 0 +0 +0.00000 55 1.3 apb * 1 0 0 +0 +0.00000 56 1.3 apb * 2 5050445 5050122 -323 -0.00006 57 1.3 apb * 3 8004766 7996348 -8418 -0.00105 58 1.3 apb * 4 10100890 10100887 -3 -0.00000 59 1.3 apb * 5 11726770 11741823 +15053 +0.00128 60 1.3 apb * 6 13055211 13046470 -8741 -0.00067 61 1.3 apb * 7 14178392 14158860 -19532 -0.00138 62 1.3 apb * 8 15151335 15151009 -326 -0.00002 63 1.3 apb * 9 16009532 16028061 +18529 +0.00116 64 1.3 apb * 10 16777216 16792588 +15372 +0.00092 65 1.3 apb * 11 17471670 17475454 +3784 +0.00022 66 1.3 apb * 12 18105656 18097235 -8421 -0.00047 67 1.3 apb * 13 18688868 18672077 -16791 -0.00090 68 1.3 apb * 14 19228837 19209625 -19212 -0.00100 69 1.3 apb * 15 19731537 19717595 -13942 -0.00071 70 1.3 apb * 16 20201781 20201774 -7 -0.00000 71 1.3 apb * 20 21827661 21842710 +15049 +0.00069 72 1.3 apb * 24 23156102 23147357 -8745 -0.00038 73 1.3 apb * 30 24781982 24767717 -14265 -0.00058 74 1.3 apb * 40 26878106 26893475 +15369 +0.00057 75 1.3 apb * 60 29832427 29818482 -13945 -0.00047 76 1.3 apb * 100 33554432 33540809 -13623 -0.00041 77 1.3 apb * 1000 50331648 50325038 -6610 -0.00013 78 1.3 apb * 10000 67108864 67125985 +17121 +0.00026 79 1.3 apb * 100000 83886080 83875492 -10588 -0.00013 80 1.3 apb * 1000000 100663296 100652005 -11291 -0.00011 81 1.3 apb * 10000000 117440512 117458739 +18227 +0.00016 82 1.3 apb * 100000000 134217728 134210175 -7553 -0.00006 83 1.3 apb * 1000000000 150994944 150980258 -14686 -0.00010 84 1.3 apb * 4294967295 161614248 161614192 -56 -0.00000 85 1.1 jmcneill */ 86 1.1 jmcneill uint32_t 87 1.1 jmcneill dtv_intlog10(uint32_t x) 88 1.1 jmcneill { 89 1.3 apb uint32_t ilog2x; 90 1.3 apb uint32_t t; 91 1.3 apb uint32_t t1; 92 1.3 apb 93 1.1 jmcneill if (__predict_false(x == 0)) 94 1.1 jmcneill return 0; 95 1.3 apb 96 1.1 jmcneill /* 97 1.3 apb * find ilog2x = floor(log2(x)), as an integer in the range [0,31]. 98 1.1 jmcneill */ 99 1.3 apb ilog2x = ilog2(x); 100 1.3 apb 101 1.3 apb /* 102 1.3 apb * Set "t" to the result of shifting x left or right 103 1.3 apb * until the most significant bit that was actually set 104 1.3 apb * moves into the 1<<24 position. 105 1.3 apb * 106 1.3 apb * Now we can think of "t" as representing 107 1.3 apb * x / 2**(floor(log2(x))), 108 1.3 apb * as a fixed-point value with 8 integer bits and 24 fraction bits. 109 1.3 apb * 110 1.3 apb * This value is in the semi-closed interval [1.0, 2.0) 111 1.3 apb * when interpreting it as a fixed-point number, or in the 112 1.3 apb * interval [0x01000000, 0x01ffffff] when examining the 113 1.3 apb * underlying uint32_t representation. 114 1.3 apb */ 115 1.3 apb t = (ilog2x > 24 ? x >> (ilog2x - 24) : x << (24 - ilog2x)); 116 1.3 apb 117 1.3 apb /* 118 1.3 apb * Calculate "t1 = 1 / t" in the 8.24 fixed-point format. 119 1.3 apb * This value is in the interval [0.5, 1.0] 120 1.3 apb * when interpreting it as a fixed-point number, or in the 121 1.3 apb * interval [0x00800000, 0x01000000] when examining the 122 1.3 apb * underlying uint32_t representation. 123 1.3 apb * 124 1.3 apb */ 125 1.3 apb t1 = ((uint64_t)1 << 48) / t; 126 1.3 apb 127 1.3 apb /* 128 1.3 apb * Calculate "t = ilog2x + t/2 - t1 + 0.5" in the 8.24 129 1.3 apb * fixed-point format. 130 1.3 apb * 131 1.3 apb * If x is a power of 2, then t is now exactly equal to log2(x) 132 1.3 apb * when interpreting it as a fixed-point number, or exactly 133 1.3 apb * log2(x) << 24 when examining the underlying uint32_t 134 1.3 apb * representation. 135 1.3 apb * 136 1.3 apb * If x is not a power of 2, then t is the result of 137 1.3 apb * using the function x/2 - 1/x + 0.5 as an approximation for 138 1.3 apb * log2(x) for x in the range [1, 2], and scaling both the 139 1.3 apb * input and the result by the appropriate number of powers of 2. 140 1.3 apb */ 141 1.3 apb t = (ilog2x << 24) + (t >> 1) - t1 + (1 << 23); 142 1.3 apb 143 1.3 apb /* 144 1.3 apb * Multiply t by log10(2) to get the final result. 145 1.3 apb * 146 1.3 apb * log10(2) is approximately 643/2136 We divide before 147 1.3 apb * multiplying to avoid overflow. 148 1.3 apb */ 149 1.3 apb return t / 2136 * 643; 150 1.1 jmcneill } 151 1.3 apb 152 1.5 jmcneill #ifdef _KERNEL 153 1.5 jmcneill MODULE(MODULE_CLASS_MISC, dtv_math, NULL); 154 1.5 jmcneill 155 1.5 jmcneill static int 156 1.5 jmcneill dtv_math_modcmd(modcmd_t cmd, void *opaque) 157 1.5 jmcneill { 158 1.5 jmcneill if (cmd == MODULE_CMD_INIT || cmd == MODULE_CMD_FINI) 159 1.5 jmcneill return 0; 160 1.5 jmcneill return ENOTTY; 161 1.5 jmcneill } 162 1.5 jmcneill #endif 163 1.5 jmcneill 164 1.3 apb #ifdef TEST_DTV_MATH 165 1.3 apb /* 166 1.3 apb * To test: 167 1.3 apb * cc -DTEST_DTV_MATH ./dtv_math.c -lm -o ./a.out && ./a.out 168 1.3 apb */ 169 1.3 apb 170 1.3 apb #include <stdio.h> 171 1.3 apb #include <inttypes.h> 172 1.3 apb #include <math.h> 173 1.3 apb 174 1.3 apb int 175 1.3 apb main(void) 176 1.3 apb { 177 1.3 apb uint32_t xlist[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 178 1.3 apb 14, 15, 16, 20, 24, 30, 40, 60, 100, 1000, 10000, 179 1.3 apb 100000, 1000000, 10000000, 100000000, 1000000000, 180 1.3 apb 0xffffffff}; 181 1.3 apb int i; 182 1.3 apb 183 1.3 apb printf("%11s %11s %11s %11s %s\n", 184 1.3 apb "x", "desired", "actual", "err_abs", "err_rel"); 185 1.3 apb for (i = 0; i < __arraycount(xlist); i++) 186 1.3 apb { 187 1.3 apb uint32_t x = xlist[i]; 188 1.3 apb uint32_t desired = (uint32_t)(log10((double)x) 189 1.3 apb * (double)(1<<24)); 190 1.3 apb uint32_t actual = dtv_intlog10(x); 191 1.3 apb int32_t err_abs = actual - desired; 192 1.3 apb double err_rel = (err_abs == 0 ? 0.0 193 1.3 apb : err_abs / (double)actual); 194 1.3 apb 195 1.3 apb printf("%11"PRIu32" %11"PRIu32" %11"PRIu32 196 1.3 apb " %+11"PRId32" %+.5f\n", 197 1.3 apb x, desired, actual, err_abs, err_rel); 198 1.3 apb } 199 1.3 apb return 0; 200 1.3 apb } 201 1.3 apb 202 1.3 apb #endif /* TEST_DTV_MATH */ 203