Skip to content

Commit 049f9ae

Browse files
committed
eliminate mpfr-new! entirely
1 parent d1107b2 commit 049f9ae

File tree

5 files changed

+18
-19
lines changed

5 files changed

+18
-19
lines changed

eval/compile.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@
296296
[n (in-naturals num-vars)])
297297
(fn->ival-fn node
298298
(lambda ()
299-
(vector-set! registers n (new-ival))
299+
(vector-set! registers n (new-ival (*rival-max-precision*)))
300300
n)
301301
constants-lookup
302302
(- n num-vars))))

infra/run-baseline.rkt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
registers
2424
precisions
2525
best-known-precisions
26+
max-precision
2627
repeats
2728
initial-repeats
2829
default-hint
@@ -160,7 +161,7 @@
160161
[n (in-naturals num-vars)])
161162
(fn->ival-fn node ; mappings are taken from Rival machine
162163
(lambda ()
163-
(vector-set! registers n (new-ival))
164+
(vector-set! registers n (new-ival (*rival-max-precision*)))
164165
n))))
165166

166167
(define start-prec (+ (discretization-target (last discs)) 10))
@@ -181,6 +182,7 @@
181182
registers
182183
precisions
183184
best-known-precisions
185+
(*rival-max-precision*)
184186
repeats
185187
initial-repeats
186188
default-hint
@@ -199,6 +201,7 @@
199201

200202
(define (baseline-apply machine pt [hint #f])
201203
(define discs (baseline-machine-discs machine))
204+
(define max-precision (baseline-machine-max-precision machine))
202205
(define start-prec (+ (discretization-target (last discs)) 10))
203206
; Load arguments
204207
(baseline-machine-load machine (vector-map ival-real pt))
@@ -212,7 +215,7 @@
212215
[bad? (raise (exn:rival:invalid "Invalid input" (current-continuation-marks) pt))]
213216
[done? fvec]
214217
[stuck? (raise (exn:rival:unsamplable "Unsamplable input" (current-continuation-marks) pt))]
215-
[(> (* 2 prec) (*rival-max-precision*)) ; max precision is taken from eval/machine.rkt
218+
[(> (* 2 prec) max-precision) ; max precision is taken from eval/machine.rkt
216219
(raise (exn:rival:unsamplable "Unsamplable input" (current-continuation-marks) pt))]
217220
[else (loop (* 2 prec) (+ iter 1))])))
218221

mpfr.rkt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,6 @@
6363

6464
(define mpfr-set! (get-mpfr-fun 'mpfr_set (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _void)))
6565

66-
(define (mpfr-new! prec)
67-
(unless (<= 2 prec (*rival-max-precision*))
68-
(error 'mpfr-new! "Cannot create an MPFR value with precision ~a" prec))
69-
(define x (parameterize ([bf-precision (*rival-max-precision*)]) (bf 0)))
70-
(set-mpfr-prec! x prec)
71-
x)
72-
7366
(define mpfr-set-prec! set-mpfr-prec!)
7467

7568
(define (bfremainder x mod)
@@ -235,5 +228,4 @@
235228
mpfr-mul!
236229
mpfr-div!
237230
mpfr-set-prec!
238-
mpfr-new!
239231
mpfr-set!)

ops/arith.rkt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
(or (ival-err x) (ival-err y))))
3636

3737
(define (ival-add x y)
38-
(ival-add! (new-ival) x y))
38+
(ival-add! (new-ival (bf-precision)) x y))
3939

4040
(define (ival-sub! out x y)
4141
(ival (eplinear! (ival-lo-val out) mpfr-sub! (ival-lo x) (ival-hi y) 'down)
@@ -44,7 +44,7 @@
4444
(or (ival-err x) (ival-err y))))
4545

4646
(define (ival-sub x y)
47-
(ival-sub! (new-ival) x y))
47+
(ival-sub! (new-ival (bf-precision)) x y))
4848

4949
(define (epmul! out a-endpoint b-endpoint a-class b-class)
5050
(match-define (endpoint a a!) a-endpoint)
@@ -66,9 +66,10 @@
6666
(and b! (bfinfinite? b) (not (= a-class 0))))))
6767

6868
(define (ival-mult x y)
69-
(ival-mult! (new-ival) x y))
69+
(ival-mult! (new-ival (bf-precision)) x y))
7070

71-
(define extra-mult-ival (new-ival))
71+
(define extra-mult-ival-prec (*rival-max-precision*))
72+
(define extra-mult-ival (new-ival extra-mult-ival-prec))
7273

7374
(define (ival-mult! out x y)
7475
(match-define (ival xlo xhi xerr? xerr) x)
@@ -96,6 +97,9 @@
9697
;; Here, the two branches of the union are meaningless on their own;
9798
;; however, both branches compute possible lo/hi's to min/max together
9899
[(0 0)
100+
(when (< extra-mult-ival-prec (*rival-max-precision*))
101+
(set! extra-mult-ival-prec (*rival-max-precision*))
102+
(set! extra-mult-ival (new-ival extra-mult-ival-prec)))
99103
(match-define (ival (endpoint lo lo!) (endpoint hi hi!) err? err)
100104
(ival-union (mkmult extra-mult-ival xhi ylo xlo ylo) (mkmult out xlo yhi xhi yhi)))
101105
(mpfr-set! (ival-lo-val out) lo 'down) ; should be exact
@@ -145,7 +149,7 @@
145149
[(0 -1) (mkdiv xhi yhi xlo yhi)]))
146150

147151
(define (ival-div x y)
148-
(ival-div! (new-ival) x y))
152+
(ival-div! (new-ival (bf-precision)) x y))
149153

150154
(define (ival-fma a b c)
151155
(ival-add (ival-mult a b) c))

ops/core.rkt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@
118118
(define (ival-hi-fixed? ival)
119119
(endpoint-immovable? (ival-hi ival)))
120120

121-
(define (new-ival)
121+
(define (new-ival precision)
122122
; Warning, leaks memory unless `mpfr-clear!` called eventually
123-
(define bf1 (mpfr-new! (bf-precision)))
124-
(define bf2 (mpfr-new! (bf-precision)))
123+
(define bf1 (parameterize ([bf-precision precision]) (bf 0)))
124+
(define bf2 (parameterize ([bf-precision precision]) (bf 0)))
125125
(ival (endpoint bf1 #f) (endpoint bf2 #f) #f #f))
126126

127127
(define (mk-big-ival x y)

0 commit comments

Comments
 (0)