sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 1 | /* |
| 2 | ** This program generates C code for the tables used to generate powers |
| 3 | ** of 10 in the powerOfTen() subroutine in util.c. |
| 4 | ** |
| 5 | ** The objective of the powerOfTen() subroutine is to provide the most |
| 6 | ** significant 96 bits of any power of 10 between -348 and +347. Rather |
| 7 | ** than generate a massive 8K table, three much smaller tables are constructed, |
| 8 | ** which can then generate the requested power of 10 using a single |
| 9 | ** 160-bit multiple. |
| 10 | ** |
| 11 | ** This program works by internally generating a table of powers of |
| 12 | ** 10 accurate to 256 bits each. It then that full-sized, high-accuracy |
| 13 | ** table to construct the three smaller tables needed by powerOfTen(). |
| 14 | ** |
| 15 | ** LIMITATION: |
| 16 | ** |
| 17 | ** This program uses the __uint128_t datatype, available in gcc/clang. |
| 18 | ** It won't build using other compilers. |
| 19 | */ |
| 20 | #include <stdint.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <stdio.h> |
| 24 | #include <assert.h> |
| 25 | |
| 26 | typedef unsigned __int128 u128; /* 128-bit unsigned integer */ |
| 27 | typedef unsigned long long int u64; /* 64-bit unsigned integer */ |
| 28 | |
| 29 | /* There is no native 256-bit unsigned integer type, so synthesize one |
| 30 | ** using four 64-bit unsigned integers. Must significant first. */ |
| 31 | struct u256 { |
| 32 | u64 a[4]; /* big-endian */ |
| 33 | }; |
| 34 | typedef struct u256 u256; |
| 35 | |
| 36 | /* |
| 37 | ** Return a u64 with the N-th bit set. |
| 38 | */ |
| 39 | #define U64_BIT(N) (((u64)1)<<(N)) |
| 40 | |
| 41 | /* Multiple *pX by 10, in-place */ |
| 42 | static void u256_times_10(u256 *pX){ |
| 43 | u64 carry = 0; |
| 44 | int i; |
| 45 | for(i=3; i>=0; i--){ |
| 46 | u128 y = (10 * (u128)pX->a[i]) + carry; |
| 47 | pX->a[i] = (u64)y; |
| 48 | carry = (u64)(y>>64); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /* Multiple *pX by 2, in-place. AKA, left-shift */ |
| 53 | static void u256_times_2(u256 *pX){ |
| 54 | u64 carry = 0; |
| 55 | int i; |
| 56 | for(i=3; i>=0; i--){ |
| 57 | u64 y = pX->a[i]; |
| 58 | pX->a[i] = (y<<1) | carry; |
| 59 | carry = y>>63; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /* Divide *pX by 10, in-place */ |
| 64 | static void u256_div_10(u256 *pX){ |
| 65 | u64 rem = 0; |
| 66 | int i; |
| 67 | for(i=0; i<4; i++){ |
| 68 | u128 acc = (((u128)rem)<<64) | pX->a[i]; |
| 69 | pX->a[i] = acc/10; |
| 70 | rem = (u64)(acc%10); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /* Divide *pX by 2, in-place, AKA, right-shift */ |
| 75 | static void u256_div_2(u256 *pX){ |
| 76 | u64 rem = 0; |
| 77 | int i; |
| 78 | for(i=0; i<4; i++){ |
| 79 | u64 y = pX->a[i]; |
| 80 | pX->a[i] = (y>>1) | (rem<<63); |
| 81 | rem = y&1; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /* Note: The main table is a little larger on the low end than the required |
| 86 | ** range of -348..+347, since we need the -351 value for the reduced tables. |
| 87 | */ |
| 88 | #define SCALE_FIRST (-351) /* Least power-of-10 */ |
| 89 | #define SCALE_LAST (+347) /* Largest power-of-10 */ |
| 90 | #define SCALE_COUNT (SCALE_LAST - SCALE_FIRST + 1) /* Size of main table */ |
| 91 | #define SCALE_ZERO (351) |
| 92 | |
| 93 | int main(int argc, char **argv){ |
| 94 | int i, j, iNext; |
| 95 | int e; |
| 96 | int bRound = 0; |
| 97 | int bTruth = 0; |
| 98 | const u64 top = ((u64)1)<<63; |
| 99 | u256 v; |
| 100 | u64 aHi[SCALE_COUNT]; |
| 101 | u64 aLo[SCALE_COUNT]; |
| 102 | int aE[SCALE_COUNT]; |
| 103 | |
| 104 | for(i=1; i<argc; i++){ |
| 105 | const char *z = argv[i]; |
| 106 | if( z[0]=='-' && z[1]=='-' && z[2]!=0 ) z++; |
| 107 | if( strcmp(z,"-round")==0 ){ |
| 108 | bRound = 1; |
| 109 | }else |
| 110 | if( strcmp(z,"-truth")==0 ){ |
| 111 | bTruth = 1; |
| 112 | }else |
| 113 | { |
| 114 | fprintf(stderr, "unknown option: \"%s\"\n", argv[i]); |
| 115 | exit(1); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /* Generate the master 256-bit power-of-10 table */ |
| 120 | v.a[0] = top; |
| 121 | v.a[1] = 0; |
| 122 | v.a[2] = 0; |
| 123 | v.a[3] = 0; |
| 124 | for(i=SCALE_ZERO, e=63; i>=0; i--){ |
| 125 | aHi[i] = v.a[0]; |
| 126 | aLo[i] = v.a[1]; |
| 127 | aE[i] = e; |
| 128 | u256_div_10(&v); |
| 129 | while( v.a[0] < top ){ |
| 130 | e++; |
| 131 | u256_times_2(&v); |
| 132 | } |
| 133 | } |
| 134 | v.a[0] = 0; |
| 135 | v.a[1] = top; |
| 136 | v.a[2] = 0; |
| 137 | v.a[3] = 0; |
| 138 | for(i=SCALE_ZERO+1, e=63; i<SCALE_COUNT; i++){ |
| 139 | u256_times_10(&v); |
| 140 | while( v.a[0]>0 ){ |
| 141 | e--; |
| 142 | u256_div_2(&v); |
| 143 | } |
| 144 | aHi[i] = v.a[1]; |
| 145 | aLo[i] = v.a[2]; |
| 146 | aE[i] = e; |
| 147 | } |
| 148 | |
| 149 | if( bTruth ){ |
| 150 | /* With the --truth flag, also output the aTruth[] table that |
| 151 | ** contains 128 bits of every power-of-two in the range */ |
| 152 | printf(" /* Powers of ten, accurate to 128 bits each */\n"); |
| 153 | printf(" static const struct {u64 hi; u64 lo;} aTruth[] = {\n"); |
| 154 | for(i=0; i<SCALE_COUNT; i++){ |
| 155 | u64 x = aHi[i]; |
| 156 | u64 y = aLo[i]; |
| 157 | int e = aE[i]; |
| 158 | char *zOp; |
| 159 | if( e>0 ){ |
| 160 | zOp = "<<"; |
| 161 | }else{ |
| 162 | e = -e; |
| 163 | zOp = ">>"; |
| 164 | } |
| 165 | printf(" {0x%016llx, 0x%016llx}, /* %2d: 1.0e%+d %s %d */\n", |
| 166 | x, y, i, i+SCALE_FIRST, zOp, e); |
| 167 | } |
| 168 | printf(" };\n"); |
| 169 | } |
| 170 | |
| 171 | /* The aBase[] table contains powers of 10 between 0 and 26. These |
| 172 | ** all fit in a single 64-bit integer. |
| 173 | */ |
| 174 | printf(" static const u64 aBase[] = {\n"); |
| 175 | for(i=SCALE_ZERO, j=0; i<SCALE_ZERO+27; i++, j++){ |
| 176 | u64 x = aHi[i]; |
| 177 | int e = aE[i]; |
| 178 | char *zOp; |
| 179 | if( e>0 ){ |
| 180 | zOp = "<<"; |
| 181 | }else{ |
| 182 | e = -e; |
| 183 | zOp = ">>"; |
| 184 | } |
| 185 | printf(" UINT64_C(0x%016llx), /* %2d: 1.0e%+d %s %d */\n", |
| 186 | x, j, i+SCALE_FIRST, zOp, e); |
| 187 | } |
| 188 | printf(" };\n"); |
| 189 | |
| 190 | /* For powers of 10 outside the range [0..26], we have to multiple |
| 191 | ** on of the aBase[] entries by a scaling factor to get the true |
| 192 | ** power of ten. The scaling factors are all approximates accurate |
| 193 | ** to 96 bytes, represented by a 64-bit integer in aScale[] for the |
| 194 | ** most significant bits and a 32-bit integer in aScaleLo[] for the |
| 195 | ** next 32 bites. |
| 196 | ** |
| 197 | ** The scale factors are at increments of 27. Except, the entry for 0 |
| 198 | ** is replaced by the -1 value as a special case. |
| 199 | */ |
| 200 | printf(" static const u64 aScale[] = {\n"); |
| 201 | for(i=j=0; i<SCALE_COUNT; i=iNext, j++){ |
| 202 | const char *zExtra = ""; |
| 203 | iNext = i+27; |
| 204 | if( i==SCALE_ZERO ){ i--; zExtra = " (special case)"; } |
| 205 | u64 x = aHi[i]; |
| 206 | int e = aE[i]; |
| 207 | char *zOp; |
| 208 | if( e>0 ){ |
| 209 | zOp = "<<"; |
| 210 | }else{ |
| 211 | e = -e; |
| 212 | zOp = ">>"; |
| 213 | } |
| 214 | printf(" UINT64_C(0x%016llx), /* %2d: 1.0e%+d %s %d%s */\n", |
| 215 | x, j, i+SCALE_FIRST, zOp, e, zExtra); |
| 216 | } |
| 217 | printf(" };\n"); |
| 218 | printf(" static const unsigned int aScaleLo[] = {\n"); |
| 219 | for(i=j=0; i<SCALE_COUNT; i=iNext, j++){ |
| 220 | const char *zExtra = ""; |
| 221 | iNext = i+27; |
| 222 | if( i==SCALE_ZERO ){ i--; zExtra = " (special case)"; } |
| 223 | u64 x = aLo[i]; |
| 224 | int e = aE[i]; |
| 225 | char *zOp; |
| 226 | if( bRound && (x & U64_BIT(31))!=0 && i!=SCALE_ZERO-1 ) x += U64_BIT(32); |
| 227 | if( e>0 ){ |
| 228 | zOp = "<<"; |
| 229 | }else{ |
| 230 | e = -e; |
| 231 | zOp = ">>"; |
| 232 | } |
| 233 | printf(" 0x%08llx, /* %2d: 1.0e%+d %s %d%s */\n", |
| 234 | x>>32, j, i+SCALE_FIRST, zOp, e, zExtra); |
| 235 | } |
| 236 | printf(" };\n"); |
| 237 | return 0; |
| 238 | } |