(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / sysdeps / ia64 / fpu / s_round.S
blobb08ede17401878e158ec821b4e4cfbcf89bfeff3
1 .file "round.s"
3 // Copyright (C) 2000, 2001, Intel Corporation
4 // All rights reserved.
5 // 
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.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
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
23 // permission.
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. 
36 // 
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.
41 // History
42 //==============================================================
43 // 10/25/2000: Created
44 //==============================================================
46 // API
47 //==============================================================
48 // double round(double x)
51 #include "libm_support.h"
53 // general input registers:  
55 round_GR_half      = r14
56 round_GR_big       = r15
57 round_GR_expmask   = r16
58 round_GR_signexp   = r17
59 round_GR_exp       = r18
60 round_GR_expdiff   = r19
62 // predicate registers used: 
63 // p6 - p10
65 // floating-point registers used: 
67 ROUND_NORM_f8        = f9                        
68 ROUND_TRUNC_f8       = f10
69 ROUND_RINT_f8        = f11
70 ROUND_FLOAT_TRUNC_f8 = f12
71 ROUND_FLOAT_RINT_f8  = f13
72 ROUND_REMAINDER      = f14
73 ROUND_HALF           = f15
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 
81 // zero. 
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
95 .align 32
96 .global round#
98 .section .text
99 .proc  round#
100 .align 32
103 round: 
104         
105 // Get exponent for +0.5
106 // Truncate x to integer
107 { .mfi
108       addl           round_GR_half  = 0x0fffe, r0
109       fcvt.fx.trunc.s1     ROUND_TRUNC_f8 = f8
110       nop.i 999
112         
113 // Get signexp of x
114 // Normalize input
115 // Form exponent mask
116 { .mfi
117       getf.exp  round_GR_signexp = f8
118       fnorm     ROUND_NORM_f8 = f8                        
119       addl      round_GR_expmask  = 0x1ffff, r0 ;;
122 // Form +0.5
123 // Round x to integer
124 { .mfi
125       setf.exp    ROUND_HALF  = round_GR_half                      
126       fcvt.fx.s1  ROUND_RINT_f8 = f8
127       nop.i 999 ;;
129 // Get exp of x
130 // Test for NAN, INF, ZERO
131 // Get exponent at which input has no fractional part
132 { .mfi
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 ;;
138 // Get exp-bigexp
139 // If exp is so big there is no fractional part, then turn on p8, off p9
140 { .mmi
141       sub    round_GR_expdiff = round_GR_exp, round_GR_big ;;
142 #ifdef _LIBC
143 (p9)  cmp.lt.or.andcm  p8,p9 = r0, round_GR_expdiff
144 #else
145 (p9)  cmp.ge.or.andcm  p8,p9 = round_GR_expdiff, r0
146 #endif
147       nop.i 999 ;;
149      
150 // Set p6 if x<0, else set p7
151 { .mfi
152       nop.m 999
153 (p9)  fcmp.lt.unc  p6,p7 = f8,f0
154       nop.i 999
156         
157 // If NAN, INF, ZERO, or no fractional part, result is just normalized input
158 { .mfi
159       nop.m 999
160 (p8)  fnorm.d.s0  f8 = f8
161       nop.i 999 ;;
164 // Float the truncated integer
165 { .mfi
166       nop.m 999
167 (p9)  fcvt.xf     ROUND_FLOAT_TRUNC_f8 = ROUND_TRUNC_f8
168       nop.i 999 ;;
171 // Float the rounded integer to get preliminary result
172 { .mfi
173       nop.m 999
174 (p9)  fcvt.xf     ROUND_FLOAT_RINT_f8 = ROUND_RINT_f8
175       nop.i 999 ;;
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
182 // Else 
183 //    result = rounded input
184 // Endif
185 { .mfi
186       nop.m 999
187 (p6)  fsub.s1   ROUND_REMAINDER = ROUND_FLOAT_TRUNC_f8, ROUND_NORM_f8 
188       nop.i 999
190         
191 { .mfi
192       nop.m 999
193 (p7)  fsub.s1   ROUND_REMAINDER = ROUND_NORM_f8, ROUND_FLOAT_TRUNC_f8
194       nop.i 999 ;;
197 // Assume preliminary result is rounded integer
198 { .mfi
199       nop.m 999
200 (p9)  fnorm.d.s0  f8 = ROUND_FLOAT_RINT_f8
201       nop.i 999 
204 // If x<0, test if result=0
205 { .mfi
206       nop.m 999
207 (p6)  fcmp.eq.unc  p10,p0 = ROUND_FLOAT_RINT_f8,f0
208       nop.i 999 ;;
211 // If x<0 and result=0, set result=-0
212 { .mfi
213       nop.m 999
214 (p10) fmerge.ns  f8 = f1,f8
215       nop.i 999
217         
218 // If x<0, test if remainder=0.5
219 { .mfi
220       nop.m 999
221 (p6)  fcmp.eq.unc  p6,p0 = ROUND_REMAINDER, ROUND_HALF
222       nop.i 999 ;; 
224         
225 // If x>0, test if remainder=0.5
226 { .mfi
227       nop.m 999
228 (p7)  fcmp.eq.unc  p7,p0 = ROUND_REMAINDER, ROUND_HALF
229       nop.i 999 ;;
232 // If x<0 and remainder=0.5, result=truncated-1.0
233 // If x>0 and remainder=0.5, result=truncated+1.0
234 // Exit
235 .pred.rel "mutex",p6,p7
236 { .mfi
237       nop.m 999
238 (p6)  fsub.d.s0  f8 = ROUND_FLOAT_TRUNC_f8,f1
239       nop.i 999 
241         
242 { .mfb
243       nop.m 999
244 (p7)  fadd.d.s0  f8 = ROUND_FLOAT_TRUNC_f8,f1
245       br.ret.sptk  b0 ;;
248 .endp round
249 ASM_SIZE_DIRECTIVE(round)