1.file "roundf.s"
2
3
4// Copyright (c) 2000 - 2003, Intel Corporation
5// All rights reserved.
6//
7//
8// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions are
10// met:
11//
12// * Redistributions of source code must retain the above copyright
13// notice, this list of conditions and the following disclaimer.
14//
15// * Redistributions in binary form must reproduce the above copyright
16// notice, this list of conditions and the following disclaimer in the
17// documentation and/or other materials provided with the distribution.
18//
19// * The name of Intel Corporation may not be used to endorse or promote
20// products derived from this software without specific prior written
21// permission.
22
23// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR ITS
27// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
31// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR TORT (INCLUDING
32// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34//
35// Intel Corporation is the author of this code, and requests that all
36// problem reports or change requests be submitted to it directly at
37// http://www.intel.com/software/products/opensource/libraries/num.htm.
38//
39// History
40//==============================================================
41// 10/25/00 Initial version
42// 06/14/01 Changed cmp to an equivalent form
43// 05/20/02 Cleaned up namespace and sf0 syntax
44// 01/20/03 Improved performance and reduced code size
45// 04/18/03 Eliminate possible WAW dependency warning
46// 09/03/03 Improved performance
47//==============================================================
48
49// API
50//==============================================================
51// float roundf(float x)
52//==============================================================
53
54// general input registers:
55// r14 - r18
56
57rSignexp   = r14
58rExp       = r15
59rExpMask   = r16
60rBigexp    = r17
61rExpHalf   = r18
62
63// floating-point registers:
64// f8 - f13
65
66fXtruncInt = f9
67fNormX     = f10
68fHalf      = f11
69fInc       = f12
70fRem       = f13
71
72// predicate registers used:
73// p6 - p10
74
75// Overview of operation
76//==============================================================
77// float roundf(float x)
78// Return an integer value (represented as a float) that is x
79// rounded to nearest integer, halfway cases rounded away from
80// zero.
81//  if x>0   result = trunc(x+0.5)
82//  if x<0   result = trunc(x-0.5)
83//
84//==============================================================
85
86// double_extended
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.
91
92// double
93// if the exponent is >= 10033 => 34(true) = 52(decimal)
94// 34 + 3ff = 433
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.
98
99// single
100// if the exponent is > 10016 => 17(true) = 23(decimal)
101// we have a significand of 24 bits 1.23-bits. (implicit 1)
102// If we multiply by 2^23, we no longer have a fractional part
103// So input is an integer value already.
104
105
106.section .text
107GLOBAL_LIBM_ENTRY(__roundf)
108
109{ .mfi
110      getf.exp         rSignexp  = f8        // Get signexp, recompute if unorm
111      fcvt.fx.trunc.s1 fXtruncInt  = f8      // Convert to int in significand
112      addl             rBigexp = 0x10016, r0 // Set exponent at which is integer
113}
114{ .mfi
115      mov              rExpHalf    = 0x0FFFE // Form sign and exponent of 0.5
116      fnorm.s1         fNormX  = f8          // Normalize input
117      mov              rExpMask    = 0x1FFFF // Form exponent mask
118}
119;;
120
121{ .mfi
122      setf.exp         fHalf = rExpHalf      // Form 0.5
123      fclass.m         p7,p0 = f8, 0x0b      // Test x unorm
124      nop.i            0
125}
126;;
127
128{ .mfb
129      nop.m            0
130      fclass.m         p6,p0 = f8, 0x1e3     // Test x natval, nan, inf
131(p7)  br.cond.spnt     ROUND_UNORM           // Branch if x unorm
132}
133;;
134
135ROUND_COMMON:
136// Return here from ROUND_UNORM
137{ .mfb
138      nop.m            0
139      fcmp.lt.s1       p8,p9 = f8, f0        // Test if x < 0
140(p6)  br.cond.spnt     ROUND_SPECIAL         // Exit if x natval, nan, inf
141}
142;;
143
144{ .mfi
145      nop.m            0
146      fcvt.xf          f8 = fXtruncInt        // Pre-Result if 0.5 <= |x| < 2^23
147      nop.i            0
148}
149;;
150
151{ .mfi
152      and              rExp = rSignexp, rExpMask // Get biased exponent
153      fmerge.s         fInc = fNormX, f1      // Form increment if |rem| >= 0.5
154      nop.i            0
155}
156;;
157
158{ .mmi
159      cmp.lt           p6,p0 = rExp, rExpHalf // Is |x| < 0.5?
160      cmp.ge           p7,p0 = rExp, rBigexp  // Is |x| >= 2^23?
161      cmp.lt           p10,p0 = rExp, rExpHalf // Is |x| < 0.5?
162}
163;;
164
165// We must correct result if |x| < 0.5, or |x| >= 2^23
166.pred.rel "mutex",p6,p7
167{ .mfi
168      nop.m            0
169(p6)  fmerge.s         f8 = fNormX, f0        // If |x| < 0.5, result sgn(x)*0
170      nop.i            0
171}
172{ .mfb
173(p7)  cmp.eq           p10,p0 = r0, r0        // Also turn on p10 if |x| >= 2^23
174(p7)  fma.s.s0         f8 = fNormX, f1, f0    // If |x| >= 2^23, result x
175(p10) br.ret.spnt      b0                     // Exit |x| < 0.5 or |x| >= 2^23
176}
177;;
178
179// Here if 0.5 <= |x| < 2^23
180{ .mfi
181      nop.m            0
182(p9)  fms.s1           fRem = fNormX, f1, f8  // Get remainder = x - trunc(x)
183      nop.i            0
184}
185{ .mfi
186      nop.m            0
187(p8)  fms.s1           fRem = f8, f1, fNormX  // Get remainder = trunc(x) - x
188      nop.i            0
189}
190;;
191
192{ .mfi
193      nop.m            0
194      fcmp.ge.s1       p9,p0 = fRem, fHalf    // Test |rem| >= 0.5
195      nop.i            0
196}
197;;
198
199// If x < 0 and remainder <= -0.5, then subtract 1 from result
200// If x > 0 and remainder >= +0.5, then add 1 to result
201{ .mfb
202      nop.m            0
203(p9)  fma.s.s0         f8 = f8, f1, fInc
204      br.ret.sptk      b0
205}
206;;
207
208
209ROUND_SPECIAL:
210// Here if x natval, nan, inf
211{ .mfb
212      nop.m            0
213      fma.s.s0         f8 = f8, f1, f0
214      br.ret.sptk      b0
215}
216;;
217
218ROUND_UNORM:
219// Here if x unorm
220{ .mfi
221      getf.exp         rSignexp  = fNormX     // Get signexp, recompute if unorm
222      fcmp.eq.s0       p7,p0 = f8, f0         // Dummy op to set denormal flag
223      nop.i            0
224}
225{ .mfb
226      nop.m            0
227      fcvt.fx.trunc.s1 fXtruncInt  = fNormX   // Convert to int in significand
228      br.cond.sptk     ROUND_COMMON           // Return to main path
229}
230;;
231
232GLOBAL_LIBM_END(__roundf)
233libm_alias_float (__round, round)
234