Skip to content

Commit f252dff

Browse files
committed
Refactor eplinear and eppow to pass the endpoint components directly
1 parent c89fbe7 commit f252dff

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

ops/arith.rkt

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,31 @@
1515
ival-hypot)
1616

1717
;; Endpoint computation for both `add`, `sub`, and `hypot` (which has an add inside)
18-
(define (eplinear bffn a-endpoint b-endpoint)
19-
(match-define (endpoint a a!) a-endpoint)
20-
(match-define (endpoint b b!) b-endpoint)
18+
(define (eplinear bffn a a! b b!)
2119
(define-values (val exact?) (bf-return-exact? bffn (list a b)))
22-
(endpoint val (or (and a! b! exact?) (and a! (bfinfinite? a)) (and b! (bfinfinite? b)))))
20+
(values val (or (and a! b! exact?) (and a! (bfinfinite? a)) (and b! (bfinfinite? b)))))
2321

24-
(define (eplinear! out mpfr-fn! a-endpoint b-endpoint rnd)
25-
(match-define (endpoint a a!) a-endpoint)
26-
(match-define (endpoint b b!) b-endpoint)
22+
(define (eplinear! out mpfr-fn! a a! b b! rnd)
2723
(mpfr-set-prec! out (bf-precision))
2824
(define exact? (= 0 (mpfr-fn! out a b rnd)))
29-
(endpoint out (or (and a! b! exact?) (and a! (bfinfinite? a)) (and b! (bfinfinite? b)))))
25+
(values out (or (and a! b! exact?) (and a! (bfinfinite? a)) (and b! (bfinfinite? b)))))
3026

3127
(define (ival-add! out x y)
32-
(ival (eplinear! (ival-lo-val out) mpfr-add! (ival-lo x) (ival-lo y) 'down)
33-
(eplinear! (ival-hi-val out) mpfr-add! (ival-hi x) (ival-hi y) 'up)
28+
(define-values (lo-val lo-imm?) (eplinear! (ival-lo-val out) mpfr-add! (ival-lo-val x) (ival-lo-fixed? x) (ival-lo-val y) (ival-lo-fixed? y) 'down))
29+
(define-values (hi-val hi-imm?) (eplinear! (ival-hi-val out) mpfr-add! (ival-hi-val x) (ival-hi-fixed? x) (ival-hi-val y) (ival-hi-fixed? y) 'up))
30+
(ival (endpoint lo-val lo-imm?)
31+
(endpoint hi-val hi-imm?)
3432
(or (ival-err? x) (ival-err? y))
3533
(or (ival-err x) (ival-err y))))
3634

3735
(define (ival-add x y)
3836
(ival-add! (new-ival) x y))
3937

4038
(define (ival-sub! out x y)
41-
(ival (eplinear! (ival-lo-val out) mpfr-sub! (ival-lo x) (ival-hi y) 'down)
42-
(eplinear! (ival-hi-val out) mpfr-sub! (ival-hi x) (ival-lo y) 'up)
39+
(define-values (lo-val lo-imm?) (eplinear! (ival-lo-val out) mpfr-sub! (ival-lo-val x) (ival-lo-fixed? x) (ival-hi-val y) (ival-hi-fixed? y) 'down))
40+
(define-values (hi-val hi-imm?) (eplinear! (ival-hi-val out) mpfr-sub! (ival-hi-val x) (ival-hi-fixed? x) (ival-lo-val y) (ival-lo-fixed? y) 'up))
41+
(ival (endpoint lo-val lo-imm?)
42+
(endpoint hi-val hi-imm?)
4343
(or (ival-err? x) (ival-err? y))
4444
(or (ival-err x) (ival-err y))))
4545

@@ -158,7 +158,9 @@
158158
(define err (or (ival-err x) (ival-err y)))
159159
(define x* (ival-exact-fabs x))
160160
(define y* (ival-exact-fabs y))
161-
(ival (rnd 'down eplinear bfhypot (ival-lo x*) (ival-lo y*))
162-
(rnd 'up eplinear bfhypot (ival-hi x*) (ival-hi y*))
161+
(define-values (lo-val lo-imm?) (rnd 'down eplinear bfhypot (ival-lo-val x*) (ival-lo-fixed? x*) (ival-lo-val y*) (ival-lo-fixed? y*)))
162+
(define-values (hi-val hi-imm?) (rnd 'up eplinear bfhypot (ival-hi-val x*) (ival-hi-fixed? x*) (ival-hi-val y*) (ival-hi-fixed? y*)))
163+
(ival (endpoint lo-val lo-imm?)
164+
(endpoint hi-val hi-imm?)
163165
err?
164166
err))

ops/pow.rkt

Lines changed: 10 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,8 @@
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!) (rnd 'down eppow (endpoint-val a) (endpoint-immovable? a) (endpoint-val b) (endpoint-immovable? b) x-class y-class))
43+
(define-values (hi hi!) (rnd 'up eppow (endpoint-val c) (endpoint-immovable? c) (endpoint-val d) (endpoint-immovable? d) x-class y-class))
4644

4745
(define-values (real-lo! real-hi!)
4846
(cond

0 commit comments

Comments
 (0)