3 // Copyright (c) 2000, 2001, Intel Corporation
4 // All rights reserved.
6 // Contributed 10/25/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 // 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.
29 //==============================================================
30 // 10/25/2000: Created
31 //==============================================================
34 //==============================================================
35 // long double roundl(long double x)
38 #include "libm_support.h"
40 // general input registers:
44 roundl_GR_expmask = r16
45 roundl_GR_signexp = r17
47 roundl_GR_expdiff = r19
49 // predicate registers used:
52 // floating-point registers used:
57 ROUNDL_FLOAT_TRUNC_f8 = f12
58 ROUNDL_FLOAT_RINT_f8 = f13
59 ROUNDL_REMAINDER = f14
62 // Overview of operation
63 //==============================================================
65 // long double roundl(long double x)
66 // Return an integer value (represented as a long double) that is x
67 // rounded to nearest integer, halfway cases rounded away from
69 // if x>0 result = trunc(x+0.5)
70 // if x<0 result = trunc(x-0.5)
71 // *******************************************************************************
73 // Set denormal flag for denormal input and
74 // and take denormal fault if necessary.
76 // If x is NAN, ZERO, INFINITY, or >= 2^63 then return
78 // qnan snan inf norm unorm 0 -+
79 // 1 1 1 0 0 1 11 0xe7
92 // Get exponent for +0.5
93 // Truncate x to integer
95 addl roundl_GR_half = 0x0fffe, r0
96 fcvt.fx.trunc.s1 ROUNDL_TRUNC_f8 = f8
102 // Form exponent mask
104 getf.exp roundl_GR_signexp = f8
105 fnorm ROUNDL_NORM_f8 = f8
106 addl roundl_GR_expmask = 0x1ffff, r0 ;;
110 // Round x to integer
112 setf.exp ROUNDL_HALF = roundl_GR_half
113 fcvt.fx.s1 ROUNDL_RINT_f8 = f8
117 // Test for NAN, INF, ZERO
118 // Get exponent at which input has no fractional part
120 and roundl_GR_exp = roundl_GR_expmask, roundl_GR_signexp
121 fclass.m p8,p9 = f8,0xe7
122 addl roundl_GR_big = 0x1003e, r0 ;;
126 // If exp is so big there is no fractional part, then turn on p8, off p9
128 sub roundl_GR_expdiff = roundl_GR_exp, roundl_GR_big ;;
130 (p9) cmp.lt.or.andcm p8,p9 = r0, roundl_GR_expdiff
132 (p9) cmp.ge.or.andcm p8,p9 = roundl_GR_expdiff, r0
137 // Set p6 if x<0, else set p7
140 (p9) fcmp.lt.unc p6,p7 = f8,f0
144 // If NAN, INF, ZERO, or no fractional part, result is just normalized input
147 (p8) fnorm.s0 f8 = f8
151 // Float the truncated integer
154 (p9) fcvt.xf ROUNDL_FLOAT_TRUNC_f8 = ROUNDL_TRUNC_f8
158 // Float the rounded integer to get preliminary result
161 (p9) fcvt.xf ROUNDL_FLOAT_RINT_f8 = ROUNDL_RINT_f8
165 // If x<0 and the difference of the truncated input minus the input is 0.5
166 // then result = truncated input - 1.0
167 // Else if x>0 and the difference of the input minus truncated input is 0.5
168 // then result = truncated input + 1.0
170 // result = rounded input
174 (p6) fsub.s1 ROUNDL_REMAINDER = ROUNDL_FLOAT_TRUNC_f8, ROUNDL_NORM_f8
180 (p7) fsub.s1 ROUNDL_REMAINDER = ROUNDL_NORM_f8, ROUNDL_FLOAT_TRUNC_f8
184 // Assume preliminary result is rounded integer
187 (p9) fnorm.s0 f8 = ROUNDL_FLOAT_RINT_f8
191 // If x<0, test if result=0
194 (p6) fcmp.eq.unc p10,p0 = ROUNDL_FLOAT_RINT_f8,f0
198 // If x<0 and result=0, set result=-0
201 (p10) fmerge.ns f8 = f1,f8
205 // If x<0, test if remainder=0.5
208 (p6) fcmp.eq.unc p6,p0 = ROUNDL_REMAINDER, ROUNDL_HALF
212 // If x>0, test if remainder=0.5
215 (p7) fcmp.eq.unc p7,p0 = ROUNDL_REMAINDER, ROUNDL_HALF
219 // If x<0 and remainder=0.5, result=truncated-1.0
220 // If x>0 and remainder=0.5, result=truncated+1.0
222 .pred.rel "mutex",p6,p7
225 (p6) fsub.s0 f8 = ROUNDL_FLOAT_TRUNC_f8,f1
231 (p7) fadd.s0 f8 = ROUNDL_FLOAT_TRUNC_f8,f1
236 ASM_SIZE_DIRECTIVE(roundl)