3 // Copyright (C) 2000, 2001, Intel Corporation
4 // All rights reserved.
6 // Contributed 7/7/2000 by John Harrison, Cristina Iordache, Ted Kubaska,
7 // Bob Norin, 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.
49 //==============================================================
51 //==============================================================
54 //==============================================================
55 // double trunc(double x)
56 //==============================================================
58 #include "libm_support.h"
60 // general input registers:
62 TRUNC_GR_signexp = r15
63 TRUNC_GR_exponent = r16
64 TRUNC_GR_expmask = r17
67 // floating-point registers:
70 // predicate registers used:
71 // p6, p7, p8, p9, p10, p11
73 // Overview of operation
74 //==============================================================
75 // double trunc(double x)
76 // Return an integer value (represented as a double) less than or
77 // equal to x in magnitude.
78 // This is x rounded toward zero to an integral value.
79 //==============================================================
82 // if the exponent is > 1003e => 3F(true) = 63(decimal)
83 // we have a significand of 64 bits 1.63-bits.
84 // If we multiply by 2^63, we no longer have a fractional part
85 // So input is an integer value already.
88 // if the exponent is >= 10033 => 34(true) = 52(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.
95 // if the exponent is > 10016 => 17(true) = 23(decimal)
96 // we have a significand of 24 bits 1.23-bits. (implicit 1)
97 // If we multiply by 2^23, we no longer have a fractional part
98 // So input is an integer value already.
103 getf.exp TRUNC_GR_signexp = f8
104 fcvt.fx.trunc.s1 f9 = f8
105 addl TRUNC_GR_bigexp = 0x10033, r0
108 mov TRUNC_GR_FFFF = 0x0FFFF
110 mov TRUNC_GR_expmask = 0x1FFFF
112 // get the exponent of x
113 // convert x to integer in signficand of f9
114 // Normalize x - this will raise invalid on SNaNs, the
115 // denormal operand flag - and possibly a spurious U flag
116 // get exponent only mask (will exclude sign bit)
120 fclass.m p7,p8 = f8, 0x0b
125 fcmp.eq.unc.s1 p9,p0 = f8,f0
128 // fclass to set p7 if unnorm
130 and TRUNC_GR_exponent = TRUNC_GR_signexp, TRUNC_GR_expmask ;;
131 (p8) cmp.ge.unc p10,p11 = TRUNC_GR_exponent, TRUNC_GR_bigexp
132 (p8) cmp.ne.unc p6,p0 = TRUNC_GR_exponent, TRUNC_GR_signexp
134 // Get the exponent of x
135 // Test if exponent such that result already an integer
138 (p9) cmp.eq.andcm p10,p11 = r0, r0
139 (p6) cmp.lt.unc p6,p0 = TRUNC_GR_exponent, TRUNC_GR_FFFF
142 // If -1 < x < 0, set p6, turn off p10 and p11, and set result to -0.0
144 (p6) cmp.eq.andcm p10,p11 = r0, r0
145 (p6) fmerge.s f8 = f8, f0
148 // If not a unnorm, set p10 if x already is a big int, nan, or inf?
149 // If not a unnorm, set p10 if x already is a big int, nan, or inf?
150 .pred.rel "mutex",p10,p11
153 (p11) fcvt.xf f8 = f9
158 (p10) fma.d.s1 f8 = f11,f1,f0
161 // If not a unnorm and not an big int, nan,or +/-inf convert signficand
163 // If not a unorm and a big int, nan, or +/-inf, return fnorm'd x
164 // If not a unorm, Return
165 // If unnorm, get the exponent again - perhaps it wasn't a denorm.
167 (p7) getf.exp TRUNC_GR_signexp = f11
168 (p7) fcvt.fx.trunc.s1 f12 = f11
172 and TRUNC_GR_exponent = TRUNC_GR_signexp, TRUNC_GR_expmask
173 fcmp.lt.unc.s1 p9,p0 = f8,f0
177 cmp.ge.unc p10,p11 = TRUNC_GR_exponent, TRUNC_GR_bigexp
181 // If a unnorm, check to see if value is already a big int.
184 (p11) fcvt.xf f8 = f12
189 (p10) fma.d.s1 f8 = f11,f1,f0
194 (p9) fmerge.ns f8 = f1,f8
197 // If so return it. Otherwise, return (fcvt.xf(fcvt.fx.trunc(x)))
198 // Make sure the result is negative if it should be - that is
199 // negative(denormal) -> -0.
201 ASM_SIZE_DIRECTIVE(trunc)