1*fb95eabdSjmcneill /* $NetBSD: dtv_math.c,v 1.5 2011/08/09 01:42:24 jmcneill Exp $ */
2ee71f9a6Sjmcneill
3ee71f9a6Sjmcneill /*-
4ee71f9a6Sjmcneill * Copyright (c) 2011 Alan Barrett <apb@NetBSD.org>
5ee71f9a6Sjmcneill * All rights reserved.
6ee71f9a6Sjmcneill *
7ee71f9a6Sjmcneill * Redistribution and use in source and binary forms, with or without
8ee71f9a6Sjmcneill * modification, are permitted provided that the following conditions
9ee71f9a6Sjmcneill * are met:
10ee71f9a6Sjmcneill * 1. Redistributions of source code must retain the above copyright
11ee71f9a6Sjmcneill * notice, this list of conditions and the following disclaimer.
12ee71f9a6Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
13ee71f9a6Sjmcneill * notice, this list of conditions and the following disclaimer in the
14ee71f9a6Sjmcneill * documentation and/or other materials provided with the distribution.
15ee71f9a6Sjmcneill *
16ee71f9a6Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17ee71f9a6Sjmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18ee71f9a6Sjmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19ee71f9a6Sjmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20ee71f9a6Sjmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21ee71f9a6Sjmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22ee71f9a6Sjmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23ee71f9a6Sjmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24ee71f9a6Sjmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25ee71f9a6Sjmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26ee71f9a6Sjmcneill * POSSIBILITY OF SUCH DAMAGE.
27ee71f9a6Sjmcneill */
28ee71f9a6Sjmcneill
29ee71f9a6Sjmcneill #include <sys/cdefs.h>
30*fb95eabdSjmcneill __KERNEL_RCSID(0, "$NetBSD: dtv_math.c,v 1.5 2011/08/09 01:42:24 jmcneill Exp $");
31ee71f9a6Sjmcneill
32f3fc71eeSjmcneill #include <sys/types.h>
33ee71f9a6Sjmcneill #include <sys/bitops.h>
34*fb95eabdSjmcneill #include <sys/module.h>
35ee71f9a6Sjmcneill
36*fb95eabdSjmcneill #include <dev/dtv/dtv_math.h>
37217d9c2aSapb
38ee71f9a6Sjmcneill /*
39ee71f9a6Sjmcneill * dtv_intlog10 -- return an approximation to log10(x) * 1<<24,
40ee71f9a6Sjmcneill * using integer arithmetic.
41ee71f9a6Sjmcneill *
4276108b75Sapb * As a special case, returns 0 when x == 0. The mathematical
4376108b75Sapb * result is -infinity.
44ee71f9a6Sjmcneill *
4576108b75Sapb * This function uses 0.5 + x/2 - 1/x as an approximation to
4676108b75Sapb * log2(x) for x in the range [1.0, 2.0], and scales the input value
4776108b75Sapb * to fit this range. The resulting error is always better than
4876108b75Sapb * 0.2%.
49ee71f9a6Sjmcneill *
5076108b75Sapb * Here's a table of the desired and actual results, as well
5176108b75Sapb * as the absolute and relative errors, for several values of x.
5276108b75Sapb *
5376108b75Sapb * x desired actual err_abs err_rel
5476108b75Sapb * 0 0 0 +0 +0.00000
5576108b75Sapb * 1 0 0 +0 +0.00000
5676108b75Sapb * 2 5050445 5050122 -323 -0.00006
5776108b75Sapb * 3 8004766 7996348 -8418 -0.00105
5876108b75Sapb * 4 10100890 10100887 -3 -0.00000
5976108b75Sapb * 5 11726770 11741823 +15053 +0.00128
6076108b75Sapb * 6 13055211 13046470 -8741 -0.00067
6176108b75Sapb * 7 14178392 14158860 -19532 -0.00138
6276108b75Sapb * 8 15151335 15151009 -326 -0.00002
6376108b75Sapb * 9 16009532 16028061 +18529 +0.00116
6476108b75Sapb * 10 16777216 16792588 +15372 +0.00092
6576108b75Sapb * 11 17471670 17475454 +3784 +0.00022
6676108b75Sapb * 12 18105656 18097235 -8421 -0.00047
6776108b75Sapb * 13 18688868 18672077 -16791 -0.00090
6876108b75Sapb * 14 19228837 19209625 -19212 -0.00100
6976108b75Sapb * 15 19731537 19717595 -13942 -0.00071
7076108b75Sapb * 16 20201781 20201774 -7 -0.00000
7176108b75Sapb * 20 21827661 21842710 +15049 +0.00069
7276108b75Sapb * 24 23156102 23147357 -8745 -0.00038
7376108b75Sapb * 30 24781982 24767717 -14265 -0.00058
7476108b75Sapb * 40 26878106 26893475 +15369 +0.00057
7576108b75Sapb * 60 29832427 29818482 -13945 -0.00047
7676108b75Sapb * 100 33554432 33540809 -13623 -0.00041
7776108b75Sapb * 1000 50331648 50325038 -6610 -0.00013
7876108b75Sapb * 10000 67108864 67125985 +17121 +0.00026
7976108b75Sapb * 100000 83886080 83875492 -10588 -0.00013
8076108b75Sapb * 1000000 100663296 100652005 -11291 -0.00011
8176108b75Sapb * 10000000 117440512 117458739 +18227 +0.00016
8276108b75Sapb * 100000000 134217728 134210175 -7553 -0.00006
8376108b75Sapb * 1000000000 150994944 150980258 -14686 -0.00010
8476108b75Sapb * 4294967295 161614248 161614192 -56 -0.00000
85ee71f9a6Sjmcneill */
86ee71f9a6Sjmcneill uint32_t
dtv_intlog10(uint32_t x)87ee71f9a6Sjmcneill dtv_intlog10(uint32_t x)
88ee71f9a6Sjmcneill {
8976108b75Sapb uint32_t ilog2x;
9076108b75Sapb uint32_t t;
9176108b75Sapb uint32_t t1;
9276108b75Sapb
93ee71f9a6Sjmcneill if (__predict_false(x == 0))
94ee71f9a6Sjmcneill return 0;
9576108b75Sapb
96ee71f9a6Sjmcneill /*
9776108b75Sapb * find ilog2x = floor(log2(x)), as an integer in the range [0,31].
98ee71f9a6Sjmcneill */
9976108b75Sapb ilog2x = ilog2(x);
10076108b75Sapb
10176108b75Sapb /*
10276108b75Sapb * Set "t" to the result of shifting x left or right
10376108b75Sapb * until the most significant bit that was actually set
10476108b75Sapb * moves into the 1<<24 position.
10576108b75Sapb *
10676108b75Sapb * Now we can think of "t" as representing
10776108b75Sapb * x / 2**(floor(log2(x))),
10876108b75Sapb * as a fixed-point value with 8 integer bits and 24 fraction bits.
10976108b75Sapb *
11076108b75Sapb * This value is in the semi-closed interval [1.0, 2.0)
11176108b75Sapb * when interpreting it as a fixed-point number, or in the
11276108b75Sapb * interval [0x01000000, 0x01ffffff] when examining the
11376108b75Sapb * underlying uint32_t representation.
11476108b75Sapb */
11576108b75Sapb t = (ilog2x > 24 ? x >> (ilog2x - 24) : x << (24 - ilog2x));
11676108b75Sapb
11776108b75Sapb /*
11876108b75Sapb * Calculate "t1 = 1 / t" in the 8.24 fixed-point format.
11976108b75Sapb * This value is in the interval [0.5, 1.0]
12076108b75Sapb * when interpreting it as a fixed-point number, or in the
12176108b75Sapb * interval [0x00800000, 0x01000000] when examining the
12276108b75Sapb * underlying uint32_t representation.
12376108b75Sapb *
12476108b75Sapb */
12576108b75Sapb t1 = ((uint64_t)1 << 48) / t;
12676108b75Sapb
12776108b75Sapb /*
12876108b75Sapb * Calculate "t = ilog2x + t/2 - t1 + 0.5" in the 8.24
12976108b75Sapb * fixed-point format.
13076108b75Sapb *
13176108b75Sapb * If x is a power of 2, then t is now exactly equal to log2(x)
13276108b75Sapb * when interpreting it as a fixed-point number, or exactly
13376108b75Sapb * log2(x) << 24 when examining the underlying uint32_t
13476108b75Sapb * representation.
13576108b75Sapb *
13676108b75Sapb * If x is not a power of 2, then t is the result of
13776108b75Sapb * using the function x/2 - 1/x + 0.5 as an approximation for
13876108b75Sapb * log2(x) for x in the range [1, 2], and scaling both the
13976108b75Sapb * input and the result by the appropriate number of powers of 2.
14076108b75Sapb */
14176108b75Sapb t = (ilog2x << 24) + (t >> 1) - t1 + (1 << 23);
14276108b75Sapb
14376108b75Sapb /*
14476108b75Sapb * Multiply t by log10(2) to get the final result.
14576108b75Sapb *
14676108b75Sapb * log10(2) is approximately 643/2136 We divide before
14776108b75Sapb * multiplying to avoid overflow.
14876108b75Sapb */
14976108b75Sapb return t / 2136 * 643;
150ee71f9a6Sjmcneill }
15176108b75Sapb
152*fb95eabdSjmcneill #ifdef _KERNEL
153*fb95eabdSjmcneill MODULE(MODULE_CLASS_MISC, dtv_math, NULL);
154*fb95eabdSjmcneill
155*fb95eabdSjmcneill static int
dtv_math_modcmd(modcmd_t cmd,void * opaque)156*fb95eabdSjmcneill dtv_math_modcmd(modcmd_t cmd, void *opaque)
157*fb95eabdSjmcneill {
158*fb95eabdSjmcneill if (cmd == MODULE_CMD_INIT || cmd == MODULE_CMD_FINI)
159*fb95eabdSjmcneill return 0;
160*fb95eabdSjmcneill return ENOTTY;
161*fb95eabdSjmcneill }
162*fb95eabdSjmcneill #endif
163*fb95eabdSjmcneill
16476108b75Sapb #ifdef TEST_DTV_MATH
16576108b75Sapb /*
16676108b75Sapb * To test:
16776108b75Sapb * cc -DTEST_DTV_MATH ./dtv_math.c -lm -o ./a.out && ./a.out
16876108b75Sapb */
16976108b75Sapb
17076108b75Sapb #include <stdio.h>
17176108b75Sapb #include <inttypes.h>
17276108b75Sapb #include <math.h>
17376108b75Sapb
17476108b75Sapb int
main(void)17576108b75Sapb main(void)
17676108b75Sapb {
17776108b75Sapb uint32_t xlist[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
17876108b75Sapb 14, 15, 16, 20, 24, 30, 40, 60, 100, 1000, 10000,
17976108b75Sapb 100000, 1000000, 10000000, 100000000, 1000000000,
18076108b75Sapb 0xffffffff};
18176108b75Sapb int i;
18276108b75Sapb
18376108b75Sapb printf("%11s %11s %11s %11s %s\n",
18476108b75Sapb "x", "desired", "actual", "err_abs", "err_rel");
18576108b75Sapb for (i = 0; i < __arraycount(xlist); i++)
18676108b75Sapb {
18776108b75Sapb uint32_t x = xlist[i];
18876108b75Sapb uint32_t desired = (uint32_t)(log10((double)x)
18976108b75Sapb * (double)(1<<24));
19076108b75Sapb uint32_t actual = dtv_intlog10(x);
19176108b75Sapb int32_t err_abs = actual - desired;
19276108b75Sapb double err_rel = (err_abs == 0 ? 0.0
19376108b75Sapb : err_abs / (double)actual);
19476108b75Sapb
19576108b75Sapb printf("%11"PRIu32" %11"PRIu32" %11"PRIu32
19676108b75Sapb " %+11"PRId32" %+.5f\n",
19776108b75Sapb x, desired, actual, err_abs, err_rel);
19876108b75Sapb }
19976108b75Sapb return 0;
20076108b75Sapb }
20176108b75Sapb
20276108b75Sapb #endif /* TEST_DTV_MATH */
203