Skip to content

optimize f_pow when base is int - #41

Open
GGrush-Doudizhu wants to merge 1 commit into
armoha:mainfrom
GGrush-Doudizhu:pow-dev
Open

optimize f_pow when base is int#41
GGrush-Doudizhu wants to merge 1 commit into
armoha:mainfrom
GGrush-Doudizhu:pow-dev

Conversation

@GGrush-Doudizhu

Copy link
Copy Markdown
Contributor

Optimize f_pow when the base is int and the exponent is EUDVariable.

  • Add fast paths for bases 0 and 1.
  • Factor powers of two from even constant bases:
    a**b = odd_part**b << (trailing_zeros * b).
  • Preserve the existing generic _pow path for unsupported cases.

@armoha armoha left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Unlike _pow, this generates multiple triggers on every call sites.

(1) _pow(0, EUDVariable) case is uncommon and not that slow to avoid using it.

(2) pow(int, EUDVariable) branch has unnecessary copies.
When a is not a power of 2, _pow(odd_part, b) is called anyway. The cost of _pow(a, b) depends on the 1-bits in b so _pow(odd_part, b) has same cost as original _pow(a, b).

(3) When a is a power of 2, the _constmul (full 32 bits multiplication) + f_bitlshift replacement isn't clearly better than _pow. f_bitlshift(1, EUDVariable) is not specialized and it rather fallbacks to f_bitlshift(EUDVariable, EUDVariable)` in current implementation.

@armoha
armoha force-pushed the main branch 3 times, most recently from c98c3c9 to cc0caaa Compare August 29, 2026 01:56
@armoha
armoha force-pushed the main branch 3 times, most recently from d8c2375 to a99250d Compare September 7, 2026 13:20
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.

2 participants