Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys add dtv_intlog10 implementation, from apb@



details:   https://anonhg.NetBSD.org/src/rev/1487240bc7ce
branches:  trunk
changeset: 767332:1487240bc7ce
user:      jmcneill <jmcneill%NetBSD.org@localhost>
date:      Fri Jul 15 20:27:41 2011 +0000

description:
add dtv_intlog10 implementation, from apb@

diffstat:

 sys/dev/dtv/dtv_math.c   |  75 ++++++++++++++++++++++++++++++++++++++++++++++++
 sys/dev/dtv/dtvif.h      |   7 +++-
 sys/dev/dtv/files.dtv    |   3 +-
 sys/modules/dtv/Makefile |   3 +-
 4 files changed, 85 insertions(+), 3 deletions(-)

diffs (137 lines):

diff -r 05913c0311da -r 1487240bc7ce sys/dev/dtv/dtv_math.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/dtv/dtv_math.c    Fri Jul 15 20:27:41 2011 +0000
@@ -0,0 +1,75 @@
+/* $NetBSD: dtv_math.c,v 1.1 2011/07/15 20:27:42 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2011 Alan Barrett <apb%NetBSD.org@localhost>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: dtv_math.c,v 1.1 2011/07/15 20:27:42 jmcneill Exp $");
+
+#include <sys/bitops.h>
+
+#include <dev/dtv/dtvif.h>
+
+
+#define LOG10_2_x24 5050445    /* floor(log10(2.0) * 2**24 */
+
+/*
+ * dtv_intlog10 -- return an approximation to log10(x) * 1<<24,
+ * using integer arithmetic.
+ *
+ * As a special case, returns 0 when x == 0.
+ *
+ * Results should be approximately as follows, bearing in
+ * mind that this function returns only an approximation
+ * to the exact results.
+ *
+ * dtv_intlog10(0) = 0 (special case; the mathematical value is undefined)
+ * dtv_intlog10(1) = 0
+ * dtv_intlog10(2) = 5050445 (approx 0.30102999 * 2**24)
+ * dtv_intlog10(10) = 16777216 (1.0 * 2**24)
+ * dtv_intlog10(100) = 33554432 (2.0 * 2**24)
+ * dtv_intlog10(1000) = 50331648 (3.0 * 2**24)
+ * dtv_intlog10(10000) = 67108864 (4.0 * 2**24)
+ * dtv_intlog10(100000) = 83886080 (5.0 * 2**24)
+ * dtv_intlog10(1000000) = 100663296 (6.0 * 2**24)
+ * dtv_intlog10(10000000) = 117440512 (7.0 * 2**24)
+ * dtv_intlog10(100000000) = 134217728 (8.0 * 2**24)
+ * dtv_intlog10(1000000000) = 150994944 (9.0 * 2**24)
+ * dtv_intlog10(4294967295) = 161614248 (approx 9.63295986 * 2**24)
+ */
+uint32_t
+dtv_intlog10(uint32_t x)
+{
+       if (__predict_false(x == 0))
+               return 0;
+       /*
+        * all we do is find log2(x), as an integer between 0 and 31,
+        * and scale it.  Thus, there are only 32 values that this
+        * function will ever return.  To do a better job, we would
+        * need a lookup table and interpolation.
+        */
+       return (uint32_t)(LOG10_2_x24) * (uint32_t)ilog2(x);
+}
diff -r 05913c0311da -r 1487240bc7ce sys/dev/dtv/dtvif.h
--- a/sys/dev/dtv/dtvif.h       Fri Jul 15 19:41:28 2011 +0000
+++ b/sys/dev/dtv/dtvif.h       Fri Jul 15 20:27:41 2011 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: dtvif.h,v 1.1 2011/07/09 14:46:56 jmcneill Exp $ */
+/* $NetBSD: dtvif.h,v 1.2 2011/07/15 20:27:42 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2011 Jared D. McNeill <jmcneill%invisible.ca@localhost>
@@ -35,6 +35,8 @@
 #ifndef _DEV_DTV_DTVIF_H
 #define _DEV_DTV_DTVIF_H
 
+#include <sys/device.h>
+
 #include <dev/dtv/dtvio.h>
 
 #define        DTV_DEVICE_FRONTEND     0
@@ -77,4 +79,7 @@
 
 void   dtv_submit_payload(device_t, const struct dtv_payload *);
 
+/* dtv_math.c */
+uint32_t       dtv_intlog10(uint32_t);
+
 #endif /* !_DEV_DTV_DTVIF_H */
diff -r 05913c0311da -r 1487240bc7ce sys/dev/dtv/files.dtv
--- a/sys/dev/dtv/files.dtv     Fri Jul 15 19:41:28 2011 +0000
+++ b/sys/dev/dtv/files.dtv     Fri Jul 15 20:27:41 2011 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: files.dtv,v 1.2 2011/07/13 22:43:04 jmcneill Exp $
+# $NetBSD: files.dtv,v 1.3 2011/07/15 20:27:42 jmcneill Exp $
 
 define dtvbus { }
 
@@ -8,4 +8,5 @@
 file   dev/dtv/dtv_demux.c     dtv
 file   dev/dtv/dtv_device.c    dtv
 file   dev/dtv/dtv_ioctl.c     dtv
+file   dev/dtv/dtv_math.c      dtv
 file   dev/dtv/dtv_scatter.c   dtv
diff -r 05913c0311da -r 1487240bc7ce sys/modules/dtv/Makefile
--- a/sys/modules/dtv/Makefile  Fri Jul 15 19:41:28 2011 +0000
+++ b/sys/modules/dtv/Makefile  Fri Jul 15 20:27:41 2011 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.2 2011/07/13 22:43:33 jmcneill Exp $
+# $NetBSD: Makefile,v 1.3 2011/07/15 20:27:41 jmcneill Exp $
 
 .include "../Makefile.inc"
 
@@ -10,6 +10,7 @@
        dtv_demux.c \
        dtv_device.c \
        dtv_ioctl.c \
+       dtv_math.c \
        dtv_scatter.c
 
 WARNS= 4



Home | Main Index | Thread Index | Old Index