2.5-18.1
[glibc.git] / sysdeps / ia64 / fpu / s_nexttoward.S
blob741fea02b062d822bbd04792b71fec7ea837d73a
1 .file "nexttoward.s"
4 // Copyright (c) 2001 - 2004, Intel Corporation
5 // All rights reserved.
6 //
7 // Contributed 2001 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 // 08/15/01 Initial version 
43 // 08/23/01 Corrected error tag number
44 // 05/20/02 Cleaned up namespace and sf0 syntax
45 // 02/10/03 Reordered header: .section, .global, .proc, .align
46 // 12/14/04 Added error handling on underflow.
48 // API
49 //==============================================================
50 // double nexttoward( double x, long double y );
51 // input  floating point f8, f9
52 // output floating point f8
54 // Registers used
55 //==============================================================
56 GR_max_pexp     = r14
57 GR_min_pexp     = r15
58 GR_exp          = r16
59 GR_sig          = r17
60 GR_lnorm_sig    = r18
61 GR_sign_mask    = r19
62 GR_exp_mask     = r20
63 GR_sden_sig     = r21
64 GR_new_sig      = r22
65 GR_new_exp      = r23
66 GR_lden_sig     = r24
67 GR_snorm_sig    = r25
68 GR_exp1         = r26
69 GR_x_exp        = r27
70 GR_min_den_rexp = r28
71 // r36-39 parameters for libm_error_support
73 GR_SAVE_B0                = r34
74 GR_SAVE_GP                = r35
75 GR_SAVE_PFS               = r32
77 GR_Parameter_X            = r36
78 GR_Parameter_Y            = r37
79 GR_Parameter_RESULT       = r38
80 GR_Parameter_TAG          = r39
82 FR_lnorm_sig       = f10
83 FR_lnorm_exp       = f11
84 FR_lnorm           = f12
85 FR_sden_sig        = f13
86 FR_sden_exp        = f14
87 FR_sden            = f15
88 FR_save_f8         = f33
89 FR_new_exp         = f34
90 FR_new_sig         = f35
91 FR_lden_sig        = f36
92 FR_snorm_sig       = f37
93 FR_exp1            = f38
94 FR_tmp             = f39
97 // Overview of operation
98 //==============================================================
99 // nexttoward determines the next representable value 
100 // after x in the direction of y. 
103 .section .text
104 GLOBAL_LIBM_ENTRY(nexttoward)
106 // Extract signexp from x
107 // Is x < y ?  p10 if yes, p11 if no
108 // Form smallest denormal significand = ulp size
109 { .mfi
110       getf.exp GR_exp      = f8
111       fcmp.lt.s1 p10,p11 = f8, f9                
112       addl GR_sden_sig = 0x800, r0
114 // Form largest normal significand 0xfffffffffffff800
115 // Form smallest normal exponent
116 { .mfi
117       addl GR_lnorm_sig = -0x800,r0
118       nop.f 999
119       addl GR_min_pexp = 0x0fc01, r0 ;;
121 // Extract significand from x
122 // Is x=y?
123 // Form largest normal exponent
124 { .mfi
125       getf.sig GR_sig      = f8
126       fcmp.eq.s0 p6,p0 = f8, f9                
127       addl GR_max_pexp = 0x103fe, r0
129 // Move largest normal significand to fp reg for special cases
130 { .mfi
131       setf.sig FR_lnorm_sig = GR_lnorm_sig
132       nop.f 999
133       addl GR_sign_mask = 0x20000, r0 ;;
136 // Move smallest denormal significand and signexp to fp regs
137 // Is x=nan?
138 // Set p12 and p13 based on whether significand increases or decreases
139 // It increases (p12 set) if x<y and x>=0 or if x>y and x<0
140 // It decreases (p13 set) if x<y and x<0  or if x>y and x>=0
141 { .mfi
142       setf.sig FR_sden_sig = GR_sden_sig
143       fclass.m  p8,p0 = f8, 0xc3           
144 (p10) cmp.lt p12,p13 = GR_exp, GR_sign_mask
146 { .mfi
147       setf.exp FR_sden_exp = GR_min_pexp
148 (p11) cmp.ge p12,p13 = GR_exp, GR_sign_mask ;;
151 .pred.rel "mutex",p12,p13
153 // Form expected new significand, adding or subtracting 1 ulp increment
154 // If x=y set result to y
155 // Form smallest normal significand and largest denormal significand
156 { .mfi
157 (p12) add GR_new_sig = GR_sig, GR_sden_sig
158 (p6)  fnorm.d.s0 f8=f9  //Normalise
159       dep.z GR_snorm_sig = 1,63,1 // 0x8000000000000000
161 { .mlx
162 (p13) sub GR_new_sig = GR_sig, GR_sden_sig
163       movl GR_lden_sig = 0x7ffffffffffff800 ;;
166 // Move expected result significand and signexp to fp regs
167 // Is y=nan?
168 // Form new exponent in case result exponent needs incrementing or decrementing
169 { .mfi
170       setf.exp FR_new_exp = GR_exp
171       fclass.m  p9,p0 = f9, 0xc3           
172 (p12) add GR_exp1 = 1, GR_exp
174 { .mib
175       setf.sig FR_new_sig = GR_new_sig
176 (p13) add GR_exp1 = -1, GR_exp
177 (p6)  br.ret.spnt    b0 ;;             // Exit if x=y
180 // Move largest normal signexp to fp reg for special cases
181 // Is x=zero?
182 { .mfi
183       setf.exp FR_lnorm_exp = GR_max_pexp
184       fclass.m  p7,p0 = f8, 0x7
185       nop.i 999
187 { .mfb
188       nop.m 999
189 (p8)  fma.s0 f8 = f8,f1,f9                     
190 (p8)  br.ret.spnt    b0 ;;             // Exit if x=nan
193 // Move exp+-1 and smallest normal significand to fp regs for special cases
194 // Is x=inf?
195 { .mfi
196       setf.exp FR_exp1 = GR_exp1
197       fclass.m  p6,p0 = f8, 0x23           
198       addl GR_exp_mask = 0x1ffff, r0
200 { .mfb
201       setf.sig FR_snorm_sig = GR_snorm_sig
202 (p9)  fma.s0 f8 = f8,f1,f9                     
203 (p9)  br.ret.spnt    b0 ;;             // Exit if y=nan
206 // Move largest denormal significand to fp regs for special cases
207 // Save x
208 { .mfb
209       setf.sig FR_lden_sig = GR_lden_sig
210       mov FR_save_f8 = f8
211 (p7)  br.cond.spnt NEXT_ZERO ;;   // Exit if x=0   
214 // Mask off the sign to get x_exp
215 { .mfb
216       and GR_x_exp = GR_exp_mask, GR_exp
217       nop.f 999
218 (p6)  br.cond.spnt NEXT_INF ;;   // Exit if x=inf   
221 // Check 6 special cases when significand rolls over:
222 //  1 sig size incr, x_sig=max_sig, x_exp < max_exp
223 //     Set p6, result is sig=min_sig, exp++
224 //  2 sig size incr, x_sig=max_sig, x_exp >= max_exp
225 //     Set p7, result is inf, signal overflow
226 //  3 sig size decr, x_sig=min_sig, x_exp > min_exp
227 //     Set p8, result is sig=max_sig, exp--
228 //  4 sig size decr, x_sig=min_sig, x_exp = min_exp
229 //     Set p9, result is sig=max_den_sig, exp same, signal underflow and inexact
230 //  5 sig size decr, x_sig=min_den_sig, x_exp = min_exp
231 //     Set p10, result is zero, sign of x, signal underflow and inexact
232 //  6 sig size decr, x_sig=min_sig, x_exp < min_exp 
233 //     Set p14, result is zero, sign of x, signal underflow and inexact
235 // Form exponent of smallest double denormal (if normalized register format)
236 { .mmi
237       adds GR_min_den_rexp = -52, GR_min_pexp
238 (p12) cmp.eq.unc p6,p0 = GR_new_sig, r0
239 (p13) cmp.eq.unc p8,p10 = GR_new_sig, GR_lden_sig ;;
242 { .mmi
243 (p6)  cmp.lt.unc p6,p7 = GR_x_exp, GR_max_pexp
244 (p8)  cmp.gt.unc p8,p9 = GR_x_exp, GR_min_pexp
245 (p10) cmp.eq.unc p10,p0 = GR_new_sig, r0 ;;
248 // Create small normal in case need to generate underflow flag
249 { .mfi
250 (p10) cmp.le.unc p10,p0 = GR_x_exp, GR_min_pexp
251       fmerge.se FR_tmp = FR_sden_exp, FR_lnorm_sig
252 (p9)  cmp.gt.unc p9,p14 = GR_x_exp, GR_min_den_rexp
254 // Branch if cases 1, 2, 3
255 { .bbb
256 (p6)  br.cond.spnt NEXT_EXPUP
257 (p7)  br.cond.spnt NEXT_OVERFLOW
258 (p8)  br.cond.spnt NEXT_EXPDOWN ;;
261 // Branch if cases 4, 5, 6
262 { .bbb
263 (p9)  br.cond.spnt NEXT_NORM_TO_DENORM
264 (p10) br.cond.spnt NEXT_UNDERFLOW_TO_ZERO
265 (p14) br.cond.spnt NEXT_UNDERFLOW_TO_ZERO ;;
268 // Here if no special cases
269 // Set p6 if result will be a denormal, so can force underflow flag
270 //    Case 1:  x_exp=min_exp, x_sig=unnormalized
271 //    Case 2:  x_exp<min_exp
272 { .mfi
273       cmp.lt p6,p7 = GR_x_exp, GR_min_pexp
274       fmerge.se f8 = FR_new_exp, FR_new_sig
275       nop.i 999 ;;
278 { .mfi
279       nop.m 999
280       nop.f 999
281 (p7)  tbit.z p6,p0 = GR_new_sig, 63 ;;
284 NEXT_COMMON_FINISH:
285 // Force underflow and inexact if denormal result
286 { .mfi
287       nop.m 999
288 (p6)  fma.d.s0 FR_tmp = FR_tmp,FR_tmp,f0
289       nop.i 999
291 { .mfb
292       nop.m 999
293       fnorm.d.s0 f8 = f8 // Final normalization to result precision
294 (p6)  br.cond.spnt NEXT_UNDERFLOW ;;
297 { .mfb
298       nop.m 999
299       nop.f 999
300       br.ret.sptk b0;;
303 //Special cases
304 NEXT_EXPUP:
305 { .mfb
306       cmp.lt p6,p7 = GR_x_exp, GR_min_pexp
307       fmerge.se f8 = FR_exp1, FR_snorm_sig
308       br.cond.sptk NEXT_COMMON_FINISH ;;
311 NEXT_EXPDOWN:
312 { .mfb
313       cmp.lt p6,p7 = GR_x_exp, GR_min_pexp
314       fmerge.se f8 = FR_exp1, FR_lnorm_sig
315       br.cond.sptk NEXT_COMMON_FINISH ;;
318 NEXT_NORM_TO_DENORM:
319 { .mfi
320       nop.m 999
321       fmerge.se f8 = FR_new_exp, FR_lden_sig
322       nop.i 999
324 // Force underflow and inexact if denormal result
325 { .mfb
326       nop.m 999
327       fma.d.s0 FR_tmp = FR_tmp,FR_tmp,f0
328       br.cond.sptk NEXT_UNDERFLOW ;;
331 NEXT_UNDERFLOW_TO_ZERO:
332 { .mfb
333       cmp.eq p6,p0 = r0,r0
334       fmerge.s f8 = FR_save_f8,f0
335       br.cond.sptk NEXT_COMMON_FINISH ;;
338 NEXT_INF: 
339 // Here if f8 is +- infinity
340 // INF
341 // if f8 is +inf, no matter what y is return  largest double
342 // if f8 is -inf, no matter what y is return -largest double
344 { .mfi
345       nop.m 999
346       fmerge.se FR_lnorm = FR_lnorm_exp,FR_lnorm_sig
347       nop.i 999 ;;
350 { .mfb
351       nop.m 999
352       fmerge.s f8 = f8,FR_lnorm                
353       br.ret.sptk    b0 ;;                        
356 NEXT_ZERO: 
358 // Here if f8 is +- zero
359 // ZERO
360 // if f8 is zero and y is +, return + smallest double denormal 
361 // if f8 is zero and y is -, return - smallest double denormal 
363 { .mfi
364       nop.m 999
365       fmerge.se FR_sden = FR_sden_exp,FR_sden_sig
366       nop.i 999 ;;
369 // Create small normal to generate underflow flag
370 { .mfi
371       nop.m 999
372       fmerge.se FR_tmp = FR_sden_exp, FR_lnorm_sig
373       nop.i 999 ;;
376 // Add correct sign from direction arg
377 { .mfi
378       nop.m 999
379       fmerge.s f8 = f9,FR_sden                
380       nop.i 999 ;;
383 // Force underflow and inexact flags
384 { .mfb
385       nop.m 999
386       fma.d.s0 FR_tmp = FR_tmp,FR_tmp,f0
387       br.cond.sptk NEXT_UNDERFLOW ;;
390 NEXT_UNDERFLOW: 
391 // Here if result is a denorm, or input is finite and result is zero
392 // Call error support to report possible range error
393 { .mib
394       alloc          r32=ar.pfs,2,2,4,0
395       mov           GR_Parameter_TAG = 271      // Error code
396       br.cond.sptk  __libm_error_region    // Branch to error call
400 NEXT_OVERFLOW: 
401 // Here if input is finite, but result will be infinite
402 // Use frcpa to generate infinity of correct sign
403 // Call error support to report possible range error
404 { .mfi
405       alloc          r32=ar.pfs,2,2,4,0
406       frcpa.s1 f8,p6 = FR_save_f8, f0
407       nop.i 999 ;;
410 // Create largest double
411 { .mfi
412       nop.m 999
413       fmerge.se FR_lnorm = FR_lnorm_exp,FR_lnorm_sig
414       nop.i 999 ;;
417 // Force overflow and inexact flags to be set
418 { .mfb
419       mov           GR_Parameter_TAG = 199      // Error code
420       fma.d.s0 FR_tmp = FR_lnorm,FR_lnorm,f0
421       br.cond.sptk  __libm_error_region    // Branch to error call
425 GLOBAL_LIBM_END(nexttoward)
428 LOCAL_LIBM_ENTRY(__libm_error_region)
429 .prologue
431 // (1)
432 { .mfi
433         add   GR_Parameter_Y=-32,sp             // Parameter 2 value
434         nop.f 0
435 .save   ar.pfs,GR_SAVE_PFS
436         mov  GR_SAVE_PFS=ar.pfs                 // Save ar.pfs
438 { .mfi
439 .fframe 64
440         add sp=-64,sp                          // Create new stack
441         nop.f 0
442         mov GR_SAVE_GP=gp                      // Save gp
446 // (2)
447 { .mmi
448         stfd [GR_Parameter_Y] = f9,16         // STORE Parameter 2 on stack
449         add GR_Parameter_X = 16,sp            // Parameter 1 address
450 .save   b0, GR_SAVE_B0
451         mov GR_SAVE_B0=b0                     // Save b0
454 .body
455 // (3)
456 { .mib
457         stfd [GR_Parameter_X] = FR_save_f8              // STORE Parameter 1 on stack
458         add   GR_Parameter_RESULT = 0,GR_Parameter_Y           // Parameter 3 address
459         nop.b 0                                
461 { .mib
462         stfd [GR_Parameter_Y] = f8              // STORE Parameter 3 on stack
463         add   GR_Parameter_Y = -16,GR_Parameter_Y
464         br.call.sptk b0=__libm_error_support#   // Call error handling function
466 { .mmi
467         nop.m 0
468         nop.m 0
469         add   GR_Parameter_RESULT = 48,sp
472 // (4)
473 { .mmi
474         ldfd  f8 = [GR_Parameter_RESULT]       // Get return result off stack
475 .restore sp
476         add   sp = 64,sp                       // Restore stack pointer
477         mov   b0 = GR_SAVE_B0                  // Restore return address
479 { .mib
480         mov   gp = GR_SAVE_GP                  // Restore gp
481         mov   ar.pfs = GR_SAVE_PFS             // Restore ar.pfs
482         br.ret.sptk     b0                     // Return
485 LOCAL_LIBM_END(__libm_error_region)
488 .type   __libm_error_support#,@function
489 .global __libm_error_support#