Home | History | Annotate | Line # | Download | only in dtv
dtv_math.c revision 1.2
      1 /* $NetBSD: dtv_math.c,v 1.2 2011/07/16 16:13:13 jmcneill 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.2 2011/07/16 16:13:13 jmcneill Exp $");
     31 
     32 #include <sys/types.h>
     33 #include <sys/bitops.h>
     34 
     35 #include <dev/dtv/dtvif.h>
     36 
     37 
     38 #define LOG10_2_x24 5050445	/* floor(log10(2.0) * 2**24 */
     39 
     40 /*
     41  * dtv_intlog10 -- return an approximation to log10(x) * 1<<24,
     42  * using integer arithmetic.
     43  *
     44  * As a special case, returns 0 when x == 0.
     45  *
     46  * Results should be approximately as follows, bearing in
     47  * mind that this function returns only an approximation
     48  * to the exact results.
     49  *
     50  * dtv_intlog10(0) = 0 (special case; the mathematical value is undefined)
     51  * dtv_intlog10(1) = 0
     52  * dtv_intlog10(2) = 5050445 (approx 0.30102999 * 2**24)
     53  * dtv_intlog10(10) = 16777216 (1.0 * 2**24)
     54  * dtv_intlog10(100) = 33554432 (2.0 * 2**24)
     55  * dtv_intlog10(1000) = 50331648 (3.0 * 2**24)
     56  * dtv_intlog10(10000) = 67108864 (4.0 * 2**24)
     57  * dtv_intlog10(100000) = 83886080 (5.0 * 2**24)
     58  * dtv_intlog10(1000000) = 100663296 (6.0 * 2**24)
     59  * dtv_intlog10(10000000) = 117440512 (7.0 * 2**24)
     60  * dtv_intlog10(100000000) = 134217728 (8.0 * 2**24)
     61  * dtv_intlog10(1000000000) = 150994944 (9.0 * 2**24)
     62  * dtv_intlog10(4294967295) = 161614248 (approx 9.63295986 * 2**24)
     63  */
     64 uint32_t
     65 dtv_intlog10(uint32_t x)
     66 {
     67 	if (__predict_false(x == 0))
     68 		return 0;
     69 	/*
     70 	 * all we do is find log2(x), as an integer between 0 and 31,
     71 	 * and scale it.  Thus, there are only 32 values that this
     72 	 * function will ever return.  To do a better job, we would
     73 	 * need a lookup table and interpolation.
     74 	 */
     75 	return (uint32_t)(LOG10_2_x24) * (uint32_t)ilog2(x);
     76 }
     77