]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/ia64/fpu/s_nexttowardl.S
ia64: move from main tree
[thirdparty/glibc.git] / sysdeps / ia64 / fpu / s_nexttowardl.S
1 .file "nexttowardl.s"
2
3
4 // Copyright (c) 2001 - 2004, Intel Corporation
5 // All rights reserved.
6 //
7 // Contributed 2001 by the Intel Numerics Group, Intel Corporation
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are
11 // met:
12 //
13 // * Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // * Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // * The name of Intel Corporation may not be used to endorse or promote
21 // products derived from this software without specific prior written
22 // permission.
23
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR ITS
28 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
32 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR TORT (INCLUDING
33 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 //
36 // Intel Corporation is the author of this code, and requests that all
37 // problem reports or change requests be submitted to it directly at
38 // http://www.intel.com/software/products/opensource/libraries/num.htm.
39 //
40 // History
41 //==============================================================
42 // 08/15/01 Initial version
43 // 08/23/01 Corrected error tag number
44 // 05/20/02 Cleaned up namespace and sf0 syntax
45 // 02/10/03 Reordered header: .section, .global, .proc, .align
46 // 12/14/04 Added error handling on underflow.
47 //
48 // API
49 //==============================================================
50 // long double nexttowardl( long double x, long double y );
51 // input floating point f8, f9
52 // output floating point f8
53 //
54 // Registers used
55 //==============================================================
56 GR_max_pexp = r14
57 GR_min_pexp = r15
58 GR_exp = r16
59 GR_sig = r17
60 GR_lnorm_sig = r18
61 GR_sign_mask = r19
62 GR_exp_mask = r20
63 GR_sden_sig = r21
64 GR_new_sig = r22
65 GR_new_exp = r23
66 GR_lden_sig = r24
67 GR_snorm_sig = r25
68 GR_exp1 = r26
69 GR_x_exp = r27
70 // r36-39 parameters for libm_error_support
71
72 GR_SAVE_B0 = r34
73 GR_SAVE_GP = r35
74 GR_SAVE_PFS = r32
75
76 GR_Parameter_X = r36
77 GR_Parameter_Y = r37
78 GR_Parameter_RESULT = r38
79 GR_Parameter_TAG = r39
80
81 FR_lnorm_sig = f10
82 FR_lnorm_exp = f11
83 FR_lnorm = f12
84 FR_sden_sig = f13
85 FR_den_exp = f14
86 FR_sden = f15
87 FR_snorm_exp = f32
88 FR_save_f8 = f33
89 FR_new_exp = f34
90 FR_new_sig = f35
91 FR_lden_sig = f36
92 FR_snorm_sig = f37
93 FR_exp1 = f38
94 FR_tmp = f39
95
96 //
97 // Overview of operation
98 //==============================================================
99 // nexttowardl determines the next representable value
100 // after x in the direction of y.
101
102
103 .section .text
104 GLOBAL_LIBM_ENTRY(nexttowardl)
105
106 // Extract signexp from x
107 // Is x < y ? p10 if yes, p11 if no
108 // Form smallest denormal significand = ulp size
109 { .mfi
110 getf.exp GR_exp = f8
111 fcmp.lt.s1 p10,p11 = f8, f9
112 addl GR_sden_sig = 0x1, r0
113 }
114 // Form largest normal significand 0xffffffffffffffff
115 // Form smallest normal exponent
116 { .mfi
117 addl GR_lnorm_sig = -0x1,r0
118 nop.f 999
119 addl GR_min_pexp = 0x0c001, r0 ;;
120 }
121
122 // Extract significand from x
123 // Is x=y? This fcmp also sets Invalid and Denormal if required
124 // Form largest normal exponent
125 { .mfi
126 getf.sig GR_sig = f8
127 fcmp.eq.s0 p6,p0 = f8, f9
128 addl GR_max_pexp = 0x13ffe, r0
129 }
130 // Move largest normal significand to fp reg for special cases
131 { .mfi
132 setf.sig FR_lnorm_sig = GR_lnorm_sig
133 nop.f 999
134 addl GR_sign_mask = 0x20000, r0 ;;
135 }
136
137 // Move smallest denormal significand and exp to fp regs
138 // Is x=nan?
139 // Set p12 and p13 based on whether significand increases or decreases
140 // It increases (p12 set) if x<y and x>=0 or if x>y and x<0
141 // It decreases (p13 set) if x<y and x<0 or if x>y and x>=0
142 { .mfi
143 setf.sig FR_sden_sig = GR_sden_sig
144 fclass.m p8,p0 = f8, 0xc3
145 (p10) cmp.lt p12,p13 = GR_exp, GR_sign_mask
146 }
147 // Move smallest normal exp to fp regs
148 { .mfi
149 setf.exp FR_snorm_exp = GR_min_pexp
150 nop.f 999
151 (p11) cmp.ge p12,p13 = GR_exp, GR_sign_mask ;;
152 }
153
154 .pred.rel "mutex",p12,p13
155
156 // Form expected new significand, adding or subtracting 1 ulp increment
157 // If x=y set result to y
158 // Form smallest normal significand and largest denormal significand
159 { .mfi
160 (p12) add GR_new_sig = GR_sig, GR_sden_sig
161 (p6) fmerge.s f8=f9,f9
162 dep.z GR_snorm_sig = 1,63,1 // 0x8000000000000000
163 }
164 { .mlx
165 (p13) sub GR_new_sig = GR_sig, GR_sden_sig
166 movl GR_lden_sig = 0x7fffffffffffffff ;;
167 }
168
169 // Move expected result significand and signexp to fp regs
170 // Is y=nan?
171 // Form new exponent in case result exponent needs incrementing or decrementing
172 { .mfi
173 setf.exp FR_new_exp = GR_exp
174 fclass.m p9,p0 = f9, 0xc3
175 (p12) add GR_exp1 = 1, GR_exp
176 }
177 { .mib
178 setf.sig FR_new_sig = GR_new_sig
179 (p13) add GR_exp1 = -1, GR_exp
180 (p6) br.ret.spnt b0 ;; // Exit if x=y
181 }
182
183 // Move largest normal signexp to fp reg for special cases
184 // Is x=zero?
185 { .mfi
186 setf.exp FR_lnorm_exp = GR_max_pexp
187 fclass.m p7,p0 = f8, 0x7
188 nop.i 999
189 }
190 { .mfb
191 setf.exp FR_den_exp = GR_min_pexp
192 (p8) fma.s0 f8 = f8,f1,f9
193 (p8) br.ret.spnt b0 ;; // Exit if x=nan
194 }
195
196 // Move exp+-1 and smallest normal significand to fp regs for special cases
197 // Is x=inf?
198 { .mfi
199 setf.exp FR_exp1 = GR_exp1
200 fclass.m p6,p0 = f8, 0x23
201 addl GR_exp_mask = 0x1ffff, r0
202 }
203 { .mfb
204 setf.sig FR_snorm_sig = GR_snorm_sig
205 (p9) fma.s0 f8 = f8,f1,f9
206 (p9) br.ret.spnt b0 ;; // Exit if y=nan
207 }
208
209 // Move largest denormal significand to fp regs for special cases
210 // Save x
211 { .mfb
212 setf.sig FR_lden_sig = GR_lden_sig
213 mov FR_save_f8 = f8
214 (p7) br.cond.spnt NEXT_ZERO ;; // Exit if x=0
215 }
216
217 // Mask off the sign to get x_exp
218 { .mfb
219 and GR_x_exp = GR_exp_mask, GR_exp
220 nop.f 999
221 (p6) br.cond.spnt NEXT_INF ;; // Exit if x=inf
222 }
223
224 // Check 5 special cases when significand rolls over:
225 // 1 sig size incr, x_sig=max_sig, x_exp < max_exp
226 // Set p6, result is sig=min_sig, exp++
227 // 2 sig size incr, x_sig=max_sig, x_exp >= max_exp
228 // Set p7, result is inf, signal overflow
229 // 3 sig size decr, x_sig=min_sig, x_exp > min_exp
230 // Set p8, result is sig=max_sig, exp--
231 // 4 sig size decr, x_sig=min_sig, x_exp = min_exp
232 // Set p9, result is sig=max_den_sig, exp same, signal underflow and inexact
233 // 5 sig size decr, x_sig=min_den_sig, x_exp = min_exp
234 // Set p10, result is zero, sign of x, signal underflow and inexact
235 //
236 { .mmi
237 (p12) cmp.eq.unc p6,p0 = GR_new_sig, r0
238 (p13) cmp.eq.unc p9,p10 = GR_new_sig, GR_lden_sig
239 nop.i 999
240 ;;
241 }
242
243 { .mmi
244 (p6) cmp.lt.unc p6,p7 = GR_x_exp, GR_max_pexp
245 (p10) cmp.eq.unc p10,p0 = GR_new_sig, r0
246 (p9) cmp.le.unc p9,p8 = GR_x_exp, GR_min_pexp
247 ;;
248 }
249
250 // Create small normal in case need to generate underflow flag
251 { .mfi
252 nop.m 999
253 fmerge.se FR_tmp = FR_snorm_exp, FR_lnorm_sig
254 nop.i 999
255 }
256 // Branch if cases 1, 2, 3
257 { .bbb
258 (p6) br.cond.spnt NEXT_EXPUP
259 (p7) br.cond.spnt NEXT_OVERFLOW
260 (p8) br.cond.spnt NEXT_EXPDOWN ;;
261 }
262
263 // Branch if cases 4, 5
264 { .mbb
265 nop.m 999
266 (p9) br.cond.spnt NEXT_NORM_TO_DENORM
267 (p10) br.cond.spnt NEXT_UNDERFLOW_TO_ZERO
268 ;;
269 }
270
271 // Here if no special cases
272 // Set p6 if result will be a denormal, so can force underflow flag
273 // Case 1: x_exp=min_exp, x_sig=unnormalized
274 // Case 2: x_exp<min_exp
275 { .mfi
276 cmp.lt p6,p7 = GR_x_exp, GR_min_pexp
277 fmerge.se f8 = FR_new_exp, FR_new_sig
278 nop.i 999 ;;
279 }
280
281 { .mfi
282 nop.m 999
283 nop.f 999
284 (p6) tbit.z p6,p0 = GR_new_sig, 63 ;;
285 }
286
287 NEXT_COMMON_FINISH:
288 // Force underflow and inexact if denormal result
289 { .mfi
290 nop.m 999
291 (p6) fma.s0 FR_tmp = FR_tmp,FR_tmp,f0
292 nop.i 999
293 }
294 { .mfb
295 nop.m 999
296 fnorm.s0 f8 = f8 // Final normalization to result precision
297 (p6) br.cond.spnt NEXT_UNDERFLOW ;;
298 }
299
300 { .mfb
301 nop.m 999
302 nop.f 999
303 br.ret.sptk b0;;
304 }
305
306 //Special cases
307 NEXT_EXPUP:
308 { .mfb
309 cmp.lt p6,p7 = GR_x_exp, GR_min_pexp
310 fmerge.se f8 = FR_exp1, FR_snorm_sig
311 br.cond.sptk NEXT_COMMON_FINISH ;;
312 }
313
314 NEXT_EXPDOWN:
315 { .mfb
316 cmp.lt p6,p7 = GR_x_exp, GR_min_pexp
317 fmerge.se f8 = FR_exp1, FR_lnorm_sig
318 br.cond.sptk NEXT_COMMON_FINISH ;;
319 }
320
321 NEXT_NORM_TO_DENORM:
322 { .mfi
323 nop.m 999
324 fmerge.se f8 = FR_exp1, FR_lden_sig
325 nop.i 999
326 }
327 // Force underflow and inexact
328 { .mfb
329 nop.m 999
330 fma.s0 FR_tmp = FR_tmp,FR_tmp,f0
331 br.cond.sptk NEXT_UNDERFLOW ;;
332 }
333
334 NEXT_UNDERFLOW_TO_ZERO:
335 { .mfb
336 cmp.eq p6,p0 = r0,r0
337 fmerge.s f8 = FR_save_f8,f0
338 br.cond.sptk NEXT_COMMON_FINISH ;;
339 }
340
341 NEXT_INF:
342 // Here if f8 is +- infinity
343 // INF
344 // if f8 is +inf, no matter what y is return largest long double
345 // if f8 is -inf, no matter what y is return -largest long double
346
347 // Create largest long double
348 { .mfi
349 nop.m 999
350 fmerge.se FR_lnorm = FR_lnorm_exp,FR_lnorm_sig
351 nop.i 999 ;;
352 }
353
354 { .mfb
355 nop.m 999
356 fmerge.s f8 = f8,FR_lnorm
357 br.ret.sptk b0 ;;
358 }
359
360 NEXT_ZERO:
361
362 // Here if f8 is +- zero
363 // ZERO
364 // if f8 is zero and y is +, return + smallest long double denormal
365 // if f8 is zero and y is -, return - smallest long double denormal
366
367 { .mfi
368 nop.m 999
369 fmerge.se FR_sden = f0,FR_sden_sig
370 nop.i 999 ;;
371 }
372
373 // Create small normal to generate underflow flag
374 { .mfi
375 nop.m 999
376 fmerge.se FR_tmp = FR_snorm_exp, FR_lnorm_sig
377 nop.i 999 ;;
378 }
379
380 // Add correct sign from direction arg
381 { .mfi
382 nop.m 999
383 fmerge.s f8 = f9,FR_sden
384 nop.i 999 ;;
385 }
386
387 // Force underflow and inexact flags
388 { .mfb
389 nop.m 999
390 fma.s0 FR_tmp = FR_tmp,FR_tmp,f0
391 br.cond.sptk NEXT_UNDERFLOW ;;
392 }
393
394 NEXT_UNDERFLOW:
395 // Here if result is a denorm, or input is finite and result is zero
396 // Call error support to report possible range error
397 { .mib
398 alloc r32=ar.pfs,2,2,4,0
399 mov GR_Parameter_TAG = 270 // Error code
400 br.cond.sptk __libm_error_region // Branch to error call
401 }
402 ;;
403
404 NEXT_OVERFLOW:
405 // Here if input is finite, but result will be infinite
406 // Use frcpa to generate infinity of correct sign
407 // Call error support to report possible range error
408 { .mfi
409 alloc r32=ar.pfs,2,2,4,0
410 frcpa.s1 f8,p6 = FR_save_f8, f0
411 nop.i 999 ;;
412 }
413
414 // Create largest double
415 { .mfi
416 nop.m 999
417 fmerge.se FR_lnorm = FR_lnorm_exp,FR_lnorm_sig
418 nop.i 999 ;;
419 }
420
421 // Force overflow and inexact flags to be set
422 { .mfb
423 mov GR_Parameter_TAG = 198 // Error code
424 fma.s0 FR_tmp = FR_lnorm,FR_lnorm,f0
425 br.cond.sptk __libm_error_region // Branch to error call
426 }
427 ;;
428
429 GLOBAL_LIBM_END(nexttowardl)
430
431
432 LOCAL_LIBM_ENTRY(__libm_error_region)
433 .prologue
434
435 // (1)
436 { .mfi
437 add GR_Parameter_Y=-32,sp // Parameter 2 value
438 nop.f 0
439 .save ar.pfs,GR_SAVE_PFS
440 mov GR_SAVE_PFS=ar.pfs // Save ar.pfs
441 }
442 { .mfi
443 .fframe 64
444 add sp=-64,sp // Create new stack
445 nop.f 0
446 mov GR_SAVE_GP=gp // Save gp
447 };;
448
449
450 // (2)
451 { .mmi
452 stfe [GR_Parameter_Y] = f9,16 // STORE Parameter 2 on stack
453 add GR_Parameter_X = 16,sp // Parameter 1 address
454 .save b0, GR_SAVE_B0
455 mov GR_SAVE_B0=b0 // Save b0
456 };;
457
458 .body
459 // (3)
460 { .mib
461 stfe [GR_Parameter_X] = FR_save_f8 // STORE Parameter 1 on stack
462 add GR_Parameter_RESULT = 0,GR_Parameter_Y // Parameter 3 address
463 nop.b 0
464 }
465 { .mib
466 stfe [GR_Parameter_Y] = f8 // STORE Parameter 3 on stack
467 add GR_Parameter_Y = -16,GR_Parameter_Y
468 br.call.sptk b0=__libm_error_support# // Call error handling function
469 };;
470 { .mmi
471 nop.m 0
472 nop.m 0
473 add GR_Parameter_RESULT = 48,sp
474 };;
475
476 // (4)
477 { .mmi
478 ldfe f8 = [GR_Parameter_RESULT] // Get return result off stack
479 .restore sp
480 add sp = 64,sp // Restore stack pointer
481 mov b0 = GR_SAVE_B0 // Restore return address
482 };;
483 { .mib
484 mov gp = GR_SAVE_GP // Restore gp
485 mov ar.pfs = GR_SAVE_PFS // Restore ar.pfs
486 br.ret.sptk b0 // Return
487 };;
488
489 LOCAL_LIBM_END(__libm_error_region)
490
491
492 .type __libm_error_support#,@function
493 .global __libm_error_support#
494