Updated to fedora-glibc-20050106T1443
[glibc.git] / sysdeps / ia64 / fpu / s_nextafter.S
blob8c77aa492b98d2a643d8cd809f2551d8fa61c4ce
1 .file "nextafter.s"
4 // Copyright (c) 2000 - 2003, Intel Corporation
5 // All rights reserved.
6 //
7 // Contributed 2000 by the Intel Numerics Group, Intel Corporation
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are
11 // met:
13 // * Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
16 // * Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
20 // * The name of Intel Corporation may not be used to endorse or promote
21 // products derived from this software without specific prior written
22 // permission.
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR ITS 
28 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
30 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
31 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
32 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR TORT (INCLUDING
33 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
34 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
35 // 
36 // Intel Corporation is the author of this code, and requests that all
37 // problem reports or change requests be submitted to it directly at 
38 // http://www.intel.com/software/products/opensource/libraries/num.htm.
40 // History
41 //==============================================================
42 // 02/02/00 Initial version 
43 // 03/03/00 Modified to conform to C9X, and improve speed of main path
44 // 03/14/00 Fixed case where x is a power of 2, and x > y, improved speed
45 // 04/04/00 Unwind support added
46 // 05/12/00 Fixed erroneous denormal flag setting for exponent change cases 1,3
47 // 08/15/00 Bundle added after call to __libm_error_support to properly
48 //          set [the previously overwritten] GR_Parameter_RESULT.
49 // 09/09/00 Updated fcmp so that qnans do not raise invalid
50 // 12/15/00 Corrected behavior when both args are zero to conform to C99, and
51 //          fixed flag settings for several cases
52 // 05/20/02 Cleaned up namespace and sf0 syntax
53 // 02/10/03 Reordered header: .section, .global, .proc, .align
55 // API
56 //==============================================================
57 // double nextafter( double x, double y );
58 // input  floating point f8, f9
59 // output floating point f8
61 // Registers used
62 //==============================================================
63 nextafter_GR_max_pexp     = r14
64 nextafter_GR_min_pexp     = r15
65 nextafter_GR_exp          = r16
66 nextafter_GR_sig          = r17
67 nextafter_GR_lnorm_sig    = r18
68 nextafter_GR_sign_mask    = r19
69 nextafter_GR_exp_mask     = r20
70 nextafter_GR_sden_sig     = r21
71 nextafter_GR_new_sig      = r22
72 nextafter_GR_new_exp      = r23
73 nextafter_GR_lden_sig     = r24
74 nextafter_GR_snorm_sig    = r25
75 nextafter_GR_exp1         = r26
76 nextafter_GR_x_exp        = r27
77 nextafter_GR_min_den_rexp = r28
78 // r36-39 parameters for libm_error_support
80 GR_SAVE_B0                = r34
81 GR_SAVE_GP                = r35
82 GR_SAVE_PFS               = r32
84 GR_Parameter_X            = r36
85 GR_Parameter_Y            = r37
86 GR_Parameter_RESULT       = r38
88 NEXTAFTER_lnorm_sig       = f10
89 NEXTAFTER_lnorm_exp       = f11
90 NEXTAFTER_lnorm           = f12
91 NEXTAFTER_sden_sig        = f13
92 NEXTAFTER_sden_exp        = f14
93 NEXTAFTER_sden            = f15
94 NEXTAFTER_save_f8         = f33
95 NEXTAFTER_new_exp         = f34
96 NEXTAFTER_new_sig         = f35
97 NEXTAFTER_lden_sig        = f36
98 NEXTAFTER_snorm_sig       = f37
99 NEXTAFTER_exp1            = f38
100 NEXTAFTER_tmp             = f39
103 // Overview of operation
104 //==============================================================
105 // nextafter determines the next representable value 
106 // after x in the direction of y. 
109 .section .text
110 GLOBAL_LIBM_ENTRY(nextafter)
112 // Extract signexp from x
113 // Is x < y ?  p10 if yes, p11 if no
114 // Form smallest denormal significand = ulp size
115 { .mfi
116       getf.exp nextafter_GR_exp      = f8
117       fcmp.lt.s1 p10,p11 = f8, f9                
118       addl nextafter_GR_sden_sig = 0x800, r0
120 // Form largest normal significand 0xfffffffffffff800
121 // Form smallest normal exponent
122 { .mfi
123       addl nextafter_GR_lnorm_sig = -0x800,r0
124       nop.f 999
125       addl nextafter_GR_min_pexp = 0x0fc01, r0 ;;
127 // Extract significand from x
128 // Is x=y?
129 // Form largest normal exponent
130 { .mfi
131       getf.sig nextafter_GR_sig      = f8
132       fcmp.eq.s0 p6,p0 = f8, f9                
133       addl nextafter_GR_max_pexp = 0x103fe, r0
135 // Move largest normal significand to fp reg for special cases
136 { .mfi
137       setf.sig NEXTAFTER_lnorm_sig = nextafter_GR_lnorm_sig
138       nop.f 999
139       addl nextafter_GR_sign_mask = 0x20000, r0 ;;
142 // Move smallest denormal significand and signexp to fp regs
143 // Is x=nan?
144 // Set p12 and p13 based on whether significand increases or decreases
145 // It increases (p12 set) if x<y and x>=0 or if x>y and x<0
146 // It decreases (p13 set) if x<y and x<0  or if x>y and x>=0
147 { .mfi
148       setf.sig NEXTAFTER_sden_sig = nextafter_GR_sden_sig
149       fclass.m  p8,p0 = f8, 0xc3           
150 (p10) cmp.lt p12,p13 = nextafter_GR_exp, nextafter_GR_sign_mask
152 { .mfi
153       setf.exp NEXTAFTER_sden_exp = nextafter_GR_min_pexp
154 (p11) cmp.ge p12,p13 = nextafter_GR_exp, nextafter_GR_sign_mask ;;
157 .pred.rel "mutex",p12,p13
159 // Form expected new significand, adding or subtracting 1 ulp increment
160 // If x=y set result to y
161 // Form smallest normal significand and largest denormal significand
162 { .mfi
163 (p12) add nextafter_GR_new_sig = nextafter_GR_sig, nextafter_GR_sden_sig
164 (p6)  fmerge.s f8=f9,f9
165       dep.z nextafter_GR_snorm_sig = 1,63,1 // 0x8000000000000000
167 { .mlx
168 (p13) sub nextafter_GR_new_sig = nextafter_GR_sig, nextafter_GR_sden_sig
169       movl nextafter_GR_lden_sig = 0x7ffffffffffff800 ;;
172 // Move expected result significand and signexp to fp regs
173 // Is y=nan?
174 // Form new exponent in case result exponent needs incrementing or decrementing
175 { .mfi
176       setf.exp NEXTAFTER_new_exp = nextafter_GR_exp
177       fclass.m  p9,p0 = f9, 0xc3           
178 (p12) add nextafter_GR_exp1 = 1, nextafter_GR_exp
180 { .mib
181       setf.sig NEXTAFTER_new_sig = nextafter_GR_new_sig
182 (p13) add nextafter_GR_exp1 = -1, nextafter_GR_exp
183 (p6)  br.ret.spnt    b0 ;;             // Exit if x=y
186 // Move largest normal signexp to fp reg for special cases
187 // Is x=zero?
188 { .mfi
189       setf.exp NEXTAFTER_lnorm_exp = nextafter_GR_max_pexp
190       fclass.m  p7,p0 = f8, 0x7
191       nop.i 999
193 { .mfb
194       nop.m 999
195 (p8)  fma.s0 f8 = f8,f1,f9                     
196 (p8)  br.ret.spnt    b0 ;;             // Exit if x=nan
199 // Move exp+-1 and smallest normal significand to fp regs for special cases
200 // Is x=inf?
201 { .mfi
202       setf.exp NEXTAFTER_exp1 = nextafter_GR_exp1
203       fclass.m  p6,p0 = f8, 0x23           
204       addl nextafter_GR_exp_mask = 0x1ffff, r0
206 { .mfb
207       setf.sig NEXTAFTER_snorm_sig = nextafter_GR_snorm_sig
208 (p9)  fma.s0 f8 = f8,f1,f9                     
209 (p9)  br.ret.spnt    b0 ;;             // Exit if y=nan
212 // Move largest denormal significand to fp regs for special cases
213 // Save x
214 { .mfb
215       setf.sig NEXTAFTER_lden_sig = nextafter_GR_lden_sig
216       mov NEXTAFTER_save_f8 = f8
217 (p7)  br.cond.spnt NEXTAFTER_ZERO ;;   // Exit if x=0   
220 // Mask off the sign to get x_exp
221 { .mfb
222       and nextafter_GR_x_exp = nextafter_GR_exp_mask, nextafter_GR_exp
223       nop.f 999
224 (p6)  br.cond.spnt NEXTAFTER_INF ;;   // Exit if x=inf   
227 // Check 6 special cases when significand rolls over:
228 //  1 sig size incr, x_sig=max_sig, x_exp < max_exp
229 //     Set p6, result is sig=min_sig, exp++
230 //  2 sig size incr, x_sig=max_sig, x_exp >= max_exp
231 //     Set p7, result is inf, signal overflow
232 //  3 sig size decr, x_sig=min_sig, x_exp > min_exp
233 //     Set p8, result is sig=max_sig, exp--
234 //  4 sig size decr, x_sig=min_sig, x_exp = min_exp
235 //     Set p9, result is sig=max_den_sig, exp same, signal underflow and inexact
236 //  5 sig size decr, x_sig=min_den_sig, x_exp = min_exp
237 //     Set p10, result is zero, sign of x, signal underflow and inexact
238 //  6 sig size decr, x_sig=min_sig, x_exp < min_exp 
239 //     Set p14, result is zero, sign of x, signal underflow and inexact
241 // Form exponent of smallest double denormal (if normalized register format)
242 { .mmi
243       adds nextafter_GR_min_den_rexp = -52, nextafter_GR_min_pexp
244 (p12) cmp.eq.unc p6,p0 = nextafter_GR_new_sig, r0
245 (p13) cmp.eq.unc p8,p10 = nextafter_GR_new_sig, nextafter_GR_lden_sig ;;
248 { .mmi
249 (p6)  cmp.lt.unc p6,p7 = nextafter_GR_x_exp, nextafter_GR_max_pexp
250 (p8)  cmp.gt.unc p8,p9 = nextafter_GR_x_exp, nextafter_GR_min_pexp
251 (p10) cmp.eq.unc p10,p0 = nextafter_GR_new_sig, r0 ;;
254 // Create small normal in case need to generate underflow flag
255 { .mfi
256 (p10) cmp.le.unc p10,p0 = nextafter_GR_x_exp, nextafter_GR_min_pexp
257       fmerge.se NEXTAFTER_tmp = NEXTAFTER_sden_exp, NEXTAFTER_lnorm_sig
258 (p9)  cmp.gt.unc p9,p14 = nextafter_GR_x_exp, nextafter_GR_min_den_rexp
260 // Branch if cases 1, 2, 3
261 { .bbb
262 (p6)  br.cond.spnt NEXTAFTER_EXPUP
263 (p7)  br.cond.spnt NEXTAFTER_OVERFLOW
264 (p8)  br.cond.spnt NEXTAFTER_EXPDOWN ;;
267 // Branch if cases 4, 5, 6
268 { .bbb
269 (p9)  br.cond.spnt NEXTAFTER_NORM_TO_DENORM
270 (p10) br.cond.spnt NEXTAFTER_UNDERFLOW_TO_ZERO
271 (p14) br.cond.spnt NEXTAFTER_UNDERFLOW_TO_ZERO ;;
274 // Here if no special cases
275 // Set p6 if result will be a denormal, so can force underflow flag
276 //    Case 1:  x_exp=min_exp, x_sig=unnormalized
277 //    Case 2:  x_exp<min_exp
278 { .mfi
279       cmp.lt p6,p7 = nextafter_GR_x_exp, nextafter_GR_min_pexp
280       fmerge.se f8 = NEXTAFTER_new_exp, NEXTAFTER_new_sig
281       nop.i 999 ;;
284 { .mfi
285       nop.m 999
286       nop.f 999
287 (p7)  tbit.z p6,p0 = nextafter_GR_new_sig, 63 ;;
290 NEXTAFTER_COMMON_FINISH:
291 // Force underflow and inexact if denormal result
292 { .mfi
293       nop.m 999
294 (p6)  fma.d.s0 NEXTAFTER_tmp = NEXTAFTER_tmp,NEXTAFTER_tmp,f0
295       nop.i 999 ;;
298 // Final normalization to result precision and exit
299 { .mfb
300       nop.m 999
301       fnorm.d.s0 f8 = f8
302       br.ret.sptk b0;;
305 //Special cases
306 NEXTAFTER_EXPUP:
307 { .mfb
308       cmp.lt p6,p7 = nextafter_GR_x_exp, nextafter_GR_min_pexp
309       fmerge.se f8 = NEXTAFTER_exp1, NEXTAFTER_snorm_sig
310       br.cond.sptk NEXTAFTER_COMMON_FINISH ;;
313 NEXTAFTER_EXPDOWN:
314 { .mfb
315       cmp.lt p6,p7 = nextafter_GR_x_exp, nextafter_GR_min_pexp
316       fmerge.se f8 = NEXTAFTER_exp1, NEXTAFTER_lnorm_sig
317       br.cond.sptk NEXTAFTER_COMMON_FINISH ;;
320 NEXTAFTER_NORM_TO_DENORM:
321 { .mfi
322       nop.m 999
323       fmerge.se f8 = NEXTAFTER_new_exp, NEXTAFTER_lden_sig
324       nop.i 999
326 // Force underflow and inexact if denormal result
327 { .mfb
328       nop.m 999
329       fma.d.s0 NEXTAFTER_tmp = NEXTAFTER_tmp,NEXTAFTER_tmp,f0
330       br.ret.sptk b0 ;;
333 NEXTAFTER_UNDERFLOW_TO_ZERO:
334 { .mfb
335       cmp.eq p6,p0 = r0,r0
336       fmerge.s f8 = NEXTAFTER_save_f8,f0
337       br.cond.sptk NEXTAFTER_COMMON_FINISH ;;
340 NEXTAFTER_INF: 
341 // Here if f8 is +- infinity
342 // INF
343 // if f8 is +inf, no matter what y is return  largest double
344 // if f8 is -inf, no matter what y is return -largest double
346 { .mfi
347       nop.m 999
348       fmerge.se NEXTAFTER_lnorm = NEXTAFTER_lnorm_exp,NEXTAFTER_lnorm_sig
349       nop.i 999 ;;
352 { .mfb
353       nop.m 999
354       fmerge.s f8 = f8,NEXTAFTER_lnorm                
355       br.ret.sptk    b0 ;;                        
358 NEXTAFTER_ZERO: 
360 // Here if f8 is +- zero
361 // ZERO
362 // if f8 is zero and y is +, return + smallest double denormal 
363 // if f8 is zero and y is -, return - smallest double denormal 
365 { .mfi
366       nop.m 999
367       fmerge.se NEXTAFTER_sden = NEXTAFTER_sden_exp,NEXTAFTER_sden_sig
368       nop.i 999 ;;
371 // Create small normal to generate underflow flag
372 { .mfi
373       nop.m 999
374       fmerge.se NEXTAFTER_tmp = NEXTAFTER_sden_exp, NEXTAFTER_lnorm_sig
375       nop.i 999 ;;
378 // Add correct sign from direction arg
379 { .mfi
380       nop.m 999
381       fmerge.s f8 = f9,NEXTAFTER_sden                
382       nop.i 999 ;;
385 { .mfb
386       nop.m 999
387       fma.d.s0 NEXTAFTER_tmp = NEXTAFTER_tmp,NEXTAFTER_tmp,f0
388       br.ret.sptk    b0 ;;                        
391 GLOBAL_LIBM_END(nextafter)
392 // Stack operations when calling error support.
393 //       (1)               (2)                          (3) (call)              (4)
394 //   sp   -> +          psp -> +                     psp -> +                   sp -> +
395 //           |                 |                            |                         |
396 //           |                 | <- GR_Y               R3 ->| <- GR_RESULT            | -> f8
397 //           |                 |                            |                         |
398 //           | <-GR_Y      Y2->|                       Y2 ->| <- GR_Y                 |
399 //           |                 |                            |                         |
400 //           |                 | <- GR_X               X1 ->|                         |
401 //           |                 |                            |                         |
402 //  sp-64 -> +          sp ->  +                     sp ->  +                         +
403 //    save ar.pfs          save b0                                               restore gp
404 //    save gp                                                                    restore ar.pfs
408 LOCAL_LIBM_ENTRY(__libm_error_region)
409 NEXTAFTER_OVERFLOW: 
410 // Here if f8 is finite, but result will be infinite
411 // Use frcpa to generate infinity of correct sign
412 // Call error support to report possible range error
413 .prologue
415 { .mfi
416       alloc          r32=ar.pfs,2,2,4,0
417       frcpa.s1 f8,p6 = NEXTAFTER_save_f8, f0
418       nop.i 999 ;;
421 // Create largest double
422 { .mfi
423       nop.m 999
424       fmerge.se NEXTAFTER_lnorm = NEXTAFTER_lnorm_exp,NEXTAFTER_lnorm_sig
425       nop.i 999 ;;
428 // Force overflow and inexact flags to be set
429 { .mfi
430       mov           r39 = 154      // Error code
431       fma.d.s0 NEXTAFTER_tmp = NEXTAFTER_lnorm,NEXTAFTER_lnorm,f0
432       nop.i 999
436 // (1)
437 { .mfi
438         add   GR_Parameter_Y=-32,sp             // Parameter 2 value
439         nop.f 0
440 .save   ar.pfs,GR_SAVE_PFS
441         mov  GR_SAVE_PFS=ar.pfs                 // Save ar.pfs
443 { .mfi
444 .fframe 64
445         add sp=-64,sp                          // Create new stack
446         nop.f 0
447         mov GR_SAVE_GP=gp                      // Save gp
451 // (2)
452 { .mmi
453         stfd [GR_Parameter_Y] = f9,16         // STORE Parameter 2 on stack
454         add GR_Parameter_X = 16,sp            // Parameter 1 address
455 .save   b0, GR_SAVE_B0
456         mov GR_SAVE_B0=b0                     // Save b0
459 .body
460 // (3)
461 { .mib
462         stfd [GR_Parameter_X] = NEXTAFTER_save_f8              // STORE Parameter 1 on stack
463         add   GR_Parameter_RESULT = 0,GR_Parameter_Y           // Parameter 3 address
464         nop.b 0                                
466 { .mib
467         stfd [GR_Parameter_Y] = f8              // STORE Parameter 3 on stack
468         add   GR_Parameter_Y = -16,GR_Parameter_Y
469         br.call.sptk b0=__libm_error_support#   // Call error handling function
471 { .mmi
472         nop.m 0
473         nop.m 0
474         add   GR_Parameter_RESULT = 48,sp
477 // (4)
478 { .mmi
479         ldfd  f8 = [GR_Parameter_RESULT]       // Get return result off stack
480 .restore sp
481         add   sp = 64,sp                       // Restore stack pointer
482         mov   b0 = GR_SAVE_B0                  // Restore return address
484 { .mib
485         mov   gp = GR_SAVE_GP                  // Restore gp
486         mov   ar.pfs = GR_SAVE_PFS             // Restore ar.pfs
487         br.ret.sptk     b0                     // Return
490 LOCAL_LIBM_END(__libm_error_region)
493 .type   __libm_error_support#,@function
494 .global __libm_error_support#