forked from pascal-cuoq/abstract_floats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloat.ml
3139 lines (2784 loc) · 107 KB
/
float.ml
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
let phys_equal = ( == )
let (!=) = `Use_phys_equal
let (==) = `Use_phys_equal
type abstract_float = float array
(*
The type [abstract_float] is represented using an array of (unboxed) floats.
Array [t] with length 1:
a single floating-point number
(can be NaN, +inf, -inf, or a finite value)
Array [t] with length >=2:
header + bounds. the first field is header. the rest of fields are bounds.
the length of t can only be 2, 3 or 5.
length of 2:
only intended to distinguish a header from a single
floating-point number. a.(1) repeats a.(0).
length of 5:
the FP number could be both pos normalish and neg
normalish. the last four fields indicating two pairs of bounds
(first neg bounds, then pos bounds).
length of 3:
the fp number could be either pos normalish or neg normalish,
the fields .(1) and .(2) provide the bounds.
The header (found in t.(0)) can indicate:
at least one of the NaN values present
all NaN values present
FP number can be in negative normalish range
FP number can be in positive normalish range
-inf present
+inf present
-0.0 present
+0.0 present
Vocabulary:
- normalish means the intervals of normal (or subnormal) values
- finite means the normalish and zero components of the representation
- nonzero means the normalish and infinite components, but usually not NaN
*)
let sign_bit = 0x8000_0000_0000_0000L
let payload_mask = 0x800F_FFFF_FFFF_FFFFL
let header_mask = 0x0FF0_0000_0000_0000L
let to_payload n = Int64.logand n payload_mask
let is_pos f = Int64.(logand (bits_of_float f) sign_bit) = 0L
let is_neg f = Int64.(logand (bits_of_float f) sign_bit) <> 0L
let is_NaN f = classify_float f = FP_nan
let is_zero f = classify_float f = FP_zero
let is_inf f = classify_float f = FP_infinite
let is_pos_zero f = Int64.bits_of_float f = 0L
let is_neg_zero f = Int64.bits_of_float f = sign_bit
let fsucc f = Int64.(float_of_bits @@ succ @@ bits_of_float f)
let fpred f = Int64.(float_of_bits @@ pred @@ bits_of_float f)
let largest_neg = -4.94e-324
let smallest_pos = +4.94e-324
let smallest_neg = -.max_float
let largest_pos = max_float
let dump_internal a =
let l = Array.length a in
Format.printf "[|";
for i = 0 to l-1 do
if i = 0 || l = 2
then Format.printf "0x%016Lx" (Int64.bits_of_float a.(i))
else Format.printf "%.16e" a.(i);
if i < l-1
then Format.printf ","
done;
Format.printf "|]@\n";
(*
***** UNITY OF REPRESENTATION *****
( UoR )
Every abstract float has one and only one representation
1. Single floats include positive zero, negative zero,
positive infinity, negative infinity, NaN value should
be represented by a singleton.
2. Any header is represented by an abstract float of size 2.
The second field of the abstract
float should have the same value as the first field. This
guarantees this abstract float has a unique representation.
*)
(*
*********************************************************************
* *
* Internal layout *
* *
*********************************************************************
*******************
* Header.t *
*******************
From left to right: bits 0 - 7
|----------------------------------- positive_zero
|
| |------------------------------- negative_zero
| |
| | |--------------------------- positive_inf
| | |
| | | |----------------------- negative_inf
| | | |
| | | |
+---+---+---+---+---+---+---+---+
| h | h | h | h | h | h | h | h |
+---+---+---+---+---+---+---+---+
| | | |
| | | |
| | | |------- at_least_one_NaN
| | |
| | |----------- all_NaN (both quiet and signalling)
| |
| |--------------- negative_normalish
|
|------------------- positive_normalish
Notes:
1. three possibilities of NaN are encoded:
1) no NaN is present
2) at least one NaN is present
3) both NaNs are present
*********************************************************************
*************************
* abstract_float.(0) *
*************************
NaN sign bit (1 bit)
|
| Unused (3 bits)
| / \
| s | 0 | 0 | 0 | h | h | h | … | h | h | p | p | p | … | p |
| \ / \ /
| \ / \ (52 bits) /
| Header.t (8 bits) \ /
| \ /
+----------------------------------------- NaN payload
(optional)
Notes:
1. the NaN payload is a NaN's significand and sign bit. This is
required only when [at_least_one_NaN] flag is set
and [all_NaNs] is unset in [Header.t]
*)
module Header : sig
type t
(** abstract type for header *)
type nan_result =
| One_NaN of Int64.t (** abstract float has one NaN value in payload *)
| All_NaN (** abstract float contains all possible NaN values *)
| No_NaN (** abstract float contains no NaN value *)
type flag
(** abstract flag indicating property of abstract float *)
val at_least_one_NaN : flag
(** [at_least_one_NaN] indicates at least one of NaN value
is present.
When this flag is on, payload should be set *)
val all_NaNs : flag
val negative_normalish : flag
(** [negative_normalish] indicates some negative normalish
values are present *)
val positive_normalish : flag
(** [positive_normalish] indicates some positive normalish
positive normalish range *)
val negative_inf : flag
(** [negative_inf] indicates -inf is present *)
val positive_inf : flag
val negative_zero : flag
val positive_zero : flag
val equal : flag -> flag -> bool
val bottom : t
val top : t
val is_bottom : t -> bool
val is_top : t -> bool
val pretty: Format.formatter -> abstract_float -> unit
(** [pretty fmt a] pretty-prints the header of [a] on [fmt] *)
val combine : t -> t -> t
(** [combine t1 t2] is the join of [t1] and [t2] *)
val narrow : t -> t -> t
val test : t -> flag -> bool
(** [test t f] is [true] if [f] is set in [t] *)
val has_inf_zero_or_NaN : abstract_float -> bool
(** [has_inf_zero_or_NaN a] is [true] if [a] contains
some infinity, some zero, or a NaN value *)
val has_normalish : t -> bool
(** [has_normalish h] is [true] if [h] contains normalish values *)
val has_zeros : t -> bool
val has_infs : t -> bool
val both_have_NaNs : t -> t -> bool
val set_flag : t -> flag -> t
(** [set t f] is [t] with flag [f] set *)
val unset_flag : t -> flag -> t
val flag_of_float : float -> flag
(** [flag_of_float f] is flag that would be set to indicate the presence
of [f]. [flag_of_float nan] is [at_least_one_NaN] *)
val of_flag : flag -> t
(** [of_flag f] is a header with flag [t] set *)
val of_flags : flag list -> t
val set_all_NaNs : t -> t
val set_all_zeros : t -> t
val exactly_one_NaN : t -> bool
(** [exactly_one_NaN t f] is [true] if [f] contains at least one NaN *)
val is_exactly : t -> flag -> bool
(** [is_exactly h f] is [true] if [t] has only [f] on *)
val size : t -> int
(** [size h] is the length of abstract float corresponding to the given
header. Note that the header alone is not always sufficient information
to decide that the representation should be a single float.
Hence this function always returns at least 2. *)
val of_abstract_float : abstract_float -> t
(** [of_abstract_float a] is the header of the abstract float [a].
Note: the abstract float [a] has to have size >= 2. In other words,
[a] cannot be a singleton floating point number *)
val allocate_abstract_float_with_NaN : t -> nan_result -> abstract_float
(** [allocate_abstract_float h nr] allocates an abstract float of size
indicated by [h], with payload set according to [nr], and
normalish fields, if any, uninitialized. *)
val allocate_abstract_float : t -> abstract_float
(** [allocate_abstract_float h] allocates an abstract float of
size indicated by [h], of which the normalish fields, if any,
are uninitialized. *)
val reconstruct_NaN : abstract_float -> nan_result
(** [reconstruct_NaN a] is the NaN representation in [a], if there is one. *)
val check: abstract_float -> bool
(** [assert (check a);] stops execution if a is ill-formed. *)
val is_header_included : abstract_float -> abstract_float -> bool
(** [is_header_included a1 a2] is true if the values indicated by [a1]'s
header are present in [a2]'s header. *)
val neg: t -> t
val sqrt: t -> t
val add: t -> t -> t
val sub: t -> t -> t
val mult: t -> t -> t
val div: t -> t -> t
val fmod: t -> t -> t
val meet : t -> t -> t
val reverse_add : t -> t -> t
val reverse_mult : t -> t -> t
val reverse_div : t -> t -> t
val reverse_div2 : t -> t -> t
end = struct
type t = int
type flag = int
type nan_result =
| One_NaN of int64
| All_NaN
| No_NaN
let at_least_one_NaN = 1
let all_NaNs = 2
let negative_normalish = 4
let positive_normalish = 8
let negative_inf = 16
let positive_inf = 32
let negative_zero = 64
let positive_zero = 128
let equal (flg1:int) (flg2:int) = flg1 = flg2
let bottom = 0
let top = 255
let is_bottom x = x = 0
let is_top x = x = 255
let combine h1 h2 = h1 lor h2
let narrow h1 h2 = h1 land h2
let cancel_flags h1 h2 = h1 land h2
let test h flag = h land flag <> 0
let test_both h1 h2 flag = h1 land h2 land flag <> 0
let set_flag = combine
let unset_flag h1 h2 = h1 land (lnot h2)
let of_flag f = f
let of_flags fs = List.fold_left ( + ) 0 fs
let flag_of_float f =
match classify_float f with
| FP_zero -> if is_pos_zero f then positive_zero else negative_zero
| FP_normal | FP_subnormal ->
if is_pos f then positive_normalish else negative_normalish
| FP_infinite ->
if is_pos f then positive_inf else negative_inf
| _ -> at_least_one_NaN
let set_all_NaNs h = h lor (at_least_one_NaN + all_NaNs)
let set_all_zeros h = h lor (negative_zero + positive_zero)
let get_NaN_part h = h land (at_least_one_NaN + all_NaNs)
let exactly_one_NaN h = (get_NaN_part h) = at_least_one_NaN
let both_have_NaNs h1 h2 = (h1 land h2 land at_least_one_NaN) <> 0
let of_abstract_float a =
assert (Array.length a >= 2);
let l = Int64.shift_right_logical (Int64.bits_of_float a.(0)) 52 in
(Int64.to_int l) land 255
let has_inf_zero_or_NaN a =
assert (Array.length a >= 2);
let exceptional_flags =
positive_inf + negative_inf + positive_zero + negative_zero +
at_least_one_NaN
in
(of_abstract_float a) land exceptional_flags <> 0
let naN_of_abstract_float a =
Int64.(logor 0x7ff0000000000000L (bits_of_float a.(0)))
let pretty fmt a =
let h = of_abstract_float a in
Format.fprintf fmt "{";
let started = ref false in
let comma fmt =
if !started then Format.fprintf fmt ",";
started := true;
in
let add fmt sign symb =
comma fmt;
Format.fprintf fmt sign;
Format.fprintf fmt symb
in
let print_sign i symb =
match i land 3 with
| 0 -> ()
| 1 -> add fmt "-" symb
| 2 -> add fmt "+" symb
| 3 -> add fmt "±" symb
| _ -> assert false
in
print_sign (h lsr 6) "0";
print_sign (h lsr 4) "∞";
if get_NaN_part h <> 0
then begin
comma fmt;
Format.fprintf fmt "NaN";
if not (test h all_NaNs)
then Format.fprintf fmt ":%016Lx" (naN_of_abstract_float a)
end;
Format.fprintf fmt "}"
let is_exactly h flag = h = flag
let normalish_mask = negative_normalish + positive_normalish
let has_normalish h = (h land normalish_mask) <> 0
let zeros_mask = positive_zero + negative_zero
let has_zeros h = (h land zeros_mask) <> 0
let has_infs h = (h land (positive_inf + negative_inf)) <> 0
let size h =
let posneg = h land normalish_mask in
if posneg = normalish_mask then 5
else if posneg <> 0 then 3
else 2
let allocate_abstract_float_with_NaN h nr =
match nr with
| No_NaN ->
assert (get_NaN_part h = 0);
Array.make (size h) (Int64.float_of_bits (Int64.of_int (h lsl 52)))
| One_NaN n ->
assert (exactly_one_NaN h);
let f = Int64.(float_of_bits
(logor (of_int (h lsl 52)) (to_payload n))) in
Array.make (size h) f
| All_NaN ->
assert (get_NaN_part h <> 0 && not (exactly_one_NaN h));
Array.make (size h) (Int64.float_of_bits (Int64.of_int (h lsl 52)))
let allocate_abstract_float h =
assert (test h all_NaNs || (not (test h at_least_one_NaN)));
Array.make (size h) (Int64.float_of_bits (Int64.of_int (h lsl 52)))
(** [reconstruct_NaN a] returns the bits of the single NaN value
optionally contained in [a] *)
let reconstruct_NaN a =
assert (Array.length a >= 2);
let h = of_abstract_float a in
if get_NaN_part h <> 0 then begin
if exactly_one_NaN h then One_NaN (naN_of_abstract_float a)
else All_NaN
end else No_NaN
let is_header_included a1 a2 =
assert (Array.length a1 >= 2);
assert (Array.length a2 >= 2);
let b1 = Int64.bits_of_float a1.(0) in
let b2 = Int64.bits_of_float a2.(0) in
let i1 = Int64.to_int b1 in
let i2 = Int64.to_int b2 in
i2 lor (i1 land 0x0FF0000000000000) = i2
&&
(* Still, if a1 and a2 contain one NaN each, we need to check for
exact correspondance of the payloads *)
(i1 land 0x0030000000000000 <> 0x0010000000000000 ||
i2 land 0x0030000000000000 <> 0x0010000000000000 ||
Int64.(logor b1 0x0FF0000000000000L = logor b2 0x0FF0000000000000L))
(* All the invariants that a float array should satisfy in order to be
a well-formed abstract_float are expressed in function [check] *)
exception Err
let check a =
let l = Array.length a in
let result =
try
match l with
| 1 -> true
| 2 | 3 | 5 ->
let h = of_abstract_float a in
let a0 = Int64.bits_of_float a.(0) in
let n = get_NaN_part h in
if (n <> at_least_one_NaN) && (to_payload a0) <> 0L
then raise Err;
if n <> at_least_one_NaN && n <> 0 &&
n <> at_least_one_NaN + all_NaNs
then raise Err;
if l <> size h then raise Err;
if l = 2
then begin
if Int64.bits_of_float a.(1) <> a0 then raise Err;
if h <> 0 && (h land (pred h) = 0) then raise Err;
true
end
else begin
if l = 3 && (-. a.(1)) = a.(2) &&
(h land (negative_inf + positive_inf +
negative_zero + positive_zero + at_least_one_NaN)) = 0
then raise Err;
if (l = 5 || (l = 3 && test h negative_normalish)) &&
not (a.(1) < infinity && -. a.(1) <= a.(2) && a.(2) < 0.)
then raise Err;
if (l = 5 || (l = 3 && test h positive_normalish)) &&
not (a.(l-2) < 0. && -. a.(l-2) <= a.(l-1) && a.(l-1) < infinity)
then raise Err;
true
end;
| _ -> false
with Err -> false
in
if not result
then begin
Format.printf "Problem with abstract float representation@ [|";
for i = 0 to l-1 do
if i = 0 || l = 2
then Format.printf "0x%016Lx" (Int64.bits_of_float a.(i))
else Format.printf "%.16e" a.(i);
if i < l-1
then Format.printf ","
done;
Format.printf "|]@\n";
end;
result
(* sqrt(-0.) = -0., sqrt(+0.) = +0., sqrt(+inf) = +inf *)
let sqrt h =
let nn = h land (negative_normalish + negative_inf + at_least_one_NaN) in
let h = h lxor nn in (* clear negative_normalish and negative_inf if
present *)
let add_NaN = -nn lsr (Sys.word_size - 1 - 2) in
(* if nn is nonzero, add_NaN is 3.
if nn is zero, add_NaN is 0. *)
h lor add_NaN
let neg h =
let neg = h land (negative_zero + negative_inf + negative_normalish) in
let pos = h land (positive_zero + positive_inf + positive_normalish) in
(get_NaN_part h) lor (neg lsl 1) lor (pos lsr 1)
let add h1 h2 =
let h1negintopos = h1 lsl 1 in
let h2negintopos = h2 lsl 1 in
let pos_zero =
(* +0.0 is present if +0.0 is present in one operand and any zero
in the other. All computations in the positive_zero bit. HFP 3.1.4 *)
let has_any_zero1 = h1 lor h1negintopos in
let has_any_zero2 = h2 lor h2negintopos in
((h1 land has_any_zero2) lor (h2 land has_any_zero1)) land positive_zero
in
let neg_zero =
(* -0.0 is present in result if -0.0 is present
in both operands. HFP 3.1.4 *)
h1 land h2 land negative_zero
in
let nan =
(* NaN is present for +inf on one side and -inf on the other.
Compute in the positive_inf bit. *)
(h1 land h2negintopos) lor (h2 land h1negintopos)
in
(* Move to the at_least_one_NaN (1) bit: *)
let nan = nan lsr 5 in
(* Any NaN as operand? *)
let nan = (nan lor h1 lor h2) land at_least_one_NaN in
let nan = (- nan) land 3 in
(* Compute both infinities in parallel.
An infinity can arise from that infinity in one operand and
any finite value or the same infinity in the other.*)
let transfers_inf =
negative_zero + positive_zero + negative_normalish + positive_normalish
in
(* Finite values transfer all infinities, but if finite values are
absent, h1 can only contribute to create the infinities it has. *)
let h1_transfer = if h1 land transfers_inf = 0 then h1 else -1 in
let h2_transfer = if h2 land transfers_inf = 0 then h2 else -1 in
let infinities = (h1 land h2_transfer) lor (h2 land h1_transfer) in
let infinities = infinities land (negative_inf lor positive_inf) in
pos_zero lor neg_zero lor nan lor infinities
let sub h1 h2 = add h1 (neg h2)
(* only to implement div from mult *)
let inv h =
let stay =
at_least_one_NaN + all_NaNs +
negative_normalish + positive_normalish
in
let stay = h land stay in
let new_infs = (h lsr 2) land (positive_inf + negative_inf) in
let new_zeroes = (h land (positive_inf + negative_inf)) lsl 2 in
stay lor new_infs lor new_zeroes
let mult h1 h2 =
(* has_finite indicates the presente of finite negative values in
negative_zero, of finite positive values in positive_zero *)
let has_finite1 = (h1 lsl 4) lor h1 in
let has_finite2 = (h2 lsl 4) lor h2 in
let same_signs12 = has_finite1 land h2 in
let same_signs21 = has_finite2 land h1 in
(* Compute in positive_zero whether two positive factors can result in
+0, and in negative_zero whether two negative factors can: *)
let same_signs = same_signs12 lor same_signs21 in
(* Put the two possibilities together in positive_zero: *)
let pos_zero = same_signs lor (same_signs lsl 1) in
let pos_zero = pos_zero land positive_zero in
(* Compute in negative_zero bit: *)
let finite_pos2_neg_zero_1 = (has_finite2 lsr 1) land h1 in
let finite_pos1_neg_zero_2 = (has_finite1 lsr 1) land h2 in
let h2sr = h2 lsr 1 in
let finite_neg1_pos_zero_2 = has_finite1 land h2sr in
let h1sr = h1 lsr 1 in
let finite_neg2_pos_zero_1 = has_finite2 land h1sr in
let opposite_signs =
finite_pos2_neg_zero_1 lor finite_pos1_neg_zero_2 lor
finite_neg1_pos_zero_2 lor finite_neg2_pos_zero_1
in
let neg_zero = opposite_signs land negative_zero in
let zeroes = pos_zero lor neg_zero in
(* compute in the infinities bits: *)
let has_nonzero1 = (h1 lsl 2) lor h1 in
let has_nonzero2 = (h2 lsl 2) lor h2 in
(* compute in the negative_inf bit: *)
let neg_nonzero_1_pos_inf_2 = has_nonzero1 land h2sr in
let neg_nonzero_2_pos_inf_1 = has_nonzero2 land h1sr in
let pos_nonzero_1_neg_inf_2 = (has_nonzero1 lsr 1) land h2 in
let pos_nonzero_2_neg_inf_1 = (has_nonzero2 lsr 1) land h1 in
let neg_inf =
pos_nonzero_2_neg_inf_1 lor neg_nonzero_2_pos_inf_1 lor
pos_nonzero_1_neg_inf_2 lor neg_nonzero_1_pos_inf_2
in
let neg_inf = neg_inf land negative_inf in
(* +inf is obtained by multiplying nonzero and inf of the same sign: *)
let pos_inf12 = has_nonzero1 land h2 in
let pos_inf21 = has_nonzero2 land h1 in
let pos_inf = pos_inf12 lor pos_inf21 in
let pos_inf = pos_inf lor (pos_inf lsl 1) in
let pos_inf = pos_inf land positive_inf in
let infinities = neg_inf lor pos_inf in
(* Compute in negative_zero and negative_inf bits: *)
let merge_posneg1 = h1sr lor h1 in
let merge_posneg2 = h2sr lor h2 in
let nan12 = (merge_posneg1 lsl 2) land merge_posneg2 in
let nan21 = (merge_posneg2 lsl 2) land merge_posneg1 in
let nan_zero_times_inf = (nan12 lor nan21) land negative_zero in
(* are there NaNs in operands? *)
let nan_as_op = (h1 lor h2) land at_least_one_NaN in
let nan = nan_zero_times_inf lor nan_as_op in
(* Map nonzero to 3 and 0 to 0: *)
let nan = (- nan) lsr (Sys.word_size - 1 - 2) in
infinities lor zeroes lor nan
let div h1 h2 = mult h1 (inv h2)
let fmod h1 h2 =
let h = bottom in
let y_has_pn =
test h2 (positive_normalish + negative_normalish
+ positive_inf + negative_inf) in
let h =
let x_zeros = h1 land (positive_zero + negative_zero) in
if y_has_pn then
h lor x_zeros else h in
if (test h1 (positive_inf + negative_inf) &&
test h2 (positive_zero + negative_zero)) ||
test h1 at_least_one_NaN || test h2 at_least_one_NaN || has_infs h1 ||
(h1 <> 0 && (h2 land (positive_zero + negative_zero) <> 0)) then
set_all_NaNs h else h
let meet h1 h2 =
h1 land h2 land
(lnot (at_least_one_NaN + all_NaNs +
positive_normalish + negative_normalish))
let reverse_add h1 h2 =
if both_have_NaNs h1 h2 then
positive_zero + negative_zero + positive_inf + negative_inf
else begin
let h = bottom in
let h =
if get_NaN_part h2 <> 0 then begin
let h = if test h1 negative_inf
then set_flag h positive_inf else h in
let h = if test h1 positive_inf
then set_flag h negative_inf else h in
h end else h in
let h =
let p =
positive_inf + positive_zero + negative_zero +
positive_normalish + negative_normalish in
if (h1 land p <> 0) && test h2 positive_inf then
let h = set_flag h positive_inf in
if test h1 positive_inf then begin
let h = set_flag h positive_zero in
set_flag h negative_zero
end
else h else h in
let h =
let p =
negative_inf + positive_zero + negative_zero +
positive_normalish + negative_normalish in
if h1 land p <> 0 && test h2 negative_inf then
let h = set_flag h negative_inf in
if test h1 negative_inf then
let h = set_flag h positive_zero in
set_flag h negative_zero
else h else h in
let h =
if test h2 positive_zero && test h1 positive_zero then
set_flag (set_flag h positive_zero) negative_zero
else if test h2 positive_zero && test h1 negative_zero then
set_flag h positive_zero
else
h in
let h =
if test h2 negative_zero && test h1 negative_zero then
set_flag h negative_zero else h in
h
end
let reverse_mult h1 h2 =
if both_have_NaNs h1 h2 then
positive_zero + negative_zero + positive_inf + negative_inf
else begin
let h = bottom in
let h =
if test h2 positive_inf then
let h =
if test h1 positive_normalish || test h1 positive_inf then
set_flag h positive_inf else h in
if test h1 negative_normalish || test h1 negative_inf then
set_flag h negative_inf else h
else h in
let h =
if test h2 negative_inf then
let h =
if test h1 negative_normalish || test h1 negative_inf then
set_flag h positive_inf else h in
if test h1 positive_normalish || test h1 positive_inf then
set_flag h negative_inf else h
else h in
let h =
if test h2 positive_zero then
let h =
if test h1 positive_normalish || test h1 positive_zero then
set_flag h positive_zero else h in
if test h1 negative_normalish || test h1 negative_zero then
set_flag h negative_zero else h
else h in
let h =
if test h2 negative_zero then
let h =
if test h1 positive_normalish || test h1 positive_zero then
set_flag h negative_zero else h in
if test h1 negative_normalish || test h1 negative_zero then
set_flag h positive_zero else h
else h in
let h =
if get_NaN_part h2 <> 0 then
let h =
if test h1 positive_inf || test h1 negative_inf then
set_flag h (negative_zero + positive_zero) else h in
let h =
if test h1 positive_zero || test h1 negative_zero then
set_flag h (negative_inf + positive_inf) else h in
if get_NaN_part h1 <> 0 then
set_flag h (positive_zero + negative_zero +
positive_inf + negative_inf) else h
else h in
h
end
let reverse_div h1 h2 =
if both_have_NaNs h1 h2 then
positive_zero + negative_zero + positive_inf + negative_inf
else begin
let h = bottom in
let h =
if (((test h1 positive_zero || test h1 positive_normalish)
&& test h2 positive_inf) ||
((test h1 negative_zero || test h1 negative_normalish)
&& test h2 negative_inf)) then
set_flag h positive_inf else h in
let h =
if (((test h1 positive_zero || test h1 positive_normalish)
&& test h2 negative_inf) ||
((test h1 negative_zero || test h1 negative_normalish)
&& test h2 positive_inf)) then
set_flag h negative_inf else h in
let h =
if (((test h1 positive_inf || test h1 positive_normalish)
&& test h2 positive_zero) ||
((test h1 negative_inf || test h1 negative_normalish)
&& test h2 negative_zero)) then
set_flag h positive_zero else h in
let h =
if (((test h1 positive_inf || test h1 positive_normalish)
&& test h2 negative_zero) ||
((test h1 negative_inf || test h1 negative_normalish)
&& test h2 positive_zero)) then
set_flag h negative_zero else h in
let h =
if ((test h1 positive_inf && test h1 negative_inf) &&
(get_NaN_part h2 <> 0)) then
set_flag h (positive_inf + negative_inf) else h in
let h =
if ((test h1 positive_zero || test h1 positive_zero) &&
(get_NaN_part h2 <> 0)) then
set_all_zeros h else h in
h
end
let reverse_div2 h1 h2 =
if both_have_NaNs h1 h2 then
positive_zero + negative_zero + positive_inf + negative_inf
else begin
let h = bottom in
let h =
if (((test h1 positive_normalish || test h1 positive_zero)
&& (test h2 positive_zero)) ||
((test h1 negative_normalish || test h1 negative_zero)
&& (test h2 negative_zero))) then
set_flag h positive_inf else h in
let h =
if (((test h1 positive_normalish || test h1 positive_zero)
&& (test h2 negative_zero)) ||
((test h1 negative_normalish || test h1 negative_zero)
&& (test h2 positive_zero))) then
set_flag h negative_inf else h in
let h =
if (((test h1 positive_normalish || test h1 positive_inf)
&& (test h2 positive_inf)) ||
((test h1 negative_normalish || test h1 negative_inf)
&& (test h2 negative_inf))) then
set_flag h positive_zero else h in
let h =
if (((test h1 positive_normalish || test h1 positive_inf)
&& (test h2 negative_inf)) ||
((test h1 negative_normalish || test h1 negative_inf)
&& (test h2 positive_inf))) then
set_flag h negative_zero else h in
let h =
if ((test h1 positive_inf && test h1 negative_inf) &&
(get_NaN_part h2 <> 0)) then
set_flag h (positive_inf + negative_inf) else h in
let h =
if ((test h1 positive_zero || test h1 positive_zero) &&
(get_NaN_part h2 <> 0)) then
set_all_zeros h else h in
h
end
end
(*
If negative_normalish, the negative bounds are always at t.(1) and t.(2)
If positive_normalish, the positive bounds are always at:
let l = Array.length t in t.(l-2) and t.(l-1)
Each pair of bounds of a same sign is represented
as -lower_bound, upper_bound.
*)
(** [copy_bounds a1 a2] will copy bounds of [a1] to the freshly
allocated [a2]. This does not break UoR *)
let copy_bounds a1 a2 =
assert (Array.length a1 >= 2);
assert (Array.length a2 >= 2);
match Array.length a1, Array.length a2 with
| 2, _ -> ()
| 3, ((3 | 5) as l) -> begin
if Header.(test (of_abstract_float a1) positive_normalish) then
(a2.(l - 2) <- a1.(1); a2.(l - 1) <- a1.(2))
else
(a2.(1) <- a1.(1); a2.(2) <- a1.(2))
end
| 5, 5 ->
for i = 1 to 4 do
a2.(i) <- a1.(i)
done
| _ -> assert false
(** [set_opp_neg_lower a f] sets lower bound of negative normalish to [-. f] *)
let set_opp_neg_lower a f =
assert(0.0 < f);
assert(f < infinity);
a.(1) <- f
(** [set_neg_lower a f] sets lower bound of positive normalish to [f] *)
let set_neg_lower a f =
set_opp_neg_lower a (-. f)
(** [set_neg_upper a f] sets upper bound of negative normalish to [f] *)
let set_neg_upper a f =
assert(neg_infinity < f);
assert(f < 0.0);
a.(2) <- f
(** [set_neg a l u] sets bounds of negative normalish to [l] and [u] *)
let set_neg a l u =
assert(neg_infinity < l);
assert(l <= u);
assert(u < 0.0);
a.(1) <- -. l;
a.(2) <- u
(** [set_opp_pos_lower a f] sets lower bound of positive normalish to [-. f] *)
let set_opp_pos_lower a f =
assert(neg_infinity < f);
assert(f < 0.0);
a.(Array.length a - 2) <- f
(** [set_pos_lower a f] sets lower bound of positive normalish to [f] *)
let set_pos_lower a f =
set_opp_pos_lower a (-. f)
(** [set_pos_upper a f] sets upper bound of positive normalish to [f] *)
let set_pos_upper a f =
assert(0.0 < f);
assert(f < infinity);
a.(Array.length a - 1) <- f
(** [set_pos a l u] sets bound of positive normalish to [l] and [u] *)
let set_pos a l u =
assert(0.0 < l);
assert(l <= u);
assert(u < infinity);
let le = Array.length a in
a.(le - 2) <- -. l;
a.(le - 1) <- u
(* [get_opp_neg_lower a] is the lower neg bonud of [a], in negative *)
let get_opp_neg_lower a : float = a.(1)
(* [get_neg_upper a] is the upper neg bonud of [a] *)
let get_neg_upper a : float = a.(2)
(* [get_opp_pos_lower a] is the upper neg bonud of [a], in negative *)
let get_opp_pos_lower a : float = a.(Array.length a - 2)
(* [get_pos_upper a] is the upper pos bonud of [a] *)
let get_pos_upper a : float = a.(Array.length a - 1)
(* [get_finite_upper a] returns the highest finite value contained in [a],
or [neg_infinity] if [a] contains no finite values *)
let get_finite_upper a =
let h = Header.of_abstract_float a in
if Header.(test h positive_normalish)
then get_pos_upper a
else if Header.(test h positive_zero)
then 0.0
else if Header.(test h negative_zero)
then (-0.0)
else if Header.(test h negative_normalish)
then get_neg_upper a
else neg_infinity
(* [get_opp_finite_lower a] returns the opposite of the lowest finite value
contained in [a], or [neg_infinity] if [a] contains no finite values *)
let get_opp_finite_lower a =
let h = Header.of_abstract_float a in
if Header.(test h negative_normalish)
then get_opp_neg_lower a
else if Header.(test h negative_zero)
then 0.0
else if Header.(test h positive_zero)
then (-0.0)
else if Header.(test h positive_normalish)
then get_opp_pos_lower a
else neg_infinity
(* [set_same_bound a f] sets pos or neg bound of [a] to [f].
[a] is expected to have size 3 (one header and one pair of bound *)