From: drh <> Date: Fri, 30 Jun 2023 18:35:43 +0000 (+0000) Subject: Experiments with a new algorithm for converting ieee-754 binary64 numbers X-Git-Tag: version-3.43.0~162^2~11 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a1b0ff173520fc73a182e57b06b1d0228b343711;p=thirdparty%2Fsqlite.git Experiments with a new algorithm for converting ieee-754 binary64 numbers into decimal. FossilOrigin-Name: e923405e448385085224f9289991b303d86b02763535ea77d6fcee98ba6fc1f2 --- diff --git a/manifest b/manifest index 0b84625ebf..95d4799b5d 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Add\ssupport\sfor\ssqlite3_stmt_scanstatus_v2()\sprofiling\sof\sGROUP\sBY\sclauses\sthat\suse\sa\stemp\sb-tree,\sand\sfor\ssub-queries\simplemented\sas\sco-routines. -D 2023-06-30T18:31:37.545 +C Experiments\swith\sa\snew\salgorithm\sfor\sconverting\sieee-754\sbinary64\snumbers\ninto\sdecimal. +D 2023-06-30T18:35:43.344 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -590,7 +590,7 @@ F src/delete.c cd5f5cd06ed0b6a882ec1a8c2a0d73b3cecb28479ad19e9931c4706c5e2182be F src/expr.c 8d1656b65e26af3e34f78e947ac423f0d20c214ed25a67486e433bf16ca6b543 F src/fault.c 460f3e55994363812d9d60844b2a6de88826e007 F src/fkey.c a7fcbf7e66d14dbb73cf49f31489ebf66d0e6006c62b95246924a3bae9f37b36 -F src/func.c 6b4804738b4d869f40625958b476a8f964d3df65b626e72a530d76051863cf32 +F src/func.c 699e19ca0dc580a024eb343f3a3abfb646e5438e36d377023d485a3038d789d5 F src/global.c bd0892ade7289f6e20bff44c07d06371f2ff9b53cea359e7854b9b72f65adc30 F src/hash.c 9ee4269fb1d6632a6fecfb9479c93a1f29271bddbbaf215dd60420bcb80c7220 F src/hash.h 3340ab6e1d13e725571d7cee6d3e3135f0779a7d8e76a9ce0a85971fa3953c51 @@ -642,7 +642,7 @@ F src/shell.c.in 2c02c819349de410d63fcc0217763dfe5a42dbe58f2d68046d4ea8a376d12c2 F src/sqlite.h.in 3076d78836b6dac53b3ab0875fc8fd15bca8077aad4d33c85336e05af6aef8c7 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8 F src/sqlite3ext.h da473ce2b3d0ae407a6300c4a164589b9a6bfdbec9462688a8593ff16f3bb6e4 -F src/sqliteInt.h b677ba33397479f9a2fa1223fcd24b561cc8c77c7d073798f7b7118e15d69dbb +F src/sqliteInt.h 6ef127217c54e4707b1ca500508340542ca3c2a5aa08ee7a00aaa4834e68992d F src/sqliteLimit.h 33b1c9baba578d34efe7dfdb43193b366111cdf41476b1e82699e14c11ee1fb6 F src/status.c 160c445d7d28c984a0eae38c144f6419311ed3eace59b44ac6dafc20db4af749 F src/table.c 0f141b58a16de7e2fbe81c308379e7279f4c6b50eb08efeec5892794a0ba30d1 @@ -705,7 +705,7 @@ F src/trigger.c ad6ab9452715fa9a8075442e15196022275b414b9141b566af8cdb7a1605f2b0 F src/update.c 0aa36561167a7c40d01163238c297297962f31a15a8d742216b3c37cdf25f731 F src/upsert.c 5303dc6c518fa7d4b280ec65170f465c7a70b7ac2b22491598f6d0b4875b3145 F src/utf.c ee39565f0843775cc2c81135751ddd93eceb91a673ea2c57f61c76f288b041a0 -F src/util.c 4264102045fdb36e9af3ff361e390a5f7a76342a2bd7069e55d8ad332026d6b5 +F src/util.c 447fd1d981bab37788397c24f980ce555438168f28badde1227441f6d67f1214 F src/vacuum.c 604fcdaebe76f3497c855afcbf91b8fa5046b32de3045bab89cc008d68e40104 F src/vdbe.c 74282a947234513872a83b0bab1b8c644ece64b3e27b053ef17677c8ff9c81e0 F src/vdbe.h 41485521f68e9437fdb7ec4a90f9d86ab294e9bb8281e33b235915e29122cfc0 @@ -2041,9 +2041,11 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P f936f101406069b29218c89a36581b4497226fb61906782ea368f12d943c901c 4e8718dc35dbbaf75f17265a88d14acd9750dc75efbadf41377f9c97e732009c -R 4da01a7efae23c104b98c2eba70ec2ef -T +closed 4e8718dc35dbbaf75f17265a88d14acd9750dc75efbadf41377f9c97e732009c -U dan -Z 8ca5997f8884b68f398b8d0f077d59ae +P 7afad1f759f7ceda873c6d869422fd56fe4399c2d24d47ad9bc3b84b06d830d1 +R b84c58ae5f50c9a21c1125fd73d7f10d +T *branch * fp-to-decimal-refactor +T *sym-fp-to-decimal-refactor * +T -sym-trunk * +U drh +Z aad0befdc05a87e7f362b0543d2a6962 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index f482cd32b9..2a088950c5 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -7afad1f759f7ceda873c6d869422fd56fe4399c2d24d47ad9bc3b84b06d830d1 \ No newline at end of file +e923405e448385085224f9289991b303d86b02763535ea77d6fcee98ba6fc1f2 \ No newline at end of file diff --git a/src/func.c b/src/func.c index b243591863..4cf930b208 100644 --- a/src/func.c +++ b/src/func.c @@ -2371,6 +2371,23 @@ static void signFunc( sqlite3_result_int(context, x<0.0 ? -1 : x>0.0 ? +1 : 0); } + +#if 1 /* Temporary prototyping logic */ +static void fpdecodeFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + double r = sqlite3_value_double(argv[0]); + FpDecode s; + char zBuf[50]; + UNUSED_PARAMETER(argc); + sqlite3FpDecode(&s, r); + sqlite3_snprintf(sizeof(zBuf), zBuf, "%c%.*s/%d", s.sign, s.n, s.z, s.iDP); + sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); +} +#endif /* Temporary prototyping logic */ + /* ** All of the FuncDef structures in the aBuiltinFunc[] array above ** to the global function hash table. This occurs at start-time (as @@ -2442,6 +2459,11 @@ void sqlite3RegisterBuiltinFunctions(void){ FUNCTION(unicode, 1, 0, 0, unicodeFunc ), FUNCTION(char, -1, 0, 0, charFunc ), FUNCTION(abs, 1, 0, 0, absFunc ), + +#if 1 /* Temporary prototyping function */ + FUNCTION(fpdecode, 1, 0, 0, fpdecodeFunc ), +#endif + #ifndef SQLITE_OMIT_FLOATING_POINT FUNCTION(round, 1, 0, 0, roundFunc ), FUNCTION(round, 2, 0, 0, roundFunc ), diff --git a/src/sqliteInt.h b/src/sqliteInt.h index d7a2d90fc2..b4705b5e26 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -1239,6 +1239,7 @@ typedef struct Schema Schema; typedef struct Expr Expr; typedef struct ExprList ExprList; typedef struct FKey FKey; +typedef struct FpDecode FpDecode; typedef struct FuncDestructor FuncDestructor; typedef struct FuncDef FuncDef; typedef struct FuncDefHash FuncDefHash; @@ -4597,6 +4598,20 @@ struct PrintfArguments { sqlite3_value **apArg; /* The argument values */ }; +/* +** An instance of this object receives the decoding of a floating point +** value into an approximate decimal representation. +*/ +struct FpDecode { + char sign; /* '+' or '-' */ + char isNan; /* True if not-a-number */ + char isInf; /* True if infinity */ + int n; /* Significant digits in the decode */ + int iDP; /* Location of the decimal point */ + char z[24]; /* Significiant digits */ +}; + +void sqlite3FpDecode(FpDecode*,double); char *sqlite3MPrintf(sqlite3*,const char*, ...); char *sqlite3VMPrintf(sqlite3*,const char*, va_list); #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) diff --git a/src/util.c b/src/util.c index abd36eda87..29bc8524da 100644 --- a/src/util.c +++ b/src/util.c @@ -928,6 +928,69 @@ int sqlite3Atoi(const char *z){ return x; } +/* +** Decode a floating-point value into an approximate decimal +** representation. +*/ +void sqlite3FpDecode(FpDecode *p, double r){ + int i; + u64 v; + int e, exp = 0; + if( r<0.0 ){ + p->sign = '-'; + r = -r; + }else if( r==0.0 ){ + p->sign = '+'; + p->n = 1; + p->iDP = 1; + p->z[0] = '0'; + return; + }else{ + p->sign = '+'; + } + memcpy(&v,&r,8); + e = v>>52; + if( e==0x7ff ){ + if( v==0x7ff0000000000000L ){ + p->isInf = 1; + p->isNan = 0; + p->z[0] = 'I'; + p->z[1] = 'n'; + p->z[2] = 'f'; + }else{ + p->isInf = 0; + p->isNan = 1; + p->z[0] = 'N'; + p->z[1] = 'a'; + p->z[2] = 'N'; + } + p->n = 3; + p->iDP = 3; + return; + } + /* At this point, r is positive (non-zero) and is not Inf or NaN. + ** The strategy is to multiple or divide r by powers of 10 until + ** it is in between 1.0e+17 and 1.0e+19. Then convert r into + ** an unsigned 64-bit integer v, and extract digits from v. + */ + if( r>=1.0e+19 ){ + while( r>=1.0e+119 ){ exp+=100; r /= 1.0e+100; } + while( r>=1.0e+29 ){ exp+=10; r /= 1.0e+10; } + while( r>=1.0e+19 ){ exp++; r /= 10.0; } + }else if( r<1.0e+17 ){ + while( r<1.0e-97 ){ exp-=100; r *= 1.0e+100; } + while( r<1.0e+07 ){ exp-=10; r *= 1.0e+10; } + while( r<1.0e+17 ){ exp--; r *= 10.0; } + } + v = (u64)r; + i = sizeof(p->z); + while( v ){ p->z[i--] = (v%10) + '0'; v /= 10; } + p->n = sizeof(p->z) - i; + p->iDP = p->n + exp; + memmove(p->z, &p->z[i+1], p->n); + while( p->n>0 && p->z[p->n-1]=='0' ){ p->n--; } +} + /* ** Try to convert z into an unsigned 32-bit integer. Return true on ** success and false if there is an error.