]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Include the float-point table generator utility in the tools directory. fp-accuracy
authordrh <>
Tue, 17 Mar 2026 09:45:47 +0000 (09:45 +0000)
committerdrh <>
Tue, 17 Mar 2026 09:45:47 +0000 (09:45 +0000)
FossilOrigin-Name: 4ab10b251d870af8297b9e505452393f6000f83784c822ff128e7aec06f8c72f

manifest
manifest.uuid
src/util.c
tool/mkfptab.c [new file with mode: 0644]

index 0957ae5cc3d19f9fb0ac498bc776c88460402e38..142aa253b982fcf006d420788fb78be5f90c7c6d 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Reorder\sbounds\schecking\sin\ssqlite3Fp10Convert2()
-D 2026-03-17T09:18:36.777
+C Include\sthe\sfloat-point\stable\sgenerator\sutility\sin\sthe\stools\sdirectory.
+D 2026-03-17T09:45:47.412
 F .fossil-settings/binary-glob 61195414528fb3ea9693577e1980230d78a1f8b0a54c78cf1b9b24d0a409ed6a x
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
@@ -797,7 +797,7 @@ F src/trigger.c 4bf3bfb3851d165e4404a9f9e69357345f3f7103378c07e07139fdd8aeb7bd20
 F src/update.c 3e5e7ff66fa19ebe4d1b113d480639a24cc1175adbefabbd1a948a07f28e37cf
 F src/upsert.c 215328c3f91623c520ec8672c44323553f12caeb4f01b1090ebdca99fdf7b4f1
 F src/utf.c 7267c3fb9e2467020507601af3354c2446c61f444387e094c779dccd5ca62165
-F src/util.c ced03971dad5ae9418a2ced329506de16e5ff2ed7152e059400499332c1a593b
+F src/util.c cf91389b58590edfb5978199ef59488ef8e3723e1ba1aa0ff15c62f8a658b95f
 F src/vacuum.c d3d35d8ae893d419ade5fa196d761a83bddcbb62137a1a157ae751ef38b26e82
 F src/vdbe.c 5328c99dd256ee8132383565a86e253543a85daccfd7477c52f20bac6b385a7f
 F src/vdbe.h 966d0677a540b7ea6549b7c4e1312fc0d830fce3a235a58c801f2cc31cf5ecf9
@@ -2136,6 +2136,7 @@ F tool/mkautoconfamal.sh 06fbe090b81c24e592c1f22b404334f805ba74d482a9260f2ac81e6
 F tool/mkccode.tcl c42a8f8cf78f92e83795d5447460dbce7aaf78a3bbf9082f1507dc71a3665f3c x
 F tool/mkcombo.tcl 2a5189b219c4a495e1ff7fc980bd568d3cfb82ae9d50c84e77f7a161e96fc132
 F tool/mkctimec.tcl 3fb5cad05922f5da61262cb6bcd5868a34e94a49ca8833ae2d7796e7df075576 x
+F tool/mkfptab.c 24ea40113f96584caca3f6dd06b4ad5adffa0f39023910cb83fa7ef2ffa9b0ba
 F tool/mkkeywordhash.c 82d5af1d0e677900739fba59155cddac172d8c712c2d91ab73d6e6bcb30060f0
 F tool/mkmsvcmin.tcl d76c45efda1cce2d4005bcea7b8a22bb752e3256009f331120fb4fecb14ebb7a
 F tool/mkopcodec.tcl 33d20791e191df43209b77d37f0ff0904620b28465cca6990cf8d60da61a07ef
@@ -2192,8 +2193,8 @@ F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee
 F tool/warnings.sh d924598cf2f55a4ecbc2aeb055c10bd5f48114793e7ba25f9585435da29e7e98
 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
 F tool/winmain.c 00c8fb88e365c9017db14c73d3c78af62194d9644feaf60e220ab0f411f3604c
-P a19c0785dcfb9fb74963c45b161b12ac8f4379e2b990f6c5de0d7b959c5be98d
-R 31509aecbe36d702bbcca4c3d7b1c6e3
+P 23ad656edb3a63e7e6602770e1f2c4a5ef1f3d41565e7f13408b5305619dfa3d
+R 0212d90b78d20068a702d7e70e4b68de
 U drh
-Z 3ad3a7a0646ac1e8f0a50096402694d2
+Z a52753c1b6f0991d0a121a4d3cb8bc3e
 # Remove this line to create a well-formed Fossil manifest.
index 44a851c32e34d8b9ebe8eea654555738f73af58f..9d1ef375b7940e290314f1e59647edf7e2507802 100644 (file)
@@ -1 +1 @@
-23ad656edb3a63e7e6602770e1f2c4a5ef1f3d41565e7f13408b5305619dfa3d
+4ab10b251d870af8297b9e505452393f6000f83784c822ff128e7aec06f8c72f
index 8a31c29fec310d19acdf0d98874d96568c3caf2c..1d9c30fc2c981dd11090300b48dd4309e610191a 100644 (file)
@@ -555,6 +555,9 @@ static u64 sqlite3Multiply160(u64 a, u32 aLo, u64 b, u32 *pLo){
 ** (2) For p outside the range 0 to 26, use aScale[] for the initial value
 **     then refine that result (if necessary) by a single multiplication
 **     against aBase[].
+**
+** The constant tables aBase[], aScale[], and aScaleLo[] are generated
+** by the C program at ../tool/mkfptab.c run with the --round option.
 */
 static u64 powerOfTen(int p, u32 *pLo){
   static const u64 aBase[] = {
@@ -600,7 +603,7 @@ static u64 powerOfTen(int p, u32 *pLo){
     UINT64_C(0xf2d56790ab41c2a2), /* 10: 1.0e-81 << 333 */
     UINT64_C(0xc428d05aa4751e4c), /* 11: 1.0e-54 << 243 */
     UINT64_C(0x9e74d1b791e07e48), /* 12: 1.0e-27 << 153 */
-    UINT64_C(0xcccccccccccccccc), /* 13: 1.0e+0 << 63 (Special case) */
+    UINT64_C(0xcccccccccccccccc), /* 13: 1.0e-1 << 67 (special case) */
     UINT64_C(0xcecb8f27f4200f3a), /* 14: 1.0e+27 >> 26 */
     UINT64_C(0xa70c3c40a64e6c51), /* 15: 1.0e+54 >> 116 */
     UINT64_C(0x86f0ac99b4e8dafd), /* 16: 1.0e+81 >> 206 */
@@ -628,7 +631,7 @@ static u64 powerOfTen(int p, u32 *pLo){
     0xfae27299, /* 10: 1.0e-81 << 333 */
     0xaa97e14c, /* 11: 1.0e-54 << 243 */
     0x775ea265, /* 12: 1.0e-27 << 153 */
-    0xcccccccc, /* 13: 1.0e-1 << 67 (Special case) */
+    0xcccccccc, /* 13: 1.0e-1 << 67 (special case) */
     0x00000000, /* 14: 1.0e+27 >> 26 */
     0x999090b6, /* 15: 1.0e+54 >> 116 */
     0x69a028bb, /* 16: 1.0e+81 >> 206 */
diff --git a/tool/mkfptab.c b/tool/mkfptab.c
new file mode 100644 (file)
index 0000000..e40e04b
--- /dev/null
@@ -0,0 +1,238 @@
+/*
+** This program generates C code for the tables used to generate powers
+** of 10 in the powerOfTen() subroutine in util.c.
+**
+** The objective of the powerOfTen() subroutine is to provide the most
+** significant 96 bits of any power of 10 between -348 and +347.  Rather
+** than generate a massive 8K table, three much smaller tables are constructed,
+** which can then generate the requested power of 10 using a single
+** 160-bit multiple.
+**
+** This program works by internally generating a table of powers of
+** 10 accurate to 256 bits each.  It then that full-sized, high-accuracy
+** table to construct the three smaller tables needed by powerOfTen().
+**
+** LIMITATION:
+**
+** This program uses the __uint128_t datatype, available in gcc/clang.
+** It won't build using other compilers.
+*/
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <assert.h>
+
+typedef unsigned __int128 u128;          /* 128-bit unsigned integer */
+typedef unsigned long long int u64;      /* 64-bit unsigned integer */
+
+/* There is no native 256-bit unsigned integer type, so synthesize one
+** using four 64-bit unsigned integers.  Must significant first. */
+struct u256 {
+  u64 a[4];  /* big-endian */
+};
+typedef struct u256 u256;
+
+/*
+** Return a u64 with the N-th bit set.
+*/
+#define U64_BIT(N)  (((u64)1)<<(N))
+
+/* Multiple *pX by 10, in-place */
+static void u256_times_10(u256 *pX){
+  u64 carry = 0;
+  int i;
+  for(i=3; i>=0; i--){
+    u128 y = (10 * (u128)pX->a[i]) + carry;
+    pX->a[i] = (u64)y;
+    carry = (u64)(y>>64);
+  }
+}
+
+/* Multiple *pX by 2, in-place.  AKA, left-shift */
+static void u256_times_2(u256 *pX){
+  u64 carry = 0;
+  int i;
+  for(i=3; i>=0; i--){
+    u64 y = pX->a[i];
+    pX->a[i] = (y<<1) | carry;
+    carry = y>>63;
+  }
+}
+
+/* Divide *pX by 10, in-place */
+static void u256_div_10(u256 *pX){
+  u64 rem = 0;
+  int i;
+  for(i=0; i<4; i++){
+    u128 acc = (((u128)rem)<<64) | pX->a[i];
+    pX->a[i] = acc/10;
+    rem = (u64)(acc%10);
+  }
+}
+
+/* Divide *pX by 2, in-place,  AKA, right-shift */
+static void u256_div_2(u256 *pX){
+  u64 rem = 0;
+  int i;
+  for(i=0; i<4; i++){
+    u64 y = pX->a[i];
+    pX->a[i] = (y>>1) | (rem<<63);
+    rem = y&1;
+  }
+}
+
+/* Note: The main table is a little larger on the low end than the required
+** range of -348..+347, since we need the -351 value for the reduced tables.
+*/
+#define SCALE_FIRST  (-351)                          /* Least power-of-10 */
+#define SCALE_LAST   (+347)                          /* Largest power-of-10 */
+#define SCALE_COUNT  (SCALE_LAST - SCALE_FIRST + 1)  /* Size of main table */
+#define SCALE_ZERO   (351)
+
+int main(int argc, char **argv){
+  int i, j, iNext;
+  int e;
+  int bRound = 0;
+  int bTruth = 0;
+  const u64 top = ((u64)1)<<63;
+  u256 v;
+  u64 aHi[SCALE_COUNT];
+  u64 aLo[SCALE_COUNT];
+  int aE[SCALE_COUNT];
+
+  for(i=1; i<argc; i++){
+    const char *z = argv[i];
+    if( z[0]=='-' && z[1]=='-' && z[2]!=0 ) z++;
+    if( strcmp(z,"-round")==0 ){
+      bRound = 1;
+    }else
+    if( strcmp(z,"-truth")==0 ){
+      bTruth = 1;
+    }else
+    {
+      fprintf(stderr, "unknown option: \"%s\"\n", argv[i]);
+      exit(1);
+    }
+  }
+
+  /* Generate the master 256-bit power-of-10 table */
+  v.a[0] = top;
+  v.a[1] = 0;
+  v.a[2] = 0;
+  v.a[3] = 0;
+  for(i=SCALE_ZERO, e=63; i>=0; i--){
+    aHi[i] = v.a[0];
+    aLo[i] = v.a[1];
+    aE[i] = e;
+    u256_div_10(&v);
+    while( v.a[0] < top ){
+      e++;
+      u256_times_2(&v);
+    }
+  }
+  v.a[0] = 0;
+  v.a[1] = top;
+  v.a[2] = 0;
+  v.a[3] = 0;
+  for(i=SCALE_ZERO+1, e=63; i<SCALE_COUNT; i++){
+    u256_times_10(&v);
+    while( v.a[0]>0 ){
+      e--;
+      u256_div_2(&v);
+    }
+    aHi[i] = v.a[1];
+    aLo[i] = v.a[2];
+    aE[i] = e;
+  }
+
+  if( bTruth ){
+    /* With the --truth flag, also output the aTruth[] table that
+    ** contains 128 bits of every power-of-two in the range */
+    printf("  /* Powers of ten, accurate to 128 bits each */\n");
+    printf("  static const struct {u64 hi; u64 lo;} aTruth[] = {\n");
+    for(i=0; i<SCALE_COUNT; i++){
+      u64 x = aHi[i];
+      u64 y = aLo[i];
+      int e = aE[i];
+      char *zOp;
+      if( e>0 ){
+        zOp = "<<";
+      }else{
+        e = -e;
+        zOp = ">>";
+      }
+      printf("   {0x%016llx, 0x%016llx}, /* %2d: 1.0e%+d %s %d */\n",
+           x, y, i, i+SCALE_FIRST, zOp, e);
+    }
+    printf("  };\n");
+  }
+
+  /* The aBase[] table contains powers of 10 between 0 and 26.  These
+  ** all fit in a single 64-bit integer.
+  */
+  printf("  static const u64 aBase[] = {\n");
+  for(i=SCALE_ZERO, j=0; i<SCALE_ZERO+27; i++, j++){
+    u64 x = aHi[i];
+    int e = aE[i];
+    char *zOp;
+    if( e>0 ){
+      zOp = "<<";
+    }else{
+      e = -e;
+      zOp = ">>";
+    }
+    printf("    UINT64_C(0x%016llx), /* %2d: 1.0e%+d %s %d */\n",
+           x, j, i+SCALE_FIRST, zOp, e);
+  }
+  printf("  };\n");
+
+  /* For powers of 10 outside the range [0..26], we have to multiple
+  ** on of the aBase[] entries by a scaling factor to get the true
+  ** power of ten.  The scaling factors are all approximates accurate
+  ** to 96 bytes, represented by a 64-bit integer in aScale[] for the
+  ** most significant bits and a 32-bit integer in aScaleLo[] for the
+  ** next 32 bites.
+  **
+  ** The scale factors are at increments of 27.  Except, the entry for 0
+  ** is replaced by the -1 value as a special case.
+  */
+  printf("  static const u64 aScale[] = {\n");
+  for(i=j=0; i<SCALE_COUNT; i=iNext, j++){
+    const char *zExtra = "";
+    iNext = i+27;
+    if( i==SCALE_ZERO ){ i--; zExtra = " (special case)"; }
+    u64 x = aHi[i];
+    int e = aE[i];
+    char *zOp;
+    if( e>0 ){
+      zOp = "<<";
+    }else{
+      e = -e;
+      zOp = ">>";
+    }
+    printf("    UINT64_C(0x%016llx), /* %2d: 1.0e%+d %s %d%s */\n",
+           x, j, i+SCALE_FIRST, zOp, e, zExtra);
+  }
+  printf("  };\n");
+  printf("  static const unsigned int aScaleLo[] = {\n");
+  for(i=j=0; i<SCALE_COUNT; i=iNext, j++){
+    const char *zExtra = "";
+    iNext = i+27;
+    if( i==SCALE_ZERO ){ i--; zExtra = " (special case)"; }
+    u64 x = aLo[i];
+    int e = aE[i];
+    char *zOp;
+    if( bRound && (x & U64_BIT(31))!=0 && i!=SCALE_ZERO-1 ) x += U64_BIT(32);
+    if( e>0 ){
+      zOp = "<<";
+    }else{
+      e = -e;
+      zOp = ">>";
+    }
+    printf("    0x%08llx, /* %2d: 1.0e%+d %s %d%s */\n",
+           x>>32, j, i+SCALE_FIRST, zOp, e, zExtra);
+  }
+  printf("  };\n");
+  return 0;
+}