float.pony

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
use @"llvm.fabs.f32"[F32](x: F32)
use @"llvm.fabs.f64"[F64](x: F64)
use @"llvm.ceil.f32"[F32](x: F32)
use @"llvm.ceil.f64"[F64](x: F64)
use @"llvm.floor.f32"[F32](x: F32)
use @"llvm.floor.f64"[F64](x: F64)
use @"llvm.round.f32"[F32](x: F32)
use @"llvm.round.f64"[F64](x: F64)
use @"llvm.trunc.f32"[F32](x: F32)
use @"llvm.trunc.f64"[F64](x: F64)
use @"llvm.log.f32"[F32](x: F32)
use @"llvm.log.f64"[F64](x: F64)
use @"llvm.log2.f32"[F32](x: F32)
use @"llvm.log2.f64"[F64](x: F64)
use @"llvm.log10.f32"[F32](x: F32)
use @"llvm.log10.f64"[F64](x: F64)
use @logbf[F32](x: F32)
use @logb[F64](x: F64)
use @"llvm.pow.f32"[F32](x: F32, y: F32)
use @"llvm.pow.f64"[F64](x: F64, y: F64)
use @"llvm.powi.f32.i32"[F32](x: F32, y: I32) if not windows
use @"llvm.powi.f64.i32"[F64](x: F64, y: I32) if not windows
use @"llvm.sqrt.f32"[F32](x: F32)
use @"llvm.sqrt.f64"[F64](x: F64)
use @cbrtf[F32](x: F32)
use @cbrt[F64](x: F64)
use @"llvm.exp.f32"[F32](x: F32)
use @"llvm.exp.f64"[F64](x: F64)
use @"llvm.exp2.f32"[F32](x: F32)
use @"llvm.exp2.f64"[F64](x: F64)
use @"llvm.cos.f32"[F32](x: F32)
use @"llvm.cos.f64"[F64](x: F64)
use @"llvm.sin.f32"[F32](x: F32)
use @"llvm.sin.f64"[F64](x: F64)
use @tanf[F32](x: F32)
use @coshf[F32](x: F32)
use @sinhf[F32](x: F32)
use @tanhf[F32](x: F32)
use @acosf[F32](x: F32)
use @asinf[F32](x: F32)
use @atanf[F32](x: F32)
use @atan2f[F32](x: F32, y: F32)
use @acoshf[F32](x: F32)
use @asinhf[F32](x: F32)
use @atanhf[F32](x: F32)
use @tan[F64](x: F64)
use @cosh[F64](x: F64)
use @sinh[F64](x: F64)
use @tanh[F64](x: F64)
use @acos[F64](x: F64)
use @asin[F64](x: F64)
use @atan[F64](x: F64)
use @atan2[F64](x: F64, y: F64)
use @acosh[F64](x: F64)
use @asinh[F64](x: F64)
use @atanh[F64](x: F64)
use @"llvm.copysign.f32"[F32](x: F32, sign: F32)
use @"llvm.copysign.f64"[F64](x: F64, sign: F64)
use @frexp[F64](value: F64, exponent: Pointer[U32])
use @ldexpf[F32](value: F32, exponent: I32)
use @ldexp[F64](value: F64, exponent: I32)

primitive F32 is FloatingPoint[F32]
  new create(value: F32 = 0) => value
  new pi() => 3.14159265358979323846
  new e() => 2.71828182845904523536

  new _nan() => compile_intrinsic
  new _inf(negative: Bool) => compile_intrinsic

  new from_bits(i: U32) => compile_intrinsic
  fun bits(): U32 => compile_intrinsic
  new from[B: (Number & Real[B] val)](a: B) => a.f32()

  new min_value() =>
    """
    Minimum negative value representable.
    """
    from_bits(0xFF7FFFFF)

  new max_value() =>
    """
    Maximum positive value representable.
    """
    from_bits(0x7F7FFFFF)

  new min_normalised() =>
    """
    Minimum positive value representable at full precision (ie a normalised
    number).
    """
    from_bits(0x00800000)

  new epsilon() =>
    """
    Minimum positive value such that (1 + epsilon) != 1.
    """
    from_bits(0x34000000)

  fun tag radix(): U8 =>
    """
    Exponent radix.
    """
    2

  fun tag precision2(): U8 =>
    """
    Mantissa precision in bits.
    """
    24

  fun tag precision10(): U8 =>
    """
    Mantissa precision in decimal digits.
    """
    6

  fun tag min_exp2(): I16 =>
    """
    Minimum exponent value such that (2^exponent) - 1 is representable at full
    precision (ie a normalised number).
    """
    -125

  fun tag min_exp10(): I16 =>
    """
    Minimum exponent value such that (10^exponent) - 1 is representable at full
    precision (ie a normalised number).
    """
    -37

  fun tag max_exp2(): I16 =>
    """
    Maximum exponent value such that (2^exponent) - 1 is representable.
    """
    128

  fun tag max_exp10(): I16 =>
    """
    Maximum exponent value such that (10^exponent) - 1 is representable.
    """
    38

  fun abs(): F32 => @"llvm.fabs.f32"(this)
  fun ceil(): F32 => @"llvm.ceil.f32"(this)
  fun floor(): F32 => @"llvm.floor.f32"(this)
  fun round(): F32 => @"llvm.round.f32"(this)
  fun trunc(): F32 => @"llvm.trunc.f32"(this)

  fun min(y: F32): F32 => if this < y then this else y end
  fun max(y: F32): F32 => if this > y then this else y end

  fun fld(y: F32): F32 =>
    (this / y).floor()

  fun fld_unsafe(y: F32): F32 =>
    (this /~ y).floor()

  fun mod(y: F32): F32 =>
    let r = this % y
    if r == F32(0.0) then
      r.copysign(y)
    elseif (r > 0) xor (y > 0) then
      r + y
    else
      r
    end

  fun mod_unsafe(y: F32): F32 =>
    let r = this %~ y
    if r == F32(0.0) then
      r.copysign(y)
    elseif (r > 0) xor (y > 0) then
      r + y
    else
      r
    end

  fun finite(): Bool =>
    """
    Check whether this number is finite, ie not +/-infinity and not NaN.
    """
    // True if exponent is not all 1s
    (bits() and 0x7F800000) != 0x7F800000

  fun infinite(): Bool =>
    """
    Check whether this number is +/-infinity
    """
    // True if exponent is all 1s and mantissa is 0
    ((bits() and 0x7F800000) == 0x7F800000) and  // exp
    ((bits() and 0x007FFFFF) == 0)  // mantissa

  fun nan(): Bool =>
    """
    Check whether this number is NaN.
    """
    // True if exponent is all 1s and mantissa is non-0
    ((bits() and 0x7F800000) == 0x7F800000) and  // exp
    ((bits() and 0x007FFFFF) != 0)  // mantissa

  fun ldexp(x: F32, exponent: I32): F32 =>
    @ldexpf(x, exponent)

  fun frexp(): (F32, U32) =>
    var exponent: U32 = 0
    var mantissa = @frexp(f64(), addressof exponent)
    (mantissa.f32(), exponent)

  fun log(): F32 => @"llvm.log.f32"(this)
  fun log2(): F32 => @"llvm.log2.f32"(this)
  fun log10(): F32 => @"llvm.log10.f32"(this)
  fun logb(): F32 => @logbf(this)

  fun pow(y: F32): F32 => @"llvm.pow.f32"(this, y)
  fun powi(y: I32): F32 =>
    ifdef windows then
      pow(y.f32())
    else
      @"llvm.powi.f32.i32"(this, y)
    end

  fun sqrt(): F32 =>
    if this < 0.0 then
      _nan()
    else
      @"llvm.sqrt.f32"(this)
    end

  fun sqrt_unsafe(): F32 =>
    """
    Unsafe operation.
    If this is negative, the result is undefined.
    """
    @"llvm.sqrt.f32"(this)

  fun cbrt(): F32 => @cbrtf(this)
  fun exp(): F32 => @"llvm.exp.f32"(this)
  fun exp2(): F32 => @"llvm.exp2.f32"(this)

  fun cos(): F32 => @"llvm.cos.f32"(this)
  fun sin(): F32 => @"llvm.sin.f32"(this)
  fun tan(): F32 => @tanf(this)

  fun cosh(): F32 => @coshf(this)
  fun sinh(): F32 => @sinhf(this)
  fun tanh(): F32 => @tanhf(this)

  fun acos(): F32 => @acosf(this)
  fun asin(): F32 => @asinf(this)
  fun atan(): F32 => @atanf(this)
  fun atan2(y: F32): F32 => @atan2f(this, y)

  fun acosh(): F32 => @acoshf(this)
  fun asinh(): F32 => @asinhf(this)
  fun atanh(): F32 => @atanhf(this)

  fun copysign(sign: F32): F32 => @"llvm.copysign.f32"(this, sign)

  fun hash(): USize => bits().hash()
  fun hash64(): U64 => bits().hash64()

  fun i128(): I128 => f64().i128()
  fun u128(): U128 => f64().u128()

  fun i128_unsafe(): I128 =>
    """
    Unsafe operation.
    If the value doesn't fit in the destination type, the result is undefined.
    """
    f64_unsafe().i128_unsafe()

  fun u128_unsafe(): U128 =>
    """
    Unsafe operation.
    If the value doesn't fit in the destination type, the result is undefined.
    """
    f64_unsafe().u128_unsafe()

primitive F64 is FloatingPoint[F64]
  new create(value: F64 = 0) => value
  new pi() => 3.14159265358979323846
  new e() => 2.71828182845904523536

  new _nan() => compile_intrinsic
  new _inf(negative: Bool) => compile_intrinsic

  new from_bits(i: U64) => compile_intrinsic
  fun bits(): U64 => compile_intrinsic
  new from[B: (Number & Real[B] val)](a: B) => a.f64()

  new min_value() =>
    """
    Minimum negative value representable.
    """
    from_bits(0xFFEF_FFFF_FFFF_FFFF)

  new max_value() =>
    """
    Maximum positive value representable.
    """
    from_bits(0x7FEF_FFFF_FFFF_FFFF)

  new min_normalised() =>
    """
    Minimum positive value representable at full precision (ie a normalised
    number).
    """
    from_bits(0x0010_0000_0000_0000)

  new epsilon() =>
    """
    Minimum positive value such that (1 + epsilon) != 1.
    """
    from_bits(0x3CB0_0000_0000_0000)

  fun tag radix(): U8 =>
    """
    Exponent radix.
    """
    2

  fun tag precision2(): U8 =>
    """
    Mantissa precision in bits.
    """
    53

  fun tag precision10(): U8 =>
    """
    Mantissa precision in decimal digits.
    """
    15

  fun tag min_exp2(): I16 =>
    """
    Minimum exponent value such that (2^exponent) - 1 is representable at full
    precision (ie a normalised number).
    """
    -1021

  fun tag min_exp10(): I16 =>
    """
    Minimum exponent value such that (10^exponent) - 1 is representable at full
    precision (ie a normalised number).
    """
    -307

  fun tag max_exp2(): I16 =>
    """
    Maximum exponent value such that (2^exponent) - 1 is representable.
    """
    1024

  fun tag max_exp10(): I16 =>
    """
    Maximum exponent value such that (10^exponent) - 1 is representable.
    """
    308

  fun abs(): F64 => @"llvm.fabs.f64"(this)
  fun ceil(): F64 => @"llvm.ceil.f64"(this)
  fun floor(): F64 => @"llvm.floor.f64"(this)
  fun round(): F64 => @"llvm.round.f64"(this)
  fun trunc(): F64 => @"llvm.trunc.f64"(this)

  fun min(y: F64): F64 => if this < y then this else y end
  fun max(y: F64): F64 => if this > y then this else y end

  fun fld(y: F64): F64 =>
    (this / y).floor()

  fun fld_unsafe(y: F64): F64 =>
    (this /~ y).floor()

  fun mod(y: F64): F64 =>
    let r = this.rem(y)
    if r == F64(0.0) then
      r.copysign(y)
    elseif (r > 0) xor (y > 0) then
      r + y
    else
      r
    end

  fun mod_unsafe(y: F64): F64 =>
    let r = this %~ y
    if r == F64(0.0) then
      r.copysign(y)
    elseif (r > 0) xor (y > 0) then
      r + y
    else
      r
    end

  fun finite(): Bool =>
    """
    Check whether this number is finite, ie not +/-infinity and not NaN.
    """
    // True if exponent is not all 1s
    (bits() and 0x7FF0_0000_0000_0000) != 0x7FF0_0000_0000_0000

  fun infinite(): Bool =>
    """
    Check whether this number is +/-infinity
    """
    // True if exponent is all 1s and mantissa is 0
    ((bits() and 0x7FF0_0000_0000_0000) == 0x7FF0_0000_0000_0000) and  // exp
    ((bits() and 0x000F_FFFF_FFFF_FFFF) == 0)  // mantissa

  fun nan(): Bool =>
    """
    Check whether this number is NaN.
    """
    // True if exponent is all 1s and mantissa is non-0
    ((bits() and 0x7FF0_0000_0000_0000) == 0x7FF0_0000_0000_0000) and  // exp
    ((bits() and 0x000F_FFFF_FFFF_FFFF) != 0)  // mantissa

  fun ldexp(x: F64, exponent: I32): F64 =>
    @ldexp(x, exponent)

  fun frexp(): (F64, U32) =>
    var exponent: U32 = 0
    var mantissa = @frexp(this, addressof exponent)
    (mantissa, exponent)

  fun log(): F64 => @"llvm.log.f64"(this)
  fun log2(): F64 => @"llvm.log2.f64"(this)
  fun log10(): F64 => @"llvm.log10.f64"(this)
  fun logb(): F64 => @logb(this)

  fun pow(y: F64): F64 => @"llvm.pow.f64"(this, y)
  fun powi(y: I32): F64 =>
    ifdef windows then
      pow(y.f64())
    else
      @"llvm.powi.f64.i32"(this, y)
    end

  fun sqrt(): F64 =>
    if this < 0.0 then
      _nan()
    else
      @"llvm.sqrt.f64"(this)
    end

  fun sqrt_unsafe(): F64 =>
    """
    Unsafe operation.
    If this is negative, the result is undefined.
    """
    @"llvm.sqrt.f64"(this)

  fun cbrt(): F64 => @cbrt(this)
  fun exp(): F64 => @"llvm.exp.f64"(this)
  fun exp2(): F64 => @"llvm.exp2.f64"(this)

  fun cos(): F64 => @"llvm.cos.f64"(this)
  fun sin(): F64 => @"llvm.sin.f64"(this)
  fun tan(): F64 => @tan(this)

  fun cosh(): F64 => @cosh(this)
  fun sinh(): F64 => @sinh(this)
  fun tanh(): F64 => @tanh(this)

  fun acos(): F64 => @acos(this)
  fun asin(): F64 => @asin(this)
  fun atan(): F64 => @atan(this)
  fun atan2(y: F64): F64 => @atan2(this, y)

  fun acosh(): F64 => @acosh(this)
  fun asinh(): F64 => @asinh(this)
  fun atanh(): F64 => @atanh(this)

  fun copysign(sign: F64): F64 => @"llvm.copysign.f64"(this, sign)

  fun hash(): USize => bits().hash()
  fun hash64(): U64 => bits().hash64()

  fun i128(): I128 =>
    if this > I128.max_value().f64() then
      return I128.max_value()
    elseif this < I128.min_value().f64() then
      return I128.min_value()
    end

    let bit = bits()
    let high = (bit >> 32).u32()
    let ex = ((high and 0x7FF00000) >> 20) - 1023

    if ex < 0 then
      return 0
    end

    let s = ((high and 0x80000000) >> 31).i128()
    var r = ((bit and 0x000FFFFFFFFFFFFF) or 0x0010000000000000).i128()
    let ex' = ex.u128()

    if ex' > 52 then
      r = r << (ex' - 52)
    else
      r = r >> (52 - ex')
    end

    (r xor s) - s

  fun u128(): U128 =>
    if this > U128.max_value().f64() then
      return U128.max_value()
    elseif this < U128.min_value().f64() then
      return U128.min_value()
    end

    let bit = bits()
    let high = (bit >> 32).u32()
    let ex = ((high and 0x7FF00000) >> 20) - 1023

    if (ex < 0) or ((high and 0x80000000) != 0) then
      return 0
    end

    var r = ((bit and 0x000FFFFFFFFFFFFF) or 0x0010000000000000).u128()
    let ex' = ex.u128()

    if ex' > 52 then
      r = r << (ex' - 52)
    else
      r = r >> (52 - ex')
    end

    r.u128()

  fun i128_unsafe(): I128 =>
    """
    Unsafe operation.
    If the value doesn't fit in the destination type, the result is undefined.
    """
    i128()

  fun u128_unsafe(): U128 =>
    """
    Unsafe operation.
    If the value doesn't fit in the destination type, the result is undefined.
    """
    u128()

type Float is (F32 | F64)