Home | History | Annotate | Line # | Download | only in dtv
dtv_math.c revision 1.4
      1 /* $NetBSD: dtv_math.c,v 1.4 2011/07/16 22:41:59 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.4 2011/07/16 22:41:59 apb Exp $");
     31 
     32 #include <sys/types.h>
     33 #include <sys/bitops.h>
     34 
     35 #include <dev/dtv/dtvif.h>
     36 
     37 /*
     38  * dtv_intlog10 -- return an approximation to log10(x) * 1<<24,
     39  * using integer arithmetic.
     40  *
     41  * As a special case, returns 0 when x == 0.  The mathematical
     42  * result is -infinity.
     43  *
     44  * This function uses 0.5 + x/2 - 1/x as an approximation to
     45  * log2(x) for x in the range [1.0, 2.0], and scales the input value
     46  * to fit this range.  The resulting error is always better than
     47  * 0.2%.
     48  *
     49  * Here's a table of the desired and actual results, as well
     50  * as the absolute and relative errors, for several values of x.
     51  *
     52  *           x     desired      actual     err_abs err_rel
     53  *           0           0           0          +0 +0.00000
     54  *           1           0           0          +0 +0.00000
     55  *           2     5050445     5050122        -323 -0.00006
     56  *           3     8004766     7996348       -8418 -0.00105
     57  *           4    10100890    10100887          -3 -0.00000
     58  *           5    11726770    11741823      +15053 +0.00128
     59  *           6    13055211    13046470       -8741 -0.00067
     60  *           7    14178392    14158860      -19532 -0.00138
     61  *           8    15151335    15151009        -326 -0.00002
     62  *           9    16009532    16028061      +18529 +0.00116
     63  *          10    16777216    16792588      +15372 +0.00092
     64  *          11    17471670    17475454       +3784 +0.00022
     65  *          12    18105656    18097235       -8421 -0.00047
     66  *          13    18688868    18672077      -16791 -0.00090
     67  *          14    19228837    19209625      -19212 -0.00100
     68  *          15    19731537    19717595      -13942 -0.00071
     69  *          16    20201781    20201774          -7 -0.00000
     70  *          20    21827661    21842710      +15049 +0.00069
     71  *          24    23156102    23147357       -8745 -0.00038
     72  *          30    24781982    24767717      -14265 -0.00058
     73  *          40    26878106    26893475      +15369 +0.00057
     74  *          60    29832427    29818482      -13945 -0.00047
     75  *         100    33554432    33540809      -13623 -0.00041
     76  *        1000    50331648    50325038       -6610 -0.00013
     77  *       10000    67108864    67125985      +17121 +0.00026
     78  *      100000    83886080    83875492      -10588 -0.00013
     79  *     1000000   100663296   100652005      -11291 -0.00011
     80  *    10000000   117440512   117458739      +18227 +0.00016
     81  *   100000000   134217728   134210175       -7553 -0.00006
     82  *  1000000000   150994944   150980258      -14686 -0.00010
     83  *  4294967295   161614248   161614192         -56 -0.00000
     84  */
     85 uint32_t
     86 dtv_intlog10(uint32_t x)
     87 {
     88 	uint32_t ilog2x;
     89 	uint32_t t;
     90 	uint32_t t1;
     91 
     92 	if (__predict_false(x == 0))
     93 		return 0;
     94 
     95 	/*
     96 	 * find ilog2x = floor(log2(x)), as an integer in the range [0,31].
     97 	 */
     98 	ilog2x = ilog2(x);
     99 
    100 	/*
    101 	 * Set "t" to the result of shifting x left or right
    102 	 * until the most significant bit that was actually set
    103 	 * moves into the 1<<24 position.
    104 	 *
    105 	 * Now we can think of "t" as representing
    106 	 * x / 2**(floor(log2(x))),
    107 	 * as a fixed-point value with 8 integer bits and 24 fraction bits.
    108 	 *
    109 	 * This value is in the semi-closed interval [1.0, 2.0)
    110 	 * when interpreting it as a fixed-point number, or in the
    111 	 * interval [0x01000000, 0x01ffffff] when examining the
    112 	 * underlying uint32_t representation.
    113 	 */
    114 	t = (ilog2x > 24 ? x >> (ilog2x - 24) : x << (24 - ilog2x));
    115 
    116 	/*
    117 	 * Calculate "t1 = 1 / t" in the 8.24 fixed-point format.
    118 	 * This value is in the interval [0.5, 1.0]
    119 	 * when interpreting it as a fixed-point number, or in the
    120 	 * interval [0x00800000, 0x01000000] when examining the
    121 	 * underlying uint32_t representation.
    122 	 *
    123 	 */
    124 	t1 = ((uint64_t)1 << 48) / t;
    125 
    126 	/*
    127 	 * Calculate "t = ilog2x + t/2 - t1 + 0.5" in the 8.24
    128 	 * fixed-point format.
    129 	 *
    130 	 * If x is a power of 2, then t is now exactly equal to log2(x)
    131 	 * when interpreting it as a fixed-point number, or exactly
    132 	 * log2(x) << 24 when examining the underlying uint32_t
    133 	 * representation.
    134 	 *
    135 	 * If x is not a power of 2, then t is the result of
    136 	 * using the function x/2 - 1/x + 0.5 as an approximation for
    137 	 * log2(x) for x in the range [1, 2], and scaling both the
    138 	 * input and the result by the appropriate number of powers of 2.
    139 	 */
    140 	t = (ilog2x << 24) + (t >> 1) - t1 + (1 << 23);
    141 
    142 	/*
    143 	 * Multiply t by log10(2) to get the final result.
    144 	 *
    145 	 * log10(2) is approximately 643/2136  We divide before
    146 	 * multiplying to avoid overflow.
    147 	 */
    148 	return t / 2136 * 643;
    149 }
    150 
    151 #ifdef TEST_DTV_MATH
    152 /*
    153  * To test:
    154  *	cc -DTEST_DTV_MATH ./dtv_math.c -lm -o ./a.out && ./a.out
    155  */
    156 
    157 #include <stdio.h>
    158 #include <inttypes.h>
    159 #include <math.h>
    160 
    161 int
    162 main(void)
    163 {
    164 	uint32_t xlist[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
    165 			14, 15, 16, 20, 24, 30, 40, 60, 100, 1000, 10000,
    166 			100000, 1000000, 10000000, 100000000, 1000000000,
    167 			0xffffffff};
    168 	int i;
    169 
    170 	printf("%11s %11s %11s %11s %s\n",
    171 		"x", "desired", "actual", "err_abs", "err_rel");
    172 	for (i = 0; i < __arraycount(xlist); i++)
    173 	{
    174 		uint32_t x = xlist[i];
    175 		uint32_t desired = (uint32_t)(log10((double)x)
    176 						* (double)(1<<24));
    177 		uint32_t actual = dtv_intlog10(x);
    178 		int32_t err_abs = actual - desired;
    179 		double err_rel = (err_abs == 0 ? 0.0
    180 				: err_abs / (double)actual);
    181 
    182 		printf("%11"PRIu32" %11"PRIu32" %11"PRIu32
    183 			" %+11"PRId32" %+.5f\n",
    184 			x, desired, actual, err_abs, err_rel);
    185 	}
    186 	return 0;
    187 }
    188 
    189 #endif /* TEST_DTV_MATH */
    190