Skip to content

Commit ec901a6

Browse files
authored
Merge pull request #122 from herbie-fp/inline-endpoint
Refactor `eplinear` and `eppow` to pass the endpoint components directly
2 parents 030949d + 5aaf8d3 commit ec901a6

File tree

4 files changed

+54
-46
lines changed

4 files changed

+54
-46
lines changed

main.rkt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@
140140
racket/cmdline)
141141
(define repl-mode repl-main)
142142
(command-line #:program "racket -l rival"
143-
#:once-each [("--profile") "Print timing results"
144-
(set! repl-mode repl-profile)]
143+
#:once-each [("--profile") "Print timing results" (set! repl-mode repl-profile)]
145144
#:args ([file #f])
146145
(if file
147146
(call-with-input-file file repl-mode)

ops/arith.rkt

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,31 @@
1616
ival-hypot)
1717

1818
;; Endpoint computation for both `add`, `sub`, and `hypot` (which has an add inside)
19-
(define (eplinear bffn a-endpoint b-endpoint)
20-
(match-define (endpoint a a!) a-endpoint)
21-
(match-define (endpoint b b!) b-endpoint)
19+
(define (eplinear bffn a a! b b!)
2220
(define-values (val exact?) (bf-return-exact? bffn (list a b)))
23-
(endpoint val (or (and a! b! exact?) (and a! (bfinfinite? a)) (and b! (bfinfinite? b)))))
21+
(values val (or (and a! b! exact?) (and a! (bfinfinite? a)) (and b! (bfinfinite? b)))))
2422

25-
(define (eplinear! out mpfr-fn! a-endpoint b-endpoint rnd)
26-
(match-define (endpoint a a!) a-endpoint)
27-
(match-define (endpoint b b!) b-endpoint)
23+
(define (eplinear! out mpfr-fn! a a! b b! rnd)
2824
(mpfr-set-prec! out (bf-precision))
2925
(define exact? (= 0 (mpfr-fn! out a b rnd)))
30-
(endpoint out (or (and a! b! exact?) (and a! (bfinfinite? a)) (and b! (bfinfinite? b)))))
26+
(values out (or (and a! b! exact?) (and a! (bfinfinite? a)) (and b! (bfinfinite? b)))))
3127

3228
(define (ival-add! out x y)
33-
(ival (eplinear! (ival-lo-val out) mpfr-add! (ival-lo x) (ival-lo y) 'down)
34-
(eplinear! (ival-hi-val out) mpfr-add! (ival-hi x) (ival-hi y) 'up)
35-
(or (ival-err? x) (ival-err? y))
36-
(or (ival-err x) (ival-err y))))
29+
(match-define (ival (endpoint xlo xlo!) (endpoint xhi xhi!) xerr? xerr) x)
30+
(match-define (ival (endpoint ylo ylo!) (endpoint yhi yhi!) yerr? yerr) y)
31+
(define-values (lo lo!) (eplinear! (ival-lo-val out) mpfr-add! xlo xlo! ylo ylo! 'down))
32+
(define-values (hi hi!) (eplinear! (ival-hi-val out) mpfr-add! xhi xhi! yhi yhi! 'up))
33+
(ival (endpoint lo lo!) (endpoint hi hi!) (or xerr? yerr?) (or xerr yerr)))
3734

3835
(define (ival-add x y)
3936
(ival-add! (new-ival (bf-precision)) x y))
4037

4138
(define (ival-sub! out x y)
42-
(ival (eplinear! (ival-lo-val out) mpfr-sub! (ival-lo x) (ival-hi y) 'down)
43-
(eplinear! (ival-hi-val out) mpfr-sub! (ival-hi x) (ival-lo y) 'up)
44-
(or (ival-err? x) (ival-err? y))
45-
(or (ival-err x) (ival-err y))))
39+
(match-define (ival (endpoint xlo xlo!) (endpoint xhi xhi!) xerr? xerr) x)
40+
(match-define (ival (endpoint ylo ylo!) (endpoint yhi yhi!) yerr? yerr) y)
41+
(define-values (lo lo!) (eplinear! (ival-lo-val out) mpfr-sub! xlo xlo! yhi yhi! 'down))
42+
(define-values (hi hi!) (eplinear! (ival-hi-val out) mpfr-sub! xhi xhi! ylo ylo! 'up))
43+
(ival (endpoint lo lo!) (endpoint hi hi!) (or xerr? yerr?) (or xerr yerr)))
4644

4745
(define (ival-sub x y)
4846
(ival-sub! (new-ival (bf-precision)) x y))
@@ -161,12 +159,11 @@
161159
(define (ival-hypot! out x y)
162160
(define err? (or (ival-err? x) (ival-err? y)))
163161
(define err (or (ival-err x) (ival-err y)))
164-
(define x* (ival-exact-fabs x))
165-
(define y* (ival-exact-fabs y))
166-
(ival (eplinear! (ival-lo-val out) mpfr-hypot! (ival-lo x*) (ival-lo y*) 'down)
167-
(eplinear! (ival-hi-val out) mpfr-hypot! (ival-hi x*) (ival-hi y*) 'up)
168-
err?
169-
err))
162+
(match-define (ival (endpoint xlo xlo!) (endpoint xhi xhi!) _ _) (ival-exact-fabs x))
163+
(match-define (ival (endpoint ylo ylo!) (endpoint yhi yhi!) _ _) (ival-exact-fabs y))
164+
(define-values (lo lo!) (eplinear! (ival-lo-val out) mpfr-hypot! xlo xlo! ylo ylo! 'down))
165+
(define-values (hi hi!) (eplinear! (ival-hi-val out) mpfr-hypot! xhi xhi! yhi yhi! 'up))
166+
(ival (endpoint lo lo!) (endpoint hi hi!) err? err))
170167

171168
(define (ival-hypot x y)
172169
(ival-hypot! (new-ival (bf-precision)) x y))

ops/pow.rkt

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,17 @@
1313
[(and (< (mpfr-exp (ival-hi-val x)) 1) (not (bfinfinite? (ival-hi-val x)))) -1]
1414
[else 0]))
1515

16-
(define (eppow a-endpoint b-endpoint a-class b-class)
17-
(match-define (endpoint a a!) a-endpoint)
18-
(match-define (endpoint b b!) b-endpoint)
16+
(define (eppow a a! b b! a-class b-class)
1917
(when (bfzero? a)
2018
(set! a 0.bf)) ; Handle (-0)^(-1)
2119
(define-values (val exact?) (bf-return-exact? bfexpt (list a b)))
22-
(endpoint val
23-
(or (and a! b! exact?)
24-
(and a! (bf=? a 1.bf))
25-
(and a! (bfzero? a) (not (= b-class 0)))
26-
(and a! (bfinfinite? a) (not (= b-class 0)))
27-
(and b! (bfzero? b))
28-
(and b! (bfinfinite? b) (not (= a-class 0))))))
20+
(values val
21+
(or (and a! b! exact?)
22+
(and a! (bf=? a 1.bf))
23+
(and a! (bfzero? a) (not (= b-class 0)))
24+
(and a! (bfinfinite? a) (not (= b-class 0)))
25+
(and b! (bfzero? b))
26+
(and b! (bfinfinite? b) (not (= a-class 0))))))
2927

3028
(define (ival-copy-movability i1 i2)
3129
(ival (endpoint (ival-lo-val i1) (ival-lo-fixed? i2))
@@ -41,8 +39,24 @@
4139
(define y-class (classify-ival y))
4240

4341
(define (mk-pow a b c d)
44-
(match-define (endpoint lo lo!) (rnd 'down eppow a b x-class y-class))
45-
(match-define (endpoint hi hi!) (rnd 'up eppow c d x-class y-class))
42+
(define-values (lo lo!)
43+
(rnd 'down
44+
eppow
45+
(endpoint-val a)
46+
(endpoint-immovable? a)
47+
(endpoint-val b)
48+
(endpoint-immovable? b)
49+
x-class
50+
y-class))
51+
(define-values (hi hi!)
52+
(rnd 'up
53+
eppow
54+
(endpoint-val c)
55+
(endpoint-immovable? c)
56+
(endpoint-val d)
57+
(endpoint-immovable? d)
58+
x-class
59+
y-class))
4660

4761
(define-values (real-lo! real-hi!)
4862
(cond

repl.rkt

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
(require "eval/main.rkt"
1212
"eval/machine.rkt"
1313
"utils.rkt")
14-
(provide repl-main repl-profile)
14+
(provide repl-main
15+
repl-profile)
1516

1617
(define (create-discs args bodies repl)
1718
(for/list ([body (in-list bodies)])
@@ -185,8 +186,7 @@
185186
(define (repl-run repl cmd #:print? [print? #t])
186187
(with-handlers ([exn:fail:user? (lambda (e) (eprintf "ERROR ~a\n" (exn-message e)))])
187188
(match cmd
188-
[`(set precision fp64)
189-
(set-repl-precision! repl 'fp64)]
189+
[`(set precision fp64) (set-repl-precision! repl 'fp64)]
190190
[`(set precision ,(? integer? n))
191191
(when (< n 4)
192192
(raise-user-error 'set "Precision must be an integer greater than 3"))
@@ -258,13 +258,11 @@
258258
(define m0 (current-memory-use 'cumulative))
259259
(define repl (make-repl))
260260
(match-define (cons t1 m1)
261-
(profile
262-
(begin
263-
(for ([cmd (in-list cmds)])
264-
(repl-run repl cmd #:print? #f))
265-
(cons (current-inexact-milliseconds)
266-
(current-memory-use 'cumulative)))
267-
#:delay 0.001))
261+
(profile (begin
262+
(for ([cmd (in-list cmds)])
263+
(repl-run repl cmd #:print? #f))
264+
(cons (current-inexact-milliseconds) (current-memory-use 'cumulative)))
265+
#:delay 0.001))
268266
(eprintf "Ran in ~ams, allocated ~aMB\n"
269267
(~r (- t1 t0) #:precision '(= 3))
270268
(~r (/ (- m1 m0) 1000000) #:precision '(= 3))))

0 commit comments

Comments
 (0)