4 // Copyright (c) 2000 - 2003, Intel Corporation
5 // All rights reserved.
7 // Contributed 2000 by the Intel Numerics Group, Intel Corporation
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are
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
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.
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.
41 //==============================================================
43 // 02/08/01 Corrected behavior for all rounding modes.
44 // 05/20/02 Cleaned up namespace and sf0 syntax
45 // 02/10/03 Reordered header: .section, .global, .proc, .align
46 //==============================================================
49 //==============================================================
50 // long double nearbyintl(long double x)
52 // general registers used:
55 nearbyint_GR_signexp = r14
56 nearbyint_GR_exponent = r15
57 nearbyint_GR_17ones = r16
58 nearbyint_GR_10033 = r17
59 nearbyint_GR_fpsr = r18
60 nearbyint_GR_rcs0 = r19
61 nearbyint_GR_rcs0_mask = r20
64 // predicate registers used:
67 // floating-point registers used:
69 NEARBYINT_NORM_f8 = f9
70 NEARBYINT_FLOAT_INT_f8 = f10
71 NEARBYINT_INT_f8 = f11
72 NEARBYINT_SIGNED_FLOAT_INT_f8 = f12
74 // Overview of operation
75 //==============================================================
77 // long double nearbyintl(long double x)
78 // Return an integer value (represented as a long double) that is
79 // x rounded to integer in current rounding mode
80 // *******************************************************************************
82 // Set denormal flag for denormal input and
83 // and take denormal fault if necessary.
85 // Is the input an integer value already?
88 // if the exponent is >= 1003e => 3F(true) = 63(decimal)
89 // we have a significand of 64 bits 1.63-bits.
90 // If we multiply by 2^63, we no longer have a fractional part
91 // So input is an integer value already.
94 // if the exponent is >= 10033 => 34(true) = 52(decimal)
96 // we have a significand of 53 bits 1.52-bits. (implicit 1)
97 // If we multiply by 2^52, we no longer have a fractional part
98 // So input is an integer value already.
101 // if the exponent is >= 10016 => 17(true) = 23(decimal)
102 // we have a significand of 53 bits 1.52-bits. (implicit 1)
103 // If we multiply by 2^52, we no longer have a fractional part
104 // So input is an integer value already.
106 // If x is NAN, ZERO, or INFINITY, then return
108 // qnan snan inf norm unorm 0 -+
109 // 1 1 1 0 0 1 11 0xe7
113 GLOBAL_LIBM_ENTRY(nearbyintl)
116 mov nearbyint_GR_fpsr = ar40 // Read the fpsr--need to check rc.s0
117 fcvt.fx.s1 NEARBYINT_INT_f8 = f8
118 addl nearbyint_GR_10033 = 0x1003e, r0
122 fnorm.s1 NEARBYINT_NORM_f8 = f8
123 mov nearbyint_GR_17ones = 0x1FFFF
129 fclass.m.unc p6,p0 = f8, 0xe7
130 mov nearbyint_GR_rcs0_mask = 0x0c00
136 (p6) fnorm.s0 f8 = f8
137 (p6) br.ret.spnt b0 // Exit if x nan, inf, zero
143 fcvt.xf NEARBYINT_FLOAT_INT_f8 = NEARBYINT_INT_f8
149 getf.exp nearbyint_GR_signexp = NEARBYINT_NORM_f8
150 fcmp.eq.s0 p8,p0 = f8,f0 // Dummy op to set denormal
159 and nearbyint_GR_exponent = nearbyint_GR_signexp, nearbyint_GR_17ones
164 cmp.ge.unc p7,p6 = nearbyint_GR_exponent, nearbyint_GR_10033
165 and nearbyint_GR_rcs0 = nearbyint_GR_rcs0_mask, nearbyint_GR_fpsr
170 // Check to see if s0 rounding mode is round to nearest. If not then set s2
171 // rounding mode to that of s0 and repeat conversions.
172 // Must merge the original sign for cases where the result is zero or the input
173 // is the largest that still has a fraction (0x1007dfffffffffff)
176 cmp.ne p11,p0 = nearbyint_GR_rcs0, r0
177 (p6) fmerge.s NEARBYINT_SIGNED_FLOAT_INT_f8 = f8, NEARBYINT_FLOAT_INT_f8
178 (p11) br.cond.spnt NEARBYINT_NOT_ROUND_NEAREST // Branch if not round to nearest
184 (p7) fnorm.s0 f8 = f8
191 (p6) fnorm.s0 f8 = NEARBYINT_SIGNED_FLOAT_INT_f8
197 NEARBYINT_NOT_ROUND_NEAREST:
198 // Set rounding mode of s2 to that of s0
200 mov nearbyint_GR_rcs0 = r0 // Clear so we don't come back here
208 fcvt.fx.s2 NEARBYINT_INT_f8 = f8
215 fcvt.xf NEARBYINT_FLOAT_INT_f8 = NEARBYINT_INT_f8
216 br.cond.sptk NEARBYINT_COMMON
221 GLOBAL_LIBM_END(nearbyintl)