Skip to content

gh-100239: specialize small integer powers (x**2, x**3) of compact ints - #156918

Open
cocolato wants to merge 1 commit into
python:mainfrom
cocolato:feat/small-power
Open

cocolato wants to merge 1 commit into
python:mainfrom
cocolato:feat/small-power

Conversation

@cocolato

@cocolato cocolato commented Sep 4, 2026 •

Copy link
Copy Markdown
Member

Extend the binaryop_extend_descrs table with NB_POWER/NB_INPLACE_POWER descriptors that strength-reduce x ** 2 and x ** 3 on exact compact ints into x*x and x*x*x.

Test on Apple M5 Pro, 15 cores 48 GB macOS 26.6.2, n = 2,000,000 x 4 passes for every workload;:

def pow_mixed(n):
    t = 0
    for i in range(n):
        t += (i & 7) ** 2 + (i & 3) ** 3
    return t

def pow2(n):
    t = 0
    for i in range(n):
        t += (i & 1023) ** 2
    return t

def pow3(n):
    t = 0
    for i in range(n):
        t += (i & 63) ** 3
    return t

def poly(n):
    t = 0
    for i in range(n):
        a = i & 255
        t += 3 * a ** 2 + 5 * a + 7
    return t

def miss(n):
    t = 0
    for i in range(n):
        t += (i & 7) ** 4
    return t
Workload main (ms) optimized (ms) Relative
pow_mixed ((i&7)**2 + (i&3)**3) 310.1 ± 4.9 229.0 ± 4.2 1.35 ± 0.03
pow3 (x**3) 289.7 ± 7.8 239.3 ± 13.5 1.21 ± 0.08
pow2 (x**2) 268.5 ± 14.8 232.5 ± 6.4 1.16 ± 0.07
poly (3*a**2 + 5*a + 7) 413.3 ± 5.0 387.8 ± 15.7 1.07 ± 0.05
miss ((i&7)**4, guard must reject) 257.3 ± 4.7 255.1 ± 5.7 1.01 ± 0.03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

1 participant