Skip to content

Commit 94bb919

Browse files
committed
Better parsing of floating points and to_fp of float32
1 parent 1020d51 commit 94bb919

File tree

1 file changed

+22
-27
lines changed

1 file changed

+22
-27
lines changed

src/smtml/smtlib.ml

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ module Term = struct
110110
Bytes.to_string bs
111111
in
112112
let bv = set b 0 '0' in
113-
Expr.value (Str bv)
113+
let len = String.length bv in
114+
let int = Z.of_string bv in
115+
Expr.value (Bitv (Bitvector.make int (len - 2)))
114116

115117
let colon ?loc (symbol : t) (term : t) : t =
116118
match Expr.view symbol with
@@ -215,32 +217,16 @@ module Term = struct
215217
| "bvsge", [ a; b ] -> Expr.raw_relop Ty_none Ge a b
216218
| "bvuge", [ a; b ] -> Expr.raw_relop Ty_none GeU a b
217219
| "concat", [ a; b ] -> Expr.raw_concat a b
218-
| "fp", [ s; eb; i ] -> (
219-
match (Expr.view s, Expr.view eb, Expr.view i) with
220-
| Val (Str sign), Val (Str eb), Val (Str i) -> begin
221-
match (String.length sign, String.length eb, String.length i) with
222-
(* 32 bit float -> sign = 1, eb = 8, i = 24 - 1 = 23 *)
223-
| 3, 10, 25 ->
224-
let sign = Int32.of_string sign in
225-
let exponent = Int32.of_string eb in
226-
let mantissa = Int32.of_string i in
227-
Expr.value (Num (F32 (combine_to_int32 sign exponent mantissa)))
228-
(* 64 bit float -> sign = 1, eb = 11, i = 53 - 1 = 52 *)
229-
| 3, 13, 54 -> Expr.value (Num (F64 (combine_to_int64 sign eb i)))
230-
| _ -> Fmt.failwith "%afp size not supported" pp_loc loc
231-
end
232-
| Val (Str sign), Val (Bitv eb), Val (Str i) -> begin
233-
match (String.length sign, Bitvector.numbits eb, String.length i) with
234-
| 3, 8, 25 ->
235-
let sign = Int32.of_string sign in
236-
let exponent = Bitvector.to_int32 eb in
237-
let mantissa = Int32.of_string i in
238-
Expr.value (Num (F32 (combine_to_int32 sign exponent mantissa)))
239-
| _ -> Fmt.failwith "%afp size not supported" pp_loc loc
240-
end
241-
| _ ->
242-
Fmt.failwith "%acould not parse fp: %a %a %a" pp_loc loc Expr.pp s
243-
Expr.pp eb Expr.pp i )
220+
| ( "fp"
221+
, [ { node = Val (Bitv sign); _ }
222+
; { node = Val (Bitv eb); _ }
223+
; { node = Val (Bitv i); _ }
224+
] ) ->
225+
let fp = Bitvector.(concat sign (concat eb i)) in
226+
let fp_sz = Bitvector.numbits fp in
227+
if fp_sz = 32 then Expr.value (Num (F32 (Bitvector.to_int32 fp)))
228+
else if fp_sz = 64 then Expr.value (Num (F64 (Bitvector.to_int64 fp)))
229+
else Fmt.failwith "%afp size not supported" pp_loc loc
244230
| "fp.abs", [ a ] -> Expr.raw_unop Ty_none Abs a
245231
| "fp.neg", [ a ] -> Expr.raw_unop Ty_none Neg a
246232
| "fp.add", [ rm; a; b ] -> make_fp_binop symbol Add rm a b
@@ -307,6 +293,15 @@ module Term = struct
307293
match int_of_string_opt i2 with None -> assert false | Some i2 -> i2
308294
in
309295
Expr.raw_unop Ty_regexp (Regexp_loop (i1, i2)) a
296+
| ( "to_fp"
297+
, [ "11"; "53" ]
298+
, [ { node =
299+
Symbol { name = Simple ("roundNearestTiesToEven" | "RNE"); _ }
300+
; _
301+
}
302+
; a
303+
] ) ->
304+
Expr.raw_cvtop (Ty_fp 64) PromoteF32 a
310305
| _ ->
311306
Fmt.failwith "%acould not parse indexed app: %a" pp_loc loc Expr.pp id )
312307
| Symbol id ->

0 commit comments

Comments
 (0)