Updated to fedora-glibc-20050106T1443
[glibc.git] / sysdeps / ia64 / fpu / s_nextafterl.S
blob05bdd9c17aa9e9e1e8b500b146c4b71836bae6c3
1 .file "nextafterl.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 Fixed case of smallest long double normal to largest denormal,
51 //          now adhere to C99 for two zero args, and fixed flag settings 
52 //          for several cases
53 // 05/20/02 Cleaned up namespace and sf0 syntax
54 // 02/10/03 Reordered header: .section, .global, .proc, .align
56 // API
57 //==============================================================
58 // long double nextafterl( long double x, long double y );
59 // input  floating point f8, f9
60 // output floating point f8
62 // Registers used
63 //==============================================================
64 nextafter_GR_max_pexp     = r14
65 nextafter_GR_min_pexp     = r15
66 nextafter_GR_exp          = r16
67 nextafter_GR_sig          = r17
68 nextafter_GR_lnorm_sig    = r18
69 nextafter_GR_sign_mask    = r19
70 nextafter_GR_exp_mask     = r20
71 nextafter_GR_sden_sig     = r21
72 nextafter_GR_new_sig      = r22
73 nextafter_GR_new_exp      = r23
74 nextafter_GR_lden_sig     = r24
75 nextafter_GR_snorm_sig    = r25
76 nextafter_GR_exp1         = r26
77 nextafter_GR_x_exp        = r27
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_den_exp         = f14
93 NEXTAFTER_sden            = f15
94 NEXTAFTER_snorm_exp       = f32
95 NEXTAFTER_save_f8         = f33
96 NEXTAFTER_new_exp         = f34
97 NEXTAFTER_new_sig         = f35
98 NEXTAFTER_lden_sig        = f36
99 NEXTAFTER_snorm_sig       = f37
100 NEXTAFTER_exp1            = f38
101 NEXTAFTER_tmp             = f39
104 // Overview of operation
105 //==============================================================
106 // nextafterl determines the next representable value 
107 // after x in the direction of y. 
110 .section .text
111 GLOBAL_LIBM_ENTRY(nextafterl)
113 // Extract signexp from x
114 // Is x < y ?  p10 if yes, p11 if no
115 // Form smallest denormal significand = ulp size
116 { .mfi
117       getf.exp nextafter_GR_exp      = f8
118       fcmp.lt.s1 p10,p11 = f8, f9                
119       addl nextafter_GR_sden_sig = 0x1, r0
121 // Form largest normal significand 0xffffffffffffffff
122 // Form smallest normal exponent
123 { .mfi
124       addl nextafter_GR_lnorm_sig = -0x1,r0
125       nop.f 999
126       addl nextafter_GR_min_pexp = 0x0c001, r0 ;;
129 // Extract significand from x
130 // Is x=y?   This fcmp also sets Invalid and Denormal if required
131 // Form largest normal exponent
132 { .mfi
133       getf.sig nextafter_GR_sig      = f8
134       fcmp.eq.s0 p6,p0 = f8, f9
135       addl nextafter_GR_max_pexp = 0x13ffe, r0
137 // Move largest normal significand to fp reg for special cases
138 { .mfi
139       setf.sig NEXTAFTER_lnorm_sig = nextafter_GR_lnorm_sig
140       nop.f 999
141       addl nextafter_GR_sign_mask = 0x20000, r0 ;;
144 // Move smallest denormal significand and exp to fp regs
145 // Is x=nan?
146 // Set p12 and p13 based on whether significand increases or decreases
147 // It increases (p12 set) if x<y and x>=0 or if x>y and x<0
148 // It decreases (p13 set) if x<y and x<0  or if x>y and x>=0
149 { .mfi
150       setf.sig NEXTAFTER_sden_sig = nextafter_GR_sden_sig
151       fclass.m  p8,p0 = f8, 0xc3           
152 (p10) cmp.lt p12,p13 = nextafter_GR_exp, nextafter_GR_sign_mask
154 // Move smallest normal exp to fp regs
155 { .mfi
156       setf.exp NEXTAFTER_snorm_exp = nextafter_GR_min_pexp
157       nop.f 999
158 (p11) cmp.ge p12,p13 = nextafter_GR_exp, nextafter_GR_sign_mask ;;
161 .pred.rel "mutex",p12,p13
163 // Form expected new significand, adding or subtracting 1 ulp increment
164 // If x=y set result to y
165 // Form smallest normal significand and largest denormal significand
166 { .mfi
167 (p12) add nextafter_GR_new_sig = nextafter_GR_sig, nextafter_GR_sden_sig
168 (p6)  fmerge.s f8=f9,f9
169       dep.z nextafter_GR_snorm_sig = 1,63,1 // 0x8000000000000000
171 { .mlx
172 (p13) sub nextafter_GR_new_sig = nextafter_GR_sig, nextafter_GR_sden_sig
173       movl nextafter_GR_lden_sig = 0x7fffffffffffffff ;;
176 // Move expected result significand and signexp to fp regs
177 // Is y=nan?
178 // Form new exponent in case result exponent needs incrementing or decrementing
179 { .mfi
180       setf.exp NEXTAFTER_new_exp = nextafter_GR_exp
181       fclass.m  p9,p0 = f9, 0xc3           
182 (p12) add nextafter_GR_exp1 = 1, nextafter_GR_exp
184 { .mib
185       setf.sig NEXTAFTER_new_sig = nextafter_GR_new_sig
186 (p13) add nextafter_GR_exp1 = -1, nextafter_GR_exp
187 (p6)  br.ret.spnt    b0 ;;             // Exit if x=y
190 // Move largest normal signexp to fp reg for special cases
191 // Is x=zero?
192 { .mfi
193       setf.exp NEXTAFTER_lnorm_exp = nextafter_GR_max_pexp
194       fclass.m  p7,p0 = f8, 0x7
195       nop.i 999
197 { .mfb
198       setf.exp NEXTAFTER_den_exp = nextafter_GR_min_pexp
199 (p8)  fma.s0 f8 = f8,f1,f9                     
200 (p8)  br.ret.spnt    b0 ;;             // Exit if x=nan
203 // Move exp+-1 and smallest normal significand to fp regs for special cases
204 // Is x=inf?
205 { .mfi
206       setf.exp NEXTAFTER_exp1 = nextafter_GR_exp1
207       fclass.m  p6,p0 = f8, 0x23           
208       addl nextafter_GR_exp_mask = 0x1ffff, r0
210 { .mfb
211       setf.sig NEXTAFTER_snorm_sig = nextafter_GR_snorm_sig
212 (p9)  fma.s0 f8 = f8,f1,f9                     
213 (p9)  br.ret.spnt    b0 ;;             // Exit if y=nan
216 // Move largest denormal significand to fp regs for special cases
217 // Save x
218 { .mfb
219       setf.sig NEXTAFTER_lden_sig = nextafter_GR_lden_sig
220       mov NEXTAFTER_save_f8 = f8
221 (p7)  br.cond.spnt NEXTAFTER_ZERO ;;   // Exit if x=0   
224 // Mask off the sign to get x_exp
225 { .mfb
226       and nextafter_GR_x_exp = nextafter_GR_exp_mask, nextafter_GR_exp
227       nop.f 999
228 (p6)  br.cond.spnt NEXTAFTER_INF ;;   // Exit if x=inf   
231 // Check 5 special cases when significand rolls over:
232 //  1 sig size incr, x_sig=max_sig, x_exp < max_exp
233 //     Set p6, result is sig=min_sig, exp++
234 //  2 sig size incr, x_sig=max_sig, x_exp >= max_exp
235 //     Set p7, result is inf, signal overflow
236 //  3 sig size decr, x_sig=min_sig, x_exp > min_exp
237 //     Set p8, result is sig=max_sig, exp--
238 //  4 sig size decr, x_sig=min_sig, x_exp = min_exp
239 //     Set p9, result is sig=max_den_sig, exp same, signal underflow and inexact
240 //  5 sig size decr, x_sig=min_den_sig, x_exp = min_exp
241 //     Set p10, result is zero, sign of x, signal underflow and inexact
243 { .mmi
244 (p12) cmp.eq.unc p6,p0 = nextafter_GR_new_sig, r0
245 (p13) cmp.eq.unc p9,p10 = nextafter_GR_new_sig, nextafter_GR_lden_sig
246       nop.i 999
250 { .mmi
251 (p6)  cmp.lt.unc p6,p7 = nextafter_GR_x_exp, nextafter_GR_max_pexp
252 (p10) cmp.eq.unc p10,p0 = nextafter_GR_new_sig, r0
253 (p9)  cmp.le.unc p9,p8 = nextafter_GR_x_exp, nextafter_GR_min_pexp 
257 // Create small normal in case need to generate underflow flag
258 { .mfi
259       nop.m 999
260       fmerge.se NEXTAFTER_tmp = NEXTAFTER_snorm_exp, NEXTAFTER_lnorm_sig
261       nop.i 999
263 // Branch if cases 1, 2, 3
264 { .bbb
265 (p6)  br.cond.spnt NEXTAFTER_EXPUP
266 (p7)  br.cond.spnt NEXTAFTER_OVERFLOW
267 (p8)  br.cond.spnt NEXTAFTER_EXPDOWN ;;
270 // Branch if cases 4, 5
271 { .mbb
272       nop.m 999
273 (p9)  br.cond.spnt NEXTAFTER_NORM_TO_DENORM
274 (p10) br.cond.spnt NEXTAFTER_UNDERFLOW_TO_ZERO
278 // Here if no special cases
279 // Set p6 if result will be a denormal, so can force underflow flag
280 //    Case 1:  x_exp=min_exp, x_sig=unnormalized
281 //    Case 2:  x_exp<min_exp
282 { .mfi
283       cmp.lt p6,p7 = nextafter_GR_x_exp, nextafter_GR_min_pexp
284       fmerge.se f8 = NEXTAFTER_new_exp, NEXTAFTER_new_sig
285       nop.i 999 ;;
288 { .mfi
289       nop.m 999
290       nop.f 999
291 (p6)  tbit.z p6,p0 = nextafter_GR_new_sig, 63 ;;
294 NEXTAFTER_COMMON_FINISH:
295 // Force underflow and inexact if denormal result
296 { .mfi
297       nop.m 999
298 (p6)  fma.s0 NEXTAFTER_tmp = NEXTAFTER_tmp,NEXTAFTER_tmp,f0
299       nop.i 999 ;;
302 // Final normalization to result precision and exit
303 { .mfb
304       nop.m 999
305       fnorm.s0 f8 = f8
306       br.ret.sptk b0;;
309 //Special cases
310 NEXTAFTER_EXPUP:
311 { .mfb
312       cmp.lt p6,p7 = nextafter_GR_x_exp, nextafter_GR_min_pexp
313       fmerge.se f8 = NEXTAFTER_exp1, NEXTAFTER_snorm_sig
314       br.cond.sptk NEXTAFTER_COMMON_FINISH ;;
317 NEXTAFTER_EXPDOWN:
318 { .mfb
319       cmp.lt p6,p7 = nextafter_GR_x_exp, nextafter_GR_min_pexp
320       fmerge.se f8 = NEXTAFTER_exp1, NEXTAFTER_lnorm_sig
321       br.cond.sptk NEXTAFTER_COMMON_FINISH ;;
324 NEXTAFTER_NORM_TO_DENORM:
325 { .mfi
326       nop.m 999
327       fmerge.se f8 = NEXTAFTER_exp1, NEXTAFTER_lden_sig
328       nop.i 999
330 // Force underflow and inexact
331 { .mfb
332       nop.m 999
333       fma.s0 NEXTAFTER_tmp = NEXTAFTER_tmp,NEXTAFTER_tmp,f0
334       br.ret.sptk b0 ;;
337 NEXTAFTER_UNDERFLOW_TO_ZERO:
338 { .mfb
339       cmp.eq p6,p0 = r0,r0
340       fmerge.s f8 = NEXTAFTER_save_f8,f0
341       br.cond.sptk NEXTAFTER_COMMON_FINISH ;;
344 NEXTAFTER_INF: 
345 // Here if f8 is +- infinity
346 // INF
347 // if f8 is +inf, no matter what y is return  largest long double
348 // if f8 is -inf, no matter what y is return -largest long double
350 // Create largest long double
351 { .mfi
352       nop.m 999
353       fmerge.se NEXTAFTER_lnorm = NEXTAFTER_lnorm_exp,NEXTAFTER_lnorm_sig
354       nop.i 999 ;;
357 { .mfb
358       nop.m 999
359       fmerge.s f8 = f8,NEXTAFTER_lnorm                
360       br.ret.sptk    b0 ;;                        
363 NEXTAFTER_ZERO: 
365 // Here if f8 is +- zero
366 // ZERO
367 // if f8 is zero and y is +, return + smallest long double denormal 
368 // if f8 is zero and y is -, return - smallest long double denormal 
370 { .mfi
371       nop.m 999
372       fmerge.se NEXTAFTER_sden = f0,NEXTAFTER_sden_sig
373       nop.i 999 ;;
376 // Create small normal to generate underflow flag
377 { .mfi
378       nop.m 999
379       fmerge.se NEXTAFTER_tmp = NEXTAFTER_snorm_exp, NEXTAFTER_lnorm_sig
380       nop.i 999 ;;
383 // Add correct sign from direction arg
384 { .mfi
385       nop.m 999
386       fmerge.s f8 = f9,NEXTAFTER_sden                
387       nop.i 999 ;;
390 // Force underflow and inexact flags
391 { .mfb
392       nop.m 999
393       fma.s0 NEXTAFTER_tmp = NEXTAFTER_tmp,NEXTAFTER_tmp,f0
394       br.ret.sptk    b0 ;;                        
397 GLOBAL_LIBM_END(nextafterl)
398 // Stack operations when calling error support.
399 //       (1)               (2)                          (3) (call)              (4)
400 //   sp   -> +          psp -> +                     psp -> +                   sp -> +
401 //           |                 |                            |                         |
402 //           |                 | <- GR_Y               R3 ->| <- GR_RESULT            | -> f8
403 //           |                 |                            |                         |
404 //           | <-GR_Y      Y2->|                       Y2 ->| <- GR_Y                 |
405 //           |                 |                            |                         |
406 //           |                 | <- GR_X               X1 ->|                         |
407 //           |                 |                            |                         |
408 //  sp-64 -> +          sp ->  +                     sp ->  +                         +
409 //    save ar.pfs          save b0                                               restore gp
410 //    save gp                                                                    restore ar.pfs
414 LOCAL_LIBM_ENTRY(__libm_error_region)
415 NEXTAFTER_OVERFLOW: 
416 // Here if f8 is finite, but result will be infinite
417 // Use frcpa to generate infinity of correct sign
418 // Call error support to report possible range error
419 .prologue
421 { .mfi
422       alloc          r32=ar.pfs,2,2,4,0
423       frcpa.s1 f8,p6 = NEXTAFTER_save_f8, f0
424       nop.i 999 ;;
427 // Create largest long double
428 { .mfi
429       nop.m 999
430       fmerge.se NEXTAFTER_lnorm = NEXTAFTER_lnorm_exp,NEXTAFTER_lnorm_sig
431       nop.i 999 ;;
434 // Force overflow and inexact flags to be set
435 { .mfi
436       mov           r39 = 153      // Error code
437       fma.s0  NEXTAFTER_tmp = NEXTAFTER_lnorm,NEXTAFTER_lnorm,f0
438       nop.i 999
442 // (1)
443 { .mfi
444         add   GR_Parameter_Y=-32,sp             // Parameter 2 value
445         nop.f 0
446 .save   ar.pfs,GR_SAVE_PFS
447         mov  GR_SAVE_PFS=ar.pfs                 // Save ar.pfs
449 { .mfi
450 .fframe 64
451         add sp=-64,sp                          // Create new stack
452         nop.f 0
453         mov GR_SAVE_GP=gp                      // Save gp
457 // (2)
458 { .mmi
459         stfe [GR_Parameter_Y] = f9,16         // STORE Parameter 2 on stack
460         add GR_Parameter_X = 16,sp            // Parameter 1 address
461 .save   b0, GR_SAVE_B0
462         mov GR_SAVE_B0=b0                     // Save b0
465 .body
466 // (3)
467 { .mib
468         stfe [GR_Parameter_X] = NEXTAFTER_save_f8              // STORE Parameter 1 on stack
469         add   GR_Parameter_RESULT = 0,GR_Parameter_Y           // Parameter 3 address
470         nop.b 0                                
472 { .mib
473         stfe [GR_Parameter_Y] = f8              // STORE Parameter 3 on stack
474         add   GR_Parameter_Y = -16,GR_Parameter_Y
475         br.call.sptk b0=__libm_error_support#   // Call error handling function
477 { .mmi
478         nop.m 0
479         nop.m 0
480         add   GR_Parameter_RESULT = 48,sp
483 // (4)
484 { .mmi
485         ldfe  f8 = [GR_Parameter_RESULT]       // Get return result off stack
486 .restore sp
487         add   sp = 64,sp                       // Restore stack pointer
488         mov   b0 = GR_SAVE_B0                  // Restore return address
490 { .mib
491         mov   gp = GR_SAVE_GP                  // Restore gp
492         mov   ar.pfs = GR_SAVE_PFS             // Restore ar.pfs
493         br.ret.sptk     b0                     // Return
496 LOCAL_LIBM_END(__libm_error_region)
499 .type   __libm_error_support#,@function
500 .global __libm_error_support#