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 // 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.
36 //==============================================================
38 //==============================================================
41 //==============================================================
42 // double trunc(double x)
43 //==============================================================
45 #include "libm_support.h"
47 // general input registers:
49 TRUNC_GR_signexp = r15
50 TRUNC_GR_exponent = r16
51 TRUNC_GR_expmask = r17
54 // floating-point registers:
57 // predicate registers used:
58 // p6, p7, p8, p9, p10, p11
60 // Overview of operation
61 //==============================================================
62 // double trunc(double x)
63 // Return an integer value (represented as a double) less than or
64 // equal to x in magnitude.
65 // This is x rounded toward zero to an integral value.
66 //==============================================================
69 // if the exponent is > 1003e => 3F(true) = 63(decimal)
70 // we have a significand of 64 bits 1.63-bits.
71 // If we multiply by 2^63, we no longer have a fractional part
72 // So input is an integer value already.
75 // if the exponent is >= 10033 => 34(true) = 52(decimal)
77 // we have a significand of 53 bits 1.52-bits. (implicit 1)
78 // If we multiply by 2^52, we no longer have a fractional part
79 // So input is an integer value already.
82 // if the exponent is > 10016 => 17(true) = 23(decimal)
83 // we have a significand of 24 bits 1.23-bits. (implicit 1)
84 // If we multiply by 2^23, we no longer have a fractional part
85 // So input is an integer value already.
90 getf.exp TRUNC_GR_signexp = f8
91 fcvt.fx.trunc.s1 f9 = f8
92 addl TRUNC_GR_bigexp = 0x10033, r0
95 mov TRUNC_GR_FFFF = 0x0FFFF
97 mov TRUNC_GR_expmask = 0x1FFFF
99 // get the exponent of x
100 // convert x to integer in signficand of f9
101 // Normalize x - this will raise invalid on SNaNs, the
102 // denormal operand flag - and possibly a spurious U flag
103 // get exponent only mask (will exclude sign bit)
107 fclass.m p7,p8 = f8, 0x0b
112 fcmp.eq.unc.s1 p9,p0 = f8,f0
115 // fclass to set p7 if unnorm
117 and TRUNC_GR_exponent = TRUNC_GR_signexp, TRUNC_GR_expmask ;;
118 (p8) cmp.ge.unc p10,p11 = TRUNC_GR_exponent, TRUNC_GR_bigexp
119 (p8) cmp.ne.unc p6,p0 = TRUNC_GR_exponent, TRUNC_GR_signexp
121 // Get the exponent of x
122 // Test if exponent such that result already an integer
125 (p9) cmp.eq.andcm p10,p11 = r0, r0
126 (p6) cmp.lt.unc p6,p0 = TRUNC_GR_exponent, TRUNC_GR_FFFF
129 // If -1 < x < 0, set p6, turn off p10 and p11, and set result to -0.0
131 (p6) cmp.eq.andcm p10,p11 = r0, r0
132 (p6) fmerge.s f8 = f8, f0
135 // If not a unnorm, set p10 if x already is a big int, nan, or inf?
136 // If not a unnorm, set p10 if x already is a big int, nan, or inf?
137 .pred.rel "mutex",p10,p11
140 (p11) fcvt.xf f8 = f9
145 (p10) fma.d.s1 f8 = f11,f1,f0
148 // If not a unnorm and not an big int, nan,or +/-inf convert signficand
150 // If not a unorm and a big int, nan, or +/-inf, return fnorm'd x
151 // If not a unorm, Return
152 // If unnorm, get the exponent again - perhaps it wasn't a denorm.
154 (p7) getf.exp TRUNC_GR_signexp = f11
155 (p7) fcvt.fx.trunc.s1 f12 = f11
159 and TRUNC_GR_exponent = TRUNC_GR_signexp, TRUNC_GR_expmask
160 fcmp.lt.unc.s1 p9,p0 = f8,f0
164 cmp.ge.unc p10,p11 = TRUNC_GR_exponent, TRUNC_GR_bigexp
168 // If a unnorm, check to see if value is already a big int.
171 (p11) fcvt.xf f8 = f12
176 (p10) fma.d.s1 f8 = f11,f1,f0
181 (p9) fmerge.ns f8 = f1,f8
184 // If so return it. Otherwise, return (fcvt.xf(fcvt.fx.trunc(x)))
185 // Make sure the result is negative if it should be - that is
186 // negative(denormal) -> -0.
188 ASM_SIZE_DIRECTIVE(trunc)