4 // Copyright (c) 2001 - 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 // 08/15/01 Initial version
42 // 08/23/01 Corrected error tag number
43 // 05/20/02 Cleaned up namespace and sf0 syntax
44 // 02/10/03 Reordered header: .section, .global, .proc, .align
45 // 12/14/04 Added error handling on underflow.
48 //==============================================================
49 // double nexttoward( double x, long double y );
50 // input floating point f8, f9
51 // output floating point f8
54 //==============================================================
70 // r36-39 parameters for libm_error_support
78 GR_Parameter_RESULT = r38
79 GR_Parameter_TAG = r39
96 // Overview of operation
97 //==============================================================
98 // nexttoward determines the next representable value
99 // after x in the direction of y.
103 GLOBAL_LIBM_ENTRY(nexttoward)
105 // Extract signexp from x
106 // Is x < y ? p10 if yes, p11 if no
107 // Form smallest denormal significand = ulp size
110 fcmp.lt.s1 p10,p11 = f8, f9
111 addl GR_sden_sig = 0x800, r0
113 // Form largest normal significand 0xfffffffffffff800
114 // Form smallest normal exponent
116 addl GR_lnorm_sig = -0x800,r0
118 addl GR_min_pexp = 0x0fc01, r0 ;;
120 // Extract significand from x
122 // Form largest normal exponent
125 fcmp.eq.s0 p6,p0 = f8, f9
126 addl GR_max_pexp = 0x103fe, r0
128 // Move largest normal significand to fp reg for special cases
130 setf.sig FR_lnorm_sig = GR_lnorm_sig
132 addl GR_sign_mask = 0x20000, r0 ;;
135 // Move smallest denormal significand and signexp to fp regs
137 // Set p12 and p13 based on whether significand increases or decreases
138 // It increases (p12 set) if x<y and x>=0 or if x>y and x<0
139 // It decreases (p13 set) if x<y and x<0 or if x>y and x>=0
141 setf.sig FR_sden_sig = GR_sden_sig
142 fclass.m p8,p0 = f8, 0xc3
143 (p10) cmp.lt p12,p13 = GR_exp, GR_sign_mask
146 setf.exp FR_sden_exp = GR_min_pexp
147 (p11) cmp.ge p12,p13 = GR_exp, GR_sign_mask ;;
150 .pred.rel "mutex",p12,p13
152 // Form expected new significand, adding or subtracting 1 ulp increment
153 // If x=y set result to y
154 // Form smallest normal significand and largest denormal significand
156 (p12) add GR_new_sig = GR_sig, GR_sden_sig
157 (p6) fnorm.d.s0 f8=f9 //Normalise
158 dep.z GR_snorm_sig = 1,63,1 // 0x8000000000000000
161 (p13) sub GR_new_sig = GR_sig, GR_sden_sig
162 movl GR_lden_sig = 0x7ffffffffffff800 ;;
165 // Move expected result significand and signexp to fp regs
167 // Form new exponent in case result exponent needs incrementing or decrementing
169 setf.exp FR_new_exp = GR_exp
170 fclass.m p9,p0 = f9, 0xc3
171 (p12) add GR_exp1 = 1, GR_exp
174 setf.sig FR_new_sig = GR_new_sig
175 (p13) add GR_exp1 = -1, GR_exp
176 (p6) br.ret.spnt b0 ;; // Exit if x=y
179 // Move largest normal signexp to fp reg for special cases
182 setf.exp FR_lnorm_exp = GR_max_pexp
183 fclass.m p7,p0 = f8, 0x7
188 (p8) fma.s0 f8 = f8,f1,f9
189 (p8) br.ret.spnt b0 ;; // Exit if x=nan
192 // Move exp+-1 and smallest normal significand to fp regs for special cases
195 setf.exp FR_exp1 = GR_exp1
196 fclass.m p6,p0 = f8, 0x23
197 addl GR_exp_mask = 0x1ffff, r0
200 setf.sig FR_snorm_sig = GR_snorm_sig
201 (p9) fma.s0 f8 = f8,f1,f9
202 (p9) br.ret.spnt b0 ;; // Exit if y=nan
205 // Move largest denormal significand to fp regs for special cases
208 setf.sig FR_lden_sig = GR_lden_sig
210 (p7) br.cond.spnt NEXT_ZERO ;; // Exit if x=0
213 // Mask off the sign to get x_exp
215 and GR_x_exp = GR_exp_mask, GR_exp
217 (p6) br.cond.spnt NEXT_INF ;; // Exit if x=inf
220 // Check 6 special cases when significand rolls over:
221 // 1 sig size incr, x_sig=max_sig, x_exp < max_exp
222 // Set p6, result is sig=min_sig, exp++
223 // 2 sig size incr, x_sig=max_sig, x_exp >= max_exp
224 // Set p7, result is inf, signal overflow
225 // 3 sig size decr, x_sig=min_sig, x_exp > min_exp
226 // Set p8, result is sig=max_sig, exp--
227 // 4 sig size decr, x_sig=min_sig, x_exp = min_exp
228 // Set p9, result is sig=max_den_sig, exp same, signal underflow and inexact
229 // 5 sig size decr, x_sig=min_den_sig, x_exp = min_exp
230 // Set p10, result is zero, sign of x, signal underflow and inexact
231 // 6 sig size decr, x_sig=min_sig, x_exp < min_exp
232 // Set p14, result is zero, sign of x, signal underflow and inexact
234 // Form exponent of smallest double denormal (if normalized register format)
236 adds GR_min_den_rexp = -52, GR_min_pexp
237 (p12) cmp.eq.unc p6,p0 = GR_new_sig, r0
238 (p13) cmp.eq.unc p8,p10 = GR_new_sig, GR_lden_sig ;;
242 (p6) cmp.lt.unc p6,p7 = GR_x_exp, GR_max_pexp
243 (p8) cmp.gt.unc p8,p9 = GR_x_exp, GR_min_pexp
244 (p10) cmp.eq.unc p10,p0 = GR_new_sig, r0 ;;
247 // Create small normal in case need to generate underflow flag
249 (p10) cmp.le.unc p10,p0 = GR_x_exp, GR_min_pexp
250 fmerge.se FR_tmp = FR_sden_exp, FR_lnorm_sig
251 (p9) cmp.gt.unc p9,p14 = GR_x_exp, GR_min_den_rexp
253 // Branch if cases 1, 2, 3
255 (p6) br.cond.spnt NEXT_EXPUP
256 (p7) br.cond.spnt NEXT_OVERFLOW
257 (p8) br.cond.spnt NEXT_EXPDOWN ;;
260 // Branch if cases 4, 5, 6
262 (p9) br.cond.spnt NEXT_NORM_TO_DENORM
263 (p10) br.cond.spnt NEXT_UNDERFLOW_TO_ZERO
264 (p14) br.cond.spnt NEXT_UNDERFLOW_TO_ZERO ;;
267 // Here if no special cases
268 // Set p6 if result will be a denormal, so can force underflow flag
269 // Case 1: x_exp=min_exp, x_sig=unnormalized
270 // Case 2: x_exp<min_exp
272 cmp.lt p6,p7 = GR_x_exp, GR_min_pexp
273 fmerge.se f8 = FR_new_exp, FR_new_sig
280 (p7) tbit.z p6,p0 = GR_new_sig, 63 ;;
284 // Force underflow and inexact if denormal result
287 (p6) fma.d.s0 FR_tmp = FR_tmp,FR_tmp,f0
292 fnorm.d.s0 f8 = f8 // Final normalization to result precision
293 (p6) br.cond.spnt NEXT_UNDERFLOW ;;
305 cmp.lt p6,p7 = GR_x_exp, GR_min_pexp
306 fmerge.se f8 = FR_exp1, FR_snorm_sig
307 br.cond.sptk NEXT_COMMON_FINISH ;;
312 cmp.lt p6,p7 = GR_x_exp, GR_min_pexp
313 fmerge.se f8 = FR_exp1, FR_lnorm_sig
314 br.cond.sptk NEXT_COMMON_FINISH ;;
320 fmerge.se f8 = FR_new_exp, FR_lden_sig
323 // Force underflow and inexact if denormal result
326 fma.d.s0 FR_tmp = FR_tmp,FR_tmp,f0
327 br.cond.sptk NEXT_UNDERFLOW ;;
330 NEXT_UNDERFLOW_TO_ZERO:
333 fmerge.s f8 = FR_save_f8,f0
334 br.cond.sptk NEXT_COMMON_FINISH ;;
338 // Here if f8 is +- infinity
340 // if f8 is +inf, no matter what y is return largest double
341 // if f8 is -inf, no matter what y is return -largest double
345 fmerge.se FR_lnorm = FR_lnorm_exp,FR_lnorm_sig
351 fmerge.s f8 = f8,FR_lnorm
357 // Here if f8 is +- zero
359 // if f8 is zero and y is +, return + smallest double denormal
360 // if f8 is zero and y is -, return - smallest double denormal
364 fmerge.se FR_sden = FR_sden_exp,FR_sden_sig
368 // Create small normal to generate underflow flag
371 fmerge.se FR_tmp = FR_sden_exp, FR_lnorm_sig
375 // Add correct sign from direction arg
378 fmerge.s f8 = f9,FR_sden
382 // Force underflow and inexact flags
385 fma.d.s0 FR_tmp = FR_tmp,FR_tmp,f0
386 br.cond.sptk NEXT_UNDERFLOW ;;
390 // Here if result is a denorm, or input is finite and result is zero
391 // Call error support to report possible range error
393 alloc r32=ar.pfs,2,2,4,0
394 mov GR_Parameter_TAG = 271 // Error code
395 br.cond.sptk __libm_error_region // Branch to error call
400 // Here if input is finite, but result will be infinite
401 // Use frcpa to generate infinity of correct sign
402 // Call error support to report possible range error
404 alloc r32=ar.pfs,2,2,4,0
405 frcpa.s1 f8,p6 = FR_save_f8, f0
409 // Create largest double
412 fmerge.se FR_lnorm = FR_lnorm_exp,FR_lnorm_sig
416 // Force overflow and inexact flags to be set
418 mov GR_Parameter_TAG = 199 // Error code
419 fma.d.s0 FR_tmp = FR_lnorm,FR_lnorm,f0
420 br.cond.sptk __libm_error_region // Branch to error call
424 GLOBAL_LIBM_END(nexttoward)
427 LOCAL_LIBM_ENTRY(__libm_error_region)
432 add GR_Parameter_Y=-32,sp // Parameter 2 value
434 .save ar.pfs,GR_SAVE_PFS
435 mov GR_SAVE_PFS=ar.pfs // Save ar.pfs
439 add sp=-64,sp // Create new stack
441 mov GR_SAVE_GP=gp // Save gp
447 stfd [GR_Parameter_Y] = f9,16 // STORE Parameter 2 on stack
448 add GR_Parameter_X = 16,sp // Parameter 1 address
450 mov GR_SAVE_B0=b0 // Save b0
456 stfd [GR_Parameter_X] = FR_save_f8 // STORE Parameter 1 on stack
457 add GR_Parameter_RESULT = 0,GR_Parameter_Y // Parameter 3 address
461 stfd [GR_Parameter_Y] = f8 // STORE Parameter 3 on stack
462 add GR_Parameter_Y = -16,GR_Parameter_Y
463 br.call.sptk b0=__libm_error_support# // Call error handling function
468 add GR_Parameter_RESULT = 48,sp
473 ldfd f8 = [GR_Parameter_RESULT] // Get return result off stack
475 add sp = 64,sp // Restore stack pointer
476 mov b0 = GR_SAVE_B0 // Restore return address
479 mov gp = GR_SAVE_GP // Restore gp
480 mov ar.pfs = GR_SAVE_PFS // Restore ar.pfs
481 br.ret.sptk b0 // Return
484 LOCAL_LIBM_END(__libm_error_region)
487 .type __libm_error_support#,@function
488 .global __libm_error_support#