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

cmd/compile: signed rounding adjustments unnecessary when comparing modulus to 0 #15806

Open
randall77 opened this Issue May 23, 2016 · 4 comments

Comments

Projects
None yet
7 participants
@randall77
Copy link
Contributor

commented May 23, 2016

func f(i int) bool {
    return i%16 == 0
}

Since the modulo is signed, we have to do adjustments in case i is negative. We do

  adj := i>>63 & 15
  div := (i + adj) >> 4
  mod := i - div*16
  return mod == 0

Since we're comparing against 0, the adjustment in the first line is unnecessary. Zeroness is unaffected by the adjustment.
Might be as simple as converting (Eq (Mod64 x y) (Const64 [0])) to (Eq (Mod64u x y) (Const64 [0])) (and #uses of Mod64 is 1?)

@randall77 randall77 added this to the Go1.8 milestone May 23, 2016

@quentinmit quentinmit added the NeedsFix label Oct 11, 2016

@rsc rsc modified the milestones: Go1.9, Go1.8 Oct 21, 2016

@gopherbot

This comment has been minimized.

Copy link

commented May 10, 2017

CL https://golang.org/cl/43157 mentions this issue.

@josharian josharian modified the milestones: Go1.10, Go1.9 May 18, 2017

josharian added a commit to josharian/go that referenced this issue May 23, 2017

cmd/compile: optimize signed n % c == 0
While we're here, also convert a - b == 0 to a == b.

Fixes golang#15806

Change-Id: I41f6f154cb9607d335a79542e05c2e19ce10f3dc

@bradfitz bradfitz modified the milestones: Go1.10, Go1.11 Nov 10, 2017

josharian added a commit to josharian/go that referenced this issue May 8, 2018

cmd/compile: optimize signed n % c == 0
While we're here, also convert a - b == 0 to a == b.

Fixes golang#15806

Change-Id: I41f6f154cb9607d335a79542e05c2e19ce10f3dc

@bradfitz bradfitz modified the milestones: Go1.11, Go1.12 Jun 19, 2018

@odeke-em

This comment has been minimized.

Copy link
Member

commented Feb 4, 2019

We didn't get this in for Go1.12, so I'll move it to Go1.13 or perhaps "Unreleased" if we deem it to belong there.

@gopherbot

This comment has been minimized.

Copy link

commented Mar 18, 2019

Change https://golang.org/cl/168038 mentions this issue: cmd/compile: add signed divisibility by power of 2 rules

gopherbot pushed a commit that referenced this issue Apr 23, 2019

cmd/compile: add signed divisibility by power of 2 rules
For powers of two (c=1<<k), the divisibility check x%c == 0 can be made
just by checking the trailing zeroes via a mask x&(c-1)==0 even for signed
integers.  This avoids division fixups when just divisibility check is needed.

To apply this rule the generic divisibility rule for  A%B = A-(A/B*B) is disabled
on the "opt" pass, but this does not affect generated code as this rule is applied
later.

The speed up on amd64 due to elimination of unneccessary fixup code is ~55%:

name                     old time/op  new time/op  delta
DivconstI64-4            2.08ns ± 0%  2.07ns ± 0%     ~     (p=0.079 n=5+5)
DivisiblePow2constI64-4  1.78ns ± 1%  0.81ns ± 1%  -54.55%  (p=0.008 n=5+5)
DivconstU64-4            2.08ns ± 0%  2.08ns ± 0%     ~     (p=1.000 n=5+5)
DivconstI32-4            1.53ns ± 0%  1.53ns ± 0%     ~     (all equal)
DivisiblePow2constI32-4  1.79ns ± 1%  0.81ns ± 4%  -54.75%  (p=0.008 n=5+5)
DivconstU32-4            1.78ns ± 1%  1.78ns ± 1%     ~     (p=1.000 n=5+5)
DivconstI16-4            1.54ns ± 2%  1.53ns ± 0%     ~     (p=0.333 n=5+4)
DivisiblePow2constI16-4  1.78ns ± 0%  0.79ns ± 1%  -55.39%  (p=0.000 n=4+5)
DivconstU16-4            1.00ns ± 5%  0.99ns ± 1%     ~     (p=0.730 n=5+5)
DivconstI8-4             1.54ns ± 0%  1.53ns ± 0%     ~     (p=0.714 n=4+5)
DivisiblePow2constI8-4   1.78ns ± 0%  0.80ns ± 0%  -55.06%  (p=0.000 n=5+4)
DivconstU8-4             0.93ns ± 1%  0.95ns ± 1%   +1.72%  (p=0.024 n=5+5)

A follow-up CL will address the general case of x%c == 0 for signed integers.

Updates #15806

Change-Id: I0d284863774b1bc8c4ce87443bbaec6103e14ef4
Reviewed-on: https://go-review.googlesource.com/c/go/+/168038
Reviewed-by: Keith Randall <khr@golang.org>
@gopherbot

This comment has been minimized.

Copy link

commented Apr 24, 2019

Change https://golang.org/cl/173557 mentions this issue: cmd/compile: add signed divisibility by power of 2 rules

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.