Skip to content
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

implementation and tests for clz, ctz, and popcnt #50

Merged
merged 1 commit into from
Sep 17, 2015

Conversation

naturaltransformation
Copy link
Contributor

A short, illustrative implementation of integer bit operations, unoptimized since I assume the spec isn't prescribing particular optimizations. Test cases can benefit from hex integer literals though.

@jfbastien
Copy link
Member

(drive-by confirmation that George is part of the W3C CG under his company, thanks for the contribution!)

@wayneca
Copy link

wayneca commented Sep 11, 2015

This contains code from the closed #7 that was dropped due to not being from a CG participant. Just to avoid any worries about that, this is to confirm that code was also from Intel and one of our WebAssembly CG reps has contributed it this time (I'm Intel's W3C AC rep).


(func $pcnt32 (param $x i32) (result i32)
(i32.popcnt (get_local $x))
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice to keep the function name identical to the opcode name.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On 16 September 2015 at 01:36, Dan Gohman notifications@github.com wrote:

In ml-proto/test/int32.wasm
#50 (comment):

@@ -0,0 +1,37 @@
+(; Int arith operations ;)
+
+(module

  • (func $clz32 (param $x i32) (result i32)
  • (i32.clz (get_local $x))
  • )
  • (func $ctz32 (param $x i32) (result i32)
  • (i32.ctz (get_local $x))
  • )
  • (func $pcnt32 (param $x i32) (result i32)
  • (i32.popcnt (get_local $x))
  • )

It'd be nice to keep the function name identical to the opcode name.

Btw, despite it being what Intel picked on a bad day, popcnt really is an
odd name out. It is both inconsistent with the other count operators, and
it sounds like some kind of stack operator.

Would anybody mind if we changed it? For example, how about cnz (count
non-zeros), which would be in line with the other count operators. (If you
say it's a worthwhile idea I can create an issue or PR.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may be wrong, but IIRC, the “population count” terminology long precedes Intel. It is used in GHC base package, GCC, Sparc, Cray, and CDC (the latter ones long before Intel). Even the Mark I had it but called it Sideways Add.

From: rossberg-chromium [mailto:notifications@github.com]
Sent: Wednesday, September 16, 2015 12:33 AM
To: WebAssembly/spec
Cc: Kuan, George
Subject: Re: [spec] implementation and tests for clz, ctz, and popcnt (#50)

In ml-proto/test/int32.wasmhttps://github.com//pull/50#discussion_r39600610:

@@ -0,0 +1,37 @@

+(; Int arith operations ;)

+(module

  • (func $clz32 (param $x i32) (result i32)
  • (i32.clz (get_local $x))
  • )
  • (func $ctz32 (param $x i32) (result i32)
  • (i32.ctz (get_local $x))
  • )
  • (func $pcnt32 (param $x i32) (result i32)
  • (i32.popcnt (get_local $x))
  • )
    On 16 September 2015 at 01:36, Dan Gohman notifications@github.com wrote: In ml-proto/test/int32.wasm implementation and tests for clz, ctz, and popcnt #50 (comment): > @@ -0,0 +1,37 @@ > +(; Int arith operations ;) > + > +(module > + (func $clz32 (param $x i32) (result i32) > + (i32.clz (get_local $x)) > + ) > + > + (func $ctz32 (param $x i32) (result i32) > + (i32.ctz (get_local $x)) > + ) > + > + (func $pcnt32 (param $x i32) (result i32) > + (i32.popcnt (get_local $x)) > + ) It'd be nice to keep the function name identical to the opcode name.
    Btw, despite it being what Intel picked on a bad day, popcnt really is an odd name out. It is both inconsistent with the other count operators, and it sounds like some kind of stack operator. Would anybody mind if we changed it? For example, how about cnz (count non-zeros), which would be in line with the other count operators. (If you say it's a worthwhile idea I can create an issue or PR.)


Reply to this email directly or view it on GitHubhttps://github.com//pull/50/files#r39600610.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On UltraSparc, it is known as POPC if that is any better.

From: rossberg-chromium [mailto:notifications@github.com]
Sent: Wednesday, September 16, 2015 12:33 AM
To: WebAssembly/spec
Cc: Kuan, George
Subject: Re: [spec] implementation and tests for clz, ctz, and popcnt (#50)

In ml-proto/test/int32.wasmhttps://github.com//pull/50#discussion_r39600610:

@@ -0,0 +1,37 @@

+(; Int arith operations ;)

+(module

  • (func $clz32 (param $x i32) (result i32)
  • (i32.clz (get_local $x))
  • )
  • (func $ctz32 (param $x i32) (result i32)
  • (i32.ctz (get_local $x))
  • )
  • (func $pcnt32 (param $x i32) (result i32)
  • (i32.popcnt (get_local $x))
  • )
    On 16 September 2015 at 01:36, Dan Gohman notifications@github.com wrote: In ml-proto/test/int32.wasm implementation and tests for clz, ctz, and popcnt #50 (comment): > @@ -0,0 +1,37 @@ > +(; Int arith operations ;) > + > +(module > + (func $clz32 (param $x i32) (result i32) > + (i32.clz (get_local $x)) > + ) > + > + (func $ctz32 (param $x i32) (result i32) > + (i32.ctz (get_local $x)) > + ) > + > + (func $pcnt32 (param $x i32) (result i32) > + (i32.popcnt (get_local $x)) > + ) It'd be nice to keep the function name identical to the opcode name.
    Btw, despite it being what Intel picked on a bad day, popcnt really is an odd name out. It is both inconsistent with the other count operators, and it sounds like some kind of stack operator. Would anybody mind if we changed it? For example, how about cnz (count non-zeros), which would be in line with the other count operators. (If you say it's a worthwhile idea I can create an issue or PR.)


Reply to this email directly or view it on GitHubhttps://github.com//pull/50/files#r39600610.

let rec helper n acc i =
match (n, i) with
| (n,_) when n = Int.zero -> acc
| (_,0) -> acc
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this line ever reached? It's not clear that this line will ever match if the first line doesn't.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first line matches n=Int.zero and the second when i=0 (and n <> Int.zero). It would have been clearer and more succinct if this could be have turned into an OR pattern but alas OR patterns are incompatible with guards.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my reading of the code, when i=0, n must have been shifted down to zero too, which would mean this case isn't ever taken.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Actually, that is a great catch. I was using the wrong shift operator (it should be shift_right_logical instead of shift_right, now fixed). That case happened to mask the issue by handling negative integer inputs.

@sunfishcode
Copy link
Member

This code lgtm, with a few comments above. Eventually it'll be desirable to factor this code into functions outside of arithmetic.ml, but I think this is a good first step. Also, it'd be good for someone with more OCaml experience than I to review this.

@@ -42,6 +43,9 @@ sig
val of_big_int_u : Big_int.big_int -> t
val to_value : t -> value
val of_value : int -> value -> t
val compare : t -> t -> int
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compare is polymorphic in Ocaml, you don't need to parameterise over it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. But question though, does val compare : 'a -> 'a -> int vs. val compare: t -> t -> int make a difference to specialization either way?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, they are the same (would be rather terrible if they weren't :) ).

@naturaltransformation
Copy link
Contributor Author

Any further feedback on this one? Thanks!

@rossberg
Copy link
Member

lgtm modulo nit

@sunfishcode
Copy link
Member

Merging with lgtm and nit addressed. Any further feedback can be addressed in followup patches. Thanks!

sunfishcode added a commit that referenced this pull request Sep 17, 2015
implementation and tests for clz, ctz, and popcnt
@sunfishcode sunfishcode merged commit aa25fba into WebAssembly:master Sep 17, 2015
littledan pushed a commit to littledan/spec that referenced this pull request Mar 4, 2018
eqrion pushed a commit to eqrion/wasm-spec that referenced this pull request Jul 18, 2019
* Clarify trapping semantics
alexcrichton pushed a commit to alexcrichton/spec that referenced this pull request Nov 18, 2019
* More minor opcode fixups

Reserve opcode space for signed, and unsigned min/max, floating point RecipApprox, and RecipSqrtApprox and remove explicit `-` for reserved opcodes for consistency.
dhil pushed a commit to dhil/webassembly-spec that referenced this pull request Aug 30, 2024
Update readme to link to the two active stack-switching proposals
dhil added a commit to dhil/webassembly-spec that referenced this pull request Oct 21, 2024
rossberg pushed a commit that referenced this pull request Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants