[goffice] GOQuad: add constants.



commit fe5b22fad230ade7cc072b792c151654faa2b554
Author: Morten Welinder <terra gnome org>
Date:   Mon Nov 4 10:45:03 2013 -0500

    GOQuad: add constants.
    
    The usual suspects, more or less: pi, e, ln2, sqrt2.  Also 1 and Euler
    for no good reason.

 ChangeLog              |    6 ++
 NEWS                   |    3 +
 goffice/math/go-quad.c |  120 +++++++++++++++++++++++++++++++++++++++++++++++-
 goffice/math/go-quad.h |   17 +++++++
 4 files changed, 144 insertions(+), 2 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 69791bf..6cfc531 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2013-11-04  Morten Welinder  <terra gnome org>
+
+       * goffice/math/go-quad.h (go_quad_one, go_quad_pi, go_quad_e)
+       (go_quad_ln2, go_quad_sqrt2, go_quad_euler): Supply a handful of
+       useful constants at high accuracy.
+
 2013-10-07  Morten Welinder <terra gnome org>
 
        * configure.ac: Post-release bump.
diff --git a/NEWS b/NEWS
index 059d2d6..aebd313 100644
--- a/NEWS
+++ b/NEWS
@@ -1,5 +1,8 @@
 goffice 0.10.9:
 
+Morten:
+       * Improvements to quad precision code.
+
 --------------------------------------------------------------------------
 goffice 0.10.8:
 
diff --git a/goffice/math/go-quad.c b/goffice/math/go-quad.c
index a853e3b..acb95dd 100644
--- a/goffice/math/go-quad.c
+++ b/goffice/math/go-quad.c
@@ -120,11 +120,12 @@ void *
 SUFFIX(go_quad_start) (void)
 {
        void *res = NULL;
+       static gboolean first = TRUE;
 
        if (SUFFIX(go_quad_depth)++ > 0)
                return NULL;
 
-       if (!SUFFIX(go_quad_functional) ())
+       if (!SUFFIX(go_quad_functional) () && first)
                g_warning ("quad precision math may not be completely accurate.");
 
 #ifdef i386
@@ -144,7 +145,97 @@ SUFFIX(go_quad_start) (void)
        }
 #endif
 
-       SUFFIX(CST) = 1 + SUFFIX(ldexp) (1.0, (DOUBLE_MANT_DIG + 1) / 2);
+       if (first) {
+               /*
+                * Calculate constants in a way doesn't depend on the
+                * layout of DOUBLE.  We use ~400 bits of data in the
+                * tables below -- that's way more than needed even
+                * for sunos' long double.
+                */
+
+               static const guint8 pi_hex_digits[] = {
+                       0x03, 0x24, 0x3f, 0x6a, 0x88, 0x85, 0xa3, 0x08,
+                       0xd3, 0x13, 0x19, 0x8a, 0x2e, 0x03, 0x70, 0x73,
+                       0x44, 0xa4, 0x09, 0x38, 0x22, 0x29, 0x9f, 0x31,
+                       0xd0, 0x08, 0x2e, 0xfa, 0x98, 0xec, 0x4e, 0x6c,
+                       0x89, 0x45, 0x28, 0x21, 0xe6, 0x38, 0xd0, 0x13,
+                       0x77, 0xbe, 0x54, 0x66, 0xcf, 0x34, 0xe9, 0x0c,
+                       0x6c, 0xc0, 0xac
+               };
+
+               static const guint8 e_hex_digits[] = {
+                       0x02, 0xb7, 0xe1, 0x51, 0x62, 0x8a, 0xed, 0x2a,
+                       0x6a, 0xbf, 0x71, 0x58, 0x80, 0x9c, 0xf4, 0xf3,
+                       0xc7, 0x62, 0xe7, 0x16, 0x0f, 0x38, 0xb4, 0xda,
+                       0x56, 0xa7, 0x84, 0xd9, 0x04, 0x51, 0x90, 0xcf,
+                       0xef, 0x32, 0x4e, 0x77, 0x38, 0x92, 0x6c, 0xfb,
+                       0xe5, 0xf4, 0xbf, 0x8d, 0x8d, 0x8c, 0x31, 0xd7,
+                       0x63, 0xda, 0x06
+               };
+
+               static const guint8 ln2_hex_digits[] = {
+                       0xb1, 0x72, 0x17, 0xf7, 0xd1, 0xcf, 0x79, 0xab,
+                       0xc9, 0xe3, 0xb3, 0x98, 0x03, 0xf2, 0xf6, 0xaf,
+                       0x40, 0xf3, 0x43, 0x26, 0x72, 0x98, 0xb6, 0x2d,
+                       0x8a, 0x0d, 0x17, 0x5b, 0x8b, 0xaa, 0xfa, 0x2b,
+                       0xe7, 0xb8, 0x76, 0x20, 0x6d, 0xeb, 0xac, 0x98,
+                       0x55, 0x95, 0x52, 0xfb, 0x4a, 0xfa, 0x1b, 0x10,
+                       0xed, 0x2e
+               };
+
+               static const guint8 sqrt2_hex_digits[] = {
+                       0x01, 0x6a, 0x09, 0xe6, 0x67, 0xf3, 0xbc, 0xc9,
+                       0x08, 0xb2, 0xfb, 0x13, 0x66, 0xea, 0x95, 0x7d,
+                       0x3e, 0x3a, 0xde, 0xc1, 0x75, 0x12, 0x77, 0x50,
+                       0x99, 0xda, 0x2f, 0x59, 0x0b, 0x06, 0x67, 0x32,
+                       0x2a, 0x95, 0xf9, 0x06, 0x08, 0x75, 0x71, 0x45,
+                       0x87, 0x51, 0x63, 0xfc, 0xdf, 0xb9, 0x07, 0xb6,
+                       0x72, 0x1e, 0xe9
+               };
+
+               static const guint8 euler_hex_digits[] = {
+                       0x93, 0xc4, 0x67, 0xe3, 0x7d, 0xb0, 0xc7, 0xa4,
+                       0xd1, 0xbe, 0x3f, 0x81, 0x01, 0x52, 0xcb, 0x56,
+                       0xa1, 0xce, 0xcc, 0x3a, 0xf6, 0x5c, 0xc0, 0x19,
+                       0x0c, 0x03, 0xdf, 0x34, 0x70, 0x9a, 0xff, 0xbd,
+                       0x8e, 0x4b, 0x59, 0xfa, 0x03, 0xa9, 0xf0, 0xee,
+                       0xd0, 0x64, 0x9c, 0xcb, 0x62, 0x10, 0x57, 0xd1,
+                       0x10, 0x56
+               };
+
+               first = FALSE;
+               SUFFIX(CST) = 1 + SUFFIX(ldexp) (1.0, (DOUBLE_MANT_DIG + 1) / 2);
+               SUFFIX(go_quad_constant8) ((QUAD *)&SUFFIX(go_quad_pi),
+                                          pi_hex_digits,
+                                          G_N_ELEMENTS (pi_hex_digits),
+                                          256.0,
+                                          256.0);
+               g_printerr ("pi=%.20g\n", (double)(SUFFIX(go_quad_value) (&SUFFIX(go_quad_pi))));
+
+               SUFFIX(go_quad_constant8) ((QUAD *)&SUFFIX(go_quad_e),
+                                          e_hex_digits,
+                                          G_N_ELEMENTS (e_hex_digits),
+                                          256.0,
+                                          256.0);
+
+               SUFFIX(go_quad_constant8) ((QUAD *)&SUFFIX(go_quad_ln2),
+                                          ln2_hex_digits,
+                                          G_N_ELEMENTS (ln2_hex_digits),
+                                          256.0,
+                                          1);
+
+               SUFFIX(go_quad_constant8) ((QUAD *)&SUFFIX(go_quad_sqrt2),
+                                          sqrt2_hex_digits,
+                                          G_N_ELEMENTS (sqrt2_hex_digits),
+                                          256.0,
+                                          256.0);
+
+               SUFFIX(go_quad_constant8) ((QUAD *)&SUFFIX(go_quad_euler),
+                                          euler_hex_digits,
+                                          G_N_ELEMENTS (euler_hex_digits),
+                                          256.0,
+                                          1);
+       }
 
        return res;
 }
@@ -172,6 +263,12 @@ SUFFIX(go_quad_end) (void *state)
 }
 
 const QUAD SUFFIX(go_quad_zero);
+const QUAD SUFFIX(go_quad_one) = { 1, 0 };
+const QUAD SUFFIX(go_quad_pi);
+const QUAD SUFFIX(go_quad_e);
+const QUAD SUFFIX(go_quad_ln2);
+const QUAD SUFFIX(go_quad_sqrt2);
+const QUAD SUFFIX(go_quad_euler);
 
 /**
  * go_quad_init: (skip)
@@ -327,3 +424,22 @@ SUFFIX(go_quad_dot_product) (QUAD *res, const QUAD *a, const QUAD *b, int n)
                SUFFIX(go_quad_add) (res, res, &d);
        }
 }
+
+void
+SUFFIX(go_quad_constant8) (QUAD *res, const guint8 *data, gsize n,
+                          DOUBLE base, DOUBLE scale)
+{
+       QUAD qbase, q;
+
+       *res = SUFFIX(go_quad_zero);
+       SUFFIX(go_quad_init) (&qbase, base);
+
+       while (n-- > 0) {
+               SUFFIX(go_quad_init) (&q, data[n]);
+               SUFFIX(go_quad_add) (res, res, &q);
+               SUFFIX(go_quad_div) (res, res, &qbase);
+       }
+
+       SUFFIX(go_quad_init) (&q, scale);
+       SUFFIX(go_quad_mul) (res, res, &q);
+}
diff --git a/goffice/math/go-quad.h b/goffice/math/go-quad.h
index a49979f..39c8dd2 100644
--- a/goffice/math/go-quad.h
+++ b/goffice/math/go-quad.h
@@ -27,7 +27,15 @@ void go_quad_mul12 (GOQuad *res, double x, double y);
 
 void go_quad_dot_product (GOQuad *res, const GOQuad *a, const GOQuad *b, int n);
 
+void go_quad_constant8 (GOQuad *res, const guint8 *data, gsize n, double base, double scale);
+
 GO_VAR_DECL const GOQuad go_quad_zero;
+GO_VAR_DECL const GOQuad go_quad_one;
+GO_VAR_DECL const GOQuad go_quad_pi;
+GO_VAR_DECL const GOQuad go_quad_e;
+GO_VAR_DECL const GOQuad go_quad_ln2;
+GO_VAR_DECL const GOQuad go_quad_sqrt2;
+GO_VAR_DECL const GOQuad go_quad_euler;
 
 #ifdef GOFFICE_WITH_LONG_DOUBLE
 struct GOQuadl_ {
@@ -53,7 +61,16 @@ void go_quad_mul12l (GOQuadl *res, long double x, long double y);
 void go_quad_dot_productl (GOQuadl *res,
                           const GOQuadl *a, const GOQuadl *b, int n);
 
+void go_quad_constant8l (GOQuadl *res, const guint8 *data, gsize n, long double base, long double scale);
+
 GO_VAR_DECL const GOQuadl go_quad_zerol;
+GO_VAR_DECL const GOQuadl go_quad_onel;
+GO_VAR_DECL const GOQuadl go_quad_pil;
+GO_VAR_DECL const GOQuadl go_quad_el;
+GO_VAR_DECL const GOQuadl go_quad_ln2l;
+GO_VAR_DECL const GOQuadl go_quad_sqrt2l;
+GO_VAR_DECL const GOQuadl go_quad_eulerl;
+
 #endif
 
 G_END_DECLS


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]