Skip to content

Commit 197035e

Browse files
committed
Faster flonums-between
1 parent 898aa2f commit 197035e

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

math-lib/math/private/flonum/flonum-bits.rkt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,21 @@
9999
(: flprev (Flonum -> Flonum))
100100
(define (flprev x) (flstep x -1))
101101

102-
(: flonums-between (Flonum Flonum -> Integer))
103-
(define (flonums-between x y)
104-
(- (flonum->ordinal y) (flonum->ordinal x)))
105-
106102
) ; begin-encourage-inline
103+
104+
(: flonum->chunks (-> Flonum (Values Integer Integer)))
105+
(define (flonum->chunks x)
106+
(define neg? (fl< x 0.0))
107+
(define ax (flabs x))
108+
(if neg?
109+
(values (- (flbit-field ax 0 32)) (- (flbit-field ax 32 64)))
110+
(values (flbit-field ax 0 32) (flbit-field ax 32 64))))
111+
112+
(: flonums-between (Flonum Flonum -> Integer))
113+
(define (flonums-between x y)
114+
(define-values (xlo xhi) (flonum->chunks x))
115+
(define-values (ylo yhi) (flonum->chunks y))
116+
(+ (arithmetic-shift (- yhi xhi) 32) (- ylo xlo)))
107117

108118
(: flulp (Flonum -> (U Flonum-Nan Nonnegative-Flonum)))
109119
(define (flulp x)

math-test/math/tests/flonum-tests.rkt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@
144144
(check-equal? (flulp-error x (flnext x)) 1.0
145145
(format "x = ~a" x)))
146146

147+
(for ([_ (in-range 1000)])
148+
(define x (ordinal->flonum (random-integer (flonum->ordinal -inf.0) (flonum->ordinal +inf.0))))
149+
(define y (ordinal->flonum (random-integer (flonum->ordinal -inf.0) (flonum->ordinal +inf.0))))
150+
(check-equal? (flonums-between x y)
151+
(- (flonum->ordinal y) (flonum->ordinal x))))
152+
147153
;; ===================================================================================================
148154
;; fllog2
149155

0 commit comments

Comments
 (0)