/* ** Writes the committed number traces, one line per case: ** ** reals.txt a double's IEEE 754 bit pattern, then the text SQLite ** renders it as ** subnormals.txt the same, over subnormals alone ** literals.txt a REAL literal, then the bit pattern SQLite reads it as ** ** Only SQLite's public API is used. sqlite3_snprintf("%!.17g", r) is the ** call sqlite3VdbeMemStringify makes to turn a REAL into TEXT, so the text ** here is what a query returns; sqlite3_column_double() over a literal is ** the reading a query gives that literal, so no platform strtod or printf ** enters either population. ** ** Every population is drawn from a fixed seed stated in its trace header, ** so the same oracle writes the same bytes on any machine. */ #include "sqlite3.h" #include #include #include #include typedef unsigned long long u64; /* Sample sizes. Kept in one place so a differential run at a much larger ** population can override them without touching the population shapes. */ #ifndef SUBNORMAL_SMALLEST #define SUBNORMAL_SMALLEST 512 /* the N smallest subnormals, one by one */ #endif #ifndef SUBNORMAL_PER_WIDTH #define SUBNORMAL_PER_WIDTH 20 /* per significand bit-width, 1..52 */ #endif #ifndef SUBNORMAL_UNIFORM #define SUBNORMAL_UNIFORM 256 /* uniform over the 52-bit significand */ #endif #ifndef LITERALS_PER_DIGIT #define LITERALS_PER_DIGIT 64 /* per significant-digit count, 1..30 */ #endif static FILE *out; static void emit(u64 bits) { double r; char buf[64]; memcpy(&r, &bits, 8); sqlite3_snprintf(sizeof buf, buf, "%!.17g", r); fprintf(out, "%016llx %s\n", bits, buf); } static void emit_double(double r) { u64 bits; memcpy(&bits, &r, 8); emit(bits); } /* xorshift64*, so a population is reproducible from its seed alone */ static u64 next(u64 *state) { u64 x = *state; x ^= x >> 12; x ^= x << 25; x ^= x >> 27; *state = x; return x * 0x2545f4914f6cdd1dULL; } static double sql_double(sqlite3 *db, const char *expr) { sqlite3_stmt *st = 0; char sql[128]; double r = 0.0; sqlite3_snprintf(sizeof sql, sql, "SELECT %s", expr); if (sqlite3_prepare_v2(db, sql, -1, &st, 0) == SQLITE_OK && sqlite3_step(st) == SQLITE_ROW) { r = sqlite3_column_double(st, 0); } sqlite3_finalize(st); return r; } /* The double SQLite reads the REAL literal [lit] as, refusing anything it ** does not answer as a REAL: an integer literal would take the INTEGER path ** and say nothing about how a real is read. */ static int literal_double(sqlite3 *db, const char *lit, double *r) { sqlite3_stmt *st = 0; char sql[128]; int ok = 0; sqlite3_snprintf(sizeof sql, sql, "SELECT %s", lit); if (sqlite3_prepare_v2(db, sql, -1, &st, 0) == SQLITE_OK && sqlite3_step(st) == SQLITE_ROW && sqlite3_column_type(st, 0) == SQLITE_FLOAT) { *r = sqlite3_column_double(st, 0); ok = 1; } sqlite3_finalize(st); return ok; } /* ------------------------------------------------------------------ reals */ /* The values a reader would reach for by hand: the ends of the type, the ** boundary between normals and subnormals, and the awkward ones that pin how ** many digits SQLite keeps. */ static const u64 curated[] = { 0x0000000000000000ULL, /* +0.0 */ 0x8000000000000000ULL, /* -0.0 */ 0x0000000000000001ULL, /* smallest subnormal */ 0x8000000000000001ULL, 0x0000000000000002ULL, 0x0000000000000003ULL, 0x0007ffffffffffffULL, 0x0008000000000000ULL, 0x000fffffffffffffULL, /* largest subnormal */ 0x800fffffffffffffULL, 0x0010000000000000ULL, /* smallest normal */ 0x8010000000000000ULL, 0x7fefffffffffffffULL, /* largest finite */ 0xffefffffffffffffULL, 0x0007ae86ea2925b9ULL, /* 1.0682782820147384e-308 */ 0x3fb999999999999aULL, /* 0.1 */ 0x3fd3333333333333ULL, /* 0.3 */ 0x3ff199999999999aULL, /* 1.1 */ 0x4048c28f5c28f5c3ULL, /* 49.47 */ 0x3fd5555555555555ULL, /* 1.0/3.0 */ 0x4040aaaaaaaaaaabULL, /* 100.0/3.0 */ 0x42dc12218377de26ULL, /* 123456789012345.6 */ 0x400921fb54442d11ULL, /* 3.14159265358979 */ 0x408bcfb7cd0dcb84ULL, /* 889.93094241196695, 15 digits round-trip */ 0x4081f28ffa4b4becULL, /* 573.15681709364128, 16 digits round-trip */ 0x4068ab5f34dcccadULL, /* 198.33563752922566, 17 digits round-trip */ 0x4076342b8f16d155ULL, /* 355.26683999526955 */ 0x4014000000000000ULL, /* 5.0 */ 0x4059000000000000ULL, /* 100.0 */ 0xc004000000000000ULL, /* -2.5 */ 0x43118b5443c53000ULL, /* 1234567890123456.0 */ }; static void write_reals(sqlite3 *db) { u64 state = 0x2545f4914f6cdd1dULL; size_t k; int i; fprintf(out, "# sqlite3 %s -- REAL renderings of %s\n", sqlite3_libversion(), "\"%!.17g\", one per line: bit pattern, then text"); for (k = 0; k < sizeof curated / sizeof curated[0]; k++) emit(curated[k]); /* every fourth power of ten in range, and its two neighbours in ulp: the ** 18th digit of a power of ten sits exactly on a rounding boundary */ for (i = -323; i <= 308; i += 4) { char expr[32]; u64 bits; double r; sqlite3_snprintf(sizeof expr, expr, "1e%d", i); r = sql_double(db, expr); memcpy(&bits, &r, 8); emit(bits - 1); emit(bits); emit(bits + 1); } /* either side of the boundary between subnormals and normals */ for (i = -32; i <= 32; i++) emit(0x0010000000000000ULL + (u64)i); /* doubles of the size ordinary data is made of */ for (i = 1; i <= 250; i++) { emit_double(1.0 / (double)i); emit_double((double)i / 7.0); emit_double(sqrt((double)i)); emit_double((double)i * 1.1); } for (i = 0; i < 1000;) { u64 b = next(&state); u64 e = (b >> 52) & 0x7ff; if (e >= 1 && e < 0x7ff) { emit(b); i++; } } for (i = 0; i < 1000;) { u64 b = next(&state); u64 m = b & 0x000fffffffffffffULL; if (m != 0) { emit((b & 0x8000000000000000ULL) | m); i++; } } } /* ------------------------------------------------------------- subnormals */ /* A subnormal is where SQLite's decimal decode and a correctly rounded one ** part company: sqlite3FpDecode approximates, biased low by up to 2**-64 ** relative, so the two pick neighbouring 17th digits on roughly one ** subnormal in two hundred. A population drawn from the whole significand ** range -- rather than uniformly, which would sit almost entirely in the ** widest few bits -- is what makes those show up. */ static void write_subnormals(void) { u64 state = 0x9e3779b97f4a7c15ULL; int w, i; fprintf(out, "# sqlite3 %s -- REAL renderings of subnormals, %s\n", sqlite3_libversion(), "\"%!.17g\": bit pattern, then text"); fprintf(out, "# the %d smallest one by one, 32 of those negated, the 64\n", SUBNORMAL_SMALLEST); fprintf(out, "# largest, %d per significand bit-width, %d uniform\n", SUBNORMAL_PER_WIDTH, SUBNORMAL_UNIFORM); fprintf(out, "# xorshift64* seed 0x9e3779b97f4a7c15\n"); for (i = 1; i <= SUBNORMAL_SMALLEST; i++) emit((u64)i); for (i = 1; i <= 32; i++) emit(0x8000000000000000ULL | (u64)i); for (i = 63; i >= 0; i--) emit(0x000fffffffffffffULL - (u64)i); /* one bucket per position of the significand's leading 1, so the smallest ** subnormals are as well covered as the largest */ for (w = 1; w <= 52; w++) { for (i = 0; i < SUBNORMAL_PER_WIDTH; i++) { u64 b = next(&state); u64 top = 1ULL << (w - 1); u64 m = top | (b & (top - 1)); emit((b & 0x8000000000000000ULL) | m); } } for (i = 0; i < SUBNORMAL_UNIFORM;) { u64 b = next(&state); u64 m = b & 0x000fffffffffffffULL; if (m != 0) { emit((b & 0x8000000000000000ULL) | m); i++; } } } /* --------------------------------------------------------------- literals */ /* The literals a reader would reach for by hand: the ends of the type, the ** digit counts either side of the 64-bit significand sqlite3AtoF accumulates ** into, and the twenty-digit literal that first showed the truncation. */ static const char *const curated_literals[] = { "0.0", "-0.0", "0.1", "-0.1", "1.1", "3.0e0", "1e0", "-1e0", "3.6697984035011495496e-104", /* truncated, not rounded: ...56, not ...57 */ "1.7976931348623157e308", /* largest finite */ "-1.7976931348623157e308", "2.2250738585072014e-308", /* smallest normal */ "4.9406564584124654e-324", /* smallest subnormal */ "-4.9406564584124654e-324", "1.1125369292536007e-308", /* mid subnormal */ "1e-330", /* under the smallest subnormal */ "-1e-330", "9007199254740993.0", /* 2**53 + 1, the first integer a double skips */ "18446744073709551616.0", /* 2**64, one past the significand's width */ "0.30000000000000004", "1234567890123456789.0", /* 19 digits, the last that always fits */ "12345678901234567890.0", /* 20, where the accumulator may stop */ "123456789012345678901.0", /* 21, where it always has */ }; static void write_literals(sqlite3 *db) { u64 state = 0xda942042e4dd58b5ULL; char lit[64]; size_t k; int n, i; fprintf(out, "# sqlite3 %s -- the double %s reads a REAL literal as:\n", sqlite3_libversion(), "sqlite3_column_double"); fprintf(out, "# literal, then bit pattern\n"); fprintf(out, "# %d literals per significant-digit count 1..30, decimal\n", LITERALS_PER_DIGIT); fprintf(out, "# exponent uniform in [-330, 300], either sign\n"); fprintf(out, "# xorshift64* seed 0xda942042e4dd58b5\n"); for (k = 0; k < sizeof curated_literals / sizeof curated_literals[0]; k++) { u64 bits; double r; if (!literal_double(db, curated_literals[k], &r)) { fprintf(stderr, "not a REAL literal: %s\n", curated_literals[k]); exit(1); } memcpy(&bits, &r, 8); fprintf(out, "%s %016llx\n", curated_literals[k], bits); } for (n = 1; n <= 30; n++) { for (i = 0; i < LITERALS_PER_DIGIT; i++) { u64 b = next(&state); int exponent = (int)(next(&state) % 631) - 330; int at = 0, d; u64 bits; double r; if (b & 1) lit[at++] = '-'; lit[at++] = (char)('1' + (int)((b >> 1) % 9)); if (n > 1) lit[at++] = '.'; for (d = 1; d < n; d++) { lit[at++] = (char)('0' + (int)(next(&state) % 10)); } at += sprintf(lit + at, "e%d", exponent); lit[at] = 0; if (!literal_double(db, lit, &r)) { fprintf(stderr, "not a REAL literal: %s\n", lit); exit(1); } memcpy(&bits, &r, 8); fprintf(out, "%s %016llx\n", lit, bits); } } } /* ------------------------------------------------------------------- main */ static void open_trace(const char *dir, const char *name) { char path[1024]; sqlite3_snprintf(sizeof path, path, "%s/%s", dir, name); out = fopen(path, "w"); if (out == 0) { fprintf(stderr, "cannot write %s\n", path); exit(1); } } int main(int argc, char **argv) { const char *dir = argc > 1 ? argv[1] : "."; sqlite3 *db = 0; if (sqlite3_open(":memory:", &db) != SQLITE_OK) { fprintf(stderr, "cannot open :memory:\n"); return 1; } open_trace(dir, "reals.txt"); write_reals(db); fclose(out); open_trace(dir, "subnormals.txt"); write_subnormals(); fclose(out); open_trace(dir, "literals.txt"); write_literals(db); fclose(out); open_trace(dir, "sqlite-version.txt"); fprintf(out, "%s\n%s\n", sqlite3_libversion(), sqlite3_sourceid()); fclose(out); sqlite3_close(db); return 0; }