Skip to content

Commit af06bc2

Browse files
authored
Merge pull request #125 from herbie-fp/codex/inline-epfn-for-boolean-functions
Inline epfn for boolean comparisons
2 parents ec901a6 + 2d1dbcb commit af06bc2

File tree

1 file changed

+38
-20
lines changed

1 file changed

+38
-20
lines changed

ops/core.rkt

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,6 @@
138138
(define (mk-ival x)
139139
(mk-big-ival x x))
140140

141-
(define (and-fn . as)
142-
(andmap identity as))
143-
(define (or-fn . as)
144-
(ormap identity as))
145-
146141
(define (ival-pi)
147142
(define-values (lo hi) (make-endpoint-pair))
148143
(mpfr-const-pi! lo 'down)
@@ -214,10 +209,12 @@
214209
(or (ival-err? x) (ival-err? y))
215210
(and (ival-err x) (ival-err y)))]
216211
[(boolean? (ival-lo-val x))
217-
(ival (epfn and-fn (ival-lo x) (ival-lo y))
218-
(epfn or-fn (ival-hi x) (ival-hi y))
219-
(or (ival-err? x) (ival-err? y))
220-
(and (ival-err x) (ival-err y)))]))
212+
(match-define (ival (endpoint xlo xlo!) (endpoint xhi xhi!) xerr? xerr) x)
213+
(match-define (ival (endpoint ylo ylo!) (endpoint yhi yhi!) yerr? yerr) y)
214+
(ival (endpoint (and xlo ylo) (and xlo! ylo!))
215+
(endpoint (or xhi yhi) (and xhi! yhi!))
216+
(or xerr? yerr?)
217+
(and xerr yerr))]))
221218

222219
;; This function computes and propagates the immovable? flag for endpoints
223220
(define (epfn op . args)
@@ -393,7 +390,8 @@
393390
(ormap ival-err as)))
394391

395392
(define (ival-not x)
396-
(ival (epfn not (ival-hi x)) (epfn not (ival-lo x)) (ival-err? x) (ival-err x)))
393+
(match-define (ival (endpoint xlo xlo!) (endpoint xhi xhi!) xerr? xerr) x)
394+
(ival (endpoint (not xhi) xhi!) (endpoint (not xlo) xlo!) xerr? xerr))
397395

398396
(define* ival-asin (compose (monotonic-mpfr mpfr-asin!) (clamp -1.bf 1.bf)))
399397
(define* ival-acos (compose (comonotonic-mpfr mpfr-acos!) (clamp -1.bf 1.bf)))
@@ -446,10 +444,12 @@
446444
(define* ival-erfc (comonotonic-mpfr mpfr-erfc!))
447445

448446
(define (ival-cmp x y)
449-
(define can-< (epfn bflt? (ival-lo x) (ival-hi y)))
450-
(define must-< (epfn bflt? (ival-hi x) (ival-lo y)))
451-
(define can-> (epfn bfgt? (ival-hi x) (ival-lo y)))
452-
(define must-> (epfn bfgt? (ival-lo x) (ival-hi y)))
447+
(match-define (ival (endpoint xlo xlo!) (endpoint xhi xhi!) _ _) x)
448+
(match-define (ival (endpoint ylo ylo!) (endpoint yhi yhi!) _ _) y)
449+
(define can-< (endpoint (bflt? xlo yhi) (and xlo! yhi!)))
450+
(define must-< (endpoint (bflt? xhi ylo) (and xhi! ylo!)))
451+
(define can-> (endpoint (bfgt? xhi ylo) (and xhi! ylo!)))
452+
(define must-> (endpoint (bfgt? xlo yhi) (and xlo! yhi!)))
453453
(values can-< must-< can-> must->))
454454

455455
(define (ival-<2 x y)
@@ -458,20 +458,34 @@
458458

459459
(define (ival-<=2 x y)
460460
(define-values (c< m< c> m>) (ival-cmp x y))
461-
(ival (epfn not c>) (epfn not m>) (or (ival-err? x) (ival-err? y)) (or (ival-err x) (ival-err y))))
461+
(match-define (endpoint c>-val c>!) c>)
462+
(match-define (endpoint m>-val m>!) m>)
463+
(ival (endpoint (not c>-val) c>!)
464+
(endpoint (not m>-val) m>!)
465+
(or (ival-err? x) (ival-err? y))
466+
(or (ival-err x) (ival-err y))))
462467

463468
(define (ival->2 x y)
464469
(define-values (c< m< c> m>) (ival-cmp x y))
465470
(ival m> c> (or (ival-err? x) (ival-err? y)) (or (ival-err x) (ival-err y))))
466471

467472
(define (ival->=2 x y)
468473
(define-values (c< m< c> m>) (ival-cmp x y))
469-
(ival (epfn not c<) (epfn not m<) (or (ival-err? x) (ival-err? y)) (or (ival-err x) (ival-err y))))
474+
(match-define (endpoint c<-val c<!) c<)
475+
(match-define (endpoint m<-val m<!) m<)
476+
(ival (endpoint (not c<-val) c<!)
477+
(endpoint (not m<-val) m<!)
478+
(or (ival-err? x) (ival-err? y))
479+
(or (ival-err x) (ival-err y))))
470480

471481
(define (ival-==2 x y)
472482
(define-values (c< m< c> m>) (ival-cmp x y))
473-
(ival (epfn and-fn (epfn not c<) (epfn not c>))
474-
(epfn and-fn (epfn not m<) (epfn not m>))
483+
(match-define (endpoint c<-val c<!) c<)
484+
(match-define (endpoint c>-val c>!) c>)
485+
(match-define (endpoint m<-val m<!) m<)
486+
(match-define (endpoint m>-val m>!) m>)
487+
(ival (endpoint (and (not c<-val) (not c>-val)) (and c<! c>!))
488+
(endpoint (and (not m<-val) (not m>-val)) (and m<! m>!))
475489
(or (ival-err? x) (ival-err? y))
476490
(or (ival-err x) (ival-err y))))
477491

@@ -491,8 +505,12 @@
491505

492506
(define (ival-!=2 x y)
493507
(define-values (c< m< c> m>) (ival-cmp x y))
494-
(ival (epfn or-fn m< m>)
495-
(epfn or-fn c< c>)
508+
(match-define (endpoint m<-val m<!) m<)
509+
(match-define (endpoint m>-val m>!) m>)
510+
(match-define (endpoint c<-val c<!) c<)
511+
(match-define (endpoint c>-val c>!) c>)
512+
(ival (endpoint (or m<-val m>-val) (and m<! m>!))
513+
(endpoint (or c<-val c>-val) (and c<! c>!))
496514
(or (ival-err? x) (ival-err? y))
497515
(or (ival-err x) (ival-err y))))
498516

0 commit comments

Comments
 (0)