Skip to content

Commit c7daf17

Browse files
bschommerxavierleroy
authored andcommitted
Added semantics for fmin/fmax for RISC-V
The semantics of the RISC-V variant of fmin and fmax are slightly different to the other architectures. In case of NaN instead of returning always the second one for RISC-V we return the non-NaN parameter or canonical NaN in the case that both are NaN. Bug 30035
1 parent 6c5ca86 commit c7daf17

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

riscV/Builtins1.v

+36-4
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,47 @@ Require Import String Coqlib.
1919
Require Import AST Integers Floats Values.
2020
Require Import Builtins0.
2121

22-
Inductive platform_builtin : Type := .
22+
Inductive platform_builtin : Type :=
23+
| BI_fmin
24+
| BI_fmax.
2325

2426
Local Open Scope string_scope.
2527

2628
Definition platform_builtin_table : list (string * platform_builtin) :=
27-
nil.
29+
("__builtin_fmin", BI_fmin)
30+
:: ("__builtin_fmax", BI_fmax)
31+
:: nil.
2832

2933
Definition platform_builtin_sig (b: platform_builtin) : signature :=
30-
match b with end.
34+
match b with
35+
| BI_fmin | BI_fmax =>
36+
mksignature (Tfloat :: Tfloat :: nil) Tfloat cc_default
37+
end.
38+
39+
(* Canonical NaN as defined in the RISC-V ISA, all expontent bits one, sign bit
40+
zero and quiet bit one. *)
41+
Definition canonical_nan := Float.of_bits (Int64.repr 9221120237041090560).
42+
43+
(* NaN handling as described for fmin/fmax by the RISC-V Isa. If one of the
44+
parameters is NaN, the other one is returned. Otherwise the canonical NaN
45+
value is returned. *)
46+
Definition nan_handling (f1 f2: float) : float :=
47+
if negb (Binary.is_nan _ _ f1) then f1
48+
else if negb (Binary.is_nan _ _ f2) then f2
49+
else canonical_nan.
3150

3251
Definition platform_builtin_sem (b: platform_builtin) : builtin_sem (sig_res (platform_builtin_sig b)) :=
33-
match b with end.
52+
match b with
53+
| BI_fmin =>
54+
mkbuiltin_n2t Tfloat Tfloat Tfloat
55+
(fun f1 f2 => match Float.compare f1 f2 with
56+
| Some Eq | Some Lt => f1
57+
| Some Gt | None => nan_handling f1 f2
58+
end)
59+
| BI_fmax =>
60+
mkbuiltin_n2t Tfloat Tfloat Tfloat
61+
(fun f1 f2 => match Float.compare f1 f2 with
62+
| Some Eq | Some Gt => f1
63+
| Some Lt | None => nan_handling f1 f2
64+
end)
65+
end.

0 commit comments

Comments
 (0)