2.9
[glibc/nacl-glibc.git] / sysdeps / ia64 / fpu / s_nextafterf.S
blob0c269eca9e3a470ff12a466e84cabc8236dd0f1a
1 .file "nextafterf.s"
4 // Copyright (c) 2000 - 2004, 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
54 // 12/14/04 Added error handling on underflow.
56 // API
57 //==============================================================
58 // float nextafterf( float x, float y );
59 // input  floating point f8, f9
60 // output floating point f8
62 // Registers used
63 //==============================================================
64 GR_max_pexp     = r14
65 GR_min_pexp     = r15
66 GR_exp          = r16
67 GR_sig          = r17
68 GR_lnorm_sig    = r18
69 GR_sign_mask    = r19
70 GR_exp_mask     = r20
71 GR_sden_sig     = r21
72 GR_new_sig      = r22
73 GR_new_exp      = r23
74 GR_lden_sig     = r24
75 GR_snorm_sig    = r25
76 GR_exp1         = r26
77 GR_x_exp        = r27
78 GR_min_den_rexp = r28
79 // r36-39 parameters for libm_error_support
81 GR_SAVE_B0                = r34
82 GR_SAVE_GP                = r35
83 GR_SAVE_PFS               = r32
85 GR_Parameter_X            = r36
86 GR_Parameter_Y            = r37
87 GR_Parameter_RESULT       = r38
88 GR_Parameter_TAG          = r39
90 FR_lnorm_sig       = f10
91 FR_lnorm_exp       = f11
92 FR_lnorm           = f12
93 FR_sden_sig        = f13
94 FR_sden_exp        = f14
95 FR_sden            = f15
96 FR_save_f8         = f33
97 FR_new_exp         = f34
98 FR_new_sig         = f35
99 FR_lden_sig        = f36
100 FR_snorm_sig       = f37
101 FR_exp1            = f38
102 FR_tmp             = f39
105 // Overview of operation
106 //==============================================================
107 // nextafterf determines the next representable value 
108 // after x in the direction of y. 
111 .section .text
112 GLOBAL_LIBM_ENTRY(nextafterf)
114 // Extract signexp from x
115 // Form smallest denormal significand = ulp size
116 { .mlx
117       getf.exp GR_exp      = f8
118       movl GR_sden_sig = 0x0000010000000000
120 // Form largest normal exponent
121 // Is x < y ?  p10 if yes, p11 if no
122 // Form smallest normal exponent
123 { .mfi
124       addl GR_max_pexp = 0x1007e, r0
125       fcmp.lt.s1 p10,p11 = f8, f9
126       addl GR_min_pexp = 0x0ff81, r0 ;;
129 // Is x=y?
130 { .mfi
131       getf.sig GR_sig      = f8
132       fcmp.eq.s0 p6,p0 = f8, f9
133       nop.i 0
135 // Extract significand from x
136 // Form largest normal significand
137 { .mlx
138       nop.m 0 
139       movl GR_lnorm_sig = 0xffffff0000000000 ;;
142 // Move largest normal significand to fp reg for special cases
143 { .mfi
144       setf.sig FR_lnorm_sig = GR_lnorm_sig
145       nop.f 0 
146       addl GR_sign_mask = 0x20000, r0 ;;
149 // Move smallest denormal significand and signexp to fp regs
150 // Is x=nan?
151 // Set p12 and p13 based on whether significand increases or decreases
152 // It increases (p12 set) if x<y and x>=0 or if x>y and x<0
153 // It decreases (p13 set) if x<y and x<0  or if x>y and x>=0
154 { .mfi
155       setf.sig FR_sden_sig = GR_sden_sig
156       fclass.m  p8,p0 = f8, 0xc3           
157 (p10) cmp.lt p12,p13 = GR_exp, GR_sign_mask
159 { .mfi
160       setf.exp FR_sden_exp = GR_min_pexp
161       nop.f 999
162 (p11) cmp.ge p12,p13 = GR_exp, GR_sign_mask ;;
165 .pred.rel "mutex",p12,p13
167 // Form expected new significand, adding or subtracting 1 ulp increment
168 // If x=y set result to y
169 // Form smallest normal significand and largest denormal significand
170 { .mfi
171 (p12) add GR_new_sig = GR_sig, GR_sden_sig
172 (p6)  fmerge.s f8=f9,f9
173       dep.z GR_snorm_sig = 1,63,1 // 0x8000000000000000
175 { .mlx
176 (p13) sub GR_new_sig = GR_sig, GR_sden_sig
177       movl GR_lden_sig = 0x7fffff0000000000 ;;
180 // Move expected result significand and signexp to fp regs
181 // Is y=nan?
182 // Form new exponent in case result exponent needs incrementing or decrementing
183 { .mfi
184       setf.exp FR_new_exp = GR_exp
185       fclass.m  p9,p0 = f9, 0xc3           
186 (p12) add GR_exp1 = 1, GR_exp
188 { .mib
189       setf.sig FR_new_sig = GR_new_sig
190 (p13) add GR_exp1 = -1, GR_exp
191 (p6)  br.ret.spnt    b0 ;;             // Exit if x=y
194 // Move largest normal signexp to fp reg for special cases
195 // Is x=zero?
196 { .mfi
197       setf.exp FR_lnorm_exp = GR_max_pexp
198       fclass.m  p7,p0 = f8, 0x7
199       nop.i 999
201 { .mfb
202       nop.m 999
203 (p8)  fma.s0 f8 = f8,f1,f9                     
204 (p8)  br.ret.spnt    b0 ;;             // Exit if x=nan
207 // Move exp+-1 and smallest normal significand to fp regs for special cases
208 // Is x=inf?
209 { .mfi
210       setf.exp FR_exp1 = GR_exp1
211       fclass.m  p6,p0 = f8, 0x23           
212       addl GR_exp_mask = 0x1ffff, r0
214 { .mfb
215       setf.sig FR_snorm_sig = GR_snorm_sig
216 (p9)  fma.s0 f8 = f8,f1,f9                     
217 (p9)  br.ret.spnt    b0 ;;             // Exit if y=nan
220 // Move largest denormal significand to fp regs for special cases
221 // Save x
222 { .mfb
223       setf.sig FR_lden_sig = GR_lden_sig
224       mov FR_save_f8 = f8
225 (p7)  br.cond.spnt NEXT_ZERO ;;   // Exit if x=0   
228 // Mask off the sign to get x_exp
229 { .mfb
230       and GR_x_exp = GR_exp_mask, GR_exp
231       nop.f 999
232 (p6)  br.cond.spnt NEXT_INF ;;   // Exit if x=inf   
235 // Check 6 special cases when significand rolls over:
236 //  1 sig size incr, x_sig=max_sig, x_exp < max_exp
237 //     Set p6, result is sig=min_sig, exp++
238 //  2 sig size incr, x_sig=max_sig, x_exp >= max_exp
239 //     Set p7, result is inf, signal overflow
240 //  3 sig size decr, x_sig=min_sig, x_exp > min_exp
241 //     Set p8, result is sig=max_sig, exp--
242 //  4 sig size decr, x_sig=min_sig, x_exp = min_exp
243 //     Set p9, result is sig=max_den_sig, exp same, signal underflow and inexact
244 //  5 sig size decr, x_sig=min_den_sig, x_exp = min_exp
245 //     Set p10, result is zero, sign of x, signal underflow and inexact
246 //  6 sig size decr, x_sig=min_sig, x_exp < min_exp 
247 //     Set p14, result is zero, sign of x, signal underflow and inexact
249 // Form exponent of smallest float denormal (if normalized register format)
250 { .mmi
251       adds GR_min_den_rexp = -23, GR_min_pexp
252 (p12) cmp.eq.unc p6,p0 = GR_new_sig, r0
253 (p13) cmp.eq.unc p8,p10 = GR_new_sig, GR_lden_sig ;;
256 { .mmi
257 (p6)  cmp.lt.unc p6,p7 = GR_x_exp, GR_max_pexp
258 (p8)  cmp.gt.unc p8,p9 = GR_x_exp, GR_min_pexp
259 (p10) cmp.eq.unc p10,p0 = GR_new_sig, r0 ;;
262 // Create small normal in case need to generate underflow flag
263 { .mfi
264 (p10) cmp.le.unc p10,p0 = GR_x_exp, GR_min_pexp
265       fmerge.se FR_tmp = FR_sden_exp, FR_lnorm_sig
266 (p9)  cmp.gt.unc p9,p14 = GR_x_exp, GR_min_den_rexp
268 // Branch if cases 1, 2, 3
269 { .bbb
270 (p6)  br.cond.spnt NEXT_EXPUP
271 (p7)  br.cond.spnt NEXT_OVERFLOW
272 (p8)  br.cond.spnt NEXT_EXPDOWN ;;
275 // Branch if cases 4, 5, 6
276 { .bbb
277 (p9)  br.cond.spnt NEXT_NORM_TO_DENORM
278 (p10) br.cond.spnt NEXT_UNDERFLOW_TO_ZERO
279 (p14) br.cond.spnt NEXT_UNDERFLOW_TO_ZERO ;;
282 // Here if no special cases
283 // Set p6 if result will be a denormal, so can force underflow flag
284 //    Case 1:  x_exp=min_exp, x_sig=unnormalized
285 //    Case 2:  x_exp<min_exp
286 { .mfi
287       cmp.lt p6,p7 = GR_x_exp, GR_min_pexp
288       fmerge.se f8 = FR_new_exp, FR_new_sig
289       nop.i 999 ;;
292 { .mfi
293       nop.m 999
294       nop.f 999
295 (p7)  tbit.z p6,p0 = GR_new_sig, 63 ;;
298 NEXT_COMMON_FINISH:
299 // Force underflow and inexact if denormal result
300 { .mfi
301       nop.m 999
302 (p6)  fma.s.s0 FR_tmp = FR_tmp,FR_tmp,f0
303       nop.i 999
305 { .mfb
306       nop.m 999
307       fnorm.s.s0 f8 = f8 // Final normalization to result precision
308 (p6)  br.cond.spnt NEXT_UNDERFLOW ;;
311 { .mfb
312       nop.m 999
313       nop.f 999
314       br.ret.sptk b0;;
317 //Special cases
318 NEXT_EXPUP:
319 { .mfb
320       cmp.lt p6,p7 = GR_x_exp, GR_min_pexp
321       fmerge.se f8 = FR_exp1, FR_snorm_sig
322       br.cond.sptk NEXT_COMMON_FINISH ;;
325 NEXT_EXPDOWN:
326 { .mfb
327       cmp.lt p6,p7 = GR_x_exp, GR_min_pexp
328       fmerge.se f8 = FR_exp1, FR_lnorm_sig
329       br.cond.sptk NEXT_COMMON_FINISH ;;
332 NEXT_NORM_TO_DENORM:
333 { .mfi
334       nop.m 999
335       fmerge.se f8 = FR_new_exp, FR_lden_sig
336       nop.i 999
338 // Force underflow and inexact
339 { .mfb
340       nop.m 999
341       fma.s.s0 FR_tmp = FR_tmp,FR_tmp,f0
342       br.cond.sptk NEXT_UNDERFLOW ;;
345 NEXT_UNDERFLOW_TO_ZERO:
346 { .mfb
347       cmp.eq p6,p0 = r0,r0
348       fmerge.s f8 = FR_save_f8,f0
349       br.cond.sptk NEXT_COMMON_FINISH ;;
352 NEXT_INF: 
353 // Here if f8 is +- infinity
354 // INF
355 // if f8 is +inf, no matter what y is return  largest float
356 // if f8 is -inf, no matter what y is return -largest float
358 { .mfi
359       nop.m 999
360       fmerge.se FR_lnorm = FR_lnorm_exp,FR_lnorm_sig
361       nop.i 999 ;;
364 { .mfb
365       nop.m 999
366       fmerge.s f8 = f8,FR_lnorm                
367       br.ret.sptk    b0 ;;                        
370 NEXT_ZERO: 
372 // Here if f8 is +- zero
373 // ZERO
374 // if f8 is zero and y is +, return + smallest float denormal 
375 // if f8 is zero and y is -, return - smallest float denormal 
377 { .mfi
378       nop.m 999
379       fmerge.se FR_sden = FR_sden_exp,FR_sden_sig
380       nop.i 999 ;;
383 // Create small normal to generate underflow flag
384 { .mfi
385       nop.m 999
386       fmerge.se FR_tmp = FR_sden_exp, FR_lnorm_sig
387       nop.i 999 ;;
390 // Add correct sign from direction arg
391 { .mfi
392       nop.m 999
393       fmerge.s f8 = f9,FR_sden                
394       nop.i 999 ;;
397 // Force underflow and inexact flags
398 { .mfb
399       nop.m 999
400       fma.s.s0 FR_tmp = FR_tmp,FR_tmp,f0
401       br.cond.sptk NEXT_UNDERFLOW ;;
404 NEXT_UNDERFLOW: 
405 // Here if result is a denorm, or input is finite and result is zero
406 // Call error support to report possible range error
407 { .mib
408       alloc          r32=ar.pfs,2,2,4,0
409       mov           GR_Parameter_TAG = 269      // Error code
410       br.cond.sptk  __libm_error_region    // Branch to error call
414 NEXT_OVERFLOW: 
415 // Here if input is finite, but result will be infinite
416 // Use frcpa to generate infinity of correct sign
417 // Call error support to report possible range error
418 { .mfi
419       alloc          r32=ar.pfs,2,2,4,0
420       frcpa.s1 f8,p6 = FR_save_f8, f0
421       nop.i 999 ;;
424 // Create largest double
425 { .mfi
426       nop.m 999
427       fmerge.se FR_lnorm = FR_lnorm_exp,FR_lnorm_sig
428       nop.i 999 ;;
431 // Force overflow and inexact flags to be set
432 { .mfb
433       mov           GR_Parameter_TAG = 155      // Error code
434       fma.s.s0 FR_tmp = FR_lnorm,FR_lnorm,f0
435       br.cond.sptk  __libm_error_region    // Branch to error call
439 GLOBAL_LIBM_END(nextafterf)
442 LOCAL_LIBM_ENTRY(__libm_error_region)
443 .prologue
445 // (1)
446 { .mfi
447         add   GR_Parameter_Y=-32,sp             // Parameter 2 value
448         nop.f 0
449 .save   ar.pfs,GR_SAVE_PFS
450         mov  GR_SAVE_PFS=ar.pfs                 // Save ar.pfs
452 { .mfi
453 .fframe 64
454         add sp=-64,sp                          // Create new stack
455         nop.f 0
456         mov GR_SAVE_GP=gp                      // Save gp
460 // (2)
461 { .mmi
462         stfs [GR_Parameter_Y] = f9,16         // STORE Parameter 2 on stack
463         add GR_Parameter_X = 16,sp            // Parameter 1 address
464 .save   b0, GR_SAVE_B0
465         mov GR_SAVE_B0=b0                     // Save b0
468 .body
469 // (3)
470 { .mib
471         stfs [GR_Parameter_X] = FR_save_f8              // STORE Parameter 1 on stack
472         add   GR_Parameter_RESULT = 0,GR_Parameter_Y           // Parameter 3 address
473         nop.b 0                                
475 { .mib
476         stfs [GR_Parameter_Y] = f8              // STORE Parameter 3 on stack
477         add   GR_Parameter_Y = -16,GR_Parameter_Y
478         br.call.sptk b0=__libm_error_support#   // Call error handling function
480 { .mmi
481         nop.m 0
482         nop.m 0
483         add   GR_Parameter_RESULT = 48,sp
486 // (4)
487 { .mmi
488         ldfs  f8 = [GR_Parameter_RESULT]       // Get return result off stack
489 .restore sp
490         add   sp = 64,sp                       // Restore stack pointer
491         mov   b0 = GR_SAVE_B0                  // Restore return address
493 { .mib
494         mov   gp = GR_SAVE_GP                  // Restore gp
495         mov   ar.pfs = GR_SAVE_PFS             // Restore ar.pfs
496         br.ret.sptk     b0                     // Return
499 LOCAL_LIBM_END(__libm_error_region)
502 .type   __libm_error_support#,@function
503 .global __libm_error_support#