Automatic Generation of SS tag ed Geometric PP red icates Aleksandar Nanevski, G u y B lello c h and R o b ert H arp er PSCICO project h ttp: / / w w w. cs. cm u. ed u / ~ ps ci co
Geometric Predicates P r og r a m s need t o t es t r ela t ive p os it ions of p oint s b a s ed on t heir coor d ina t es. S im p le exa m p les ( in 2 D) : Orientation test (in convex hull) does C lie left/right/on the line AB? C B A I nc irc l e test (in Dela una y t r ia ng ula t ion) does D lie in/ou t/on the c irc le ABC? A D B C 2
Geometric Predicates (cont d) O r i e n t a t i o n ( A, B, C ) = W e o n l y c o n s i d e r t h e sign o f a n e x p r e s s i o n i n v o l v i n g +, - a n d 3
Convex Hull Miscomputed U s i n g O r i e n t a t i o n P r e d i c a t e i n e x a c t a r i t h m e t i c B U s i n g O r i e n t a t i o n P r e d i c a t e i n f l o a t i n g -p o i n t a r i t h m e t i c B C D C D E E A A!! F l o a t i n g p o i n t a r i t h m e t i c e r r s i n c o n s i s t e n t l y!! 4
Floating-p p oint Filte rr ss G e t correct sign (-1, 0 o r 1 ) o f a n e x a c t e x p r e s s i o n E u s i n g f l o a t i n g -p o i n t! filters out the ea sy c a ses let F = E (X) in floating point if F > e r r or b ou nd th en 1 els e if F > er r o r b o u nd th en 1 els e inc r e as e pr e c is ion a nd r ep ea t o r s w itc h to ex a c t a r ith m etic I f th e c o r r ec t r es u lt is 0, m u s t g o to ex a c t p h a s e 5
Current Methods I n t e r v a l a r i t h m e t i c ( R. E. M o o r e, U. K u l i s c h ) +l i t t l e m o d i f i c a t i o n t o t h e s o u r c e p r o g r a m -d o e s n o t p e r f o r m w e l l i n l o w d e g r e e s S i n g l e -p h a s e f i l t e r s ( F o r t u n e & V a n W y k, L E D A ) +c o m p i l e r a v a i l a b l e -s u r p a s s e d b y m u l t i -p h a s e f i l t e r s 6
Current Methods (cont d) M u l t i -p h a s e f i l t e r s ( J. S h e w c h u k ) f o u r p h a s e s o f i n c r e a s i n g p r e c i s i o n +r e -u s e o f r e s u l t s f r o m e a r l i e r p h a s e s +b e t w e e n 8-3 5 % s l o w e r t h a n f p i m p l e m e n t a t i o n -r e q u i r e s e s t i m a t i n g r o u n d i n g e r r o r s -i n v o l v e d a n d h a r d t o i m p l e m e n t : S h e w c h u k s I n s p h e r e C i m p l e m e n t a t i o n > 5 0 0 l i n e s o f c o d e -o n l y e x i s t i n g a r e 2 D a n d 3 D O r i e n t a t i o n a n d I n s p h e r e p r e d i c a t e s, b u t w e n e e d m o r e 7
Functional p r og r am COMPILER num e r ical analy s is S tag e d g e om p r e d icate e tr ic 8
Contributions S o u r c e l a n g u a g e e x p r e s s i o n i n v o l v i n g +, -, a r i t h m e t i c t o b e p e r c e i v e d a s e x a c t n e s t e d a n o n y m o u s f u n c t i o n s ( s t a g i n g ) C o m p i l e r t o M L ( o r C ) m u l t i -p h a s e t a r g e t c o d e e r r o r b o u n d s e s t i m a t e d a u t o m a t i c a l l y f o r m a l l y s p e c i f i e d ( s o, c a n a r g u e c o r r e c t n e s s ) 9
Staging T o t e s t a s e t o f p o i n t s f o r p o s i t i o n w i t h r e s p e c t t o l i n e A C : -c o m p u t e a s m u c h a s p o s s i b l e k n o w i n g o n l y A a n d C -r e t u r n t h e r e m a i n i n g w o r k a s f u n c t i o n -a p p l y t h i s f u n c t i o n t o e a c h o f t h e p o i n t s A C 10
Overview I n t r o d u c t i o n A r b i t r a r y p r e c i s i o n a r i t h m c o m p u t i n g i n p h a s e s S t a g e c o m p i l a t i o n P e r f o r m a n c e C o n c l u s i o n s a n d F u t u r e W e t i c a n d o r k 11
Notation O p e r a t i o n s : EE xx aa cc tt + - FF ll oo aa tt ii nn g g pp oo ii nn tt ª P r e c i s i o n p = # b i t s i n m M a c h i n e E p s i l o n a n t i s s a - i f o p e r a t i o n r e s u l t i s 1. 0, t h e n t h e r o u n d i n g e r r o r i s s m a l l e r t h a n - 12
Error recovery Theorem. (Knuth) If x = a b, t h e r o u n d i n g e r r o r o f x c a n b e r e c o v e r e d b y fl o a t i n g -p o i n t o p e r a t i o n s. If i s t h e r o u n d i n g e r r o r, a n d c = x ª a t h e n S o e x a c t l y. 13
How to Increase Precision? K n u t h s t h e o r e m : P a i r ( x, ) t w i c e t h e p r e c i s i o n. ( D. P r i e s t ) S o r t e d l i s t o f f p -n u m b e r s a r b i t r a r y p r e c i s i o n. E x p a n s i o n = l i s t o f m a g n i t u d e d e c r e a s i n g n o n - o v e r l a p p i n g f l o a t s 1. 0 1 0 1 0 2 120 1. 1 0 1 0 0 2 6 7 + 1. 0 0 1 1 1 2 10 1 0 1 0 1 0 0 0 0 0 0... 0 1 1 0 1 0 0 0 0 0 0 0 0............. 0 0 1 0 0 1 1 1 0 0 0 0 14
Phases and re-u u se ( J. S h e w c h u k ) A r bi t r a r y p r e c i s i o n a r i t h m e t i c p h a s e s c a n r e u s e t h e r e s u l t s o f t h e i r p r e d e c e s s o r s. E x a m p l e : L e t a 1 b 1 = x 1 + 1 a n d a 2 b 2 = x 2 + 2. E x p a n d E a s 15
Phases and re-u u se (( cc oo nt d) S t r a t e g y f o r f i n d i n g t h e sign o f E v a l u a t e E i n p h a s e s, i n c r e a s i n g p r e c i s i o n o n d e m a n d. E x a m p l e : floating point phase ex ac t phase 16
Reusing results A x1 x 1 x 2 x 2 17
Reusing results A B x1 x 1 x 2 x 2 18
Reusing results C A B x1 x 1 x 2 x 2 2x 1 1 2x 2 2 19
Reusing results D C A B x1 x 1 x 2 x 2 2x 1 1 2x 2 2 1 1 2 2 20
Overview I n t r o d u c t i o n A r b i t r a r y p r e c i s i o n a r i t h m c o m p u t i n g i n p h a s e s S t a g e c o m p i l a t i o n P e r f o r m a n c e C o n c l u s i o n a n d F u t u r e W e t i c a n d o r k 21
Source Language B a s i c a r i t h m e t i c o p e r a t i o n s : +, -,, u n a r y -, s q A s s i g n m e n t s : v a l x = s o m e e x p r e s s i o n N e s t e d a n o n y m o u s f u n c t i o n s E x a m p l e : I m p l i c i t s i g n t e s t a t t h e l a s t a s s i g n m e n t. 22
Stage compilation E v e r y s t a g e c o m p i l e d i n t o : -f l o a t i n g -p o i n t c o d e ( p h a s e A ) -c o d e f o r e s t i m a t i n g t h e r o u n d i n g e r r o r L a t e r p h a s e s ( B, C a n d D ) s u s p e n d e d u n t i l e x p l i c i t l y e x e c u t e d 23
Stage 1 H e l p e r c o d e : - -m o d u l e s f o r a r b i t r a r y p r e c i s i o n a r i t h m e t i c C o m p i l e r o u t p u t : A C B D 24
Stage 2 A C B D 25
Overview I n t r o d u c t i o n A r b i t r a r y p r e c i s i o n a r i t h m c o m p u t i n g i n p h a s e s S t a g e c o m p i l a t i o n P e r f o r m a n c e C o n c l u s i o n a n d F u t u r e W e t i c a n d o r k 26
Performance E x p e r i m e n t s t o m a t c h S h e w c h u k s h a n d -g e n e r a t e d p r e d i c a t e s T a r g e t l a n g u a g e C N o s t a g i n g! OO rr ii ee nn tt 22 DD OO rr ii ee nn tt 33 DD II nn CC ii rr cc ll ee II nn SS pp hh ee rr ee UU nn ii ff oo rr m m RR aa nn dd oo m m PP oo ii nn t t DD ii ss tt rr ii bb uu tt ii oo nn S h e w c h u k s v e r s i o n 0. 2 0 8 m s 0. 7 0 7 m s 6. 4 4 0 m s 1 6. 4 3 0 m s A u t o m a t i c a l l y g e n e r a t e d v e r s i o n 0. 2 4 9 m s 0. 7 7 2 m s 5. 6 0 0 m s 3 9. 2 2 0 m s S l o w d o w n 1. 2 0 1. 0 9 0. 8 7 2. 3 9 27
Performance (cont d) uu nn ii ff oo rr m m rr aa nn dd oo mm tt ii ll tt ee d d gg rr ii dd 2D De ll aa uu nn aa y y TT rr ii aa nn gg uu ll aa tt ii oo nn S h e w c h u k s v e r s i o n 1 1 8 7 m s 20 6 0 m s A u t o m a t i c a l l y g e n e r a t e d v e r s i o n 1 4 1 0 m s 3 6 7 8 m s S l o w d o w n 1. 1 9 1. 7 8 cc oo -c c ii rr cc uu ll aa rr 1 1 9 0 m s 1 5 7 8 m s 1. 3 3 B e t w e e n 1 a n d 2. 4 t i m e s s l o w e r t h a n h a n d -g e n e r a t e d c o d e P o s s i b i l i t y f o r i m p r o v e m e n t w i t h a b e t t e r t r a n s l a t i o n t o C 28
Future Work E x t e n d t h e l a n g u a g e : -m u l t i p l i c a t i o n b y 2 n d o e s n t i n t r o d u c e e r r o r s. -s u m m a t i o n o f m o r e t h a n 2 e l e m e n t s c a n b e d o n e q u i c k e r. E v a l u a t e t h e e f f e c t s o f s t a g i n g. A l l o w b e t t e r c o n t r o l o v e r t h e F P a r i t h m e t i c i n S M L. -p r o c e s s o r f l a g s -p r e c i s i o n o f i n t e r n a l F P r e g i s t e r s D e s i g n a l a n g u a g e t h a t c a n c o m p i l e a n d run t h e p r e d i c a t e s -r u n -t i m e c o d e g e n e r a t i o n a n d m e t a -p r o g r a m m i n g -a p p l i c a t i o n : r o b u s t s o l i d m o d e l e r 29
Conclusions Automated conversion o f e x a c t p r e d i c a t e s i n t o f l o a t i n g p o i n t c o d e : p r e s e r v e s c o r r e c t n e s s o f c o m c o m p i l e -t i m e e r r o r a n a l y s i s n e s t e d a n o n y m o u s f u n c t i o n s ( s t a g i n g ) p e r f o r m c o d e p u t a t i o n d e s p i t e r o u n d i n g a n c e o f g e n e r a t e d p r e d i c a t e s c l o s e t o h a n d -g e n e r a t e d F ormal sp ecif ication o f t h e c o m p i l e r. M ore control n e e d e d o v e r f l o a t i n g p o i n t i n S M L. -p r o c e s s o r f l a g s -p r e c i s i o n o f i n t e r n a l F P r e g i s t e r s C omp il er c a n b e f o u n d a t h t t p : / / w w w.c s.c m u.e d u / ~ p s c i c o 30
Operations on Expansions (arbitrary precision arithmetic) Example: Adding two expansions E = e1 e3 e5 F = e e 2 4 Merge Lists e e 1 2 e e e 3 4 5 H = h h h h4 h 5 1 2 3 31
Source Language B a s i c a r i t h m e t i c o p e r a t i o n s : +, -,, s q, u n a r y A s s i g n m e n t s : v a l x = s o m e e x p r e s s i o n N e s t e d a n o n y m o u s f u n c t i o n s ( s t a g i n g ) 32
Compiling a Program C A D B 33
Compiling a Program (cont d) A reused 34
Here goes example for OO ri en tt 22 D D ii n n SS MM L L or CC Nothing! They are just often mistaken for real numbers. type pnt=real*real f u n o ri ent( A : pnt, B : pnt, C : pnt) = let v al ( a1, a2 ) = A v al ( b 1, b 2 ) = B v al ( c 1, c 2 ) = C v al d = ( c 1 -a1 ) *( c 2 -b 2 ) - ( c 2 -a2 ) *( c 1 -b 1 ) i n i f d > 0 th en L ef t els e i f d < 0 th en R i g h t els e O n end 35
What s Wrong With Floating PP oint NN uu mm bb ee rs? Floats are unevenly distributed on the real axis, thus introduc ing rounding errors in the c om p utation. 36
What s Wrong? (cont d) Example: Assume precision of 2 decimal digits. Denote th e operation of ( rounded) addition on floats. F lo at s ar e a subset b u t N O T a subty p e o f r at i o n als an d r eals. 37
+ - Types of Numbers Real numbers Usually assumed when dev elo p i ng o r p r o v i ng alg o r i t hms B asi c o p er at i o ns no t c o mp ut ab le + - Rat i o nal numbers C lo sed under b asi c o p er at i o ns S lo w + - - F lo at i ng -p o i nt numbers F ast N o t c lo sed under b asi c ar i t hmet i c o p er at i o ns ( r o undi ng er r o r s) D o no t sat i sf y usual laws li k e asso c i at i v i t y, di st r i b ut i v i t y 38
Phase A Compilation J u d g m e n t. S e l e c t e d r u l e s 39
Phase B Compilation J u d g m e n t. S e l e c t e d r u l e s 40
Operations on Expansions Example: Adding a double to an expansion. e1 e2 e3 e4 sum sum sum sum roundoff roundoff roundoff roundoff b h 1 h 2 h 3 h 4 h 5 41
Overlapping and Machine ε p b i t s 10101010 000.10 p b i t s 10001010 01011 10101010 000.10..10001010 01011 a n d x d o n o t o v e r l a p 2 -p x. I n I E E E d o u b l e p r e c i s i o n, p= 5 3. 42
Phases of Increasing Precision let R = F (X) in floating point if ab s (R) > estimated error bound th en s ign(r) els e inc rease p rec ision and r epeat or s w itc h to ex ac t ar ith m etic floating point phase inter m ed iate phases ex ac t phase F initel y many intermediate p h ases! 43
Arithmetic R ational nu m + e x a c t - s l o w b e r s Floating-p oint nu m b e r s - i n e x a c t + f a s t 44
Non-ov ov ee rr ll aa pp pp ii ng T h e r o u n d e d s u m x a n d i t s r o u n d i n g e r r o r d o n o t o v e r l a p. 1 01 01 01 0 000.1 0 0000 0.00 0000 000 1 0001 01 0 01 01 1 1 01 01 01 0 000.1 0.. 1 0001 01 0 01 01 1 M a t h e m a t i c a l l y, 45
m Expansions and Compiler Exact e xp r e s s i o n F c om p i l a t i on filters out the ea sy c a ses let R = F (X) in floating point if R > error bound th en 1 els e if R > er r or b ou nd th en 1 els e i nc rea s e p rec i s i on and r epeat or s w itc h to ex ac t ar ith m etic U s e ex pans ions for or e pr ec is e c om pu tations 46
Phases and Compiler Exact e xp r e s s i o n F let R = F (X) in floating point if R > error bound th en 1 els e if R > er r or b ou nd th en 1 els e R2 = ph as e B if ab s (R2 ) > er r or b ou nd th en s ign(r2 ) els e R3 = ph as e C if ab s (R3 ) > er r or b ou nd th en s ign(r3 ) els e c om p i l a t i on R4 = ph as e D ; (* ex ac t ph as e * ) s ign (R4 ) 47
Bounding the Rounding Error F l o a t i n g -p o i n t o p e r a t i o n s a r e c o r r e c t l y r o u n d e d. C o n s e q u e n c e : f o r a n y o p e r a t i o n 2 { +, -, } N o t i c e : ε i s s t a t i c p a r t o f t h e e r r o r w h i l e x y i s d y n a m i c. 48
Bounding Error of Composite Ex pressions I f X 1 = x 1 δ 1 p 1 a n d X 2 = x 2 δ 2 p 2 t h e n 49
Error Bounds and CC om pp ii ll at ii on S t a t i c e r r o r b o u n d -O b t a i n e d E X A C T L Y i n c o m p i l e -t i m e. -R o u n d e d a n d t h e n e m i t t e d i n t o t h e t a r g e t p r o g r a m a s a f l o a t i n g -p o i n t c o n s t a n t. D y n a m i c e r r o r b o u n d -C o d e m u s t b e g e n e r a t e d f o r i t s c o m p u t a t i o n a n d e m i t t e d i n t o t h e t a r g e t p r o g r a m. 50
Error Bounds and Compilation (( cc ont d) compilation Exact e xp r e s s i o n F let R = F (X) in floating point d y nam ic _ er r or = D (X) er r or = s tatic _ er r or * d y nam ic _ er r or if R > er r or th en 1 els e if R > er r or th en 1 els e j u m p to ph as es B, C and D 51
Compilation example S o u r c e e x p r e s s i o n c ( a + b ) 2 T a r g e t S t a n d a r d M L p r o g r a m : 52
Source Language B a s i c a r i t h m e t i c o p e r a t i o n s : +, -,, s q, u n a r y - A s s i g n m e n t s : v a l x = s o m e e x p r e s s i o n N e s t e d a n o n y m o u s f u n c t i o n s 53
Staging (cont d) 54
Overview I n t r o d u c t i o n A r b i t r a r y p r e c i s i o n a r i t h m c o m p u t i n g i n p h a s e s M u l t i -s t a g e f i l t e r s P e r f o r m a n c e C o n c l u s i o n a n d F u t u r e W e t i c a n d o r k 55