Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Strength reduction
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Other strength reduction operations == {{hatnote|This material is disputed. It is better described as [[peephole optimization]] and instruction assignment.}} Operator strength reduction uses [[Identity (mathematics)|mathematical identities]] to replace slow math operations with faster operations. The benefits depend on the target CPU and sometimes on the surrounding code (which can affect the availability of other functional units within the CPU). * replacing integer division or multiplication by a power of 2 with an [[arithmetic shift]] or [[logical shift]]<ref>In languages such as C and Java, integer division has round-towards-zero semantics, whereas a bit-shift always rounds down, requiring special treatment for negative numbers. For example, in Java, <code>-3 / 2</code> evaluates to <code>-1</code>, whereas <code>-3 >> 1</code> evaluates to <code>-2</code>. So in this case, the compiler ''cannot'' optimize division by two by replacing it by a bit shift.</ref> * replacing integer multiplication by a constant with a combination of shifts, adds or subtracts * replacing integer division by a constant with a multiplication, taking advantage of the limited range of machine integers.<ref>{{cite web|last=Granlund|first=Torbjörn|title=Division by Invariant Integers Using Multiplication|url=http://gmplib.org/~tege/divcnst-pldi94.pdf|author2=Peter L. Montgomery }}</ref> This method also works if divisor is a non-integer sufficiently greater than 1, e.g. √2 or π.<ref>{{cite web |last=Jones |first=Nigel |title=Division of integers by constants |url=https://embeddedgurus.com/stack-overflow/2009/06/division-of-integers-by-constants/ |archive-url=https://web.archive.org/web/20240326221708/https://embeddedgurus.com/stack-overflow/2009/06/division-of-integers-by-constants/ |archive-date=26 March 2024}}</ref> {| class="wikitable" |- ! Original calculation ! Replacement calculation |- | y+= 1 | y++ |- | y%2 != 0 | y & 1 |- | y = x * 2 | y = x << 1 |- | y = x / 2 | y = x >> 1 |- | y = x % 2 | y = x & 1 |- | y = x * 15 | y = (x << 4) - x |- | y = (uint16_t)x / 10 | y = ((uint32_t)x * (uint32_t)0xCCCD) >> 19) |- | y = (uint16_t)x / π | y = (((uint32_t)x * (uint32_t)0x45F3) >> 16) + x) >> 2) |}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)