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

Free multiplication in BinaryInteger/power(_:) #71

Closed
oscbyspro opened this issue Aug 21, 2024 · 2 comments
Closed

Free multiplication in BinaryInteger/power(_:) #71

oscbyspro opened this issue Aug 21, 2024 · 2 comments
Labels
addition oh, so shiny! brrr such code, much wow

Comments

@oscbyspro
Copy link
Owner

oscbyspro commented Aug 21, 2024

The initial power (1) in binary exponentiation is basically a coefficient.

You can parameterize the initial value (#53) and get a multiplication for free:

BinaryInteger/power(_:)
BinaryInteger/power(_:coefficient:) // free multiplication by the coefficient
@oscbyspro oscbyspro added the brrr such code, much wow label Aug 21, 2024
@oscbyspro oscbyspro added this to the Ultimathnum 0.9.0 milestone Aug 21, 2024
@oscbyspro oscbyspro added the addition oh, so shiny! label Aug 21, 2024
@oscbyspro
Copy link
Owner Author

oscbyspro commented Aug 22, 2024

The error indicator only works naturally for nonzero coefficients. Otherwise, it will be set regardless if either power or the multiplier overflows. I can mandate a nonzero coefficient with Nonzero<T> or add an isZero check. Hm.

The isZero check should be omitted for inlined constants, so it's probably better. The Nonzero<T> approach lets you ommit checks for dynamic values, but zero is well defined so you'd want a T version regardless. I suppose it's a question of whether the optimization is worth exposing.

@oscbyspro
Copy link
Owner Author

oscbyspro commented Dec 17, 2024

This is the 2nd time I've tried to remove the coefficient parameter to make the function simpler. So I'll just note that including the coefficient make little to no difference for small systems integers, but it is faster for arbitrary integes. Edit: It looks like most of the difference comes down to heap allocating the initial power and that using a static Self.one instead may make composition viable. Alternatively, a small-storage optimization would be more flexible (#44).

This was referenced Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition oh, so shiny! brrr such code, much wow
Projects
None yet
Development

No branches or pull requests

1 participant