17
17
// along with this program; if not, write to the Free Software
18
18
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
19
20
- //Provides: jsoo_floor_log2
21
- var log2_ok = Math . log2 && Math . log2 ( 1.1235582092889474e307 ) === 1020 ;
22
- function jsoo_floor_log2 ( x ) {
23
- if ( log2_ok ) return Math . floor ( Math . log2 ( x ) ) ;
24
- var i = 0 ;
25
- if ( x === 0 ) return Number . NEGATIVE_INFINITY ;
26
- if ( x >= 1 ) {
27
- while ( x >= 2 ) {
28
- x /= 2 ;
29
- i ++ ;
30
- }
31
- } else {
32
- while ( x < 1 ) {
33
- x *= 2 ;
34
- i -- ;
35
- }
36
- }
37
- return i ;
38
- }
20
+ //Provides: jsoo_dataview
21
+ var jsoo_dataview = new DataView ( new ArrayBuffer ( 8 ) ) ;
39
22
40
23
//Provides: caml_int64_bits_of_float const
41
- //Requires: jsoo_floor_log2, caml_int64_create_lo_mi_hi
24
+ //Requires: caml_int64_create_lo_mi_hi
25
+ //Requires: jsoo_dataview
42
26
function caml_int64_bits_of_float ( x ) {
43
- if ( ! Number . isFinite ( x ) ) {
44
- if ( Number . isNaN ( x ) ) return caml_int64_create_lo_mi_hi ( 1 , 0 , 0x7ff0 ) ;
45
- if ( x > 0 ) return caml_int64_create_lo_mi_hi ( 0 , 0 , 0x7ff0 ) ;
46
- else return caml_int64_create_lo_mi_hi ( 0 , 0 , 0xfff0 ) ;
47
- }
48
- var sign =
49
- x === 0 && 1 / x === Number . NEGATIVE_INFINITY
50
- ? 0x8000
51
- : x >= 0
52
- ? 0
53
- : 0x8000 ;
54
- if ( sign ) x = - x ;
55
- // Int64.bits_of_float 1.1235582092889474E+307 = 0x7fb0000000000000L
56
- // using Math.LOG2E*Math.log(x) in place of Math.log2 result in precision lost
57
- var exp = jsoo_floor_log2 ( x ) + 1023 ;
58
- if ( exp <= 0 ) {
59
- exp = 0 ;
60
- x /= Math . pow ( 2 , - 1026 ) ;
61
- } else {
62
- x /= Math . pow ( 2 , exp - 1027 ) ;
63
- if ( x < 16 ) {
64
- x *= 2 ;
65
- exp -= 1 ;
66
- }
67
- if ( exp === 0 ) {
68
- x /= 2 ;
69
- }
70
- }
71
- var k = Math . pow ( 2 , 24 ) ;
72
- var r3 = x | 0 ;
73
- x = ( x - r3 ) * k ;
74
- var r2 = x | 0 ;
75
- x = ( x - r2 ) * k ;
76
- var r1 = x | 0 ;
77
- r3 = ( r3 & 0xf ) | sign | ( exp << 4 ) ;
27
+ jsoo_dataview . setFloat64 ( 0 , x , true ) ;
28
+ var lo32 = jsoo_dataview . getUint32 ( 0 , true ) ;
29
+ var hi32 = jsoo_dataview . getUint32 ( 4 , true ) ;
30
+ var r1 = lo32 & 0xffffff ;
31
+ var r2 = ( lo32 >>> 24 ) | ( ( hi32 << 8 ) & 0xffffff ) ;
32
+ var r3 = ( hi32 >>> 16 ) & 0xffff ;
78
33
return caml_int64_create_lo_mi_hi ( r1 , r2 , r3 ) ;
79
34
}
80
35
81
36
//Provides: caml_int32_bits_of_float const
82
- //Requires: jsoo_floor_log2
37
+ //Requires: jsoo_dataview
83
38
function caml_int32_bits_of_float ( x ) {
84
- var float32a = new Float32Array ( 1 ) ;
85
- float32a [ 0 ] = x ;
86
- var int32a = new Int32Array ( float32a . buffer ) ;
87
- return int32a [ 0 ] | 0 ;
39
+ jsoo_dataview . setFloat32 ( 0 , x , true ) ;
40
+ return jsoo_dataview . getUint32 ( 0 , true ) | 0 ;
88
41
}
89
42
90
43
//FP literals can be written using the hexadecimal
@@ -150,24 +103,14 @@ function caml_hexstring_of_float(x, prec, style) {
150
103
}
151
104
152
105
//Provides: caml_int64_float_of_bits const
106
+ //Requires: jsoo_dataview
153
107
function caml_int64_float_of_bits ( x ) {
154
108
var lo = x . lo ;
155
109
var mi = x . mi ;
156
110
var hi = x . hi ;
157
- var exp = ( hi & 0x7fff ) >> 4 ;
158
- if ( exp === 2047 ) {
159
- if ( ( lo | mi | ( hi & 0xf ) ) === 0 )
160
- return hi & 0x8000 ? Number . NEGATIVE_INFINITY : Number . POSITIVE_INFINITY ;
161
- else return Number . NaN ;
162
- }
163
- var k = Math . pow ( 2 , - 24 ) ;
164
- var res = ( lo * k + mi ) * k + ( hi & 0xf ) ;
165
- if ( exp > 0 ) {
166
- res += 16 ;
167
- res *= Math . pow ( 2 , exp - 1027 ) ;
168
- } else res *= Math . pow ( 2 , - 1026 ) ;
169
- if ( hi & 0x8000 ) res = - res ;
170
- return res ;
111
+ jsoo_dataview . setUint32 ( 0 , lo | ( mi << 24 ) , true ) ;
112
+ jsoo_dataview . setUint32 ( 4 , ( mi >>> 8 ) | ( hi << 16 ) , true ) ;
113
+ return jsoo_dataview . getFloat64 ( 0 , true ) ;
171
114
}
172
115
173
116
//Provides: caml_nextafter_float const
@@ -192,11 +135,10 @@ function caml_trunc_float(x) {
192
135
}
193
136
194
137
//Provides: caml_int32_float_of_bits const
138
+ //Requires: jsoo_dataview
195
139
function caml_int32_float_of_bits ( x ) {
196
- var int32a = new Int32Array ( 1 ) ;
197
- int32a [ 0 ] = x ;
198
- var float32a = new Float32Array ( int32a . buffer ) ;
199
- return float32a [ 0 ] ;
140
+ jsoo_dataview . setUint32 ( 0 , x , true ) ;
141
+ return jsoo_dataview . getFloat32 ( 0 , true ) ;
200
142
}
201
143
202
144
//Provides: caml_classify_float const
@@ -244,12 +186,11 @@ function caml_ldexp_float(x, exp) {
244
186
return x ;
245
187
}
246
188
//Provides: caml_frexp_float const
247
- //Requires: jsoo_floor_log2
248
189
function caml_frexp_float ( x ) {
249
190
if ( x === 0 || ! Number . isFinite ( x ) ) return [ 0 , x , 0 ] ;
250
191
var neg = x < 0 ;
251
192
if ( neg ) x = - x ;
252
- var exp = Math . max ( - 1023 , jsoo_floor_log2 ( x ) + 1 ) ;
193
+ var exp = Math . max ( - 1023 , Math . floor ( Math . log2 ( x ) ) + 1 ) ;
253
194
x *= Math . pow ( 2 , - exp ) ;
254
195
while ( x < 0.5 ) {
255
196
x *= 2 ;
0 commit comments