Skip to content

Commit 0137ff0

Browse files
committed
quite extreme optimizations
1 parent 772e45b commit 0137ff0

File tree

2 files changed

+83
-85
lines changed

2 files changed

+83
-85
lines changed

eval/adjust.rkt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,18 +239,17 @@
239239
(define vprecs-min (make-vector (vector-length ivec) 0))
240240

241241
(define slack (get-slack))
242-
(define vlogs (vector-map (curryr minlog+maxlog+logspan slack) vregs))
242+
(define vinfo (vector-map (curryr ival+minlog+maxlog+logspan slack) vregs))
243243

244244
(for ([instr (in-vector ivec (- (vector-length ivec) 1) -1 -1)]
245245
[repeat? (in-vector vrepeats (- (vector-length vrepeats) 1) -1 -1)]
246246
[n (in-range (- (vector-length vregs) 1) -1 -1)]
247247
[hint (in-vector vhint (- (vector-length vhint) 1) -1 -1)]
248-
[output (in-vector vregs (- (vector-length vregs) 1) -1 -1)]
249-
[logs (in-vector vlogs (- (vector-length vlogs) 1) -1 -1)]
248+
[output (in-vector vinfo (- (vector-length vinfo) 1) -1 -1)]
250249
#:when (and hint (not repeat?)))
251250
(define op (car instr))
252251
(define tail-registers (drop-self-pointer (cdr instr) n))
253-
(define srcs (map (lambda (x) (cons (vector-ref vregs x) (vector-ref vlogs x))) tail-registers))
252+
(define srcs (map (lambda (x) (vector-ref vinfo x)) tail-registers))
254253

255254
(define max-prec (vector-ref vprecs-max (- n varc))) ; upper precision bound given from parent
256255
(define min-prec (vector-ref vprecs-min (- n varc))) ; lower precision bound given from parent
@@ -271,8 +270,7 @@
271270
(*sampling-iteration* (*rival-max-iterations*)))])
272271

273272
; Precision propogation for each tail instruction
274-
(define ampl-bounds
275-
(get-bounds op (cons output logs) srcs)) ; amplification bounds for children instructions
273+
(define ampl-bounds (get-bounds op output srcs)) ; amplification bounds for children instructions
276274
(for ([x (in-list tail-registers)]
277275
[bound (in-list ampl-bounds)]
278276
#:when (>= x varc)) ; when tail register is not a variable

eval/tricks.rkt

Lines changed: 79 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
(provide get-bounds
1212
get-slack
13-
minlog+maxlog+logspan)
13+
ival+minlog+maxlog+logspan)
1414

1515
(define (get-slack [iter (*sampling-iteration*)])
1616
(match iter
@@ -70,52 +70,52 @@
7070
[else (min (mpfr-exp lo) (mpfr-exp hi))])]))
7171

7272
; Returns (list (minlog x) (maxlog x) (logspan x))
73-
(define (minlog+maxlog+logspan x slack)
73+
(define (ival+minlog+maxlog+logspan x slack)
7474
(define lo (ival-lo x))
7575
(define hi (ival-hi x))
7676
(match (and (boolean? lo) (boolean? hi))
77-
[#t (list 0 0 0)]
77+
[#t (list x 0 0 0)]
7878
[#f
7979
(define lo-exp (mpfr-exp lo))
8080
(define hi-exp (mpfr-exp hi))
81-
(define logspan
81+
; logspan
82+
(define lg
8283
(match (*bumps-activated*)
8384
[#t
8485
(if (or (bfzero? lo) (bfinfinite? lo) (bfzero? hi) (bfinfinite? hi))
8586
slack
8687
(+ (abs (- lo-exp hi-exp)) 1))]
8788
[#f 0]))
88-
89-
(cond
90-
; x = [0.bf, 0.bf]
91-
[(and (bfzero? lo) (bfzero? hi)) (list (- slack) (- slack) logspan)]
92-
[(bfzero? lo)
93-
(if (bfinfinite? hi)
94-
(list (- slack) slack logspan) ; x = [0.bf, +inf]
95-
(list (- (min hi-exp 0) slack) (+ hi-exp 1) logspan))] ; x = [0.bf, +...]
96-
[(bfzero? hi)
97-
(if (bfinfinite? lo)
98-
(list (- slack) slack logspan) ; x = [-inf, 0.bf]
99-
(list (- (min lo-exp 0) slack) (+ lo-exp 1) logspan))] ; x = [-..., 0.bf]
100-
[(crosses-zero? x) ; x = [-..., +...]
101-
(cond
102-
[(and (bfinfinite? hi) (bfinfinite? lo)) (list (- slack) slack logspan)] ; [-inf, +inf]
103-
[(bfinfinite? hi)
104-
(list (- (min lo-exp 0) slack) (+ (max lo-exp 0) slack) logspan)] ; [-..., +inf]
105-
[(bfinfinite? lo)
106-
(list (- (min hi-exp 0) slack) (+ (max hi-exp 0) slack) logspan)] ; [-inf, +...]
107-
[else
108-
(list (- (min lo-exp hi-exp 0) slack)
109-
(+ (max lo-exp hi-exp) 1)
110-
logspan)])] ; x = [-..., +...]
111-
[else
112-
(cond
113-
; Can't both be inf, since:
114-
; - [inf, inf] not a valid interval
115-
; - [-inf, inf] crosses zero
116-
[(bfinfinite? lo) (list hi-exp (+ (max hi-exp 0) slack) logspan)] ; [-inf, -...]
117-
[(bfinfinite? hi) (list lo-exp (+ (max lo-exp 0) slack) logspan)] ; [+..., +inf]
118-
[else (list (min lo-exp hi-exp) (+ (max lo-exp hi-exp) 1) logspan)])])]))
89+
; minlog+maxlog
90+
(match-define (list mn mx)
91+
(cond
92+
; x = [0.bf, 0.bf]
93+
[(and (bfzero? lo) (bfzero? hi)) (list (- slack) (- slack))]
94+
[(bfzero? lo)
95+
(if (bfinfinite? hi)
96+
(list (- slack) slack) ; x = [0.bf, +inf]
97+
(list (- (min hi-exp 0) slack) (+ hi-exp 1)))] ; x = [0.bf, +...]
98+
[(bfzero? hi)
99+
(if (bfinfinite? lo)
100+
(list (- slack) slack) ; x = [-inf, 0.bf]
101+
(list (- (min lo-exp 0) slack) (+ lo-exp 1)))] ; x = [-..., 0.bf]
102+
[(crosses-zero? x) ; x = [-..., +...]
103+
(cond
104+
[(and (bfinfinite? hi) (bfinfinite? lo)) (list (- slack) slack)] ; [-inf, +inf]
105+
[(bfinfinite? hi) (list (- (min lo-exp 0) slack) (+ (max lo-exp 0) slack))] ; [-..., +inf]
106+
[(bfinfinite? lo) (list (- (min hi-exp 0) slack) (+ (max hi-exp 0) slack))] ; [-inf, +...]
107+
[else
108+
(list (- (min lo-exp hi-exp 0) slack) (+ (max lo-exp hi-exp) 1))])] ; x = [-..., +...]
109+
[else
110+
(cond
111+
; Can't both be inf, since:
112+
; - [inf, inf] not a valid interval
113+
; - [-inf, inf] crosses zero
114+
[(bfinfinite? lo) (list hi-exp (+ (max hi-exp 0) slack))] ; [-inf, -...]
115+
[(bfinfinite? hi) (list lo-exp (+ (max lo-exp 0) slack))] ; [+..., +inf]
116+
[else (list (min lo-exp hi-exp) (+ (max lo-exp hi-exp) 1))])]))
117+
; output
118+
(list x mn mx lg)]))
119119

120120
(define (logspan x)
121121
(match (*bumps-activated*)
@@ -142,8 +142,8 @@
142142
; Γ[*]'y = 1
143143
; ↑ampl[*]'y = logspan(x)
144144
; ↓ampl[*]'y = 0
145-
(match-define (cons x (list mn-x mx-x lg-x)) (first srcs))
146-
(match-define (cons y (list mn-y mx-y lg-y)) (second srcs))
145+
(match-define (list _ _ _ lg-x) (first srcs))
146+
(match-define (list _ _ _ lg-y) (second srcs))
147147
(list (cons lg-x 0) ; bounds per x
148148
(cons lg-y 0))] ; bounds per y
149149

@@ -155,8 +155,8 @@
155155
; Γ[/]'y = 1
156156
; ↑ampl[/]'y = logspan(x) + 2 * logspan(y)
157157
; ↓ampl[/]'y = 0
158-
(match-define (cons x (list mn-x mx-x lg-x)) (first srcs))
159-
(match-define (cons y (list mn-y mx-y lg-y)) (second srcs))
158+
(match-define (list _ _ _ lg-x) (first srcs))
159+
(match-define (list _ _ _ lg-y) (second srcs))
160160
(list (cons lg-y 0) ; bounds per x
161161
(cons (+ lg-x (* 2 lg-y)) 0))] ; bounds per y
162162

@@ -168,7 +168,7 @@
168168
; Γ[cbrt]'x = 1/3
169169
; ↑ampl[cbrt]'x = logspan(x)*2/3 - 1
170170
; ↓ampl[cbrt]'x = 0
171-
(match-define (cons x (list mn-x mx-x lg-x)) (first srcs))
171+
(match-define (list _ _ _ lg-x) (first srcs))
172172
(list (cons (quotient lg-x 2) 0))]
173173

174174
[(ival-add ival-sub ival-add! ival-sub!)
@@ -179,9 +179,9 @@
179179
; Γ[+ & -]'y = |y/(x+y)| & |-y/(x-y)|
180180
; ↑ampl[+ & -]'y = maxlog(y) - minlog(z)
181181
; ↓ampl[+ & -]'y = minlog(y) - maxlog(z)
182-
(match-define (cons x (list mn-x mx-x lg-x)) (first srcs))
183-
(match-define (cons y (list mn-y mx-y lg-y)) (second srcs))
184-
(match-define (cons z (list mn-z mx-z lg-z)) output)
182+
(match-define (list x _ mx-x _) (first srcs))
183+
(match-define (list y _ mx-y _) (second srcs))
184+
(match-define (list z mn-z _ _) output)
185185

186186
(if (*lower-bound-early-stopping*)
187187
(list (cons (- mx-x mn-z)
@@ -199,9 +199,9 @@
199199
; Γ[pow]'y = |y*ln(x)|
200200
; ↑ampl[pow]'y = maxlog(y) + max(|minlog(x)|,|maxlog(x)|) + logspan(z)
201201
; ↓ampl[pow]'y = minlog(y)
202-
(match-define (cons x (list mn-x mx-x lg-x)) (first srcs))
203-
(match-define (cons y (list mn-y mx-y lg-y)) (second srcs))
204-
(match-define (cons z (list mn-z mx-z lg-z)) output)
202+
(match-define (list x mn-x mx-x lg-x) (first srcs))
203+
(match-define (list y _ mx-y _) (second srcs))
204+
(match-define (list z _ _ lg-z) output)
205205

206206
; when output crosses zero and x is negative - means that y was fractional and not fixed (specific of Rival)
207207
; solution - add more slack for y to converge
@@ -231,8 +231,8 @@
231231
; Γ[exp & exp2]'x = |x| & |x*ln(2)|
232232
; ↑ampl[exp & exp2]'x = maxlog(x) + logspan(z)
233233
; ↓ampl[exp & exp2]'x = minlog(x)
234-
(match-define (cons x (list mn-x mx-x lg-x)) (first srcs))
235-
(match-define (cons z (list mn-z mx-z lg-z)) output)
234+
(match-define (list x _ mx-x _) (first srcs))
235+
(match-define (list _ _ _ lg-z) output)
236236

237237
(if (*lower-bound-early-stopping*)
238238
(list (cons (+ mx-x lg-z) (minlog x #:less-slack #t)))
@@ -242,8 +242,8 @@
242242
; Γ[tan]'x = |x / (cos(x) * sin(x))|
243243
; ↑ampl[tan]'x = maxlog(x) + max(|minlog(z)|,|maxlog(z)|) + logspan(z) + 1
244244
; ↓ampl[tan]'x = minlog(x) + min(|minlog(z)|,|maxlog(z)|) - 1
245-
(match-define (cons x (list mn-x mx-x lg-x)) (first srcs))
246-
(match-define (cons z (list mn-z mx-z lg-z)) output)
245+
(match-define (list x _ mx-x _) (first srcs))
246+
(match-define (list z mn-z mx-z lg-z) output)
247247

248248
(if (*lower-bound-early-stopping*)
249249
(list (cons (+ mx-x (max (abs mx-z) (abs mn-z)) lg-z 1)
@@ -257,8 +257,8 @@
257257
; ↑ampl[sin]'x = maxlog(x) - minlog(z)
258258
; ↓ampl[sin]'x = | - maxlog(z) - 1, if maxlog(x) > 1
259259
; | 0 else
260-
(match-define (cons x (list mn-x mx-x lg-x)) (first srcs))
261-
(match-define (cons z (list mn-z mx-z lg-z)) output)
260+
(match-define (list _ _ mx-x _) (first srcs))
261+
(match-define (list z mn-z _ _) output)
262262

263263
(if (*lower-bound-early-stopping*)
264264
(list (cons (- mx-x mn-z)
@@ -271,8 +271,8 @@
271271
; Γ[cos]'x = |x * sin(x) / cos(x)|
272272
; ↑ampl[cos]'x = maxlog(x) - minlog(z) + min(maxlog(x), 0)
273273
; ↓ampl[cos]'x = - maxlog(x) - 2
274-
(match-define (cons x (list mn-x mx-x lg-x)) (first srcs))
275-
(match-define (cons z (list mn-z mx-z lg-z)) output)
274+
(match-define (list _ _ mx-x _) (first srcs))
275+
(match-define (list z mn-z _ _) output)
276276

277277
(if (*lower-bound-early-stopping*)
278278
(list (cons (+ (- mx-x mn-z) (min mx-x 0)) (- (- 2) (maxlog z #:less-slack #t))))
@@ -282,8 +282,8 @@
282282
; Γ[sinh]'x = |x * cosh(x) / sinh(x)|
283283
; ↑ampl[sinh]'x = maxlog(x) + logspan(z) - min(minlog(x), 0)
284284
; ↓ampl[sinh]'x = max(0, minlog(x))
285-
(match-define (cons x (list mn-x mx-x lg-x)) (first srcs))
286-
(match-define (cons z (list mn-z mx-z lg-z)) output)
285+
(match-define (list x mn-x mx-x _) (first srcs))
286+
(match-define (list _ _ _ lg-z) output)
287287

288288
(if (*lower-bound-early-stopping*)
289289
(list (cons (- (+ mx-x lg-z) (min mn-x 0)) (max 0 (minlog x #:less-slack #t))))
@@ -293,8 +293,8 @@
293293
; Γ[cosh]'x = |x * sinh(x) / cosh(x)|
294294
; ↑ampl[cosh]'x = maxlog(x) + logspan(z) + min(maxlog(x), 0)
295295
; ↓ampl[cosh]'x = max(0, minlog(x) - 1)
296-
(match-define (cons x (list mn-x mx-x lg-x)) (first srcs))
297-
(match-define (cons z (list mn-z mx-z lg-z)) output)
296+
(match-define (list x _ mx-x lg-x) (first srcs))
297+
(match-define (list _ _ _ lg-z) output)
298298

299299
(if (*lower-bound-early-stopping*)
300300
(list (cons (+ mx-x lg-z (min mx-x 0)) (max 0 (- (minlog x #:less-slack #t) 1))))
@@ -304,8 +304,8 @@
304304
; Γ[log & log2 & log10]'x = |1 / ln(x)| & |ln(2) / ln(x)| & |ln(10) / ln(x)|
305305
; ↑ampl[log & log2 & log10]'x = logspan(x) - minlog(z) + 1
306306
; ↓ampl[log & log2 & log10]'x = - maxlog(z)
307-
(match-define (cons x (list mn-x mx-x lg-x)) (first srcs))
308-
(match-define (cons z (list mn-z mx-z lg-z)) output)
307+
(match-define (list _ _ _ lg-x) (first srcs))
308+
(match-define (list z mn-z _ _) output)
309309

310310
(if (*lower-bound-early-stopping*)
311311
(list (cons (+ (- lg-x mn-z) 1) (- (maxlog z #:less-slack #t))))
@@ -316,7 +316,7 @@
316316
; ↑ampl[asin]'x = | slack, if maxlog(z) > 1
317317
; | 1 else
318318
; ↓ampl[asin]'x = 0
319-
(match-define (cons z (list mn-z mx-z lg-z)) output)
319+
(match-define (list _ _ mx-z _) output)
320320

321321
(list (if (>= mx-z 1)
322322
(cons (get-slack) 0) ; assumes that log[1-x^2]/2 is equal to slack
@@ -327,7 +327,7 @@
327327
; ↑ampl[acos]'x = | slack, if maxlog(x) >= 0
328328
; | 0 else
329329
; ↓ampl[acos]'x = 0
330-
(match-define (cons x (list mn-x mx-x lg-x)) (first srcs))
330+
(match-define (list _ _ mx-x _) (first srcs))
331331

332332
(list (if (>= mx-x 0)
333333
(cons (get-slack) 0) ; assumes that log[1-x^2]/2 is equal to slack
@@ -337,8 +337,8 @@
337337
; Γ[atan]'x = | x / ((1+x^2) * arctan(x))|
338338
; ↑ampl[atan]'x = - min(|minlog(x)|, |maxlog(x)|) - minlog(z) + logspan(x)
339339
; ↓ampl[atan]'x = - max(|minlog(x)|, |maxlog(x)|) - maxlog(z) - 2
340-
(match-define (cons x (list mn-x mx-x lg-x)) (first srcs))
341-
(match-define (cons z (list mn-z mx-z lg-z)) output)
340+
(match-define (list x mn-x mx-x lg-x) (first srcs))
341+
(match-define (list z mn-z _ _) output)
342342

343343
(if (*lower-bound-early-stopping*)
344344
(list (cons (- lg-x (min (abs mn-x) (abs mx-x)) mn-z)
@@ -356,9 +356,9 @@
356356
; Γ[mod]'y ` |y/mod(x,y)|
357357
; ↑ampl[mod]'y = maxlog(y) - minlog(z)
358358
; ↓ampl[mod]'y = minlog(y) - maxlog(z) or just 0
359-
(match-define (cons x (list mn-x mx-x lg-x)) (first srcs))
360-
(match-define (cons y (list mn-y mx-y lg-y)) (second srcs))
361-
(match-define (cons z (list mn-z mx-z lg-z)) output)
359+
(match-define (list _ _ mx-x _) (first srcs))
360+
(match-define (list y _ _ _) (second srcs))
361+
(match-define (list _ mn-z _ _) output)
362362

363363
(define slack
364364
(if (crosses-zero? y)
@@ -374,8 +374,8 @@
374374
; ↑ampl[log1p]'x = | maxlog(x) - minlog(z) + slack, if x is negative
375375
; | maxlog(x) - minlog(z), else
376376
; ↓ampl[log1p]'x = 0
377-
(match-define (cons x (list mn-x mx-x lg-x)) (first srcs))
378-
(match-define (cons z (list mn-z mx-z lg-z)) output)
377+
(match-define (list x _ mx-x _) (first srcs))
378+
(match-define (list _ mn-z _ _) output)
379379

380380
(define xhi (ival-hi x))
381381
(define xlo (ival-lo x))
@@ -389,18 +389,18 @@
389389
; Γ[expm1]'x = |x * e^x / expm1|
390390
; ↑ampl[expm1]'x = max(1 + maxlog(x), 1 + maxlog(x) - minlog(z))
391391
; ↓ampl[expm1]'x = 0
392-
(match-define (cons x (list mn-x mx-x lg-x)) (first srcs))
393-
(match-define (cons z (list mn-z mx-z lg-z)) output)
392+
(match-define (list _ _ mx-x _) (first srcs))
393+
(match-define (list _ mn-z _ _) output)
394394

395395
(list (cons (max (+ 1 mx-x) (+ 1 (- mx-x mn-z))) 0))]
396396

397397
[(ival-atan2)
398398
; Γ[atan2]'x = Γ[atan2]'y = |xy / ((x^2 + y^2)*arctan(y/x))|
399399
; ↑ampl[expm1]'x = maxlog(x) + maxlog(y) - 2*min(minlog(x), minlog(y)) - minlog(z)
400400
; ↓ampl[expm1]'x = minlog(x) + minlog(y) - 2*max(maxlog(x), maxlog(y)) - maxlog(z)
401-
(match-define (cons x (list mn-x mx-x lg-x)) (first srcs))
402-
(match-define (cons y (list mn-y mx-y lg-y)) (second srcs))
403-
(match-define (cons z (list mn-z mx-z lg-z)) output)
401+
(match-define (list x mn-x mx-x _) (first srcs))
402+
(match-define (list y mn-y mx-y _) (second srcs))
403+
(match-define (list z mn-z _ _) output)
404404

405405
(if (*lower-bound-early-stopping*)
406406
(make-list 2
@@ -414,8 +414,8 @@
414414
; Γ[tanh]'x = |x / (sinh(x) * cosh(x))|
415415
; ↑ampl[tanh]'x = logspan(z) + logspan(x)
416416
; ↓ampl[tanh]'x = 0
417-
(match-define (cons x (list mn-x mx-x lg-x)) (first srcs))
418-
(match-define (cons z (list mn-z mx-z lg-z)) output)
417+
(match-define (list _ _ _ lg-x) (first srcs))
418+
(match-define (list _ _ _ lg-z) output)
419419

420420
(list (cons (+ lg-z lg-x) 0))]
421421

@@ -424,7 +424,7 @@
424424
; ↑ampl[atanh]'x = | 1, if x < 0.5
425425
; | slack
426426
; ↓ampl[atanh]'x = 0
427-
(match-define (cons x (list mn-x mx-x lg-x)) (first srcs))
427+
(match-define (list _ _ mx-x _) (first srcs))
428428
(list (if (>= mx-x 1)
429429
(cons (get-slack) 0)
430430
(cons 1 0)))]
@@ -434,7 +434,7 @@
434434
; ↑ampl[acosh]'x = | -minlog(z) + slack, if minlog(z) < 2
435435
; | 0
436436
; ↓ampl[acosh]'x = 0
437-
(match-define (cons z (list mn-z mx-z lg-z)) output)
437+
(match-define (list _ mn-z _ _) output)
438438
(list (if (< mn-z 2) ; when acosh(x) < 1
439439
(cons (- (get-slack) mn-z) 0)
440440
(cons 0 0)))]
@@ -443,7 +443,7 @@
443443
; Γ[acosh]'x = |2 x x* / x^2|
444444
; ↑ampl[pow2]'x = logspan(x) + 1
445445
; ↓ampl[pow2]'x = 0
446-
(match-define (cons x (list mn-x mx-x lg-x)) (first srcs))
446+
(match-define (list _ _ _ lg-x) (first srcs))
447447
(list (cons (+ lg-x 1) 0))]
448448

449449
; TODO

0 commit comments

Comments
 (0)