3 // Copyright (c) 2000, 2001, Intel Corporation
4 // All rights reserved.
6 // Contributed 10/19/2000 by John Harrison, Cristina Iordache, Ted Kubaska,
7 // Bob Norin, Tom Rowan, Shane Story, and Ping Tak Peter Tang of the
8 // Computational Software Lab, Intel Corporation.
10 // WARRANTY DISCLAIMER
12 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR ITS
16 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
17 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
18 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
19 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
20 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR TORT (INCLUDING
21 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 // Intel Corporation is the author of this code, and requests that all
25 // problem reports or change requests be submitted to it directly at
26 // http://developer.intel.com/opensource.
29 //==============================================================
30 // 10/19/2000: Created
31 // 2/08/01 Corrected behavior for all rounding modes.
32 //==============================================================
35 //==============================================================
36 // float nearbyintf(float x)
38 #include "libm_support.h"
41 // general registers used:
44 nearbyint_GR_signexp = r14
45 nearbyint_GR_exponent = r15
46 nearbyint_GR_17ones = r16
47 nearbyint_GR_10033 = r17
48 nearbyint_GR_fpsr = r18
49 nearbyint_GR_rcs0 = r19
50 nearbyint_GR_rcs0_mask = r20
53 // predicate registers used:
56 // floating-point registers used:
58 NEARBYINT_NORM_f8 = f9
59 NEARBYINT_FLOAT_INT_f8 = f10
60 NEARBYINT_INT_f8 = f11
62 // Overview of operation
63 //==============================================================
65 // float nearbyintf(float x)
66 // Return an integer value (represented as a float) that is x rounded to integer in current
68 // *******************************************************************************
70 // Set denormal flag for denormal input and
71 // and take denormal fault if necessary.
73 // Is the input an integer value already?
76 // if the exponent is >= 1003e => 3F(true) = 63(decimal)
77 // we have a significand of 64 bits 1.63-bits.
78 // If we multiply by 2^63, we no longer have a fractional part
79 // So input is an integer value already.
82 // if the exponent is >= 10033 => 34(true) = 52(decimal)
84 // we have a significand of 53 bits 1.52-bits. (implicit 1)
85 // If we multiply by 2^52, we no longer have a fractional part
86 // So input is an integer value already.
89 // if the exponent is >= 10016 => 17(true) = 23(decimal)
90 // we have a significand of 53 bits 1.52-bits. (implicit 1)
91 // If we multiply by 2^52, we no longer have a fractional part
92 // So input is an integer value already.
94 // If x is NAN, ZERO, or INFINITY, then return
96 // qnan snan inf norm unorm 0 -+
97 // 1 1 1 0 0 1 11 0xe7
111 mov nearbyint_GR_fpsr = ar40 // Read the fpsr--need to check rc.s0
112 fcvt.fx.s1 NEARBYINT_INT_f8 = f8
113 addl nearbyint_GR_10033 = 0x10016, r0
117 fnorm.s1 NEARBYINT_NORM_f8 = f8
118 mov nearbyint_GR_17ones = 0x1FFFF
124 fclass.m.unc p6,p0 = f8, 0xe7
125 mov nearbyint_GR_rcs0_mask = 0x0c00
132 (p6) br.ret.spnt b0 // Exit if x nan, inf, zero
138 fcvt.xf NEARBYINT_FLOAT_INT_f8 = NEARBYINT_INT_f8
144 getf.exp nearbyint_GR_signexp = NEARBYINT_NORM_f8
145 fcmp.eq.s0 p8,p0 = f8,f0 // Dummy op to set denormal
154 and nearbyint_GR_exponent = nearbyint_GR_signexp, nearbyint_GR_17ones
159 cmp.ge.unc p7,p6 = nearbyint_GR_exponent, nearbyint_GR_10033
160 and nearbyint_GR_rcs0 = nearbyint_GR_rcs0_mask, nearbyint_GR_fpsr
165 // Check to see if s0 rounding mode is round to nearest. If not then set s2
166 // rounding mode to that of s0 and repeat conversions.
169 cmp.ne p11,p0 = nearbyint_GR_rcs0, r0
170 (p6) fclass.m.unc p9,p10 = NEARBYINT_FLOAT_INT_f8, 0x07 // Test for result=0
171 (p11) br.cond.spnt L(NEARBYINT_NOT_ROUND_NEAREST) // Branch if not round to nearest
177 (p7) fnorm.s.s0 f8 = f8
182 // If result is zero, merge sign of input
185 (p9) fmerge.s f8 = f8, NEARBYINT_FLOAT_INT_f8
190 (p10) fnorm.s f8 = NEARBYINT_FLOAT_INT_f8
196 L(NEARBYINT_NOT_ROUND_NEAREST):
197 // Set rounding mode of s2 to that of s0
199 mov nearbyint_GR_rcs0 = r0 // Clear so we don't come back here
207 fcvt.fx.s2 NEARBYINT_INT_f8 = f8
214 fcvt.xf NEARBYINT_FLOAT_INT_f8 = NEARBYINT_INT_f8
215 br.cond.sptk L(NEARBYINT_COMMON)
221 ASM_SIZE_DIRECTIVE(nearbyintf)