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