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