Skip to content

Commit

Permalink
Merge pull request #87 from WebAssembly/convertion-tests
Browse files Browse the repository at this point in the history
Conversion tests
  • Loading branch information
sunfishcode committed Sep 29, 2015
2 parents ae419ca + 1d3ebbb commit 1c9d69b
Show file tree
Hide file tree
Showing 9 changed files with 394 additions and 390 deletions.
2 changes: 1 addition & 1 deletion ml-proto/src/host/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ let space = [' ''\t']
let digit = ['0'-'9']
let hexdigit = ['0'-'9''a'-'f''A'-'F']
let letter = ['a'-'z''A'-'Z']
let symbol = ['+''-''*''/''\\''^''~''=''<''>''!''?''@''#''$''%''&''|'':''`']
let symbol = ['+''-''*''/''\\''^''~''=''<''>''!''?''@''#''$''%''&''|'':''`''.']
let tick = '\''
let escape = ['n''t''\\''\'''\"']
let character = [^'"''\\''\n'] | '\\'escape | '\\'hexdigit hexdigit
Expand Down
10 changes: 10 additions & 0 deletions ml-proto/src/spec/f32_convert.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ let demote_f64 x =
let convert_s_i32 x =
make_nan_nondeterministic (F32.of_float (Int32.to_float x))

(*
* Similar to convert_u_i64 below, the high half of the i32 range are beyond
* the range where f32 can represent odd numbers.
*)
let convert_u_i32 x =
make_nan_nondeterministic
(F32.of_float (if x >= Int32.zero then
Expand All @@ -18,6 +22,12 @@ let convert_u_i32 x =
let convert_s_i64 x =
make_nan_nondeterministic (F32.of_float (Int64.to_float x))

(*
* Values in the low half of the int64 range can be converted with a signed
* conversion. The high half is beyond the range where f32 can represent odd
* numbers, so we can shift the value right, do a conversion, and then scale it
* back up, without worrying about losing the least-significant digit.
*)
let convert_u_i64 x =
make_nan_nondeterministic
(F32.of_float (if x >= Int64.zero then
Expand Down
17 changes: 13 additions & 4 deletions ml-proto/src/spec/f64_convert.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,25 @@ let promote_f32 x =
let convert_s_i32 x =
make_nan_nondeterministic (F64.of_float (Int32.to_float x))

(*
* Unlike the other convert_u functions, the high half of the i32 range is
* within the range where f32 can represent odd numbers, so we can't do the
* shift. Instead, we can use int64 signed arithmetic.
*)
let convert_u_i32 x =
make_nan_nondeterministic
(F64.of_float (if x >= Int32.zero then
Int32.to_float x
else
Int32.to_float (Int32.shift_right_logical x 1) *. 2.))
(F64.of_float
(Int64.to_float (Int64.logand (Int64.of_int32 x) 0x00000000ffffffffL)))

let convert_s_i64 x =
make_nan_nondeterministic (F64.of_float (Int64.to_float x))

(*
* Values in the low half of the int64 range can be converted with a signed
* conversion. The high half is beyond the range where f64 can represent odd
* numbers, so we can shift the value right, do a conversion, and then scale it
* back up, without worrying about losing the least-significant digit.
*)
let convert_u_i64 x =
make_nan_nondeterministic
(F64.of_float (if x >= Int64.zero then
Expand Down
16 changes: 8 additions & 8 deletions ml-proto/src/spec/i32_convert.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ let trunc_s_f32 x =
if F32.ne x x then
raise Numerics.InvalidConversionToInteger
else let xf = F32.to_float x in
if xf >= (Int32.to_float Int32.max_int) +. 1. ||
xf <= (Int32.to_float Int32.min_int) -. 1. then
if xf >= -.(Int32.to_float Int32.min_int) ||
xf < (Int32.to_float Int32.min_int) then
raise Numerics.IntegerOverflow
else
Int32.of_float xf
Expand All @@ -16,8 +16,8 @@ let trunc_u_f32 x =
if F32.ne x x then
raise Numerics.InvalidConversionToInteger
else let xf = F32.to_float x in
if xf >= (Int32.to_float Int32.max_int) *. 2. +. 2. ||
xf <= -1. then
if xf >= -.(Int32.to_float Int32.min_int) *. 2. ||
xf <= -1. then
raise Numerics.IntegerOverflow
else
Int64.to_int32 (Int64.of_float xf)
Expand All @@ -26,8 +26,8 @@ let trunc_s_f64 x =
if F64.ne x x then
raise Numerics.InvalidConversionToInteger
else let xf = F64.to_float x in
if xf >= (Int32.to_float Int32.max_int) +. 1. ||
xf <= (Int32.to_float Int32.min_int) -. 1. then
if xf >= -.(Int32.to_float Int32.min_int) ||
xf < (Int32.to_float Int32.min_int) then
raise Numerics.IntegerOverflow
else
Int32.of_float xf
Expand All @@ -36,8 +36,8 @@ let trunc_u_f64 x =
if F64.ne x x then
raise Numerics.InvalidConversionToInteger
else let xf = F64.to_float x in
if xf >= (Int32.to_float Int32.max_int) *. 2. +. 2. ||
xf <= -1. then
if xf >= -.(Int32.to_float Int32.min_int) *. 2. ||
xf <= -1. then
raise Numerics.IntegerOverflow
else
Int64.to_int32 (Int64.of_float xf)
Expand Down
20 changes: 10 additions & 10 deletions ml-proto/src/spec/i64_convert.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ let trunc_s_f32 x =
if F32.ne x x then
raise Numerics.InvalidConversionToInteger
else let xf = F32.to_float x in
if xf >= (Int64.to_float Int64.max_int) +. 1. ||
xf <= (Int64.to_float Int64.min_int) -. 1. then
if xf >= -.(Int64.to_float Int64.min_int) ||
xf < (Int64.to_float Int64.min_int) then
raise Numerics.IntegerOverflow
else
Int64.of_float xf
Expand All @@ -18,10 +18,10 @@ let trunc_u_f32 x =
if F32.ne x x then
raise Numerics.InvalidConversionToInteger
else let xf = F32.to_float x in
if xf >= (Int64.to_float Int64.max_int) *. 2. +. 2. ||
xf <= -1. then
if xf >= -.(Int64.to_float Int64.min_int) *. 2. ||
xf <= -1. then
raise Numerics.IntegerOverflow
else if xf >= (Int64.to_float Int64.max_int) +. 1. then
else if xf >= -.(Int64.to_float Int64.min_int) then
Int64.logxor (Int64.of_float (xf -. 9223372036854775808.)) Int64.min_int
else
Int64.of_float xf
Expand All @@ -30,8 +30,8 @@ let trunc_s_f64 x =
if F64.ne x x then
raise Numerics.InvalidConversionToInteger
else let xf = F64.to_float x in
if xf >= (Int64.to_float Int64.max_int) +. 1. ||
xf <= (Int64.to_float Int64.min_int) -. 1. then
if xf >= -.(Int64.to_float Int64.min_int) ||
xf < (Int64.to_float Int64.min_int) then
raise Numerics.IntegerOverflow
else
Int64.of_float xf
Expand All @@ -40,10 +40,10 @@ let trunc_u_f64 x =
if F64.ne x x then
raise Numerics.InvalidConversionToInteger
else let xf = F64.to_float x in
if xf >= (Int64.to_float Int64.max_int) *. 2. +. 2. ||
xf <= -1. then
if xf >= -.(Int64.to_float Int64.min_int) *. 2. ||
xf <= -1. then
raise Numerics.IntegerOverflow
else if xf >= (Int64.to_float Int64.max_int) +. 1. then
else if xf >= -.(Int64.to_float Int64.min_int) then
Int64.logxor (Int64.of_float (xf -. 9223372036854775808.)) Int64.min_int
else
Int64.of_float xf
Expand Down
Loading

0 comments on commit 1c9d69b

Please sign in to comment.