Skip to content

Commit af69837

Browse files
authored
Merge pull request #136 from herbie-fp/pre-fabs
Implement `ival-pre-fabs`
2 parents 26336b4 + ee4a397 commit af69837

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

mpfr.rkt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@
8585

8686
(define mpfr-abs! (get-mpfr-fun 'mpfr_abs (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
8787

88+
(define mpfr-cmpabs (get-mpfr-fun 'mpfr_cmpabs (_fun _mpfr-pointer _mpfr-pointer -> _int)))
89+
8890
(define mpfr-asin! (get-mpfr-fun 'mpfr_asin (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
8991

9092
(define mpfr-acos! (get-mpfr-fun 'mpfr_acos (_fun _mpfr-pointer _mpfr-pointer _rnd_t -> _int)))
@@ -318,6 +320,7 @@
318320
mpfr-cbrt!
319321
mpfr-neg!
320322
mpfr-abs!
323+
mpfr-cmpabs
321324
mpfr-asin!
322325
mpfr-acos!
323326
mpfr-atan!

ops/arith.rkt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@
159159
(define (ival-hypot! out x y)
160160
(define err? (or (ival-err? x) (ival-err? y)))
161161
(define err (or (ival-err x) (ival-err y)))
162-
(match-define (ival (endpoint xlo xlo!) (endpoint xhi xhi!) _ _) (ival-exact-fabs x))
163-
(match-define (ival (endpoint ylo ylo!) (endpoint yhi yhi!) _ _) (ival-exact-fabs y))
162+
(match-define (ival (endpoint xlo xlo!) (endpoint xhi xhi!) _ _) (ival-pre-fabs x))
163+
(match-define (ival (endpoint ylo ylo!) (endpoint yhi yhi!) _ _) (ival-pre-fabs y))
164164
(define-values (lo lo!) (eplinear! (ival-lo-val out) mpfr-hypot! xlo xlo! ylo ylo! 'down))
165165
(define-values (hi hi!) (eplinear! (ival-hi-val out) mpfr-hypot! xhi xhi! yhi yhi! 'up))
166166
(ival (endpoint lo lo!) (endpoint hi hi!) err? err))

ops/core.rkt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
ival-max-prec
2020
ival-exact-neg
2121
ival-exact-fabs
22+
ival-pre-fabs
2223
bf-return-exact?
2324
ival-lo-fixed?
2425
ival-hi-fixed?
@@ -385,6 +386,17 @@
385386
(define abs-hi (epunary! tmp2 mpfr-abs! (ival-hi x) 'up))
386387
(ival (endpoint (bf 0) (and xlo! xhi!)) (endpoint-max2 abs-lo abs-hi 'up) xerr? xerr)]))
387388

389+
(define (ival-pre-fabs x)
390+
(match-define (ival xlo xhi xerr? xerr) x)
391+
(match (classify-ival x)
392+
[1 x]
393+
[-1 (ival xhi xlo xerr? xerr)]
394+
[0
395+
(ival (endpoint 0.bf (and (endpoint-immovable? xlo) (endpoint-immovable? xhi)))
396+
(if (> (mpfr-cmpabs (endpoint-val xlo) (endpoint-val xhi)) 0) xlo xhi)
397+
xerr?
398+
xerr)]))
399+
388400
;; These functions execute ival-fabs and ival-neg with input's precision
389401
(define (ival-max-prec x)
390402
(max (bigfloat-precision (ival-lo-val x)) (bigfloat-precision (ival-hi-val x))))

ops/pow.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
(ival-then (ival-assert ival-maybe) (ival-union (ival-neg pospow) pospow)))))
154154

155155
(define (ival-pow2 x)
156-
((monotonic->ival (lambda (x) (bfmul x x))) (ival-exact-fabs x)))
156+
((monotonic->ival (lambda (x) (bfmul x x))) (ival-pre-fabs x)))
157157

158158
(define (ival-pow x y)
159159
(cond

0 commit comments

Comments
 (0)