Skip to content

Implement support for rotate. #149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ml-proto/host/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ rule token = parse
| (ixx as t)".shr_u"
{ BINARY (fun (e1, e2) ->
intop t (I32_shr_u (e1, e2)) (I64_shr_u (e1, e2))) }
| (ixx as t)".rotl"
{ BINARY (fun (e1, e2) ->
intop t (I32_rotl (e1, e2)) (I64_rotl (e1, e2))) }
| (ixx as t)".rotr"
{ BINARY (fun (e1, e2) ->
intop t (I32_rotr (e1, e2)) (I64_rotr (e1, e2))) }
| (fxx as t)".add"
{ BINARY (fun (e1, e2) -> floatop t (F32_add (e1, e2)) (F64_add (e1, e2))) }
| (fxx as t)".sub"
Expand Down
4 changes: 4 additions & 0 deletions ml-proto/spec/arithmetic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ struct
| Shl -> I32.shl
| ShrU -> I32.shr_u
| ShrS -> I32.shr_s
| Rotl -> I32.rotl
| Rotr -> I32.rotr
in fun v1 v2 -> Int32 (f (i32_of_value 1 v1) (i32_of_value 2 v2))

let relop op =
Expand Down Expand Up @@ -112,6 +114,8 @@ struct
| Shl -> I64.shl
| ShrU -> I64.shr_u
| ShrS -> I64.shr_s
| Rotl -> I64.rotl
| Rotr -> I64.rotr
in fun v1 v2 -> Int64 (f (i64_of_value 1 v1) (i64_of_value 2 v2))

let relop op =
Expand Down
4 changes: 4 additions & 0 deletions ml-proto/spec/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ and expr' =
| I32_shl of expr * expr
| I32_shr_s of expr * expr
| I32_shr_u of expr * expr
| I32_rotl of expr * expr
| I32_rotr of expr * expr
| I64_add of expr * expr
| I64_sub of expr * expr
| I64_mul of expr * expr
Expand All @@ -109,6 +111,8 @@ and expr' =
| I64_shl of expr * expr
| I64_shr_s of expr * expr
| I64_shr_u of expr * expr
| I64_rotl of expr * expr
| I64_rotr of expr * expr
| F32_add of expr * expr
| F32_sub of expr * expr
| F32_mul of expr * expr
Expand Down
4 changes: 4 additions & 0 deletions ml-proto/spec/desugar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ and expr' at = function
| Ast.I32_shl (e1, e2) -> Binary (Int32 I32Op.Shl, expr e1, expr e2)
| Ast.I32_shr_s (e1, e2) -> Binary (Int32 I32Op.ShrS, expr e1, expr e2)
| Ast.I32_shr_u (e1, e2) -> Binary (Int32 I32Op.ShrU, expr e1, expr e2)
| Ast.I32_rotl (e1, e2) -> Binary (Int32 I32Op.Rotl, expr e1, expr e2)
| Ast.I32_rotr (e1, e2) -> Binary (Int32 I32Op.Rotr, expr e1, expr e2)
| Ast.I64_add (e1, e2) -> Binary (Int64 I64Op.Add, expr e1, expr e2)
| Ast.I64_sub (e1, e2) -> Binary (Int64 I64Op.Sub, expr e1, expr e2)
| Ast.I64_mul (e1, e2) -> Binary (Int64 I64Op.Mul, expr e1, expr e2)
Expand All @@ -206,6 +208,8 @@ and expr' at = function
| Ast.I64_shl (e1, e2) -> Binary (Int64 I64Op.Shl, expr e1, expr e2)
| Ast.I64_shr_s (e1, e2) -> Binary (Int64 I64Op.ShrS, expr e1, expr e2)
| Ast.I64_shr_u (e1, e2) -> Binary (Int64 I64Op.ShrU, expr e1, expr e2)
| Ast.I64_rotl (e1, e2) -> Binary (Int64 I64Op.Rotl, expr e1, expr e2)
| Ast.I64_rotr (e1, e2) -> Binary (Int64 I64Op.Rotr, expr e1, expr e2)
| Ast.F32_add (e1, e2) -> Binary (Float32 F32Op.Add, expr e1, expr e2)
| Ast.F32_sub (e1, e2) -> Binary (Float32 F32Op.Sub, expr e1, expr e2)
| Ast.F32_mul (e1, e2) -> Binary (Float32 F32Op.Mul, expr e1, expr e2)
Expand Down
16 changes: 15 additions & 1 deletion ml-proto/spec/int.ml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ sig
val shl : t -> t -> t
val shr_s : t -> t -> t
val shr_u : t -> t -> t
val rotl : t -> t -> t
val rotr : t -> t -> t
val clz : t -> t
val ctz : t -> t
val popcnt : t -> t
Expand Down Expand Up @@ -133,7 +135,7 @@ struct
let or_ = Rep.logor
let xor = Rep.logxor

(* WebAssembly's shifts mask the shift count to according to the bitwidth. *)
(* WebAssembly's shifts mask the shift count according to the bitwidth. *)
let shift f x y =
f x (Rep.to_int (Rep.logand y (Rep.of_int (Rep.bitwidth - 1))))

Expand All @@ -146,6 +148,18 @@ struct
let shr_u x y =
shift Rep.shift_right_logical x y

(* We must mask the count to implement rotates via shifts. *)
let clamp_rotate_count n =
Rep.to_int (Rep.logand n (Rep.of_int (Rep.bitwidth - 1)))

let rotl x y =
let n = clamp_rotate_count y in
or_ (Rep.shift_left x n) (Rep.shift_right_logical x (Rep.bitwidth - n))

let rotr x y =
let n = clamp_rotate_count y in
or_ (Rep.shift_right_logical x n) (Rep.shift_left x (Rep.bitwidth - n))

(* clz is defined for all values, including all-zeros. *)
let clz x =
Rep.of_int
Expand Down
2 changes: 1 addition & 1 deletion ml-proto/spec/kernel.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module IntOp () =
struct
type unop = Clz | Ctz | Popcnt
type binop = Add | Sub | Mul | DivS | DivU | RemS | RemU
| And | Or | Xor | Shl | ShrU | ShrS
| And | Or | Xor | Shl | ShrU | ShrS | Rotl | Rotr
type relop = Eq | Ne | LtS | LtU | LeS | LeU | GtS | GtU | GeS | GeU
type cvtop = ExtendSInt32 | ExtendUInt32 | WrapInt64
| TruncSFloat32 | TruncUFloat32 | TruncSFloat64 | TruncUFloat64
Expand Down
15 changes: 15 additions & 0 deletions ml-proto/test/i32.wast
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
(func $shl (param $x i32) (param $y i32) (result i32) (i32.shl (get_local $x) (get_local $y)))
(func $shr_s (param $x i32) (param $y i32) (result i32) (i32.shr_s (get_local $x) (get_local $y)))
(func $shr_u (param $x i32) (param $y i32) (result i32) (i32.shr_u (get_local $x) (get_local $y)))
(func $rotl (param $x i32) (param $y i32) (result i32) (i32.rotl (get_local $x) (get_local $y)))
(func $rotr (param $x i32) (param $y i32) (result i32) (i32.rotr (get_local $x) (get_local $y)))
(func $clz (param $x i32) (result i32) (i32.clz (get_local $x)))
(func $ctz (param $x i32) (result i32) (i32.ctz (get_local $x)))
(func $popcnt (param $x i32) (result i32) (i32.popcnt (get_local $x)))
Expand Down Expand Up @@ -41,6 +43,8 @@
(export "shl" $shl)
(export "shr_s" $shr_s)
(export "shr_u" $shr_u)
(export "rotl" $rotl)
(export "rotr" $rotr)
(export "clz" $clz)
(export "ctz" $ctz)
(export "popcnt" $popcnt)
Expand Down Expand Up @@ -232,6 +236,17 @@
(assert_return (invoke "shr_u" (i32.const -1) (i32.const 0x7fffffff)) (i32.const 1))
(assert_return (invoke "shr_u" (i32.const -1) (i32.const 0x80000000)) (i32.const -1))

(assert_return (invoke "rotl" (i32.const 0xfe00dc00) (i32.const 4)) (i32.const 0xe00dc00f))
(assert_return (invoke "rotl" (i32.const 0xabcd9876) (i32.const 1)) (i32.const 0x579b30ed))
(assert_return (invoke "rotl" (i32.const 0x00008000) (i32.const 37)) (i32.const 0x00100000))
(assert_return (invoke "rotl" (i32.const 0x769abcdf) (i32.const 0x8000000d)) (i32.const 0x579beed3))

(assert_return (invoke "rotr" (i32.const 0xb0c1d2e3) (i32.const 0x0005)) (i32.const 0x1d860e97))
(assert_return (invoke "rotr" (i32.const 0xb0c1d2e3) (i32.const 0xff05)) (i32.const 0x1d860e97))
(assert_return (invoke "rotr" (i32.const 0xff00cc00) (i32.const 1)) (i32.const 0x7f806600))
(assert_return (invoke "rotr" (i32.const 0x00080000) (i32.const 4)) (i32.const 0x00008000))
(assert_return (invoke "rotr" (i32.const 0x769abcdf) (i32.const 0xffffffed)) (i32.const 0xe6fbb4d5))

(assert_return (invoke "clz" (i32.const 0xffffffff)) (i32.const 0))
(assert_return (invoke "clz" (i32.const 0)) (i32.const 32))
(assert_return (invoke "clz" (i32.const 0x00008000)) (i32.const 16))
Expand Down
17 changes: 17 additions & 0 deletions ml-proto/test/i64.wast
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
(func $shl (param $x i64) (param $y i64) (result i64) (i64.shl (get_local $x) (get_local $y)))
(func $shr_s (param $x i64) (param $y i64) (result i64) (i64.shr_s (get_local $x) (get_local $y)))
(func $shr_u (param $x i64) (param $y i64) (result i64) (i64.shr_u (get_local $x) (get_local $y)))
(func $rotl (param $x i64) (param $y i64) (result i64) (i64.rotl (get_local $x) (get_local $y)))
(func $rotr (param $x i64) (param $y i64) (result i64) (i64.rotr (get_local $x) (get_local $y)))
(func $clz (param $x i64) (result i64) (i64.clz (get_local $x)))
(func $ctz (param $x i64) (result i64) (i64.ctz (get_local $x)))
(func $popcnt (param $x i64) (result i64) (i64.popcnt (get_local $x)))
Expand Down Expand Up @@ -41,6 +43,8 @@
(export "shl" $shl)
(export "shr_s" $shr_s)
(export "shr_u" $shr_u)
(export "rotl" $rotl)
(export "rotr" $rotr)
(export "clz" $clz)
(export "ctz" $ctz)
(export "popcnt" $popcnt)
Expand Down Expand Up @@ -232,6 +236,19 @@
(assert_return (invoke "shr_u" (i64.const -1) (i64.const 0x7fffffffffffffff)) (i64.const 1))
(assert_return (invoke "shr_u" (i64.const -1) (i64.const 0x8000000000000000)) (i64.const -1))

(assert_return (invoke "rotl" (i64.const 1) (i64.const 1)) (i64.const 2))
(assert_return (invoke "rotl" (i64.const 1) (i64.const 0)) (i64.const 1))
(assert_return (invoke "rotl" (i64.const -1) (i64.const 1)) (i64.const -1))
(assert_return (invoke "rotl" (i64.const 0xabd1234ef567809c) (i64.const 63)) (i64.const 0x55e891a77ab3c04e))
(assert_return (invoke "rotl" (i64.const 0xabd1234ef567809c) (i64.const 0x800000000000003f)) (i64.const 0x55e891a77ab3c04e))

(assert_return (invoke "rotr" (i64.const 1) (i64.const 1)) (i64.const 0x8000000000000000))
(assert_return (invoke "rotr" (i64.const 1) (i64.const 0)) (i64.const 1))
(assert_return (invoke "rotr" (i64.const -1) (i64.const 1)) (i64.const -1))
(assert_return (invoke "rotr" (i64.const 0xabcd1234ef567809) (i64.const 53)) (i64.const 0x6891a77ab3c04d5e))
(assert_return (invoke "rotr" (i64.const 0xabcd1234ef567809) (i64.const 0x35)) (i64.const 0x6891a77ab3c04d5e))
(assert_return (invoke "rotr" (i64.const 0xabcd1234ef567809) (i64.const 0xf5)) (i64.const 0x6891a77ab3c04d5e))

(assert_return (invoke "clz" (i64.const 0xffffffffffffffff)) (i64.const 0))
(assert_return (invoke "clz" (i64.const 0)) (i64.const 64))
(assert_return (invoke "clz" (i64.const 0x00008000)) (i64.const 48))
Expand Down