[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 2/3] Several optimizations for R6RS fixnum arithmetic
From: |
Andreas Rottmann |
Subject: |
[PATCH 2/3] Several optimizations for R6RS fixnum arithmetic |
Date: |
Sat, 2 Apr 2011 19:42:27 +0200 |
* module/rnrs/arithmetic/fixnums.scm (assert-fixnum): Is now a
macro.
(assert-fixnums): New procedure checking a the elements of a list
for fixnum-ness. All callers applying `assert-fixnum' to a list
now changed to use this procedure.
* module/rnrs/arithmetic/fixnums.scm (define-fxop*): New for defining
n-ary inlinable special-casing the binary case using `case-lambda'.
All applicable procedures redefined using this macro.
* module/rnrs/arithmetic/fixnums.scm: Alias all predicates to
their non-fixnum counterparts.
---
module/rnrs/arithmetic/fixnums.scm | 86 +++++++++++++++++-------------------
1 files changed, 41 insertions(+), 45 deletions(-)
diff --git a/module/rnrs/arithmetic/fixnums.scm
b/module/rnrs/arithmetic/fixnums.scm
index befbe9d..03511ed 100644
--- a/module/rnrs/arithmetic/fixnums.scm
+++ b/module/rnrs/arithmetic/fixnums.scm
@@ -87,6 +87,7 @@
most-negative-fixnum)
(ice-9 optargs)
(rnrs base (6))
+ (rnrs control (6))
(rnrs arithmetic bitwise (6))
(rnrs conditions (6))
(rnrs exceptions (6))
@@ -105,50 +106,45 @@
(>= obj most-negative-fixnum)
(<= obj most-positive-fixnum)))
- (define (assert-fixnum . args)
+ (define-syntax assert-fixnum
+ (syntax-rules ()
+ ((_ arg ...)
+ (or (and (fixnum? arg) ...)
+ (raise (make-assertion-violation))))))
+
+ (define (assert-fixnums args)
(or (for-all fixnum? args) (raise (make-assertion-violation))))
- (define (fx=? fx1 fx2 . rst)
- (let ((args (cons* fx1 fx2 rst)))
- (apply assert-fixnum args)
- (apply = args)))
-
- (define (fx>? fx1 fx2 . rst)
- (let ((args (cons* fx1 fx2 rst)))
- (apply assert-fixnum args)
- (apply > args)))
-
- (define (fx<? fx1 fx2 . rst)
- (let ((args (cons* fx1 fx2 rst)))
- (apply assert-fixnum rst)
- (apply < args)))
-
- (define (fx>=? fx1 fx2 . rst)
- (let ((args (cons* fx1 fx2 rst)))
- (apply assert-fixnum rst)
- (apply >= args)))
-
- (define (fx<=? fx1 fx2 . rst)
- (let ((args (cons* fx1 fx2 rst)))
- (apply assert-fixnum rst)
- (apply <= args)))
-
- (define (fxzero? fx) (assert-fixnum fx) (zero? fx))
- (define (fxpositive? fx) (assert-fixnum fx) (positive? fx))
- (define (fxnegative? fx) (assert-fixnum fx) (negative? fx))
- (define (fxodd? fx) (assert-fixnum fx) (odd? fx))
- (define (fxeven? fx) (assert-fixnum fx) (even? fx))
-
- (define (fxmax fx1 fx2 . rst)
- (let ((args (cons* fx1 fx2 rst)))
- (apply assert-fixnum args)
- (apply max args)))
-
- (define (fxmin fx1 fx2 . rst)
- (let ((args (cons* fx1 fx2 rst)))
- (apply assert-fixnum args)
- (apply min args)))
-
+ (define-syntax define-fxop*
+ (syntax-rules ()
+ ((_ name op)
+ (define name
+ (case-lambda
+ ((x y)
+ (assert-fixnum x y)
+ (op x y))
+ (args
+ (assert-fixnums args)
+ (apply op args)))))))
+
+ ;; All these predicates don't check their arguments for fixnum-ness,
+ ;; as this doesn't seem to be strictly required by R6RS.
+
+ (define fx=? =)
+ (define fx>? >)
+ (define fx<? <)
+ (define fx>=? >=)
+ (define fx<=? <=)
+
+ (define fxzero? zero?)
+ (define fxpositive? positive?)
+ (define fxnegative? negative?)
+ (define fxodd? odd?)
+ (define fxeven? even?)
+
+ (define-fxop* fxmax max)
+ (define-fxop* fxmin min)
+
(define (fx+ fx1 fx2)
(assert-fixnum fx1 fx2)
(let ((r (+ fx1 fx2)))
@@ -219,9 +215,9 @@
(values s0 s1)))
(define (fxnot fx) (assert-fixnum fx) (lognot fx))
- (define (fxand . args) (apply assert-fixnum args) (apply logand args))
- (define (fxior . args) (apply assert-fixnum args) (apply logior args))
- (define (fxxor . args) (apply assert-fixnum args) (apply logxor args))
+ (define-fxop* fxand logand)
+ (define-fxop* fxior logior)
+ (define-fxop* fxxor logxor)
(define (fxif fx1 fx2 fx3)
(assert-fixnum fx1 fx2 fx3)
--
1.7.4.1
- R6RS fixnum arithmetic optimizations, Andreas Rottmann, 2011/04/02
- [PATCH 1/3] Add a few benchmarks for R6RS fixnum arithmetic, Andreas Rottmann, 2011/04/02
- [PATCH 2/3] Several optimizations for R6RS fixnum arithmetic,
Andreas Rottmann <=
- [PATCH 3/3] Add `fixnum?' VM primitive, Andreas Rottmann, 2011/04/02
- Re: [PATCH 3/3] Add `fixnum?' VM primitive, Andy Wingo, 2011/04/04
- Re: [PATCH 3/3] Add `fixnum?' VM primitive, Andreas Rottmann, 2011/04/04
- define-inlinable, Ludovic Courtès, 2011/04/06
- Re: define-inlinable, Andreas Rottmann, 2011/04/06
- Re: define-inlinable, Ludovic Courtès, 2011/04/06
- Re: define-inlinable, Andy Wingo, 2011/04/11
- Re: define-inlinable, Ludovic Courtès, 2011/04/11
- Re: define-inlinable, Andy Wingo, 2011/04/11
- Re: define-inlinable, Andreas Rottmann, 2011/04/11