-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
Signed non-negative divide by power of 2. #44530
Comments
The two functions give different results when called with a negative number, so they're not equivalent. Of course in this case you're calling them with a positive argument, but they need to be compiled to handle every possible input. |
I can understand that.But I don't know how this optimization is reflected in the code.But I test that unsigned divided by 2 works. |
So can you expand on what issue are you seeing in the generated code? It's true that |
Oh you wanted to know when that SSA rule is triggered. For example, when the input and output arguments are of |
In this ssa,It can be triggered if the int is not negative.There are other rules that optimize uint. // Unsigned divide by power of 2. Strength reduce to a shift.
(Div8u n (Const8 [c])) && isPowerOfTwo8(c) => (Rsh8Ux64 n (Const64 <typ.UInt64> [log8(c)]))
(Div16u n (Const16 [c])) && isPowerOfTwo16(c) => (Rsh16Ux64 n (Const64 <typ.UInt64> [log16(c)]))
(Div32u n (Const32 [c])) && isPowerOfTwo32(c) => (Rsh32Ux64 n (Const64 <typ.UInt64> [log32(c)]))
(Div64u n (Const64 [c])) && isPowerOfTwo64(c) => (Rsh64Ux64 n (Const64 <typ.UInt64> [log64(c)]))
(Div64u n (Const64 [-1<<63])) => (Rsh64Ux64 n (Const64 <typ.UInt64> [63]))
// Signed non-negative divide by power of 2.
(Div8 n (Const8 [c])) && isNonNegative(n) && isPowerOfTwo8(c) => (Rsh8Ux64 n (Const64 <typ.UInt64> [log8(c)]))
(Div16 n (Const16 [c])) && isNonNegative(n) && isPowerOfTwo16(c) => (Rsh16Ux64 n (Const64 <typ.UInt64> [log16(c)]))
(Div32 n (Const32 [c])) && isNonNegative(n) && isPowerOfTwo32(c) => (Rsh32Ux64 n (Const64 <typ.UInt64> [log32(c)]))
(Div64 n (Const64 [c])) && isNonNegative(n) && isPowerOfTwo64(c) => (Rsh64Ux64 n (Const64 <typ.UInt64> [log64(c)]))
(Div64 n (Const64 [-1<<63])) && isNonNegative(n) => (Const64 [0]) |
|
I think int c >= 0 will use Signed non-negative divide by power of 2. |
I'm afraid I don't understand what you're asking. |
Oh, I think I got it. OP wants to know what kind of code will triggers this kind of rules:
Since
doesn't, but also replacing with |
I want to know when this rule(Signed non-negative divide by power of 2) is triggered? |
I've tried a lot of code and haven't triggered this rule yet. So I'm very curious about the circumstances that trigger this rule. |
|
thanks. i will try it |
The rules are triggered several times when compiling the standard library. I have verified that this line in the
triggers the Closing here since we don't use the issue tracker for questions. |
What version of Go are you using (
go version
)?What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
When I checked the source code in compile/ssa/gen/generic.rules l951, I found that function a should be optimized to b.
What did you expect to see?
The same instructions should be used.
What did you see instead?
The text was updated successfully, but these errors were encountered: