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 // 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.
42 //==============================================================
43 // 10/25/2000: Created
44 //==============================================================
47 //==============================================================
48 // double round(double x)
51 #include "libm_support.h"
53 // general input registers:
57 round_GR_expmask = r16
58 round_GR_signexp = r17
60 round_GR_expdiff = r19
62 // predicate registers used:
65 // floating-point registers used:
70 ROUND_FLOAT_TRUNC_f8 = f12
71 ROUND_FLOAT_RINT_f8 = f13
75 // Overview of operation
76 //==============================================================
78 // double round(double x)
79 // Return an integer value (represented as a double) that is x
80 // rounded to nearest integer, halfway cases rounded away from
82 // if x>0 result = trunc(x+0.5)
83 // if x<0 result = trunc(x-0.5)
84 // *******************************************************************************
86 // Set denormal flag for denormal input and
87 // and take denormal fault if necessary.
89 // If x is NAN, ZERO, INFINITY, or >= 2^52 then return
91 // qnan snan inf norm unorm 0 -+
92 // 1 1 1 0 0 1 11 0xe7
105 // Get exponent for +0.5
106 // Truncate x to integer
108 addl round_GR_half = 0x0fffe, r0
109 fcvt.fx.trunc.s1 ROUND_TRUNC_f8 = f8
115 // Form exponent mask
117 getf.exp round_GR_signexp = f8
118 fnorm ROUND_NORM_f8 = f8
119 addl round_GR_expmask = 0x1ffff, r0 ;;
123 // Round x to integer
125 setf.exp ROUND_HALF = round_GR_half
126 fcvt.fx.s1 ROUND_RINT_f8 = f8
130 // Test for NAN, INF, ZERO
131 // Get exponent at which input has no fractional part
133 and round_GR_exp = round_GR_expmask, round_GR_signexp
134 fclass.m p8,p9 = f8,0xe7
135 addl round_GR_big = 0x10033, r0 ;;
139 // If exp is so big there is no fractional part, then turn on p8, off p9
141 sub round_GR_expdiff = round_GR_exp, round_GR_big ;;
143 (p9) cmp.lt.or.andcm p8,p9 = r0, round_GR_expdiff
145 (p9) cmp.ge.or.andcm p8,p9 = round_GR_expdiff, r0
150 // Set p6 if x<0, else set p7
153 (p9) fcmp.lt.unc p6,p7 = f8,f0
157 // If NAN, INF, ZERO, or no fractional part, result is just normalized input
160 (p8) fnorm.d.s0 f8 = f8
164 // Float the truncated integer
167 (p9) fcvt.xf ROUND_FLOAT_TRUNC_f8 = ROUND_TRUNC_f8
171 // Float the rounded integer to get preliminary result
174 (p9) fcvt.xf ROUND_FLOAT_RINT_f8 = ROUND_RINT_f8
178 // If x<0 and the difference of the truncated input minus the input is 0.5
179 // then result = truncated input - 1.0
180 // Else if x>0 and the difference of the input minus truncated input is 0.5
181 // then result = truncated input + 1.0
183 // result = rounded input
187 (p6) fsub.s1 ROUND_REMAINDER = ROUND_FLOAT_TRUNC_f8, ROUND_NORM_f8
193 (p7) fsub.s1 ROUND_REMAINDER = ROUND_NORM_f8, ROUND_FLOAT_TRUNC_f8
197 // Assume preliminary result is rounded integer
200 (p9) fnorm.d.s0 f8 = ROUND_FLOAT_RINT_f8
204 // If x<0, test if result=0
207 (p6) fcmp.eq.unc p10,p0 = ROUND_FLOAT_RINT_f8,f0
211 // If x<0 and result=0, set result=-0
214 (p10) fmerge.ns f8 = f1,f8
218 // If x<0, test if remainder=0.5
221 (p6) fcmp.eq.unc p6,p0 = ROUND_REMAINDER, ROUND_HALF
225 // If x>0, test if remainder=0.5
228 (p7) fcmp.eq.unc p7,p0 = ROUND_REMAINDER, ROUND_HALF
232 // If x<0 and remainder=0.5, result=truncated-1.0
233 // If x>0 and remainder=0.5, result=truncated+1.0
235 .pred.rel "mutex",p6,p7
238 (p6) fsub.d.s0 f8 = ROUND_FLOAT_TRUNC_f8,f1
244 (p7) fadd.d.s0 f8 = ROUND_FLOAT_TRUNC_f8,f1
249 ASM_SIZE_DIRECTIVE(round)