Skip to content

Commit 6b4492a

Browse files
authored
Merge pull request #114 from herbie-fp/fix-112
Adding tuning formulas for `tanu`, `cosu` and `sinu`, fix #112
2 parents a0cda9e + 198b342 commit 6b4492a

File tree

4 files changed

+47
-9
lines changed

4 files changed

+47
-9
lines changed

eval/adjust.rkt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@
154154
(define vbest-precs (rival-machine-best-known-precisions machine))
155155
(define current-iter (rival-machine-iteration machine))
156156
(define bumps (rival-machine-bumps machine))
157+
(define constants-lookup (rival-machine-constant-lookup machine))
157158

158159
(define first-tuning-pass? (equal? 1 current-iter))
159160
(define varc (vector-length args))
@@ -187,7 +188,7 @@
187188
[else (path-reduction vrepeats vregs varc instr i #:reexec-val #f)]))
188189

189190
; Step 2. Precision tuning
190-
(precision-tuning ivec vregs vprecs-new varc vstart-precs vrepeats vhint)
191+
(precision-tuning ivec vregs vprecs-new varc vstart-precs vrepeats vhint constants-lookup)
191192

192193
; Step 3. Repeating precisions check + Assigning if a operation should be computed again at all
193194
; vrepeats[i] = #t if the node has the same precision as an iteration before and children have #t flag as well
@@ -226,7 +227,7 @@
226227
; clean progress of the current tuning pass and start over
227228
(vector-fill! vprecs-new 0)
228229
(vector-fill! vrepeats #f)
229-
(precision-tuning ivec vregs vprecs-new varc vstart-precs vrepeats vhint)
230+
(precision-tuning ivec vregs vprecs-new varc vstart-precs vrepeats vhint constants-lookup)
230231
(repeats)) ; do repeats again
231232

232233
; Step 5. Copying new precisions into vprecs
@@ -244,17 +245,18 @@
244245
; Roughly speaking, the upper precision bound is calculated as:
245246
; vprecs-max[i] = (+ max-prec vstart-precs[i]), where min-prec < (+ max-prec vstart-precs[i]) < max-prec
246247
; max-prec = (car (get-bounds parent))
247-
(define (precision-tuning ivec vregs vprecs-max varc vstart-precs vrepeats vhint)
248+
(define (precision-tuning ivec vregs vprecs-max varc vstart-precs vrepeats vhint constants)
248249
(define vprecs-min (make-vector (vector-length ivec) 0))
249250
(for ([instr (in-vector ivec (- (vector-length ivec) 1) -1 -1)]
250251
[repeat? (in-vector vrepeats (- (vector-length vrepeats) 1) -1 -1)]
251252
[n (in-range (- (vector-length vregs) 1) -1 -1)]
252253
[hint (in-vector vhint (- (vector-length vhint) 1) -1 -1)]
253254
[output (in-vector vregs (- (vector-length vregs) 1) -1 -1)]
255+
[constant (in-vector constants (- (vector-length constants) 1) -1 -1)]
254256
#:when (and hint (not repeat?)))
255257
(define op (car instr))
256258
(define tail-registers (drop-self-pointer (cdr instr) n))
257-
(define srcs (map (lambda (x) (vector-ref vregs x)) tail-registers))
259+
(define srcs (append (map (lambda (x) (vector-ref vregs x)) tail-registers) constant))
258260

259261
(define max-prec (vector-ref vprecs-max (- n varc))) ; upper precision bound given from parent
260262
(define min-prec (vector-ref vprecs-min (- n varc))) ; lower precision bound given from parent

eval/compile.rkt

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
(define *rival-use-shorthands* (make-parameter #t))
2020
(define *rival-name-constants* (make-parameter #f))
2121

22-
(define (fn->ival-fn node alloc-outreg!)
22+
(define (fn->ival-fn node alloc-outreg! [constants #f] [i #f])
2323
(match node
2424
[(? number?)
2525
(if (ival-point? (real->ival node))
@@ -89,9 +89,18 @@
8989

9090
[(list 'pow2 x) (list ival-pow2 x)]
9191

92-
[(list `(cosu ,n) x) (list (ival-cosu n) x)]
93-
[(list `(sinu ,n) x) (list (ival-sinu n) x)]
94-
[(list `(tanu ,n) x) (list (ival-tanu n) x)]
92+
[(list `(cosu ,n) x)
93+
(when i
94+
(vector-set! constants i (exact-floor (log n))))
95+
(list (ival-cosu n) x)]
96+
[(list `(sinu ,n) x)
97+
(when i
98+
(vector-set! constants i (exact-floor (log n))))
99+
(list (ival-sinu n) x)]
100+
[(list `(tanu ,n) x)
101+
(when i
102+
(vector-set! constants i (exact-floor (log n))))
103+
(list (ival-tanu n) x)]
95104

96105
[(list '== x y) (list ival-== x y)]
97106
[(list '!= x y) (list ival-!= x y)]
@@ -279,14 +288,18 @@
279288
(define register-count (+ (length vars) ivec-length))
280289
(define registers (make-vector register-count))
281290

291+
(define constants-lookup (make-vector ivec-length '()))
292+
282293
(define instructions
283294
(for/vector #:length ivec-length
284295
([node (in-vector nodes num-vars)]
285296
[n (in-naturals num-vars)])
286297
(fn->ival-fn node
287298
(lambda ()
288299
(vector-set! registers n (new-ival))
289-
n))))
300+
n)
301+
constants-lookup
302+
(- n num-vars))))
290303

291304
(define precisions (make-vector ivec-length)) ; vector that stores working precisions
292305
(define initial-precisions (setup-vstart-precs instructions num-vars roots discs))
@@ -311,6 +324,7 @@
311324
best-known-precisions
312325
(make-vector (vector-length roots))
313326
default-hint
327+
constants-lookup
314328
0
315329
0
316330
0

eval/machine.rkt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
best-known-precisions
3434
output-distance
3535
default-hint
36+
constant-lookup
3637
[iteration #:mutable]
3738
[bumps #:mutable]
3839
[profile-ptr #:mutable]

eval/tricks.rkt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,27 @@
378378
(define x (first srcs))
379379
(list (cons (+ (logspan x) 1) 0))]
380380

381+
[(ival-sinu ival-cosu)
382+
; Γ[sinu]'x = |x*pi/n * cos(x*pi/n) / sin(x*pi/n)|
383+
; Γ[cosu]'x = |x*pi/n * sin(x*pi/n) / cos(x*pi/n)|
384+
;
385+
; ↑ampl[sinu]'x = ↑ampl[sinu]'n = maxlog(x) - minlog(n) - minlog(z) + 2 (accounting for pi)
386+
; ↓ampl[sinu]'x = ↓ampl[sinu]'n = 0 <-- maybe can be better
387+
;
388+
; ↑ampl[cosu]'x = ↑ampl[cosu]'n = maxlog(x) - minlog(n) - minlog(z) + 2 (accounting for pi)
389+
; ↓ampl[cosu]'x = ↓ampl[cosu]'n = 0 <-- maybe can be better
390+
(define x (car srcs))
391+
(define n (cdr srcs)) ; n is already a floor(log(n))
392+
(list (cons (- (maxlog x) n (minlog z) -2) 0))]
393+
394+
[(ival-tanu)
395+
; Γ[tanu]'x = |x*pi/n * (1 / cos^2(x*pi/n)) / tan(x*pi/n)|
396+
; ↑ampl[tanu]'x = ↑ampl[tanu]'n = maxlog(x) - minlog(n) + max(|minlog(z)|, |maxlog(z)|) + 3 (accounting for pi)
397+
; ↓ampl[tanu]'x = ↓ampl[tanu]'n = 0 <-- maybe can be better
398+
(define x (car srcs))
399+
(define n (cdr srcs)) ; n is already a floor(log(n))
400+
(list (cons (- (maxlog x) n (- (max (abs (maxlog z)) (abs (minlog z)))) -3) 0))]
401+
381402
; TODO
382403
; ↑ampl[...] = slack
383404
; ↓ampl[...] = 0

0 commit comments

Comments
 (0)