| 1 |
/* |
| 2 |
** 2013-04-17 |
| 3 |
** |
| 4 |
** The author disclaims copyright to this source code. In place of |
| 5 |
** a legal notice, here is a blessing: |
| 6 |
** |
| 7 |
** May you do good and not evil. |
| 8 |
** May you find forgiveness for yourself and forgive others. |
| 9 |
** May you share freely, never taking more than you give. |
| 10 |
** |
| 11 |
****************************************************************************** |
| 12 |
** |
| 13 |
** This SQLite extension implements functions for the exact display |
| 14 |
** and input of IEEE754 Binary64 floating-point numbers. |
| 15 |
** |
| 16 |
** ieee754(X) |
| 17 |
** ieee754(Y,Z) |
| 18 |
** |
| 19 |
** In the first form, the value X should be a floating-point number. |
| 20 |
** The function will return a string of the form 'ieee754(Y,Z)' where |
| 21 |
** Y and Z are integers such that X==Y*pow(2,Z). |
| 22 |
** |
| 23 |
** In the second form, Y and Z are integers which are the mantissa and |
| 24 |
** base-2 exponent of a new floating point number. The function returns |
| 25 |
** a floating-point value equal to Y*pow(2,Z). |
| 26 |
** |
| 27 |
** Examples: |
| 28 |
** |
| 29 |
** ieee754(2.0) -> 'ieee754(2,0)' |
| 30 |
** ieee754(45.25) -> 'ieee754(181,-2)' |
| 31 |
** ieee754(2, 0) -> 2.0 |
| 32 |
** ieee754(181, -2) -> 45.25 |
| 33 |
** |
| 34 |
** Two additional functions break apart the one-argument ieee754() |
| 35 |
** result into separate integer values: |
| 36 |
** |
| 37 |
** ieee754_mantissa(45.25) -> 181 |
| 38 |
** ieee754_exponent(45.25) -> -2 |
| 39 |
** |
| 40 |
** These functions convert binary64 numbers into blobs and back again. |
| 41 |
** |
| 42 |
** ieee754_from_blob(x'3ff0000000000000') -> 1.0 |
| 43 |
** ieee754_to_blob(1.0) -> x'3ff0000000000000' |
| 44 |
** |
| 45 |
** In all single-argument functions, if the argument is an 8-byte blob |
| 46 |
** then that blob is interpreted as a big-endian binary64 value. |
| 47 |
** |
| 48 |
** |
| 49 |
** EXACT DECIMAL REPRESENTATION OF BINARY64 VALUES |
| 50 |
** ----------------------------------------------- |
| 51 |
** |
| 52 |
** This extension in combination with the separate 'decimal' extension |
| 53 |
** can be used to compute the exact decimal representation of binary64 |
| 54 |
** values. To begin, first compute a table of exponent values: |
| 55 |
** |
| 56 |
** CREATE TABLE pow2(x INTEGER PRIMARY KEY, v TEXT); |
| 57 |
** WITH RECURSIVE c(x,v) AS ( |
| 58 |
** VALUES(0,'1') |
| 59 |
** UNION ALL |
| 60 |
** SELECT x+1, decimal_mul(v,'2') FROM c WHERE x+1<=971 |
| 61 |
** ) INSERT INTO pow2(x,v) SELECT x, v FROM c; |
| 62 |
** WITH RECURSIVE c(x,v) AS ( |
| 63 |
** VALUES(-1,'0.5') |
| 64 |
** UNION ALL |
| 65 |
** SELECT x-1, decimal_mul(v,'0.5') FROM c WHERE x-1>=-1075 |
| 66 |
** ) INSERT INTO pow2(x,v) SELECT x, v FROM c; |
| 67 |
** |
| 68 |
** Then, to compute the exact decimal representation of a floating |
| 69 |
** point value (the value 47.49 is used in the example) do: |
| 70 |
** |
| 71 |
** WITH c(n) AS (VALUES(47.49)) |
| 72 |
** ---------------^^^^^---- Replace with whatever you want |
| 73 |
** SELECT decimal_mul(ieee754_mantissa(c.n),pow2.v) |
| 74 |
** FROM pow2, c WHERE pow2.x=ieee754_exponent(c.n); |
| 75 |
** |
| 76 |
** Here is a query to show various boundry values for the binary64 |
| 77 |
** number format: |
| 78 |
** |
| 79 |
** WITH c(name,bin) AS (VALUES |
| 80 |
** ('minimum positive value', x'0000000000000001'), |
| 81 |
** ('maximum subnormal value', x'000fffffffffffff'), |
| 82 |
** ('minimum positive normal value', x'0010000000000000'), |
| 83 |
** ('maximum value', x'7fefffffffffffff')) |
| 84 |
** SELECT c.name, decimal_mul(ieee754_mantissa(c.bin),pow2.v) |
| 85 |
** FROM pow2, c WHERE pow2.x=ieee754_exponent(c.bin); |
| 86 |
** |
| 87 |
*/ |
| 88 |
#include "sqlite3ext.h" |
| 89 |
SQLITE_EXTENSION_INIT1 |
| 90 |
#include <assert.h> |
| 91 |
#include <string.h> |
| 92 |
|
| 93 |
/* Mark a function parameter as unused, to suppress nuisance compiler |
| 94 |
** warnings. */ |
| 95 |
#ifndef UNUSED_PARAMETER |
| 96 |
# define UNUSED_PARAMETER(X) (void)(X) |
| 97 |
#endif |
| 98 |
|
| 99 |
/* |
| 100 |
** Implementation of the ieee754() function |
| 101 |
*/ |
| 102 |
static void ieee754func( |
| 103 |
sqlite3_context *context, |
| 104 |
int argc, |
| 105 |
sqlite3_value **argv |
| 106 |
){ |
| 107 |
if( argc==1 ){ |
| 108 |
sqlite3_int64 m, a; |
| 109 |
double r; |
| 110 |
int e; |
| 111 |
int isNeg; |
| 112 |
char zResult[100]; |
| 113 |
assert( sizeof(m)==sizeof(r) ); |
| 114 |
if( sqlite3_value_type(argv[0])==SQLITE_BLOB |
| 115 |
&& sqlite3_value_bytes(argv[0])==sizeof(r) |
| 116 |
){ |
| 117 |
const unsigned char *x = sqlite3_value_blob(argv[0]); |
| 118 |
unsigned int i; |
| 119 |
sqlite3_uint64 v = 0; |
| 120 |
for(i=0; i<sizeof(r); i++){ |
| 121 |
v = (v<<8) | x[i]; |
| 122 |
} |
| 123 |
memcpy(&r, &v, sizeof(r)); |
| 124 |
}else{ |
| 125 |
r = sqlite3_value_double(argv[0]); |
| 126 |
} |
| 127 |
if( r<0.0 ){ |
| 128 |
isNeg = 1; |
| 129 |
r = -r; |
| 130 |
}else{ |
| 131 |
isNeg = 0; |
| 132 |
} |
| 133 |
memcpy(&a,&r,sizeof(a)); |
| 134 |
if( a==0 ){ |
| 135 |
e = 0; |
| 136 |
m = 0; |
| 137 |
}else if( a==(sqlite3_int64)0x8000000000000000LL ){ |
| 138 |
e = -1996; |
| 139 |
m = -1; |
| 140 |
}else{ |
| 141 |
e = a>>52; |
| 142 |
m = a & ((((sqlite3_int64)1)<<52)-1); |
| 143 |
if( e==0 ){ |
| 144 |
m <<= 1; |
| 145 |
}else{ |
| 146 |
m |= ((sqlite3_int64)1)<<52; |
| 147 |
} |
| 148 |
while( e<1075 && m>0 && (m&1)==0 ){ |
| 149 |
m >>= 1; |
| 150 |
e++; |
| 151 |
} |
| 152 |
if( isNeg ) m = -m; |
| 153 |
} |
| 154 |
switch( *(int*)sqlite3_user_data(context) ){ |
| 155 |
case 0: |
| 156 |
sqlite3_snprintf(sizeof(zResult), zResult, "ieee754(%lld,%d)", |
| 157 |
m, e-1075); |
| 158 |
sqlite3_result_text(context, zResult, -1, SQLITE_TRANSIENT); |
| 159 |
break; |
| 160 |
case 1: |
| 161 |
sqlite3_result_int64(context, m); |
| 162 |
break; |
| 163 |
case 2: |
| 164 |
sqlite3_result_int(context, e-1075); |
| 165 |
break; |
| 166 |
} |
| 167 |
}else{ |
| 168 |
sqlite3_int64 m, e, a; |
| 169 |
double r; |
| 170 |
int isNeg = 0; |
| 171 |
m = sqlite3_value_int64(argv[0]); |
| 172 |
e = sqlite3_value_int64(argv[1]); |
| 173 |
|
| 174 |
/* Limit the range of e. Ticket 22dea1cfdb9151e4 2021-03-02 */ |
| 175 |
if( e>10000 ){ |
| 176 |
e = 10000; |
| 177 |
}else if( e<-10000 ){ |
| 178 |
e = -10000; |
| 179 |
} |
| 180 |
|
| 181 |
if( m<0 ){ |
| 182 |
if( m<(-9223372036854775807LL) ) return; |
| 183 |
isNeg = 1; |
| 184 |
m = -m; |
| 185 |
}else if( m==0 && e>-1000 && e<1000 ){ |
| 186 |
sqlite3_result_double(context, 0.0); |
| 187 |
return; |
| 188 |
} |
| 189 |
while( (m>>32)&0xffe00000 ){ |
| 190 |
m >>= 1; |
| 191 |
e++; |
| 192 |
} |
| 193 |
while( m!=0 && ((m>>32)&0xfff00000)==0 ){ |
| 194 |
m <<= 1; |
| 195 |
e--; |
| 196 |
} |
| 197 |
e += 1075; |
| 198 |
if( e<=0 ){ |
| 199 |
/* Subnormal */ |
| 200 |
if( 1-e >= 64 ){ |
| 201 |
m = 0; |
| 202 |
}else{ |
| 203 |
m >>= 1-e; |
| 204 |
} |
| 205 |
e = 0; |
| 206 |
}else if( e>0x7ff ){ |
| 207 |
e = 0x7ff; |
| 208 |
} |
| 209 |
a = m & ((((sqlite3_int64)1)<<52)-1); |
| 210 |
a |= e<<52; |
| 211 |
if( isNeg ) a |= ((sqlite3_uint64)1)<<63; |
| 212 |
memcpy(&r, &a, sizeof(r)); |
| 213 |
sqlite3_result_double(context, r); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
/* |
| 218 |
** Functions to convert between blobs and floats. |
| 219 |
*/ |
| 220 |
static void ieee754func_from_blob( |
| 221 |
sqlite3_context *context, |
| 222 |
int argc, |
| 223 |
sqlite3_value **argv |
| 224 |
){ |
| 225 |
UNUSED_PARAMETER(argc); |
| 226 |
if( sqlite3_value_type(argv[0])==SQLITE_BLOB |
| 227 |
&& sqlite3_value_bytes(argv[0])==sizeof(double) |
| 228 |
){ |
| 229 |
double r; |
| 230 |
const unsigned char *x = sqlite3_value_blob(argv[0]); |
| 231 |
unsigned int i; |
| 232 |
sqlite3_uint64 v = 0; |
| 233 |
for(i=0; i<sizeof(r); i++){ |
| 234 |
v = (v<<8) | x[i]; |
| 235 |
} |
| 236 |
memcpy(&r, &v, sizeof(r)); |
| 237 |
sqlite3_result_double(context, r); |
| 238 |
} |
| 239 |
} |
| 240 |
static void ieee754func_to_blob( |
| 241 |
sqlite3_context *context, |
| 242 |
int argc, |
| 243 |
sqlite3_value **argv |
| 244 |
){ |
| 245 |
UNUSED_PARAMETER(argc); |
| 246 |
if( sqlite3_value_type(argv[0])==SQLITE_FLOAT |
| 247 |
|| sqlite3_value_type(argv[0])==SQLITE_INTEGER |
| 248 |
){ |
| 249 |
double r = sqlite3_value_double(argv[0]); |
| 250 |
sqlite3_uint64 v; |
| 251 |
unsigned char a[sizeof(r)]; |
| 252 |
unsigned int i; |
| 253 |
memcpy(&v, &r, sizeof(r)); |
| 254 |
for(i=1; i<=sizeof(r); i++){ |
| 255 |
a[sizeof(r)-i] = v&0xff; |
| 256 |
v >>= 8; |
| 257 |
} |
| 258 |
sqlite3_result_blob(context, a, sizeof(r), SQLITE_TRANSIENT); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
/* |
| 263 |
** Functions to convert between 64-bit integers and floats. |
| 264 |
** |
| 265 |
** The bit patterns are copied. The numeric values are different. |
| 266 |
*/ |
| 267 |
static void ieee754func_from_int( |
| 268 |
sqlite3_context *context, |
| 269 |
int argc, |
| 270 |
sqlite3_value **argv |
| 271 |
){ |
| 272 |
UNUSED_PARAMETER(argc); |
| 273 |
if( sqlite3_value_type(argv[0])==SQLITE_INTEGER ){ |
| 274 |
double r; |
| 275 |
sqlite3_int64 v = sqlite3_value_int64(argv[0]); |
| 276 |
memcpy(&r, &v, sizeof(r)); |
| 277 |
sqlite3_result_double(context, r); |
| 278 |
} |
| 279 |
} |
| 280 |
static void ieee754func_to_int( |
| 281 |
sqlite3_context *context, |
| 282 |
int argc, |
| 283 |
sqlite3_value **argv |
| 284 |
){ |
| 285 |
UNUSED_PARAMETER(argc); |
| 286 |
if( sqlite3_value_type(argv[0])==SQLITE_FLOAT ){ |
| 287 |
double r = sqlite3_value_double(argv[0]); |
| 288 |
sqlite3_uint64 v; |
| 289 |
memcpy(&v, &r, sizeof(v)); |
| 290 |
sqlite3_result_int64(context, v); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
/* |
| 295 |
** SQL Function: ieee754_inc(r,N) |
| 296 |
** |
| 297 |
** Move the floating point value r by N quantums and return the new |
| 298 |
** values. |
| 299 |
** |
| 300 |
** Behind the scenes: this routine merely casts r into a 64-bit unsigned |
| 301 |
** integer, adds N, then casts the value back into float. |
| 302 |
** |
| 303 |
** Example: To find the smallest positive number: |
| 304 |
** |
| 305 |
** SELECT ieee754_inc(0.0,+1); |
| 306 |
*/ |
| 307 |
static void ieee754inc( |
| 308 |
sqlite3_context *context, |
| 309 |
int argc, |
| 310 |
sqlite3_value **argv |
| 311 |
){ |
| 312 |
double r; |
| 313 |
sqlite3_int64 N; |
| 314 |
sqlite3_uint64 m1, m2; |
| 315 |
double r2; |
| 316 |
UNUSED_PARAMETER(argc); |
| 317 |
r = sqlite3_value_double(argv[0]); |
| 318 |
N = sqlite3_value_int64(argv[1]); |
| 319 |
memcpy(&m1, &r, 8); |
| 320 |
m2 = m1 + N; |
| 321 |
memcpy(&r2, &m2, 8); |
| 322 |
sqlite3_result_double(context, r2); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
#ifdef _WIN32 |
| 327 |
__declspec(dllexport) |
| 328 |
#endif |
| 329 |
int sqlite3_ieee_init( |
| 330 |
sqlite3 *db, |
| 331 |
char **pzErrMsg, |
| 332 |
const sqlite3_api_routines *pApi |
| 333 |
){ |
| 334 |
static const struct { |
| 335 |
char *zFName; |
| 336 |
int nArg; |
| 337 |
int iAux; |
| 338 |
void (*xFunc)(sqlite3_context*,int,sqlite3_value**); |
| 339 |
} aFunc[] = { |
| 340 |
{ "ieee754", 1, 0, ieee754func }, |
| 341 |
{ "ieee754", 2, 0, ieee754func }, |
| 342 |
{ "ieee754_mantissa", 1, 1, ieee754func }, |
| 343 |
{ "ieee754_exponent", 1, 2, ieee754func }, |
| 344 |
{ "ieee754_to_blob", 1, 0, ieee754func_to_blob }, |
| 345 |
{ "ieee754_from_blob", 1, 0, ieee754func_from_blob }, |
| 346 |
{ "ieee754_to_int", 1, 0, ieee754func_to_int }, |
| 347 |
{ "ieee754_from_int", 1, 0, ieee754func_from_int }, |
| 348 |
{ "ieee754_inc", 2, 0, ieee754inc }, |
| 349 |
}; |
| 350 |
unsigned int i; |
| 351 |
int rc = SQLITE_OK; |
| 352 |
SQLITE_EXTENSION_INIT2(pApi); |
| 353 |
(void)pzErrMsg; /* Unused parameter */ |
| 354 |
for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){ |
| 355 |
rc = sqlite3_create_function(db, aFunc[i].zFName, aFunc[i].nArg, |
| 356 |
SQLITE_UTF8|SQLITE_INNOCUOUS, |
| 357 |
(void*)&aFunc[i].iAux, |
| 358 |
aFunc[i].xFunc, 0, 0); |
| 359 |
} |
| 360 |
return rc; |
| 361 |
} |