4 // Copyright (c) 2000 - 2004, Intel Corporation
5 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
12 // * Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
15 // * Redistributions in binary form must reproduce the above copyright
16 // notice, this list of conditions and the following disclaimer in the
17 // documentation and/or other materials provided with the distribution.
19 // * The name of Intel Corporation may not be used to endorse or promote
20 // products derived from this software without specific prior written
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR ITS
27 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
31 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR TORT (INCLUDING
32 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 // Intel Corporation is the author of this code, and requests that all
36 // problem reports or change requests be submitted to it directly at
37 // http://www.intel.com/software/products/opensource/libraries/num.htm.
40 //==============================================================
41 // 02/02/00 Initial version
42 // 03/03/00 Modified to conform to C9X, and improve speed of main path
43 // 03/14/00 Fixed case where x is a power of 2, and x > y, improved speed
44 // 04/04/00 Unwind support added
45 // 05/12/00 Fixed erroneous denormal flag setting for exponent change cases 1,3
46 // 08/15/00 Bundle added after call to __libm_error_support to properly
47 // set [the previously overwritten] GR_Parameter_RESULT.
48 // 09/09/00 Updated fcmp so that qnans do not raise invalid
49 // 12/15/00 Corrected behavior when both args are zero to conform to C99, and
50 // fixed flag settings for several cases
51 // 05/20/02 Cleaned up namespace and sf0 syntax
52 // 02/10/03 Reordered header: .section, .global, .proc, .align
53 // 12/14/04 Added error handling on underflow.
56 //==============================================================
57 // double nextafter( double x, double y );
58 // input floating point f8, f9
59 // output floating point f8
62 //==============================================================
78 // r36-39 parameters for libm_error_support
86 GR_Parameter_RESULT = r38
87 GR_Parameter_TAG = r39
104 // Overview of operation
105 //==============================================================
106 // nextafter determines the next representable value
107 // after x in the direction of y.
111 GLOBAL_LIBM_ENTRY(nextafter)
113 // Extract signexp from x
114 // Is x < y ? p10 if yes, p11 if no
115 // Form smallest denormal significand = ulp size
118 fcmp.lt.s1 p10,p11 = f8, f9
119 addl GR_sden_sig = 0x800, r0
121 // Form largest normal significand 0xfffffffffffff800
122 // Form smallest normal exponent
124 addl GR_lnorm_sig = -0x800,r0
126 addl GR_min_pexp = 0x0fc01, r0 ;;
128 // Extract significand from x
130 // Form largest normal exponent
133 fcmp.eq.s0 p6,p0 = f8, f9
134 addl GR_max_pexp = 0x103fe, r0
136 // Move largest normal significand to fp reg for special cases
138 setf.sig FR_lnorm_sig = GR_lnorm_sig
140 addl GR_sign_mask = 0x20000, r0 ;;
143 // Move smallest denormal significand and signexp to fp regs
145 // Set p12 and p13 based on whether significand increases or decreases
146 // It increases (p12 set) if x<y and x>=0 or if x>y and x<0
147 // It decreases (p13 set) if x<y and x<0 or if x>y and x>=0
149 setf.sig FR_sden_sig = GR_sden_sig
150 fclass.m p8,p0 = f8, 0xc3
151 (p10) cmp.lt p12,p13 = GR_exp, GR_sign_mask
154 setf.exp FR_sden_exp = GR_min_pexp
155 (p11) cmp.ge p12,p13 = GR_exp, GR_sign_mask ;;
158 .pred.rel "mutex",p12,p13
160 // Form expected new significand, adding or subtracting 1 ulp increment
161 // If x=y set result to y
162 // Form smallest normal significand and largest denormal significand
164 (p12) add GR_new_sig = GR_sig, GR_sden_sig
165 (p6) fmerge.s f8=f9,f9
166 dep.z GR_snorm_sig = 1,63,1 // 0x8000000000000000
169 (p13) sub GR_new_sig = GR_sig, GR_sden_sig
170 movl GR_lden_sig = 0x7ffffffffffff800 ;;
173 // Move expected result significand and signexp to fp regs
175 // Form new exponent in case result exponent needs incrementing or decrementing
177 setf.exp FR_new_exp = GR_exp
178 fclass.m p9,p0 = f9, 0xc3
179 (p12) add GR_exp1 = 1, GR_exp
182 setf.sig FR_new_sig = GR_new_sig
183 (p13) add GR_exp1 = -1, GR_exp
184 (p6) br.ret.spnt b0 ;; // Exit if x=y
187 // Move largest normal signexp to fp reg for special cases
190 setf.exp FR_lnorm_exp = GR_max_pexp
191 fclass.m p7,p0 = f8, 0x7
196 (p8) fma.s0 f8 = f8,f1,f9
197 (p8) br.ret.spnt b0 ;; // Exit if x=nan
200 // Move exp+-1 and smallest normal significand to fp regs for special cases
203 setf.exp FR_exp1 = GR_exp1
204 fclass.m p6,p0 = f8, 0x23
205 addl GR_exp_mask = 0x1ffff, r0
208 setf.sig FR_snorm_sig = GR_snorm_sig
209 (p9) fma.s0 f8 = f8,f1,f9
210 (p9) br.ret.spnt b0 ;; // Exit if y=nan
213 // Move largest denormal significand to fp regs for special cases
216 setf.sig FR_lden_sig = GR_lden_sig
218 (p7) br.cond.spnt NEXT_ZERO ;; // Exit if x=0
221 // Mask off the sign to get x_exp
223 and GR_x_exp = GR_exp_mask, GR_exp
225 (p6) br.cond.spnt NEXT_INF ;; // Exit if x=inf
228 // Check 6 special cases when significand rolls over:
229 // 1 sig size incr, x_sig=max_sig, x_exp < max_exp
230 // Set p6, result is sig=min_sig, exp++
231 // 2 sig size incr, x_sig=max_sig, x_exp >= max_exp
232 // Set p7, result is inf, signal overflow
233 // 3 sig size decr, x_sig=min_sig, x_exp > min_exp
234 // Set p8, result is sig=max_sig, exp--
235 // 4 sig size decr, x_sig=min_sig, x_exp = min_exp
236 // Set p9, result is sig=max_den_sig, exp same, signal underflow and inexact
237 // 5 sig size decr, x_sig=min_den_sig, x_exp = min_exp
238 // Set p10, result is zero, sign of x, signal underflow and inexact
239 // 6 sig size decr, x_sig=min_sig, x_exp < min_exp
240 // Set p14, result is zero, sign of x, signal underflow and inexact
242 // Form exponent of smallest double denormal (if normalized register format)
244 adds GR_min_den_rexp = -52, GR_min_pexp
245 (p12) cmp.eq.unc p6,p0 = GR_new_sig, r0
246 (p13) cmp.eq.unc p8,p10 = GR_new_sig, GR_lden_sig ;;
250 (p6) cmp.lt.unc p6,p7 = GR_x_exp, GR_max_pexp
251 (p8) cmp.gt.unc p8,p9 = GR_x_exp, GR_min_pexp
252 (p10) cmp.eq.unc p10,p0 = GR_new_sig, r0 ;;
255 // Create small normal in case need to generate underflow flag
257 (p10) cmp.le.unc p10,p0 = GR_x_exp, GR_min_pexp
258 fmerge.se FR_tmp = FR_sden_exp, FR_lnorm_sig
259 (p9) cmp.gt.unc p9,p14 = GR_x_exp, GR_min_den_rexp
261 // Branch if cases 1, 2, 3
263 (p6) br.cond.spnt NEXT_EXPUP
264 (p7) br.cond.spnt NEXT_OVERFLOW
265 (p8) br.cond.spnt NEXT_EXPDOWN ;;
268 // Branch if cases 4, 5, 6
270 (p9) br.cond.spnt NEXT_NORM_TO_DENORM
271 (p10) br.cond.spnt NEXT_UNDERFLOW_TO_ZERO
272 (p14) br.cond.spnt NEXT_UNDERFLOW_TO_ZERO ;;
275 // Here if no special cases
276 // Set p6 if result will be a denormal, so can force underflow flag
277 // Case 1: x_exp=min_exp, x_sig=unnormalized
278 // Case 2: x_exp<min_exp
280 cmp.lt p6,p7 = GR_x_exp, GR_min_pexp
281 fmerge.se f8 = FR_new_exp, FR_new_sig
288 (p7) tbit.z p6,p0 = GR_new_sig, 63 ;;
292 // Force underflow and inexact if denormal result
295 (p6) fma.d.s0 FR_tmp = FR_tmp,FR_tmp,f0
300 fnorm.d.s0 f8 = f8 // Final normalization to result precision
301 (p6) br.cond.spnt NEXT_UNDERFLOW ;;
313 cmp.lt p6,p7 = GR_x_exp, GR_min_pexp
314 fmerge.se f8 = FR_exp1, FR_snorm_sig
315 br.cond.sptk NEXT_COMMON_FINISH ;;
320 cmp.lt p6,p7 = GR_x_exp, GR_min_pexp
321 fmerge.se f8 = FR_exp1, FR_lnorm_sig
322 br.cond.sptk NEXT_COMMON_FINISH ;;
328 fmerge.se f8 = FR_new_exp, FR_lden_sig
331 // Force underflow and inexact if denormal result
334 fma.d.s0 FR_tmp = FR_tmp,FR_tmp,f0
335 br.cond.sptk NEXT_UNDERFLOW ;;
338 NEXT_UNDERFLOW_TO_ZERO:
341 fmerge.s f8 = FR_save_f8,f0
342 br.cond.sptk NEXT_COMMON_FINISH ;;
346 // Here if f8 is +- infinity
348 // if f8 is +inf, no matter what y is return largest double
349 // if f8 is -inf, no matter what y is return -largest double
353 fmerge.se FR_lnorm = FR_lnorm_exp,FR_lnorm_sig
359 fmerge.s f8 = f8,FR_lnorm
365 // Here if f8 is +- zero
367 // if f8 is zero and y is +, return + smallest double denormal
368 // if f8 is zero and y is -, return - smallest double denormal
372 fmerge.se FR_sden = FR_sden_exp,FR_sden_sig
376 // Create small normal to generate underflow flag
379 fmerge.se FR_tmp = FR_sden_exp, FR_lnorm_sig
383 // Add correct sign from direction arg
386 fmerge.s f8 = f9,FR_sden
390 // Force underflow and inexact flags
393 fma.d.s0 FR_tmp = FR_tmp,FR_tmp,f0
394 br.cond.sptk NEXT_UNDERFLOW ;;
398 // Here if result is a denorm, or input is finite and result is zero
399 // Call error support to report possible range error
401 alloc r32=ar.pfs,2,2,4,0
402 mov GR_Parameter_TAG = 268 // Error code
403 br.cond.sptk __libm_error_region // Branch to error call
408 // Here if input is finite, but result will be infinite
409 // Use frcpa to generate infinity of correct sign
410 // Call error support to report possible range error
412 alloc r32=ar.pfs,2,2,4,0
413 frcpa.s1 f8,p6 = FR_save_f8, f0
417 // Create largest double
420 fmerge.se FR_lnorm = FR_lnorm_exp,FR_lnorm_sig
424 // Force overflow and inexact flags to be set
426 mov GR_Parameter_TAG = 154 // Error code
427 fma.d.s0 FR_tmp = FR_lnorm,FR_lnorm,f0
428 br.cond.sptk __libm_error_region // Branch to error call
432 GLOBAL_LIBM_END(nextafter)
433 libm_alias_double_other (nextafter, nextafter)
436 LOCAL_LIBM_ENTRY(__libm_error_region)
441 add GR_Parameter_Y=-32,sp // Parameter 2 value
443 .save ar.pfs,GR_SAVE_PFS
444 mov GR_SAVE_PFS=ar.pfs // Save ar.pfs
448 add sp=-64,sp // Create new stack
450 mov GR_SAVE_GP=gp // Save gp
456 stfd [GR_Parameter_Y] = f9,16 // STORE Parameter 2 on stack
457 add GR_Parameter_X = 16,sp // Parameter 1 address
459 mov GR_SAVE_B0=b0 // Save b0
465 stfd [GR_Parameter_X] = FR_save_f8 // STORE Parameter 1 on stack
466 add GR_Parameter_RESULT = 0,GR_Parameter_Y // Parameter 3 address
470 stfd [GR_Parameter_Y] = f8 // STORE Parameter 3 on stack
471 add GR_Parameter_Y = -16,GR_Parameter_Y
472 br.call.sptk b0=__libm_error_support# // Call error handling function
477 add GR_Parameter_RESULT = 48,sp
482 ldfd f8 = [GR_Parameter_RESULT] // Get return result off stack
484 add sp = 64,sp // Restore stack pointer
485 mov b0 = GR_SAVE_B0 // Restore return address
488 mov gp = GR_SAVE_GP // Restore gp
489 mov ar.pfs = GR_SAVE_PFS // Restore ar.pfs
490 br.ret.sptk b0 // Return
493 LOCAL_LIBM_END(__libm_error_region)
496 .type __libm_error_support#,@function
497 .global __libm_error_support#