Skip to content

Commit 352c7cc

Browse files
authored
Merge branch 'main' into codex/split-epfn-into-two-functions
2 parents 88960c9 + ec901a6 commit 352c7cc

File tree

14 files changed

+414
-174
lines changed

14 files changed

+414
-174
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ report/*
88
rival-compiled
99
*~
1010
test.rival
11+
.worktrees/

AGENTS.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
# Organization
3+
4+
- The MPFR bindings live in `mpfr.rkt`; the base interval definition
5+
is in `ops/core.rkt`; the interval arithmetic functions are in
6+
`ops/`; the real evaluator is in `eval/`; the REPL is in `repl.rkt`.
7+
The tests are in `test.rkt`; the nightly is `time.rkt`. The nightly
8+
also has various helper functions in `infra/`
9+
- Mandatory auto-format. Always run `make fmt` before finishing your
10+
task to format your code correctly.
11+
- When editing an interval arithmetic function, always run the tests.
12+
Run tests with `racket -y test.rkt add` (for the right function).

eval/compile.rkt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
[(list '- x y) (list ival-sub! (alloc-outreg!) x y)]
7474
[(list '* x y) (list ival-mult! (alloc-outreg!) x y)]
7575
[(list '/ x y) (list ival-div! (alloc-outreg!) x y)]
76+
[(list 'hypot x y) (list ival-hypot! (alloc-outreg!) x y)]
7677
#;[(list '+ x y) (list ival-add x y)]
7778
#;[(list '- x y) (list ival-sub x y)]
7879
#;[(list '* x y) (list ival-mult x y)]
@@ -296,7 +297,7 @@
296297
[n (in-naturals num-vars)])
297298
(fn->ival-fn node
298299
(lambda ()
299-
(vector-set! registers n (new-ival))
300+
(vector-set! registers n (new-ival (*rival-max-precision*)))
300301
n)
301302
constants-lookup
302303
(- n num-vars))))

eval/machine.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#lang racket/base
22

3+
(require "../mpfr.rkt")
34
(provide (struct-out discretization)
45
(struct-out rival-machine)
56
*rival-max-precision*
@@ -12,7 +13,6 @@
1213
*base-tuning-precision*
1314
*bumps-activated*)
1415

15-
(define *rival-max-precision* (make-parameter 10000))
1616
(define *rival-min-precision* (make-parameter 20))
1717
(define *rival-max-iterations* (make-parameter 5))
1818
(define *rival-profile-executions* (make-parameter 1000))

eval/main.rkt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@
8383
(rival-machine-load machine (vector-map ival-real pt))
8484
(let loop ([iter 0])
8585
(define-values (good? done? bad? stuck? fvec)
86-
(parameterize ([*sampling-iteration* iter])
86+
(parameterize ([*sampling-iteration* iter]
87+
[*rival-max-precision* (rival-machine-max-precision machine)])
8788
(rival-machine-full machine (or hint (rival-machine-default-hint machine)))))
8889
(cond
8990
[bad? (raise (exn:rival:invalid "Invalid input" (current-continuation-marks) pt))]

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
@@ -161,7 +162,7 @@
161162
[n (in-naturals num-vars)])
162163
(fn->ival-fn node ; mappings are taken from Rival machine
163164
(lambda ()
164-
(vector-set! registers n (new-ival))
165+
(vector-set! registers n (new-ival (*rival-max-precision*)))
165166
n))))
166167

167168
(define start-prec (+ (discretization-target (last discs)) 10))
@@ -182,6 +183,7 @@
182183
registers
183184
precisions
184185
best-known-precisions
186+
(*rival-max-precision*)
185187
repeats
186188
initial-repeats
187189
default-hint
@@ -201,6 +203,7 @@
201203

202204
(define (baseline-apply machine pt [hint #f])
203205
(define discs (baseline-machine-discs machine))
206+
(define max-precision (baseline-machine-max-precision machine))
204207
(define start-prec (+ (discretization-target (last discs)) 10))
205208
; Load arguments
206209
(baseline-machine-load machine (vector-map ival-real pt))
@@ -214,7 +217,7 @@
214217
[bad? (raise (exn:rival:invalid "Invalid input" (current-continuation-marks) pt))]
215218
[done? fvec]
216219
[stuck? (raise (exn:rival:unsamplable "Unsamplable input" (current-continuation-marks) pt))]
217-
[(> (* 2 prec) (*rival-max-precision*)) ; max precision is taken from eval/machine.rkt
220+
[(> (* 2 prec) max-precision) ; max precision is taken from eval/machine.rkt
218221
(raise (exn:rival:unsamplable "Unsamplable input" (current-continuation-marks) pt))]
219222
[else (loop (* 2 prec) (+ iter 1))])))
220223

main.rkt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,10 @@
138138
(module+ main
139139
(require "repl.rkt"
140140
racket/cmdline)
141+
(define repl-mode repl-main)
141142
(command-line #:program "racket -l rival"
143+
#:once-each [("--profile") "Print timing results" (set! repl-mode repl-profile)]
142144
#:args ([file #f])
143145
(if file
144-
(call-with-input-file file rival-repl)
145-
(rival-repl (current-input-port)))))
146+
(call-with-input-file file repl-mode)
147+
(repl-mode (current-input-port)))))

mpfr.rkt

Lines changed: 119 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
(require math/private/bigfloat/mpfr
44
ffi/unsafe)
55

6+
(define *rival-max-precision* (make-parameter 10000))
7+
68
(provide -inf.bf
79
-1.bf
810
0.bf
@@ -13,7 +15,8 @@
1315
+inf.bf
1416
+nan.bf
1517
bf-return-exact?
16-
rnd)
18+
rnd
19+
*rival-max-precision*)
1720

1821
(define-syntax-rule (rnd mode op args ...)
1922
(parameterize ([bf-rounding-mode mode])
@@ -56,16 +59,86 @@
5659
(define mpfr-remainder!
5760
(get-mpfr-fun 'mpfr_remainder (_fun _mpfr-pointer _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
5861

59-
(define mpfr-set-prec! (get-mpfr-fun 'mpfr_set_prec (_fun _mpfr-pointer _prec_t -> _void)))
62+
(define mpfr-hypot!
63+
(get-mpfr-fun 'mpfr_hypot (_fun _mpfr-pointer _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
64+
65+
(define mpfr-pow!
66+
(get-mpfr-fun 'mpfr_pow (_fun _mpfr-pointer _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
67+
68+
(define mpfr-log! (get-mpfr-fun 'mpfr_log (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
69+
70+
(define mpfr-exp! (get-mpfr-fun 'mpfr_exp (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
71+
72+
(define mpfr-expm1! (get-mpfr-fun 'mpfr_expm1 (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
73+
74+
(define mpfr-sqrt! (get-mpfr-fun 'mpfr_sqrt (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
75+
76+
(define mpfr-sin! (get-mpfr-fun 'mpfr_sin (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
77+
78+
(define mpfr-cos! (get-mpfr-fun 'mpfr_cos (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
79+
80+
(define mpfr-tan! (get-mpfr-fun 'mpfr_tan (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
81+
82+
(define mpfr-cbrt! (get-mpfr-fun 'mpfr_cbrt (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
83+
84+
(define mpfr-neg! (get-mpfr-fun 'mpfr_neg (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
85+
86+
(define mpfr-abs! (get-mpfr-fun 'mpfr_abs (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
87+
88+
(define mpfr-asin! (get-mpfr-fun 'mpfr_asin (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
89+
90+
(define mpfr-acos! (get-mpfr-fun 'mpfr_acos (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
91+
92+
(define mpfr-atan! (get-mpfr-fun 'mpfr_atan (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
93+
94+
(define mpfr-sinh! (get-mpfr-fun 'mpfr_sinh (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
95+
96+
(define mpfr-tanh! (get-mpfr-fun 'mpfr_tanh (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
97+
98+
(define mpfr-asinh! (get-mpfr-fun 'mpfr_asinh (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
99+
100+
(define mpfr-acosh! (get-mpfr-fun 'mpfr_acosh (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
101+
102+
(define mpfr-atanh! (get-mpfr-fun 'mpfr_atanh (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
103+
104+
(define mpfr-erf! (get-mpfr-fun 'mpfr_erf (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
105+
106+
(define mpfr-erfc! (get-mpfr-fun 'mpfr_erfc (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
107+
108+
(define mpfr-log2! (get-mpfr-fun 'mpfr_log2 (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
109+
110+
(define mpfr-log10! (get-mpfr-fun 'mpfr_log10 (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
111+
112+
(define mpfr-log1p! (get-mpfr-fun 'mpfr_log1p (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
113+
114+
(define mpfr-exp2! (get-mpfr-fun 'mpfr_exp2 (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
115+
116+
(define mpfr-rint! (get-mpfr-fun 'mpfr_rint (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
117+
118+
(define mpfr-round! (get-mpfr-fun 'mpfr_round (_fun _mpfr-pointer _mpfr-pointer -> _int)))
119+
120+
(define mpfr-ceil! (get-mpfr-fun 'mpfr_ceil (_fun _mpfr-pointer _mpfr-pointer -> _int)))
121+
122+
(define mpfr-floor! (get-mpfr-fun 'mpfr_floor (_fun _mpfr-pointer _mpfr-pointer -> _int)))
123+
124+
(define mpfr-trunc! (get-mpfr-fun 'mpfr_trunc (_fun _mpfr-pointer _mpfr-pointer -> _int)))
125+
126+
(define mpfr-min!
127+
(get-mpfr-fun 'mpfr_min (_fun _mpfr-pointer _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
128+
129+
(define mpfr-max!
130+
(get-mpfr-fun 'mpfr_max (_fun _mpfr-pointer _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
131+
132+
(define mpfr-cosh! (get-mpfr-fun 'mpfr_cosh (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
133+
134+
(define mpfr-const-pi! (get-mpfr-fun 'mpfr_const_pi (_fun _mpfr-pointer _rnd_t -> _int)))
60135

61-
(define mpfr-init2! (get-mpfr-fun 'mpfr_init2 (_fun _mpfr-pointer _prec_t -> _void)))
136+
(define mpfr-atan2!
137+
(get-mpfr-fun 'mpfr_atan2 (_fun _mpfr-pointer _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
62138

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

65-
(define (mpfr-new! prec)
66-
(define bf (make-mpfr 0 0 0 #f))
67-
(mpfr-init2! bf prec)
68-
bf)
141+
(define mpfr-set-prec! set-mpfr-prec!)
69142

70143
(define (bfremainder x mod)
71144
(define out (bf 0))
@@ -229,6 +302,44 @@
229302
mpfr-sub!
230303
mpfr-mul!
231304
mpfr-div!
305+
mpfr-hypot!
306+
mpfr-pow!
307+
mpfr-exp!
308+
mpfr-expm1!
309+
mpfr-sqrt!
310+
mpfr-sin!
311+
mpfr-cos!
312+
mpfr-tan!
313+
mpfr-cosu!
314+
mpfr-sinu!
315+
mpfr-tanu!
316+
mpfr-cbrt!
317+
mpfr-neg!
318+
mpfr-abs!
319+
mpfr-asin!
320+
mpfr-acos!
321+
mpfr-atan!
322+
mpfr-sinh!
323+
mpfr-cosh!
324+
mpfr-tanh!
325+
mpfr-asinh!
326+
mpfr-acosh!
327+
mpfr-atanh!
328+
mpfr-erf!
329+
mpfr-erfc!
330+
mpfr-log2!
331+
mpfr-log10!
332+
mpfr-log1p!
333+
mpfr-exp2!
334+
mpfr-rint!
335+
mpfr-round!
336+
mpfr-ceil!
337+
mpfr-floor!
338+
mpfr-trunc!
339+
mpfr-min!
340+
mpfr-max!
341+
mpfr-log!
342+
mpfr-const-pi!
343+
mpfr-atan2!
232344
mpfr-set-prec!
233-
mpfr-new!
234345
mpfr-set!)

ops/all.rkt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
ival-sqrt
3232
ival-cbrt
3333
ival-hypot
34+
ival-hypot!
3435
ival-exp
3536
ival-exp2
3637
ival-expm1

0 commit comments

Comments
 (0)