4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 ******************************************************************************
13 ** This SQLite extension implements functions for the exact display
14 ** and input of IEEE754 Binary64 floating-point numbers.
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).
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).
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
34 #include "sqlite3ext.h"
35 SQLITE_EXTENSION_INIT1
40 ** Implementation of the ieee754() function
42 static void ieee754func(
43 sqlite3_context
*context
,
53 assert( sizeof(m
)==sizeof(r
) );
54 if( sqlite3_value_type(argv
[0])!=SQLITE_FLOAT
) return;
55 r
= sqlite3_value_double(argv
[0]);
62 memcpy(&a
,&r
,sizeof(a
));
68 m
= a
& ((((sqlite3_int64
)1)<<52)-1);
69 m
|= ((sqlite3_int64
)1)<<52;
70 while( e
<1075 && m
>0 && (m
&1)==0 ){
76 sqlite3_snprintf(sizeof(zResult
), zResult
, "ieee754(%lld,%d)",
78 sqlite3_result_text(context
, zResult
, -1, SQLITE_TRANSIENT
);
80 sqlite3_int64 m
, e
, a
;
83 m
= sqlite3_value_int64(argv
[0]);
84 e
= sqlite3_value_int64(argv
[1]);
89 }else if( m
==0 && e
>1000 && e
<1000 ){
90 sqlite3_result_double(context
, 0.0);
93 while( (m
>>32)&0xffe00000 ){
97 while( ((m
>>32)&0xfff00000)==0 ){
104 a
= m
& ((((sqlite3_int64
)1)<<52)-1);
106 if( isNeg
) a
|= ((sqlite3_int64
)1)<<63;
107 memcpy(&r
, &a
, sizeof(r
));
108 sqlite3_result_double(context
, r
);
114 __declspec(dllexport
)
116 int sqlite3_ieee_init(
119 const sqlite3_api_routines
*pApi
122 SQLITE_EXTENSION_INIT2(pApi
);
123 (void)pzErrMsg
; /* Unused parameter */
124 rc
= sqlite3_create_function(db
, "ieee754", 1, SQLITE_UTF8
, 0,
127 rc
= sqlite3_create_function(db
, "ieee754", 2, SQLITE_UTF8
, 0,