/
Floatmu.jl
1506 lines (1239 loc) · 46.2 KB
/
Floatmu.jl
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Floatmu --
#
# Copyright 2019--2023 University of Nantes, France.
#
# This file is part of the MicroFloatingPoints library.
#
# The MicroFloatingPoints library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
# The MicroFloatingPoints library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received copies of the GNU General Public License and the
# GNU Lesser General Public License along with the MicroFloatingPoints Library.
# If not, see https://www.gnu.org/licenses/.
#import Formatting
import Printf.@printf
import Base.convert, Base.show, Base.Float16, Base.Float32, Base.Float64
import Base.sign, Base.signbit, Base.bitstring
import Base.typemin, Base.typemax, Base.maxintfloat, Base.ldexp, Base.eps
import Base.floatmax, Base.floatmin, Base.precision
import Base.isinf, Base.isfinite, Base.isnan, Base.issubnormal
import Base: exponent_max, exponent_raw_max
import Base.round, Base.trunc
import Base.parse, Base.tryparse
import Base.prevfloat, Base.nextfloat
import Base.promote_rule
import Base.Math.significand, Base.Math.significand_mask, Base.Math.exponent
import Base: +, -, *, /, ^
import Base: ==, !=, <, <=, >, >=
import Base: cos, sin, tan, exp, log, sqrt, log2
import Base.iterate, Base.length, Base.eltype
import Base.prevfloat, Base.nextfloat
import Base.decompose
import Base.reinterpret
export reinterpret
export fractional_even
"""
inexact_flag
Flag set to `true` if the latest computation led to some rounding. This is a sticky flag, which must be
explictly reset.
See [`reset_inexact()`](@ref)
"""
inexact_flag = false
@doc raw"""
Floatmu{szE,szf} <: AbstractFloat
IEEE 754-compliant floating-point number with `szE` bits for the exponent
and `szf` bits for the fractional part.
A `Floatmu` object must always have a precision smaller or equal to that of a single precision float. As a consequence, the following constraints hold:
```math
\left\{\begin{array}{l}
\text{szE}\in[2,8]\\
\text{szf}\in[2,23]
\end{array}\right.
```
# Examples
Creating a `Floatmu` type equivalent to `Float32`:
```jldoctest
julia> MyFloat32 = Floatmu{8,23}
Floatmu{8, 23}
julia> a=MyFloat32(0.1)
0.1
julia> a == 0.1f0
true
```
"""
struct Floatmu{szE, szf} <: AbstractFloat
# Representation of the Floatmu as a 32 bits unsigned integer.
# The various fields (s,e,f) are aligned to the right of the integer.
v::UInt32
# Is the value an approximation by default (-1), excess (+1) or the exact value (0)
inexact::Int32
function Floatmu{szE,szf}(x::Float64) where {szE,szf}
@assert szE isa Integer "Exponent size must be an integer!"
@assert szf isa Integer "Fractional part size must be an integer!"
@assert szE >= 2 && szE <= 8 && szf >= 2 && szf <= 23 "Exponent size must be in [2,8] and fractional part size in [2,23]!"
(val,rnd) = float64_to_uint32mu(x, szE, szf)
global inexact_flag = inexact_flag || (rnd != 0)
return new{szE,szf}(val,rnd)
end
function Floatmu{szE,szf}(x::Float32) where {szE,szf}
@assert szE isa Integer "Exponent size must be an integer!"
@assert szf isa Integer "Fractional part size must be an integer!"
@assert szE >= 2 && szE <= 8 && szf >= 2 && szf <= 23 "Exponent size must be in [2,8] and fractional part size in [2,23]!"
(val,rnd) = float64_to_uint32mu(Float64(x), szE, szf)
global inexact_flag = inexact_flag || (rnd != 0)
return new{szE,szf}(val,rnd)
end
function Floatmu{szE,szf}(x::Float16) where {szE,szf}
@assert szE isa Integer "Exponent size must be an integer!"
@assert szf isa Integer "Fractional part size must be an integer!"
@assert szE >= 2 && szE <= 8 && szf >= 2 && szf <= 23 "Exponent size must be in [2,8] and fractional part size in [2,23]!"
(val,rnd) = float64_to_uint32mu(Float64(x), szE, szf)
global inexact_flag = inexact_flag || (rnd != 0)
return new{szE,szf}(val,rnd)
end
function Floatmu{szE,szf}(x::Int64) where {szE,szf}
@assert szE isa Integer "Exponent size must be an integer!"
@assert szf isa Integer "Fractional part size must be an integer!"
@assert szE >= 2 && szE <= 8 && szf >= 2 && szf <= 23 "Exponent size must be in [2,8] and fractional part size in [2,23]!"
(val,rnd) = float64_to_uint32mu(Float64(x), szE, szf)
global inexact_flag = inexact_flag || (rnd != 0)
return new{szE,szf}(val,rnd)
end
function Floatmu{szE,szf}(x::Floatmu{szEb,szfb}) where {szE,szf,szEb,szfb}
@assert szE isa Integer "Exponent size must be an integer!"
@assert szf isa Integer "Fractional part size must be an integer!"
@assert szE >= 2 && szE <= 8 && szf >= 2 && szf <= 23 "Exponent size must be in [2,8] and fractional part size in [2,23]!"
(val,rnd) = float64_to_uint32mu(convert(Float64,x),szE,szf)
global inexact_flag = inexact_flag || (rnd != 0)
return new{szE,szf}(val, rnd)
end
"""
Floatmu{szE,szf}(x::UInt32, dummy) where {szE,szf}
Constructor for internal use only, when `x` is known to be
already compliant with the requirements of the `Floatmu{szE,szf} type.
The `dummy` parameter serves only to differentiate this constructor
from the others. Use `nothing` to signal its uselessness.
"""
function Floatmu{szE,szf}(x::UInt32, dummy) where {szE,szf}
@assert szE isa Integer "Exponent size must be an integer!"
@assert szf isa Integer "Fractional part size must be an integer!"
@assert szE >= 2 && szE <= 8 && szf >= 2 && szf <= 23 "Exponent size must be in [2,8] and fractional part size in [2,23]!"
return new{szE,szf}(x,0)
end
function Floatmu{szE,szf}(x::Tuple{UInt32,Int64}, dummy) where {szE,szf}
@assert szE isa Integer "Exponent size must be an integer!"
@assert szf isa Integer "Fractional part size must be an integer!"
@assert szE >= 2 && szE <= 8 && szf >= 2 && szf <= 23 "Exponent size must be in [2,8] and fractional part size in [2,23]!"
(val,rnd) = x
global inexact_flag = inexact_flag || (rnd != 0)
return new{szE,szf}(val,rnd)
end
end
function reinterpret(::Type{Unsigned},x::Floatmu{szE,szf}) where {szE,szf}
return x.v
end
function reinterpret(::Type{UInt32},x::Floatmu{szE,szf}) where {szE,szf}
return x.v
end
function reinterpret(::Type{UInt64},x::Floatmu{szE,szf}) where {szE,szf}
return UInt64(x.v)
end
function reinterpret(::Type{Int32},x::Floatmu{szE,szf}) where {szE,szf}
return Int32(x.v)
end
function reinterpret(::Type{Int64},x::Floatmu{szE,szf}) where {szE,szf}
return Int64(x.v)
end
function reinterpret(::Type{Floatmu{szE,szf}}, x::UInt32) where {szE,szf}
return Floatmu{szE,szf}(x,nothing)
end
function reinterpret(::Type{Floatmu{szE,szf}}, x::UInt64) where {szE,szf}
return Floatmu{szE,szf}(UInt32(x),nothing)
end
promote_rule(::Type{T},::Type{Floatmu{szE, szf}}) where {T<:Integer,szE,szf} = Float64
promote_rule(::Type{Float64},::Type{Floatmu{szE, szf}}) where {szE,szf} = Float64
promote_rule(::Type{Float32},::Type{Floatmu{szE, szf}}) where {szE,szf} = Float32
promote_rule(::Type{Float16},::Type{Floatmu{szE, szf}}) where {szE,szf} = Floatmu{max(5,szE),max(10,szf)}
promote_rule(::Type{Floatmu{szEa, szfa}},::Type{Floatmu{szEb, szfb}}) where {szEa,szfa, szEb, szfb} = Floatmu{max(szEa,szEb),max(szfa,szfb)}
# Mask to retrieve the fractional part (internal use)
significand_mask(::Type{Floatmu{szE,szf}}) where {szE, szf} = UInt32((UInt32(1) << szf) - 1)
# Mask to retrieve the exponent part (internal use)
exponent_mask(::Type{Floatmu{szE,szf}}) where {szE, szf} = UInt32((UInt32(1) << UInt32(szE))-1) << UInt32(szf)
# Mask to retrieve the sign part (internal use)
sign_mask(::Type{Floatmu{szE,szf}}) where {szE, szf} = UInt32(1) << (UInt32(szE)+UInt32(szf))
precision(::Type{Floatmu{szE,szf}}) where {szE,szf} = Int64(szf+1)
precision(x::Floatmu{szE,szf}) where {szE,szf} = Int64(szf+1)
"""
Emax(::Type{Floatmu{szE,szf}}) where {szE, szf}
Maximum unbiased exponent for a `Floatmu{szE,szf}` returned as an `UInt32`.
See: `exponent_max`, `exponent_raw_max`, [`Emin`](@ref)
# Examples
```jldoctest
julia> Emax(Floatmu{8, 23})
0x0000007f
```
"""
Emax(::Type{Floatmu{szE,szf}}) where {szE, szf} = UInt32(2^(UInt32(szE)-1)-1)
exponent_max(::Type{Floatmu{szE,szf}}) where {szE, szf} = Int64(Emax(Floatmu{szE,szf}))
exponent_raw_max(::Type{Floatmu{szE,szf}}) where {szE, szf} = Int64(exponent_mask(Floatmu{szE,szf}) >> szf)
"""
fractional_even(x::Floatmu{szE,szf}) where {szE,szf}
Return `true` if the fractional part of `x` has a zero as the rightmost bit.
BEWARE: the function does not check whether `x` is an NaN or an infinite value.
"""
function fractional_even(x::Floatmu{szE,szf}) where {szE,szf}
return (x.v & 1) == 0
end
"""
Emin(::Type{Floatmu{szE,szf}}) where {szE, szf}
Minimum unbiased exponent for a `Floatmu{szE,szf}` returned as an `Int32`.
See: `exponent_max`, `exponent_raw_max`, [`Emax`](@ref)
# Examples
```jldoctest
julia> Emin(Floatmu{8, 23})
-126
```
"""
Emin(::Type{Floatmu{szE,szf}}) where {szE, szf} = Int32(1 - Emax(Floatmu{szE,szf}))
"""
bias(::Type{Floatmu{szE,szf}}) where {szE, szf}
Bias of the exponent for a `Floatmu{szE,szf}`.
# Examples
```jldoctest
julia> bias(Floatmu{8, 23})
0x0000007f
```
"""
bias(::Type{Floatmu{szE,szf}}) where {szE, szf} = Emax(Floatmu{szE,szf})
"""
Infμ(::Type{Floatmu{szE,szf}}) where {szE,szf}
Positive infinite value in the format `Floatmu{szE,szf}`.
# Examples
```jldoctest
julia> Infμ(Floatmu{8, 23}) == Inf32
true
```
"""
Infμ(::Type{Floatmu{szE,szf}}) where {szE, szf} = Floatmu{szE,szf}(exponent_mask(Floatmu{szE,szf}),nothing)
"""
NaNμ(::Type{Floatmu{szE,szf}}) where {szE, szf}
NaN in the format `Floatmu{szE,szf}`.
The canonical NaN value has a sign bit set to zero and all bits of the fractional part set to zero except
for the leftmost one.
# Examples
```jldoctest
julia> isnan(NaNμ(Floatmu{2, 2}))
true
julia> NaNμ(Floatmu{2, 2})
NaNμ{2, 2}
```
"""
NaNμ(::Type{Floatmu{szE,szf}}) where {szE, szf} = Floatmu{szE,szf}(exponent_mask(Floatmu{szE,szf}) | (UInt32(1) << (UInt32(szf)-1)),nothing)
"""
ldexp(x::Floatmu{szE,szf}, n::Integer) where {szE, szf}
Return ``x \\times 2^n``.
!!! info
This is a quick-and-dirty implementation.
# Examples
```jldoctest
julia> ldexp(Floatmu{5,3}(2.5),3)
20.0
```
"""
function ldexp(x::Floatmu{szE,szf}, n::Integer) where {szE, szf}
(isnan(x) || isinf(x)) && return x
return x*2.0^n
end
"""
eps(::Type{Floatmu{szE,szf}}) where {szE,szf}
Return the *epsilon* of the type `Floatmu{szE,szf}`, which is the
difference between 1.0 and the next float.
# Examples
```jldoctest
julia> eps(Floatmu{2, 2})
0.25
julia> eps(Floatmu{3, 5})
0.0312
julia> eps(Floatmu{8, 23})==eps(Float32)
true
```
"""
function eps(::Type{Floatmu{szE,szf}}) where {szE,szf}
# The epsilon is equal to 2^-szf
# We do not create the bit representation directly to avoid
# complications when the epsilon is subnormal (e.g., with Floatmu{2,2})
v = 2.0^-Int32(szf)
return Floatmu{szE,szf}(v)
end
"""
λ(::Type{Floatmu{szE,szf}}) where {szE,szf}
Return λ, the smallest positive *normal* number of the type `Floatmu{szE,szf}`.
# Examples
```jldoctest
julia> λ(Floatmu{2, 2})
1.0
julia> λ(Floatmu{8, 23})==floatmin(Float32)
true
```
"""
λ(::Type{Floatmu{szE,szf}}) where {szE,szf} = floatmin(Floatmu{szE,szf})
"""
μ(::Type{Floatmu{szE,szf}}) where {szE,szf}
Return μ, the smallest positive subnormal number of type `Floatmu{szE,szf}`.
# Examples
```jldoctest
julia> μ(Floatmu{2, 2})
0.25
```
"""
function μ(::Type{Floatmu{szE,szf}}) where {szE,szf}
# μ has the form: 0 000...00 000...001
return Floatmu{szE,szf}(UInt32(1),nothing)
end
"""
sign(x::Floatmu{szE,szf}) where {szE, szf}
Return `x` if `x` is zero, 1.0 if `x` is strictly positive and
-1.0 if `x` is strictly negative. Return `NaN` if x is a *Not a Number*.
# Examples
```jldoctest
julia> sign(Floatmu{2, 3}(-1.6))
-1.0
julia> sign(Floatmu{2, 3}(1.6))
1.0
julia> sign(Floatmu{2, 3}(NaN))
NaNμ{2, 3}
julia> sign(Floatmu{2, 3}(-0.0))
-0.0
```
"""
function sign(x::Floatmu{szE,szf}) where {szE, szf}
if isnan(x)
return NaNμ(Floatmu{szE,szf})
end
if (x.v & ~sign_mask(Floatmu{szE,szf})) == 0
return x
else
# Return ±Floatmu{szE,szf}(1.0)
return Floatmu{szE,szf}((x.v & sign_mask(Floatmu{szE,szf})) | (bias(Floatmu{szE,szf}) << UInt32(szf)), nothing)
end
end
"""
signbit(x::Floatmu{szE,szf}) where {szE, szf}
Return `true` if `x` is signed and `false` otherwise. The result for a NaN may vary, depending
on the value of its sign bit.
# Examples
```jldoctest
julia> signbit(Floatmu{2, 3}(1.5))
false
julia> signbit(Floatmu{2, 3}(-1.5))
true
```
The function differentiates between ``-0.0`` and ``+0.0`` even though both
values test equal.
```jldoctest
julia> signbit(Floatmu{2, 3}(-0.0))
true
julia> signbit(Floatmu{2, 3}(0.0))
false
```
"""
function signbit(x::Floatmu{szE,szf}) where {szE, szf}
return (x.v & sign_mask(Floatmu{szE,szf})) != 0
end
"""
isnan(x::Floatmu{szE,szf}) where {szE,szf}
Return `true` if `x` is a *Not an Number* and `false` otherwise.
# Examples
```jldoctest
julia> isnan(Floatmu{2, 3}(1.5))
false
julia> isnan(Floatmu{2, 3}(NaN))
true
```
"""
function isnan(x::Floatmu{szE,szf}) where {szE,szf}
# A `Floatmu` is an NaN if its exponent has only ones and
# the fractional part is not entirely made of zeroes
return ((x.v & exponent_mask(Floatmu{szE,szf})) == exponent_mask(Floatmu{szE,szf})) &&
((x.v & significand_mask(Floatmu{szE,szf})) != 0)
end
"""
isinf(x::Floatmu{szE,szf}) where {szE,szf}
Return `true` if `x` is an infinity and `false` otherwise.
# Examples
```jldoctest
julia> isinf(Floatmu{2, 2}(1.5))
false
julia> isinf(Floatmu{2, 2}(-Inf))
true
julia> isinf(Floatmu{2, 2}(9.8))
true
```
"""
function isinf(x::Floatmu{szE,szf}) where {szE,szf}
# A `Floatmu` is infinite if its exponent has only ones and
# the fractional part is made only of zeroes
return ((x.v & exponent_mask(Floatmu{szE,szf})) == exponent_mask(Floatmu{szE,szf})) &&
((x.v & significand_mask(Floatmu{szE,szf})) == 0)
end
"""
isfinite(x::Floatmu{szE,szf}) where {szE,szf}
Return `true` if `x` is finite and `false` otherwise. An NaN is not finite.
# Examples
```jldoctest
julia> isfinite(Floatmu{2, 2}(1.5))
true
julia> isfinite(Floatmu{2, 2}(3.8))
false
julia> isfinite(Floatmu{2, 2}(NaN))
false
```
"""
function isfinite(x::Floatmu{szE,szf}) where {szE,szf}
# A `Floatmu` is finite if its exponent is not entirely made of ones
return x.v & exponent_mask(Floatmu{szE,szf}) != exponent_mask(Floatmu{szE,szf})
end
"""
issubnormal(x::Floatmu{szE,szf}) where {szE,szf}
Return `true` if `x` is a [subnormal number](https://en.wikipedia.org/wiki/Denormal_number) and `false` otherwise. According to the definition, ±0.0 is not a subnormal number.
# Examples
```jldoctest
julia> issubnormal(Floatmu{2, 2}(1.0))
false
julia> issubnormal(Floatmu{2, 2}(0.25))
true
julia> issubnormal(Floatmu{2, 2}(0.0))
false
```
"""
function issubnormal(x::Floatmu{szE,szf}) where {szE,szf}
# A `Floatmu` is subnormal if its biased exponent is
# zero and its fractional part is not zero.
return ((x.v & exponent_mask(Floatmu{szE,szf})) == 0) &&
((x.v & significand_mask(Floatmu{szE,szf})) != 0)
end
exponent_one(::Type{Floatmu{szE,szf}}) where {szE,szf} = UInt32(exponent_mask(Floatmu{szE,szf}) & (1<<(szE+szf-1)-1))
# See [`Base.Math.significand`](https://docs.julialang.org/en/v1/base/numbers/#Base.Math.significand)
function significand(x::Floatmu{szE,szf}) where {szE,szf}
xu = x.v
xs = xu & ~sign_mask(Floatmu{szE,szf})
xs >= exponent_mask(Floatmu{szE,szf}) && return x # NaN or Inf
if issubnormal(x) || iszero(x)
xs == 0 && return x # +-0
m = UInt32(leading_zeros(xs) - szE)
xs <<= m
xu = xs | (xu & sign_mask(Floatmu{szE,szf}))
end
xu = (xu & ~exponent_mask(Floatmu{szE,szf})) | exponent_one(Floatmu{szE,szf})
return Floatmu{szE,szf}(xu,nothing)
end
# See [`Base.Math.significand`](https://docs.julialang.org/en/v1/base/numbers/#Base.Math.exponent)
function exponent(x::Floatmu{szE,szf}) where {szE,szf}
xs = x.v & ~sign_mask(Floatmu{szE,szf})
xs >= exponent_mask(Floatmu{szE,szf}) && throw(DomainError(x, "Cannot be NaN or Inf."))
k = Int(xs >> szf)
if k == 0 # x is subnormal
xs == 0 && throw(DomainError(x, "Cannot be subnormal converted to 0."))
m = leading_zeros(xs) - szE
k = 1 - m
end
return k - Int(bias(Floatmu{szE,szf}))
end
"""
floatmax(::Type{Floatmu{szE,szf}}) where {szE,szf}
Return the largest positive *normal* number of the type `Floatmu{szE,szf}`.
# Examples
```jldoctest
julia> floatmax(Floatmu{2, 2})
3.5
julia> floatmax(Floatmu{8, 23})==floatmax(Float32)
true
```
"""
function floatmax(::Type{Floatmu{szE,szf}}) where {szE,szf}
# The largest normal `Floatmu` is of the form:
# 0 111...110 11111...111
# where the exponent is one less than 2^szE-1
e = Emax(Floatmu{szE,szf}) + bias(Floatmu{szE,szf})
f = significand_mask(Floatmu{szE,szf})
return Floatmu{szE,szf}((e << UInt32(szf)) | f, nothing)
end
"""
floatmin(::Type{Floatmu{szE,szf}}) where {szE,szf}
Return λ, the smallest positive *normal* number of the type `Floatmu{szE,szf}`.
# Examples
```jldoctest
julia> floatmin(Floatmu{2, 2})
1.0
julia> floatmin(Floatmu{8, 23})==floatmin(Float32)
true
```
"""
function floatmin(::Type{Floatmu{szE,szf}}) where {szE,szf}
# λ is of the form: 0 000...001 000...0000
return Floatmu{szE,szf}(UInt32(UInt32(1) << UInt32(szf)), nothing)
end
"""
typemin(::Type{Floatmu{szE,szf}}) where {szE,szf}
Return the negative infinite of the type `Floatmu{szE,szf}`.
# Examples
```jldoctest
julia> typemin(Floatmu{3, 5})
-Infμ{3, 5}
```
"""
function typemin(::Type{Floatmu{szE,szf}}) where {szE,szf}
return -Infμ(Floatmu{szE,szf})
end
"""
typemax(::Type{Floatmu{szE,szf}}) where {szE,szf}
Return the positive infinite of the type `Floatmu{szE,szf}`.
# Examples
```jldoctest
julia> typemax(Floatmu{3, 5})
Infμ{3, 5}
```
"""
function typemax(::Type{Floatmu{szE,szf}}) where {szE,szf}
return Infμ(Floatmu{szE,szf})
end
"""
maxintfloat(::Type{Floatmu{szE,szf}}) where {szE,szf}
Return the smallest positive integer ``n`` such that ``n+1`` is not representable
in the type `Floatmu{szE,szf}`. The number ``n`` is returned as a
`Floatmu{szE,szf}`.
The function returns an infinite value if all integers are representable in the domain
of normal values.
# Examples
```jldoctest
julia> maxintfloat(Floatmu{3,2})
8.0
julia> maxintfloat(Floatmu{2,2})
Infμ{2, 2}
julia> maxintfloat(Floatmu{8,23})==maxintfloat(Float32)
true
```
"""
function maxintfloat(::Type{Floatmu{szE,szf}}) where {szE,szf}
m = Float64(UInt32(1) << (UInt32(szf)+1))
if m > floatmax(Floatmu{szE,szf})
return Infμ(Floatmu{szE,szf})
else
return Floatmu{szE,szf}(m)
end
end
"""
double_fields(x::Float64)
Return the sign, biased exponent, and fractional part of a Float64 number.
"""
function double_fields(x::Float64)
v = reinterpret(UInt64,x)
s = ((v & 0x8000000000000000) >> 63) % UInt32
e = ((v & 0x7ff0000000000000) >> 52) % UInt32
f= v & 0x000fffffffffffff
return (s,e,f)
end
"""
roundfrac(f,szf)
Round to nearest-even a 52 bits fractional part to `szf` bits
Return a triplet composed of the rounded fractional part, a correction to the exponent
if a bit from the fractional part spilled into the integer part, and a rounding direction
(by default: -1, by excess: 1, no rounding: 0) if some rounding had to take place.
"""
function roundfrac(f,szf)
# Creating the mask for the bits of `f` we cannot store
masktrailing = UInt64(2^(52-szf)-1)
# Integer value of the least significant bit for a `szf` bits fractional part
lsb = UInt64(2^(52-szf))
# Retrieving the 52-szf trailing bits to know in which direction to round
tailbits = f & masktrailing
# Eliminating the trailing bits
f &= ~masktrailing
if tailbits == lsb/2 # Halfway between two representable floats
if f & lsb != 0
# Rounding by excess to the next float due to the "even" rule
newf = f+lsb
if newf == 0x0010000000000000
return (0, 1, 1)
else
return (newf >> (52-szf), 0, 1)
end
else
# Rounding by default due to the "even" rule
return (f >> (52-szf), 0, -1)
end
end
if tailbits < lsb/2
# Rounding by default
return (f >> (52-szf), 0, ifelse(tailbits == 0,0,-1))
end
# tailbits > lsb/2
# Rounding by excess
newf = f+lsb
if newf == 0x0010000000000000
return (0, 1, 1)
else
return (newf >> (52-szf), 0, 1)
end
end
"""
float64_to_uint32mu(x::Float64,szE,szf)
Round `x` to the precision of a `Floatmu{szE,szf}` and
return a pair composed of the bits representation right-aligned in a `UInt32` together
with a rounding direction if rounding took place (by default: -1, by excess: 1, no rounding: 0).
"""
function float64_to_uint32mu(x::Float64,szE,szf)::Tuple{UInt32,Int64}
if isnan(x)
# NaNμ{szE,szf}: 0 111...11 1000...00
return (exponent_mask(Floatmu{szE,szf}) | (UInt32(1) << (UInt32(szf)-1)),0)
else
absx = abs(x)
# if |x| is greater or equal to floatmax(Floatmu{szE,szf} + half the distance
# between floatmax(Floatmu{szE,szf}) and Infμ{szE,szf}, we must
# round to Infμ{szE,szf}
rndpoint = (2.0-2.0^(-Int64(szf)-1))*2.0^Emax(Floatmu{szE,szf})
if absx >= rndpoint
s = (x < 0) ? UInt32(1) << (UInt32(szE)+UInt32(szf)) : 0
e = exponent_mask(Floatmu{szE,szf})
return ((s | e),ifelse(isinf(x), 0, signbit(x) ? -1 : 1)) # Infμ{szE,szf}
else
(s,e,f) = double_fields(x)
# Should we round to some subnormal `Floatmu{szE,szf}`?
# λ = 2^Emin{szE,szf}
if absx < 2.0^Emin(Floatmu{szE,szf})
# |x| <= μ/2?
# This situation occurs, in particular, for all subnormal
# double precision numbers.
if absx <= 2.0^(-Int64(szf)-1+Emin(Floatmu{szE,szf}))
if signbit(x)
return (UInt32(1) << (UInt32(szE)+UInt32(szf)), x==0.0 ? 0 : 1)
else
return (0, x==0.0 ? 0 : -1)
end
else
# `x` is subnormal in the format `Floatmu{szE,szf}`
# (but normal in double precision, see comment above) so
# we shift the fractional part such that the resulting
# exponent is Emin(Floatmu{szE,szf})
shift = Emin(Floatmu{szE,szf}) - (e - 1023)
newf = ((f >> 1) + 2^51) >> (shift-1) # Adding hidden bit
(newf, addE, inexact) = roundfrac(newf,szf)
if addE != 0 # The rounded number is normal
return ((s << (UInt32(szE)+UInt32(szf))) | (UInt32(1) << UInt32(szf)) | newf,
x < 0 ? -inexact : inexact)
else
return ((s << (UInt32(szE)+UInt32(szf))) | newf,
x < 0 ? -inexact : inexact) # e==0 (subnormal)
end
end
else # Rounding to a normal float in the format `Floatmu{szE,szf}`
newe = (e - UInt32(1023)) + bias(Floatmu{szE,szf})
(newf, addE, inexact) = roundfrac(f,szf)
return (s << (UInt32(szE)+UInt32(szf)) | ((newe+addE) << UInt32(szf)) | newf,
x < 0 ? -inexact : inexact)
end
end
end
end
"""
convert(::Type{Float64}, x::Floatmu{szE,szf}) where {szE, szf}
convert(::Type{Float32}, x::Floatmu{szE,szf}) where {szE, szf}
convert(::Type{Float16}, x::Floatmu{szE,szf}) where {szE, szf}
convert(::Type{Floatmu{szE,szf} where {szE,szf}}, x::Float64)
convert(::Type{Floatmu{szE,szf} where {szE,szf}}, x::Float32)
convert(::Type{Floatmu{szE,szf} where {szE,szf}}, x::Float16)
Convert a `Floatmu` to a double, single or half precision float, or vice-versa. For the double precision,
the conversion never introduces errors since `Float64` objects have at least twice the precision
of the fractional part of a `Floatmu` object.
# Examples
```jldoctest
julia> convert(Float64,Floatmu{8, 23}(0.1))
0.10000000149011612
julia> convert(Float32,Floatmu{8, 23}(0.1)) == 0.1f0
true
julia> convert(Float32,Floatmu{5, 10}(0.1)) == Float16(0.1)
true
julia> convert(Floatmu{2, 4},0.1)
0.125
julia> convert(Floatmu{2, 4},0.1f0)
0.125
julia> convert(Floatmu{2, 4},Float16(0.1))
0.125
julia> Floatmu{5, 10}(0.1)==Float16(0.1)
true
```
"""
function convert end
function convert(::Type{Float64}, x::Floatmu{szE,szf}) where {szE, szf}
if isnan(x)
return NaN
elseif isinf(x)
signx = (x.v & sign_mask(Floatmu{szE,szf})) >> (UInt32(szE)+UInt32(szf))
return (signx == 1) ? -Inf : Inf
else
s = (x.v & sign_mask(Floatmu{szE,szf})) >> (UInt32(szE)+UInt32(szf))
e = ((x.v & exponent_mask(Floatmu{szE,szf})) >> UInt32(szf))
f = x.v & significand_mask(Floatmu{szE,szf})
if e == 0
if f == 0
return (s==0 ? 1.0 : -1.0)*0.0
else
return (s==0 ? 1.0 : -1.0)*2.0^(Emin(Floatmu{szE,szf})-szf)*(x.v & significand_mask(Floatmu{szE,szf}))
end
else
E = Int64(e) - bias(Floatmu{szE,szf})
return (s==0 ? 1.0 : -1.0)*((2.0^UInt32(szf) + f)/2.0^UInt32(szf))*2.0^E
end
end
end
function convert(::Type{Float16}, x::Floatmu{szE,szf}) where {szE, szf}
return Float16(convert(Float64,x))
end
function convert(::Type{Float32}, x::Floatmu{szE,szf}) where {szE, szf}
return Float32(convert(Float64,x))
end
function convert(::Type{BigFloat}, x::Floatmu{szE,szf}) where {szE, szf}
return BigFloat(convert(Float64,x))
end
function convert(::Type{Floatmu{szE,szf}}, x::BigFloat) where {szE,szf}
return Floatmu{szE,szf}(float64_to_uint32mu(Float64(x),szE,szf),nothing)
end
function convert(::Type{Floatmu{szE,szf}}, x::Float64) where {szE,szf}
return Floatmu{szE,szf}(float64_to_uint32mu(x,szE,szf),nothing)
end
function convert(::Type{Floatmu{szE,szf}}, x::Float32) where {szE,szf}
return Floatmu{szE,szf}(float64_to_uint32mu(Float64(x),szE,szf),nothing)
end
function convert(::Type{Floatmu{szE,szf}}, x::Float16) where {szE,szf}
return Floatmu{szE,szf}(float64_to_uint32mu(Float64(x),szE,szf),nothing)
end
Float16(x::Floatmu{szE,szf}) where {szE,szf} = convert(Float16,x)
Float32(x::Floatmu{szE,szf}) where {szE,szf} = convert(Float32,x)
Float64(x::Floatmu{szE,szf}) where {szE,szf} = convert(Float64,x)
BigFloat(x::Floatmu{szE,szf}) where {szE,szf} = convert(BigFloat,x)
function round(x::Floatmu{szE,szf},r::RoundingMode) where {szE,szf}
return Floatmu{szE,szf}(round(Float64(x),r))
end
for Ty in (Int8, Int32, Int64, UInt8, UInt16, UInt32, UInt64)
@eval begin
trunc(::Type{$Ty}, x::Floatmu{szE,szf}) where {szE,szf} = $Ty(Float64(x))
end
end
"""
parse(::Type{Floatmu{szE,szf}}, str::AbstractString) where {szE, szf}
Parse the string `str` representing a floating-point number and convert it
to a `Floatmu{szE,szf}` object.
# Examples
```jldoctest
julia> parse(Floatmu{5, 7},"0.1")
0.1
julia> parse(Floatmu{5, 7},"1.0e10")
Infμ{5, 7}
```
The string is first converted to a `Float64` and then rounded to the precision of
the `Floatmu` object.
If the string cannot be converted to a `Float64`, the `ArgumentError` exception is
thrown.
# Examples
```jldoctest
julia> parse(Floatmu{5, 7},"0.1a")
ERROR: ArgumentError: cannot parse "0.1a" as a Floatmu{5, 7}
```
"""
function parse(::Type{Floatmu{szE,szf}}, str::AbstractString) where {szE, szf}
try
return Floatmu{szE,szf}(parse(Float64,str))
catch err
if isa(err,ArgumentError)
throw(ArgumentError("cannot parse \"$str\" as a Floatmu{$szE, $szf}"))
else
rethrow(err)
end
end
end
"""
tryparse(::Type{Floatmu{szE,szf}}, str::AbstractString) where {szE, szf}
Parse the string `str` representing a floating-point number and convert it
to a `Floatmu{szE,szf}` object.
# Examples
```jldoctest
julia> tryparse(Floatmu{5, 7},"0.1")
0.1
julia> tryparse(Floatmu{5, 7},"1.0e10")
Infμ{5, 7}
```
The string is first converted to a `Float64` and then rounded to the precision of
the `Floatmu` object.
Contrary to `parse`, if the string cannot be converted to a `Float64`, the value `nothing` is returned.
# Examples
```jldoctest
julia> tryparse(Floatmu{5, 7},"0.1a") == nothing
true
```
"""
function tryparse(::Type{Floatmu{szE,szf}}, str::AbstractString) where {szE, szf}
r = tryparse(Float64,str)
if r == nothing
return nothing
else
return Floatmu{szE,szf}(r)
end
end
# Hack to use @printf with a format depending on the `Floatmu` used.
# Since @printf is a macro, it cannot be called with anything other than a constant
# for the format string.
variable_printf(io,x::Floatmu{szE,2}) where {szE} = @printf(io,"%.3g",convert(Float64,x))
variable_printf(io,x::Floatmu{szE,3}) where {szE} = @printf(io,"%.3g",convert(Float64,x))
variable_printf(io,x::Floatmu{szE,4}) where {szE} = @printf(io,"%.3g",convert(Float64,x))
variable_printf(io,x::Floatmu{szE,5}) where {szE} = @printf(io,"%.3g",convert(Float64,x))
variable_printf(io,x::Floatmu{szE,6}) where {szE} = @printf(io,"%.3g",convert(Float64,x))
variable_printf(io,x::Floatmu{szE,7}) where {szE} = @printf(io,"%.3g",convert(Float64,x))
variable_printf(io,x::Floatmu{szE,8}) where {szE} = @printf(io,"%.3g",convert(Float64,x))
variable_printf(io,x::Floatmu{szE,9}) where {szE} = @printf(io,"%.4g",convert(Float64,x))
variable_printf(io,x::Floatmu{szE,10}) where {szE} = @printf(io,"%.4g",convert(Float64,x))
variable_printf(io,x::Floatmu{szE,11}) where {szE} = @printf(io,"%.4g",convert(Float64,x))
variable_printf(io,x::Floatmu{szE,12}) where {szE} = @printf(io,"%.4g",convert(Float64,x))
variable_printf(io,x::Floatmu{szE,13}) where {szE} = @printf(io,"%.5g",convert(Float64,x))
variable_printf(io,x::Floatmu{szE,14}) where {szE} = @printf(io,"%.5g",convert(Float64,x))
variable_printf(io,x::Floatmu{szE,15}) where {szE} = @printf(io,"%.5g",convert(Float64,x))
variable_printf(io,x::Floatmu{szE,16}) where {szE} = @printf(io,"%.6g",convert(Float64,x))
variable_printf(io,x::Floatmu{szE,17}) where {szE} = @printf(io,"%.6g",convert(Float64,x))
variable_printf(io,x::Floatmu{szE,18}) where {szE} = @printf(io,"%.6g",convert(Float64,x))
variable_printf(io,x::Floatmu{szE,19}) where {szE} = @printf(io,"%.7g",convert(Float64,x))
variable_printf(io,x::Floatmu{szE,20}) where {szE} = @printf(io,"%.7g",convert(Float64,x))
variable_printf(io,x::Floatmu{szE,21}) where {szE} = @printf(io,"%.7g",convert(Float64,x))
variable_printf(io,x::Floatmu{szE,22}) where {szE} = @printf(io,"%.7g",convert(Float64,x))
variable_printf(io,x::Floatmu{szE,23}) where {szE} = @printf(io,"%.7g",convert(Float64,x))
function show(io::IO, x::Floatmu{szE,szf}) where {szE, szf}