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 // double nearbyint(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
73 // Overview of operation
74 //==============================================================
76 // double nearbyint(double x)
77 // Return an integer value (represented as a double) that is x rounded to integer in current
79 // *******************************************************************************
81 // Set denormal flag for denormal input and
82 // and take denormal fault if necessary.
84 // Is the input an integer value already?
87 // if the exponent is >= 1003e => 3F(true) = 63(decimal)
88 // we have a significand of 64 bits 1.63-bits.
89 // If we multiply by 2^63, we no longer have a fractional part
90 // So input is an integer value already.
93 // if the exponent is >= 10033 => 34(true) = 52(decimal)
95 // we have a significand of 53 bits 1.52-bits. (implicit 1)
96 // If we multiply by 2^52, we no longer have a fractional part
97 // So input is an integer value already.
100 // if the exponent is >= 10016 => 17(true) = 23(decimal)
101 // we have a significand of 53 bits 1.52-bits. (implicit 1)
102 // If we multiply by 2^52, we no longer have a fractional part
103 // So input is an integer value already.
105 // If x is NAN, ZERO, or INFINITY, then return
107 // qnan snan inf norm unorm 0 -+
108 // 1 1 1 0 0 1 11 0xe7
112 GLOBAL_LIBM_ENTRY(nearbyint)
115 mov nearbyint_GR_fpsr = ar40 // Read the fpsr--need to check rc.s0
116 fcvt.fx.s1 NEARBYINT_INT_f8 = f8
117 addl nearbyint_GR_10033 = 0x10033, r0
121 fnorm.s1 NEARBYINT_NORM_f8 = f8
122 mov nearbyint_GR_17ones = 0x1FFFF
128 fclass.m.unc p6,p0 = f8, 0xe7
129 mov nearbyint_GR_rcs0_mask = 0x0c00
135 (p6) fnorm.d.s0 f8 = f8
136 (p6) br.ret.spnt b0 // Exit if x nan, inf, zero
142 fcvt.xf NEARBYINT_FLOAT_INT_f8 = NEARBYINT_INT_f8
148 getf.exp nearbyint_GR_signexp = NEARBYINT_NORM_f8
149 fcmp.eq.s0 p8,p0 = f8,f0 // Dummy op to set denormal
158 and nearbyint_GR_exponent = nearbyint_GR_signexp, nearbyint_GR_17ones
163 cmp.ge.unc p7,p6 = nearbyint_GR_exponent, nearbyint_GR_10033
164 and nearbyint_GR_rcs0 = nearbyint_GR_rcs0_mask, nearbyint_GR_fpsr
169 // Check to see if s0 rounding mode is round to nearest. If not then set s2
170 // rounding mode to that of s0 and repeat conversions.
173 cmp.ne p11,p0 = nearbyint_GR_rcs0, r0
174 (p6) fclass.m.unc p9,p10 = NEARBYINT_FLOAT_INT_f8, 0x07 // Test for result=0
175 (p11) br.cond.spnt NEARBYINT_NOT_ROUND_NEAREST // Branch if not round to nearest
181 (p7) fnorm.d.s0 f8 = f8
186 // If result is zero, merge sign of input
189 (p9) fmerge.s f8 = f8, NEARBYINT_FLOAT_INT_f8
194 (p10) fnorm.d.s0 f8 = NEARBYINT_FLOAT_INT_f8
200 NEARBYINT_NOT_ROUND_NEAREST:
201 // Set rounding mode of s2 to that of s0
203 mov nearbyint_GR_rcs0 = r0 // Clear so we don't come back here
211 fcvt.fx.s2 NEARBYINT_INT_f8 = f8
218 fcvt.xf NEARBYINT_FLOAT_INT_f8 = NEARBYINT_INT_f8
219 br.cond.sptk NEARBYINT_COMMON
224 GLOBAL_LIBM_END(nearbyint)