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 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
14 // * Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
17 // * Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
21 // * The name of Intel Corporation may not be used to endorse or promote
22 // products derived from this software without specific prior written
25 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR ITS
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
33 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 // Intel Corporation is the author of this code, and requests that all
38 // problem reports or change requests be submitted to it directly at
39 // http://developer.intel.com/opensource.
42 //==============================================================
43 // 10/19/2000: Created
44 // 2/08/01 Corrected behavior for all rounding modes.
45 //==============================================================
48 //==============================================================
49 // double nearbyint(double x)
51 #include "libm_support.h"
54 // general registers used:
57 nearbyint_GR_signexp = r14
58 nearbyint_GR_exponent = r15
59 nearbyint_GR_17ones = r16
60 nearbyint_GR_10033 = r17
61 nearbyint_GR_fpsr = r18
62 nearbyint_GR_rcs0 = r19
63 nearbyint_GR_rcs0_mask = r20
66 // predicate registers used:
69 // floating-point registers used:
71 NEARBYINT_NORM_f8 = f9
72 NEARBYINT_FLOAT_INT_f8 = f10
73 NEARBYINT_INT_f8 = f11
75 // Overview of operation
76 //==============================================================
78 // double nearbyint(double x)
79 // Return an integer value (represented as a double) that is x rounded to integer in current
81 // *******************************************************************************
83 // Set denormal flag for denormal input and
84 // and take denormal fault if necessary.
86 // Is the input an integer value already?
89 // if the exponent is >= 1003e => 3F(true) = 63(decimal)
90 // we have a significand of 64 bits 1.63-bits.
91 // If we multiply by 2^63, we no longer have a fractional part
92 // So input is an integer value already.
95 // if the exponent is >= 10033 => 34(true) = 52(decimal)
97 // we have a significand of 53 bits 1.52-bits. (implicit 1)
98 // If we multiply by 2^52, we no longer have a fractional part
99 // So input is an integer value already.
102 // if the exponent is >= 10016 => 17(true) = 23(decimal)
103 // we have a significand of 53 bits 1.52-bits. (implicit 1)
104 // If we multiply by 2^52, we no longer have a fractional part
105 // So input is an integer value already.
107 // If x is NAN, ZERO, or INFINITY, then return
109 // qnan snan inf norm unorm 0 -+
110 // 1 1 1 0 0 1 11 0xe7
124 mov nearbyint_GR_fpsr = ar40 // Read the fpsr--need to check rc.s0
125 fcvt.fx.s1 NEARBYINT_INT_f8 = f8
126 addl nearbyint_GR_10033 = 0x10033, r0
130 fnorm.s1 NEARBYINT_NORM_f8 = f8
131 mov nearbyint_GR_17ones = 0x1FFFF
137 fclass.m.unc p6,p0 = f8, 0xe7
138 mov nearbyint_GR_rcs0_mask = 0x0c00
145 (p6) br.ret.spnt b0 // Exit if x nan, inf, zero
151 fcvt.xf NEARBYINT_FLOAT_INT_f8 = NEARBYINT_INT_f8
157 getf.exp nearbyint_GR_signexp = NEARBYINT_NORM_f8
158 fcmp.eq.s0 p8,p0 = f8,f0 // Dummy op to set denormal
167 and nearbyint_GR_exponent = nearbyint_GR_signexp, nearbyint_GR_17ones
172 cmp.ge.unc p7,p6 = nearbyint_GR_exponent, nearbyint_GR_10033
173 and nearbyint_GR_rcs0 = nearbyint_GR_rcs0_mask, nearbyint_GR_fpsr
178 // Check to see if s0 rounding mode is round to nearest. If not then set s2
179 // rounding mode to that of s0 and repeat conversions.
182 cmp.ne p11,p0 = nearbyint_GR_rcs0, r0
183 (p6) fclass.m.unc p9,p10 = NEARBYINT_FLOAT_INT_f8, 0x07 // Test for result=0
184 (p11) br.cond.spnt L(NEARBYINT_NOT_ROUND_NEAREST) // Branch if not round to nearest
190 (p7) fnorm.d.s0 f8 = f8
195 // If result is zero, merge sign of input
198 (p9) fmerge.s f8 = f8, NEARBYINT_FLOAT_INT_f8
203 (p10) fnorm.d f8 = NEARBYINT_FLOAT_INT_f8
209 L(NEARBYINT_NOT_ROUND_NEAREST):
210 // Set rounding mode of s2 to that of s0
212 mov nearbyint_GR_rcs0 = r0 // Clear so we don't come back here
220 fcvt.fx.s2 NEARBYINT_INT_f8 = f8
227 fcvt.xf NEARBYINT_FLOAT_INT_f8 = NEARBYINT_INT_f8
228 br.cond.sptk L(NEARBYINT_COMMON)
234 ASM_SIZE_DIRECTIVE(nearbyint)