import later fedora-branch tweaks
[glibc.git] / sysdeps / ia64 / fpu / s_nextafterf.S
blob6d2a92796d851e768123ea690e87a86e7f8bb68d
1 .file "nextafterf.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 // float nextafterf( float x, float 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 // nextafterf determines the next representable value 
106 // after x in the direction of y. 
109 .section .text
110 GLOBAL_LIBM_ENTRY(nextafterf)
112 // Extract signexp from x
113 // Form smallest denormal significand = ulp size
114 { .mlx
115       getf.exp nextafter_GR_exp      = f8
116       movl nextafter_GR_sden_sig = 0x0000010000000000
118 // Form largest normal exponent
119 // Is x < y ?  p10 if yes, p11 if no
120 // Form smallest normal exponent
121 { .mfi
122       addl nextafter_GR_max_pexp = 0x1007e, r0
123       fcmp.lt.s1 p10,p11 = f8, f9
124       addl nextafter_GR_min_pexp = 0x0ff81, r0 ;;
127 // Is x=y?
128 { .mfi
129       getf.sig nextafter_GR_sig      = f8
130       fcmp.eq.s0 p6,p0 = f8, f9
131       nop.i 0
133 // Extract significand from x
134 // Form largest normal significand
135 { .mlx
136       nop.m 0 
137       movl nextafter_GR_lnorm_sig = 0xffffff0000000000 ;;
140 // Move largest normal significand to fp reg for special cases
141 { .mfi
142       setf.sig NEXTAFTER_lnorm_sig = nextafter_GR_lnorm_sig
143       nop.f 0 
144       addl nextafter_GR_sign_mask = 0x20000, r0 ;;
147 // Move smallest denormal significand and signexp to fp regs
148 // Is x=nan?
149 // Set p12 and p13 based on whether significand increases or decreases
150 // It increases (p12 set) if x<y and x>=0 or if x>y and x<0
151 // It decreases (p13 set) if x<y and x<0  or if x>y and x>=0
152 { .mfi
153       setf.sig NEXTAFTER_sden_sig = nextafter_GR_sden_sig
154       fclass.m  p8,p0 = f8, 0xc3           
155 (p10) cmp.lt p12,p13 = nextafter_GR_exp, nextafter_GR_sign_mask
157 { .mfi
158       setf.exp NEXTAFTER_sden_exp = nextafter_GR_min_pexp
159       nop.f 999
160 (p11) cmp.ge p12,p13 = nextafter_GR_exp, nextafter_GR_sign_mask ;;
163 .pred.rel "mutex",p12,p13
165 // Form expected new significand, adding or subtracting 1 ulp increment
166 // If x=y set result to y
167 // Form smallest normal significand and largest denormal significand
168 { .mfi
169 (p12) add nextafter_GR_new_sig = nextafter_GR_sig, nextafter_GR_sden_sig
170 (p6)  fmerge.s f8=f9,f9
171       dep.z nextafter_GR_snorm_sig = 1,63,1 // 0x8000000000000000
173 { .mlx
174 (p13) sub nextafter_GR_new_sig = nextafter_GR_sig, nextafter_GR_sden_sig
175       movl nextafter_GR_lden_sig = 0x7fffff0000000000 ;;
178 // Move expected result significand and signexp to fp regs
179 // Is y=nan?
180 // Form new exponent in case result exponent needs incrementing or decrementing
181 { .mfi
182       setf.exp NEXTAFTER_new_exp = nextafter_GR_exp
183       fclass.m  p9,p0 = f9, 0xc3           
184 (p12) add nextafter_GR_exp1 = 1, nextafter_GR_exp
186 { .mib
187       setf.sig NEXTAFTER_new_sig = nextafter_GR_new_sig
188 (p13) add nextafter_GR_exp1 = -1, nextafter_GR_exp
189 (p6)  br.ret.spnt    b0 ;;             // Exit if x=y
192 // Move largest normal signexp to fp reg for special cases
193 // Is x=zero?
194 { .mfi
195       setf.exp NEXTAFTER_lnorm_exp = nextafter_GR_max_pexp
196       fclass.m  p7,p0 = f8, 0x7
197       nop.i 999
199 { .mfb
200       nop.m 999
201 (p8)  fma.s0 f8 = f8,f1,f9                     
202 (p8)  br.ret.spnt    b0 ;;             // Exit if x=nan
205 // Move exp+-1 and smallest normal significand to fp regs for special cases
206 // Is x=inf?
207 { .mfi
208       setf.exp NEXTAFTER_exp1 = nextafter_GR_exp1
209       fclass.m  p6,p0 = f8, 0x23           
210       addl nextafter_GR_exp_mask = 0x1ffff, r0
212 { .mfb
213       setf.sig NEXTAFTER_snorm_sig = nextafter_GR_snorm_sig
214 (p9)  fma.s0 f8 = f8,f1,f9                     
215 (p9)  br.ret.spnt    b0 ;;             // Exit if y=nan
218 // Move largest denormal significand to fp regs for special cases
219 // Save x
220 { .mfb
221       setf.sig NEXTAFTER_lden_sig = nextafter_GR_lden_sig
222       mov NEXTAFTER_save_f8 = f8
223 (p7)  br.cond.spnt NEXTAFTER_ZERO ;;   // Exit if x=0   
226 // Mask off the sign to get x_exp
227 { .mfb
228       and nextafter_GR_x_exp = nextafter_GR_exp_mask, nextafter_GR_exp
229       nop.f 999
230 (p6)  br.cond.spnt NEXTAFTER_INF ;;   // Exit if x=inf   
233 // Check 6 special cases when significand rolls over:
234 //  1 sig size incr, x_sig=max_sig, x_exp < max_exp
235 //     Set p6, result is sig=min_sig, exp++
236 //  2 sig size incr, x_sig=max_sig, x_exp >= max_exp
237 //     Set p7, result is inf, signal overflow
238 //  3 sig size decr, x_sig=min_sig, x_exp > min_exp
239 //     Set p8, result is sig=max_sig, exp--
240 //  4 sig size decr, x_sig=min_sig, x_exp = min_exp
241 //     Set p9, result is sig=max_den_sig, exp same, signal underflow and inexact
242 //  5 sig size decr, x_sig=min_den_sig, x_exp = min_exp
243 //     Set p10, result is zero, sign of x, signal underflow and inexact
244 //  6 sig size decr, x_sig=min_sig, x_exp < min_exp 
245 //     Set p14, result is zero, sign of x, signal underflow and inexact
247 // Form exponent of smallest float denormal (if normalized register format)
248 { .mmi
249       adds nextafter_GR_min_den_rexp = -23, nextafter_GR_min_pexp
250 (p12) cmp.eq.unc p6,p0 = nextafter_GR_new_sig, r0
251 (p13) cmp.eq.unc p8,p10 = nextafter_GR_new_sig, nextafter_GR_lden_sig ;;
254 { .mmi
255 (p6)  cmp.lt.unc p6,p7 = nextafter_GR_x_exp, nextafter_GR_max_pexp
256 (p8)  cmp.gt.unc p8,p9 = nextafter_GR_x_exp, nextafter_GR_min_pexp
257 (p10) cmp.eq.unc p10,p0 = nextafter_GR_new_sig, r0 ;;
260 // Create small normal in case need to generate underflow flag
261 { .mfi
262 (p10) cmp.le.unc p10,p0 = nextafter_GR_x_exp, nextafter_GR_min_pexp
263       fmerge.se NEXTAFTER_tmp = NEXTAFTER_sden_exp, NEXTAFTER_lnorm_sig
264 (p9)  cmp.gt.unc p9,p14 = nextafter_GR_x_exp, nextafter_GR_min_den_rexp
266 // Branch if cases 1, 2, 3
267 { .bbb
268 (p6)  br.cond.spnt NEXTAFTER_EXPUP
269 (p7)  br.cond.spnt NEXTAFTER_OVERFLOW
270 (p8)  br.cond.spnt NEXTAFTER_EXPDOWN ;;
273 // Branch if cases 4, 5, 6
274 { .bbb
275 (p9)  br.cond.spnt NEXTAFTER_NORM_TO_DENORM
276 (p10) br.cond.spnt NEXTAFTER_UNDERFLOW_TO_ZERO
277 (p14) br.cond.spnt NEXTAFTER_UNDERFLOW_TO_ZERO ;;
280 // Here if no special cases
281 // Set p6 if result will be a denormal, so can force underflow flag
282 //    Case 1:  x_exp=min_exp, x_sig=unnormalized
283 //    Case 2:  x_exp<min_exp
284 { .mfi
285       cmp.lt p6,p7 = nextafter_GR_x_exp, nextafter_GR_min_pexp
286       fmerge.se f8 = NEXTAFTER_new_exp, NEXTAFTER_new_sig
287       nop.i 999 ;;
290 { .mfi
291       nop.m 999
292       nop.f 999
293 (p7)  tbit.z p6,p0 = nextafter_GR_new_sig, 63 ;;
296 NEXTAFTER_COMMON_FINISH:
297 // Force underflow and inexact if denormal result
298 { .mfi
299       nop.m 999
300 (p6)  fma.s.s0 NEXTAFTER_tmp = NEXTAFTER_tmp,NEXTAFTER_tmp,f0
301       nop.i 999 ;;
304 // Final normalization to result precision and exit
305 { .mfb
306       nop.m 999
307       fnorm.s.s0 f8 = f8
308       br.ret.sptk b0;;
311 //Special cases
312 NEXTAFTER_EXPUP:
313 { .mfb
314       cmp.lt p6,p7 = nextafter_GR_x_exp, nextafter_GR_min_pexp
315       fmerge.se f8 = NEXTAFTER_exp1, NEXTAFTER_snorm_sig
316       br.cond.sptk NEXTAFTER_COMMON_FINISH ;;
319 NEXTAFTER_EXPDOWN:
320 { .mfb
321       cmp.lt p6,p7 = nextafter_GR_x_exp, nextafter_GR_min_pexp
322       fmerge.se f8 = NEXTAFTER_exp1, NEXTAFTER_lnorm_sig
323       br.cond.sptk NEXTAFTER_COMMON_FINISH ;;
326 NEXTAFTER_NORM_TO_DENORM:
327 { .mfi
328       nop.m 999
329       fmerge.se f8 = NEXTAFTER_new_exp, NEXTAFTER_lden_sig
330       nop.i 999
332 // Force underflow and inexact
333 { .mfb
334       nop.m 999
335       fma.s.s0 NEXTAFTER_tmp = NEXTAFTER_tmp,NEXTAFTER_tmp,f0
336       br.ret.sptk b0 ;;
339 NEXTAFTER_UNDERFLOW_TO_ZERO:
340 { .mfb
341       cmp.eq p6,p0 = r0,r0
342       fmerge.s f8 = NEXTAFTER_save_f8,f0
343       br.cond.sptk NEXTAFTER_COMMON_FINISH ;;
346 NEXTAFTER_INF: 
347 // Here if f8 is +- infinity
348 // INF
349 // if f8 is +inf, no matter what y is return  largest float
350 // if f8 is -inf, no matter what y is return -largest float
352 { .mfi
353       nop.m 999
354       fmerge.se NEXTAFTER_lnorm = NEXTAFTER_lnorm_exp,NEXTAFTER_lnorm_sig
355       nop.i 999 ;;
358 { .mfb
359       nop.m 999
360       fmerge.s f8 = f8,NEXTAFTER_lnorm                
361       br.ret.sptk    b0 ;;                        
364 NEXTAFTER_ZERO: 
366 // Here if f8 is +- zero
367 // ZERO
368 // if f8 is zero and y is +, return + smallest float denormal 
369 // if f8 is zero and y is -, return - smallest float denormal 
371 { .mfi
372       nop.m 999
373       fmerge.se NEXTAFTER_sden = NEXTAFTER_sden_exp,NEXTAFTER_sden_sig
374       nop.i 999 ;;
377 // Create small normal to generate underflow flag
378 { .mfi
379       nop.m 999
380       fmerge.se NEXTAFTER_tmp = NEXTAFTER_sden_exp, NEXTAFTER_lnorm_sig
381       nop.i 999 ;;
384 // Add correct sign from direction arg
385 { .mfi
386       nop.m 999
387       fmerge.s f8 = f9,NEXTAFTER_sden                
388       nop.i 999 ;;
391 // Force underflow and inexact flags
392 { .mfb
393       nop.m 999
394       fma.s.s0 NEXTAFTER_tmp = NEXTAFTER_tmp,NEXTAFTER_tmp,f0
395       br.ret.sptk    b0 ;;                        
398 GLOBAL_LIBM_END(nextafterf)
399 // Stack operations when calling error support.
400 //       (1)               (2)                          (3) (call)              (4)
401 //   sp   -> +          psp -> +                     psp -> +                   sp -> +
402 //           |                 |                            |                         |
403 //           |                 | <- GR_Y               R3 ->| <- GR_RESULT            | -> f8
404 //           |                 |                            |                         |
405 //           | <-GR_Y      Y2->|                       Y2 ->| <- GR_Y                 |
406 //           |                 |                            |                         |
407 //           |                 | <- GR_X               X1 ->|                         |
408 //           |                 |                            |                         |
409 //  sp-64 -> +          sp ->  +                     sp ->  +                         +
410 //    save ar.pfs          save b0                                               restore gp
411 //    save gp                                                                    restore ar.pfs
415 LOCAL_LIBM_ENTRY(__libm_error_region)
416 NEXTAFTER_OVERFLOW: 
417 // Here if f8 is finite, but result will be infinite
418 // Use frcpa to generate infinity of correct sign
419 // Call error support to report possible range error
420 .prologue
422 { .mfi
423       alloc          r32=ar.pfs,2,2,4,0
424       frcpa.s1 f8,p6 = NEXTAFTER_save_f8, f0
425       nop.i 999
428 // Create largest float
429 { .mfi
430       nop.m 999
431       fmerge.se NEXTAFTER_lnorm = NEXTAFTER_lnorm_exp,NEXTAFTER_lnorm_sig
432       nop.i 999 ;;
435 // Force overflow and inexact flags to be set
436 { .mfi
437       mov           r39 = 155      // Error code
438       fma.s.s0 NEXTAFTER_tmp = NEXTAFTER_lnorm,NEXTAFTER_lnorm,f0
439       nop.i 999
443 // (1)
444 { .mfi
445         add   GR_Parameter_Y=-32,sp             // Parameter 2 value
446         nop.f 0
447 .save   ar.pfs,GR_SAVE_PFS
448         mov  GR_SAVE_PFS=ar.pfs                 // Save ar.pfs
450 { .mfi
451 .fframe 64
452         add sp=-64,sp                          // Create new stack
453         nop.f 0
454         mov GR_SAVE_GP=gp                      // Save gp
458 // (2)
459 { .mmi
460         stfs [GR_Parameter_Y] = f9,16         // STORE Parameter 2 on stack
461         add GR_Parameter_X = 16,sp            // Parameter 1 address
462 .save   b0, GR_SAVE_B0
463         mov GR_SAVE_B0=b0                     // Save b0
466 .body
467 // (3)
468 { .mib
469         stfs [GR_Parameter_X] = NEXTAFTER_save_f8              // STORE Parameter 1 on stack
470         add   GR_Parameter_RESULT = 0,GR_Parameter_Y           // Parameter 3 address
471         nop.b 0                                
473 { .mib
474         stfs [GR_Parameter_Y] = f8              // STORE Parameter 3 on stack
475         add   GR_Parameter_Y = -16,GR_Parameter_Y
476         br.call.sptk b0=__libm_error_support#   // Call error handling function
478 { .mmi
479         nop.m 0
480         nop.m 0
481         add   GR_Parameter_RESULT = 48,sp
484 // (4)
485 { .mmi
486         ldfs  f8 = [GR_Parameter_RESULT]       // Get return result off stack
487 .restore sp
488         add   sp = 64,sp                       // Restore stack pointer
489         mov   b0 = GR_SAVE_B0                  // Restore return address
491 { .mib
492         mov   gp = GR_SAVE_GP                  // Restore gp
493         mov   ar.pfs = GR_SAVE_PFS             // Restore ar.pfs
494         br.ret.sptk     b0                     // Return
497 LOCAL_LIBM_END(__libm_error_region)
500 .type   __libm_error_support#,@function
501 .global __libm_error_support#