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 // long double nexttowardl( long double x, long double y );
50 // input floating point f8, f9
51 // output floating point f8
54 //==============================================================
69 // r36-39 parameters for libm_error_support
77 GR_Parameter_RESULT = r38
78 GR_Parameter_TAG = r39
96 // Overview of operation
97 //==============================================================
98 // nexttowardl determines the next representable value
99 // after x in the direction of y.
103 GLOBAL_LIBM_ENTRY(nexttowardl)
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 = 0x1, r0
113 // Form largest normal significand 0xffffffffffffffff
114 // Form smallest normal exponent
116 addl GR_lnorm_sig = -0x1,r0
118 addl GR_min_pexp = 0x0c001, r0 ;;
121 // Extract significand from x
122 // Is x=y? This fcmp also sets Invalid and Denormal if required
123 // Form largest normal exponent
126 fcmp.eq.s0 p6,p0 = f8, f9
127 addl GR_max_pexp = 0x13ffe, r0
129 // Move largest normal significand to fp reg for special cases
131 setf.sig FR_lnorm_sig = GR_lnorm_sig
133 addl GR_sign_mask = 0x20000, r0 ;;
136 // Move smallest denormal significand and exp to fp regs
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
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 // Move smallest normal exp to fp regs
148 setf.exp FR_snorm_exp = GR_min_pexp
150 (p11) cmp.ge p12,p13 = GR_exp, GR_sign_mask ;;
153 .pred.rel "mutex",p12,p13
155 // Form expected new significand, adding or subtracting 1 ulp increment
156 // If x=y set result to y
157 // Form smallest normal significand and largest denormal significand
159 (p12) add GR_new_sig = GR_sig, GR_sden_sig
160 (p6) fmerge.s f8=f9,f9
161 dep.z GR_snorm_sig = 1,63,1 // 0x8000000000000000
164 (p13) sub GR_new_sig = GR_sig, GR_sden_sig
165 movl GR_lden_sig = 0x7fffffffffffffff ;;
168 // Move expected result significand and signexp to fp regs
170 // Form new exponent in case result exponent needs incrementing or decrementing
172 setf.exp FR_new_exp = GR_exp
173 fclass.m p9,p0 = f9, 0xc3
174 (p12) add GR_exp1 = 1, GR_exp
177 setf.sig FR_new_sig = GR_new_sig
178 (p13) add GR_exp1 = -1, GR_exp
179 (p6) br.ret.spnt b0 ;; // Exit if x=y
182 // Move largest normal signexp to fp reg for special cases
185 setf.exp FR_lnorm_exp = GR_max_pexp
186 fclass.m p7,p0 = f8, 0x7
190 setf.exp FR_den_exp = GR_min_pexp
191 (p8) fma.s0 f8 = f8,f1,f9
192 (p8) br.ret.spnt b0 ;; // Exit if x=nan
195 // Move exp+-1 and smallest normal significand to fp regs for special cases
198 setf.exp FR_exp1 = GR_exp1
199 fclass.m p6,p0 = f8, 0x23
200 addl GR_exp_mask = 0x1ffff, r0
203 setf.sig FR_snorm_sig = GR_snorm_sig
204 (p9) fma.s0 f8 = f8,f1,f9
205 (p9) br.ret.spnt b0 ;; // Exit if y=nan
208 // Move largest denormal significand to fp regs for special cases
211 setf.sig FR_lden_sig = GR_lden_sig
213 (p7) br.cond.spnt NEXT_ZERO ;; // Exit if x=0
216 // Mask off the sign to get x_exp
218 and GR_x_exp = GR_exp_mask, GR_exp
220 (p6) br.cond.spnt NEXT_INF ;; // Exit if x=inf
223 // Check 5 special cases when significand rolls over:
224 // 1 sig size incr, x_sig=max_sig, x_exp < max_exp
225 // Set p6, result is sig=min_sig, exp++
226 // 2 sig size incr, x_sig=max_sig, x_exp >= max_exp
227 // Set p7, result is inf, signal overflow
228 // 3 sig size decr, x_sig=min_sig, x_exp > min_exp
229 // Set p8, result is sig=max_sig, exp--
230 // 4 sig size decr, x_sig=min_sig, x_exp = min_exp
231 // Set p9, result is sig=max_den_sig, exp same, signal underflow and inexact
232 // 5 sig size decr, x_sig=min_den_sig, x_exp = min_exp
233 // Set p10, result is zero, sign of x, signal underflow and inexact
236 (p12) cmp.eq.unc p6,p0 = GR_new_sig, r0
237 (p13) cmp.eq.unc p9,p10 = GR_new_sig, GR_lden_sig
243 (p6) cmp.lt.unc p6,p7 = GR_x_exp, GR_max_pexp
244 (p10) cmp.eq.unc p10,p0 = GR_new_sig, r0
245 (p9) cmp.le.unc p9,p8 = GR_x_exp, GR_min_pexp
249 // Create small normal in case need to generate underflow flag
252 fmerge.se FR_tmp = FR_snorm_exp, FR_lnorm_sig
255 // Branch if cases 1, 2, 3
257 (p6) br.cond.spnt NEXT_EXPUP
258 (p7) br.cond.spnt NEXT_OVERFLOW
259 (p8) br.cond.spnt NEXT_EXPDOWN ;;
262 // Branch if cases 4, 5
265 (p9) br.cond.spnt NEXT_NORM_TO_DENORM
266 (p10) br.cond.spnt NEXT_UNDERFLOW_TO_ZERO
270 // Here if no special cases
271 // Set p6 if result will be a denormal, so can force underflow flag
272 // Case 1: x_exp=min_exp, x_sig=unnormalized
273 // Case 2: x_exp<min_exp
275 cmp.lt p6,p7 = GR_x_exp, GR_min_pexp
276 fmerge.se f8 = FR_new_exp, FR_new_sig
283 (p6) tbit.z p6,p0 = GR_new_sig, 63 ;;
287 // Force underflow and inexact if denormal result
290 (p6) fma.s0 FR_tmp = FR_tmp,FR_tmp,f0
295 fnorm.s0 f8 = f8 // Final normalization to result precision
296 (p6) br.cond.spnt NEXT_UNDERFLOW ;;
308 cmp.lt p6,p7 = GR_x_exp, GR_min_pexp
309 fmerge.se f8 = FR_exp1, FR_snorm_sig
310 br.cond.sptk NEXT_COMMON_FINISH ;;
315 cmp.lt p6,p7 = GR_x_exp, GR_min_pexp
316 fmerge.se f8 = FR_exp1, FR_lnorm_sig
317 br.cond.sptk NEXT_COMMON_FINISH ;;
323 fmerge.se f8 = FR_exp1, FR_lden_sig
326 // Force underflow and inexact
329 fma.s0 FR_tmp = FR_tmp,FR_tmp,f0
330 br.cond.sptk NEXT_UNDERFLOW ;;
333 NEXT_UNDERFLOW_TO_ZERO:
336 fmerge.s f8 = FR_save_f8,f0
337 br.cond.sptk NEXT_COMMON_FINISH ;;
341 // Here if f8 is +- infinity
343 // if f8 is +inf, no matter what y is return largest long double
344 // if f8 is -inf, no matter what y is return -largest long double
346 // Create largest long double
349 fmerge.se FR_lnorm = FR_lnorm_exp,FR_lnorm_sig
355 fmerge.s f8 = f8,FR_lnorm
361 // Here if f8 is +- zero
363 // if f8 is zero and y is +, return + smallest long double denormal
364 // if f8 is zero and y is -, return - smallest long double denormal
368 fmerge.se FR_sden = f0,FR_sden_sig
372 // Create small normal to generate underflow flag
375 fmerge.se FR_tmp = FR_snorm_exp, FR_lnorm_sig
379 // Add correct sign from direction arg
382 fmerge.s f8 = f9,FR_sden
386 // Force underflow and inexact flags
389 fma.s0 FR_tmp = FR_tmp,FR_tmp,f0
390 br.cond.sptk NEXT_UNDERFLOW ;;
394 // Here if result is a denorm, or input is finite and result is zero
395 // Call error support to report possible range error
397 alloc r32=ar.pfs,2,2,4,0
398 mov GR_Parameter_TAG = 270 // Error code
399 br.cond.sptk __libm_error_region // Branch to error call
404 // Here if input is finite, but result will be infinite
405 // Use frcpa to generate infinity of correct sign
406 // Call error support to report possible range error
408 alloc r32=ar.pfs,2,2,4,0
409 frcpa.s1 f8,p6 = FR_save_f8, f0
413 // Create largest double
416 fmerge.se FR_lnorm = FR_lnorm_exp,FR_lnorm_sig
420 // Force overflow and inexact flags to be set
422 mov GR_Parameter_TAG = 198 // Error code
423 fma.s0 FR_tmp = FR_lnorm,FR_lnorm,f0
424 br.cond.sptk __libm_error_region // Branch to error call
428 GLOBAL_LIBM_END(nexttowardl)
431 LOCAL_LIBM_ENTRY(__libm_error_region)
436 add GR_Parameter_Y=-32,sp // Parameter 2 value
438 .save ar.pfs,GR_SAVE_PFS
439 mov GR_SAVE_PFS=ar.pfs // Save ar.pfs
443 add sp=-64,sp // Create new stack
445 mov GR_SAVE_GP=gp // Save gp
451 stfe [GR_Parameter_Y] = f9,16 // STORE Parameter 2 on stack
452 add GR_Parameter_X = 16,sp // Parameter 1 address
454 mov GR_SAVE_B0=b0 // Save b0
460 stfe [GR_Parameter_X] = FR_save_f8 // STORE Parameter 1 on stack
461 add GR_Parameter_RESULT = 0,GR_Parameter_Y // Parameter 3 address
465 stfe [GR_Parameter_Y] = f8 // STORE Parameter 3 on stack
466 add GR_Parameter_Y = -16,GR_Parameter_Y
467 br.call.sptk b0=__libm_error_support# // Call error handling function
472 add GR_Parameter_RESULT = 48,sp
477 ldfe f8 = [GR_Parameter_RESULT] // Get return result off stack
479 add sp = 64,sp // Restore stack pointer
480 mov b0 = GR_SAVE_B0 // Restore return address
483 mov gp = GR_SAVE_GP // Restore gp
484 mov ar.pfs = GR_SAVE_PFS // Restore ar.pfs
485 br.ret.sptk b0 // Return
488 LOCAL_LIBM_END(__libm_error_region)
491 .type __libm_error_support#,@function
492 .global __libm_error_support#