Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Add the ieee754_to_blob() and ieee754_from_blob() functions. Fix the handling of subnormal forms in the two-argument version of ieee754(). |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
c78cbf2e86850cc6882d3f0bd5415f6e |
User & Date: | drh 2020-06-26 15:32:29.122 |
Context
2020-06-26
| ||
15:42 | Improvements to speedtest1. Added the --memdb and --output options. The --verify option now outputs a hash of SQL outputs. The speed-check.sh script disables the hashing feature with --legacy and adds the --verify option. (check-in: f3455cecf2 user: drh tags: trunk) | |
15:32 | Add the ieee754_to_blob() and ieee754_from_blob() functions. Fix the handling of subnormal forms in the two-argument version of ieee754(). (check-in: c78cbf2e86 user: drh tags: trunk) | |
04:34 | Fix a possible null pointer deref following OOM. Discovered by dbsqlfuzz. (check-in: cc888878ea user: drh tags: trunk) | |
Changes
Changes to ext/misc/ieee754.c.
︙ | ︙ | |||
33 34 35 36 37 38 39 40 41 42 43 44 45 46 | ** ** Two additional functions break apart the one-argument ieee754() ** result into separate integer values: ** ** ieee754_mantissa(45.25) -> 181 ** ieee754_exponent(45.25) -> -2 ** ** In all single-argument functions, if the argument is an 8-byte blob ** then that blob is interpreted as a big-endian binary64 value. ** ** ** EXACT DECIMAL REPRESENTATION OF BINARY64 VALUES ** ----------------------------------------------- ** | > > > > > | 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | ** ** Two additional functions break apart the one-argument ieee754() ** result into separate integer values: ** ** ieee754_mantissa(45.25) -> 181 ** ieee754_exponent(45.25) -> -2 ** ** These functions convert binary64 numbers into blobs and back again. ** ** ieee754_from_blob(x'3ff0000000000000') -> 1.0 ** ieee754_to_blob(1.0) -> x'3ff0000000000000' ** ** In all single-argument functions, if the argument is an 8-byte blob ** then that blob is interpreted as a big-endian binary64 value. ** ** ** EXACT DECIMAL REPRESENTATION OF BINARY64 VALUES ** ----------------------------------------------- ** |
︙ | ︙ | |||
156 157 158 159 160 161 162 | int isNeg = 0; m = sqlite3_value_int64(argv[0]); e = sqlite3_value_int64(argv[1]); if( m<0 ){ isNeg = 1; m = -m; if( m<0 ) return; | | | > > > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | | | > > > | | 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | int isNeg = 0; m = sqlite3_value_int64(argv[0]); e = sqlite3_value_int64(argv[1]); if( m<0 ){ isNeg = 1; m = -m; if( m<0 ) return; }else if( m==0 && e>-1000 && e<1000 ){ sqlite3_result_double(context, 0.0); return; } while( (m>>32)&0xffe00000 ){ m >>= 1; e++; } while( m!=0 && ((m>>32)&0xfff00000)==0 ){ m <<= 1; e--; } e += 1075; if( e<=0 ){ /* Subnormal */ m >>= 1-e; e = 0; }else if( e>0x7ff ){ e = 0x7ff; } a = m & ((((sqlite3_int64)1)<<52)-1); a |= e<<52; if( isNeg ) a |= ((sqlite3_uint64)1)<<63; memcpy(&r, &a, sizeof(r)); sqlite3_result_double(context, r); } } /* ** Functions to convert between blobs and floats. */ static void ieee754func_from_blob( sqlite3_context *context, int argc, sqlite3_value **argv ){ if( sqlite3_value_type(argv[0])==SQLITE_BLOB && sqlite3_value_bytes(argv[0])==sizeof(double) ){ double r; const unsigned char *x = sqlite3_value_blob(argv[0]); int i; sqlite3_uint64 v = 0; for(i=0; i<sizeof(r); i++){ v = (v<<8) | x[i]; } memcpy(&r, &v, sizeof(r)); sqlite3_result_double(context, r); } } static void ieee754func_to_blob( sqlite3_context *context, int argc, sqlite3_value **argv ){ if( sqlite3_value_type(argv[0])==SQLITE_FLOAT || sqlite3_value_type(argv[0])==SQLITE_INTEGER ){ double r = sqlite3_value_double(argv[0]); sqlite3_uint64 v; unsigned char a[sizeof(r)]; int i; memcpy(&v, &r, sizeof(r)); for(i=1; i<=sizeof(r); i++){ a[sizeof(r)-i] = v&0xff; v >>= 8; } sqlite3_result_blob(context, a, sizeof(r), SQLITE_TRANSIENT); } } #ifdef _WIN32 __declspec(dllexport) #endif int sqlite3_ieee_init( sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi ){ static const struct { char *zFName; int nArg; int iAux; void (*xFunc)(sqlite3_context*,int,sqlite3_value**); } aFunc[] = { { "ieee754", 1, 0, ieee754func }, { "ieee754", 2, 0, ieee754func }, { "ieee754_mantissa", 1, 1, ieee754func }, { "ieee754_exponent", 1, 2, ieee754func }, { "ieee754_to_blob", 1, 0, ieee754func_to_blob }, { "ieee754_from_blob", 1, 0, ieee754func_from_blob }, }; int i; int rc = SQLITE_OK; SQLITE_EXTENSION_INIT2(pApi); (void)pzErrMsg; /* Unused parameter */ for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){ rc = sqlite3_create_function(db, aFunc[i].zFName, aFunc[i].nArg, SQLITE_UTF8|SQLITE_INNOCUOUS, (void*)&aFunc[i].iAux, aFunc[i].xFunc, 0, 0); } return rc; } |
Changes to test/ieee754.test.
︙ | ︙ | |||
19 20 21 22 23 24 25 | foreach {id float rep} { 1 1.0 1,0 2 2.0 2,0 3 0.5 1,-1 4 1.5 3,-1 5 0.0 0,-1075 | | | | 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | foreach {id float rep} { 1 1.0 1,0 2 2.0 2,0 3 0.5 1,-1 4 1.5 3,-1 5 0.0 0,-1075 6 4.9406564584124654e-324 1,-1074 7 2.2250738585072009e-308 4503599627370495,-1074 8 2.2250738585072014e-308 1,-1022 } { do_test ieee754-100-$id-1 { db eval "SELECT ieee754($float);" } "ieee754($rep)" do_test ieee754-100-$id-2 { db eval "SELECT ieee754($rep)==$float;" |
︙ | ︙ |