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

Enhancements to expt #651

Closed

Conversation

jpellegrini
Copy link
Contributor

  • Don't use Scheme arithmentic where not needed in exponentiation by squaring (the exponent is known to be a fixnum).
  • Fast path for exponent=2 (much much faster).
  • Change name exact_exponent_expt -> fixnum_exponent_expt
  • Move the bignum check to my_expt, so the exponent is always a fixnum when calling fixnum_exponent_expt.
  • Since the exponent is always a fixnum in fixnum_exponent_expt, compare with MAKE_INT(0) instead of using zerop.

Results:

  • for some integer exponents, expt is faster
  • specifically, for exponent = 2, it is more than twice as fast

* Don't use Scheme arithmentic where not needed in exponentiation by
  squaring (the exponent is known to be a fixnum).
* Fast path for exponent=2 (much much faster).
* Change name `exact_exponent_expt` -> `fixnum_exponent_expt`
* Move the bignum check to `my_expt`, so the exponent is always a fixnum
  when calling `fixnum_exponent_expt`.
* Since the exponent is always a fixnum in `fixnum_exponent_expt`, compare
  with `MAKE_INT(0)` instead of using `zerop`.

Results:
* for some integer exponents, `expt` is faster
* specifically, for exponent = 2, it is more than twice as fast
@egallesio
Copy link
Owner

Hi @jpellegrini,

Just realized that I forgot to merge thi PR before working on expt.
I'll try to do the adaptation.

@egallesio
Copy link
Owner

Merged by hand. It seems OK.

Furthermore, exact_exponent_expt take a long instead of a SCM now

The function signature was

static inline SCM fixnum_exponent_expt(SCM x, SCM y)                    

It is now

static inline SCM fixnum_exponent_expt(SCM x, long y)                   

We have fewer conversions from Scheme fixnums to longs now.
Not tested if it is really more efficient (it should).

@egallesio egallesio closed this Sep 5, 2024
@jpellegrini
Copy link
Contributor Author

It seems generally faster!

This:

(let ((a 0)
      (b 10)
      (e 2.0))
  (time
   (repeat 10_000_000
     (set! a (expt b e))))
  a)

Runs i nsomething like half the time, compared to the STklos checkout from last week. 😀

@jpellegrini
Copy link
Contributor Author

Runs i nsomething like half the time, compared to the STklos checkout from last week. 😀

Even with the float exponent!

@egallesio
Copy link
Owner

That's great!
It's crazy the number of places and things to optimize/correct on a single function.

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.

None yet

2 participants