]>
Commit | Line | Data |
---|---|---|
d5efd131 MF |
1 | .file "nexttowardf.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 | ||
0347518d MF |
24 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
25 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
d5efd131 | 26 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
0347518d | 27 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR ITS |
d5efd131 | 28 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
0347518d MF |
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 | |
d5efd131 | 32 | // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR TORT (INCLUDING |
0347518d MF |
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 | // | |
d5efd131 | 36 | // Intel Corporation is the author of this code, and requests that all |
0347518d | 37 | // problem reports or change requests be submitted to it directly at |
d5efd131 MF |
38 | // http://www.intel.com/software/products/opensource/libraries/num.htm. |
39 | // | |
40 | // History | |
41 | //============================================================== | |
0347518d | 42 | // 08/15/01 Initial version |
d5efd131 MF |
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 | // float nexttowardf( float 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 | GR_min_den_rexp = r28 | |
71 | // r36-39 parameters for libm_error_support | |
72 | ||
73 | GR_SAVE_B0 = r34 | |
74 | GR_SAVE_GP = r35 | |
75 | GR_SAVE_PFS = r32 | |
76 | ||
77 | GR_Parameter_X = r36 | |
78 | GR_Parameter_Y = r37 | |
79 | GR_Parameter_RESULT = r38 | |
80 | GR_Parameter_TAG = r39 | |
81 | ||
82 | FR_lnorm_sig = f10 | |
83 | FR_lnorm_exp = f11 | |
84 | FR_lnorm = f12 | |
85 | FR_sden_sig = f13 | |
86 | FR_sden_exp = f14 | |
87 | FR_sden = f15 | |
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 | //============================================================== | |
0347518d MF |
99 | // nexttowardf determines the next representable value |
100 | // after x in the direction of y. | |
d5efd131 MF |
101 | |
102 | ||
103 | .section .text | |
104 | GLOBAL_LIBM_ENTRY(nexttowardf) | |
105 | ||
106 | // Extract signexp from x | |
107 | // Form smallest denormal significand = ulp size | |
108 | { .mlx | |
109 | getf.exp GR_exp = f8 | |
110 | movl GR_sden_sig = 0x0000010000000000 | |
111 | } | |
112 | // Form largest normal exponent | |
113 | // Is x < y ? p10 if yes, p11 if no | |
114 | // Form smallest normal exponent | |
115 | { .mfi | |
116 | addl GR_max_pexp = 0x1007e, r0 | |
117 | fcmp.lt.s1 p10,p11 = f8, f9 | |
118 | addl GR_min_pexp = 0x0ff81, r0 ;; | |
119 | } | |
120 | ||
121 | // Is x=y? | |
122 | { .mfi | |
123 | getf.sig GR_sig = f8 | |
124 | fcmp.eq.s0 p6,p0 = f8, f9 | |
125 | nop.i 0 | |
126 | } | |
127 | // Extract significand from x | |
128 | // Form largest normal significand | |
129 | { .mlx | |
0347518d | 130 | nop.m 0 |
d5efd131 MF |
131 | movl GR_lnorm_sig = 0xffffff0000000000 ;; |
132 | } | |
133 | ||
134 | // Move largest normal significand to fp reg for special cases | |
135 | { .mfi | |
136 | setf.sig FR_lnorm_sig = GR_lnorm_sig | |
0347518d | 137 | nop.f 0 |
d5efd131 MF |
138 | addl GR_sign_mask = 0x20000, r0 ;; |
139 | } | |
140 | ||
141 | // Move smallest denormal significand and signexp to fp regs | |
142 | // Is x=nan? | |
143 | // Set p12 and p13 based on whether significand increases or decreases | |
144 | // It increases (p12 set) if x<y and x>=0 or if x>y and x<0 | |
145 | // It decreases (p13 set) if x<y and x<0 or if x>y and x>=0 | |
146 | { .mfi | |
147 | setf.sig FR_sden_sig = GR_sden_sig | |
0347518d | 148 | fclass.m p8,p0 = f8, 0xc3 |
d5efd131 MF |
149 | (p10) cmp.lt p12,p13 = GR_exp, GR_sign_mask |
150 | } | |
151 | { .mfi | |
152 | setf.exp FR_sden_exp = GR_min_pexp | |
153 | nop.f 999 | |
154 | (p11) cmp.ge p12,p13 = GR_exp, GR_sign_mask ;; | |
155 | } | |
156 | ||
157 | .pred.rel "mutex",p12,p13 | |
158 | ||
159 | // Form expected new significand, adding or subtracting 1 ulp increment | |
160 | // If x=y set result to y | |
161 | // Form smallest normal significand and largest denormal significand | |
162 | { .mfi | |
163 | (p12) add GR_new_sig = GR_sig, GR_sden_sig | |
164 | (p6) fnorm.s.s0 f8=f9 //Normalise | |
165 | dep.z GR_snorm_sig = 1,63,1 // 0x8000000000000000 | |
166 | } | |
167 | { .mlx | |
168 | (p13) sub GR_new_sig = GR_sig, GR_sden_sig | |
169 | movl GR_lden_sig = 0x7fffff0000000000 ;; | |
170 | } | |
171 | ||
172 | // Move expected result significand and signexp to fp regs | |
173 | // Is y=nan? | |
174 | // Form new exponent in case result exponent needs incrementing or decrementing | |
175 | { .mfi | |
176 | setf.exp FR_new_exp = GR_exp | |
0347518d | 177 | fclass.m p9,p0 = f9, 0xc3 |
d5efd131 MF |
178 | (p12) add GR_exp1 = 1, GR_exp |
179 | } | |
180 | { .mib | |
181 | setf.sig FR_new_sig = GR_new_sig | |
182 | (p13) add GR_exp1 = -1, GR_exp | |
183 | (p6) br.ret.spnt b0 ;; // Exit if x=y | |
184 | } | |
185 | ||
186 | // Move largest normal signexp to fp reg for special cases | |
187 | // Is x=zero? | |
188 | { .mfi | |
189 | setf.exp FR_lnorm_exp = GR_max_pexp | |
190 | fclass.m p7,p0 = f8, 0x7 | |
191 | nop.i 999 | |
192 | } | |
193 | { .mfb | |
194 | nop.m 999 | |
0347518d | 195 | (p8) fma.s0 f8 = f8,f1,f9 |
d5efd131 MF |
196 | (p8) br.ret.spnt b0 ;; // Exit if x=nan |
197 | } | |
198 | ||
199 | // Move exp+-1 and smallest normal significand to fp regs for special cases | |
200 | // Is x=inf? | |
201 | { .mfi | |
202 | setf.exp FR_exp1 = GR_exp1 | |
0347518d | 203 | fclass.m p6,p0 = f8, 0x23 |
d5efd131 MF |
204 | addl GR_exp_mask = 0x1ffff, r0 |
205 | } | |
206 | { .mfb | |
207 | setf.sig FR_snorm_sig = GR_snorm_sig | |
0347518d | 208 | (p9) fma.s0 f8 = f8,f1,f9 |
d5efd131 MF |
209 | (p9) br.ret.spnt b0 ;; // Exit if y=nan |
210 | } | |
211 | ||
212 | // Move largest denormal significand to fp regs for special cases | |
213 | // Save x | |
214 | { .mfb | |
215 | setf.sig FR_lden_sig = GR_lden_sig | |
216 | mov FR_save_f8 = f8 | |
0347518d | 217 | (p7) br.cond.spnt NEXT_ZERO ;; // Exit if x=0 |
d5efd131 MF |
218 | } |
219 | ||
220 | // Mask off the sign to get x_exp | |
221 | { .mfb | |
222 | and GR_x_exp = GR_exp_mask, GR_exp | |
223 | nop.f 999 | |
0347518d | 224 | (p6) br.cond.spnt NEXT_INF ;; // Exit if x=inf |
d5efd131 MF |
225 | } |
226 | ||
227 | // Check 6 special cases when significand rolls over: | |
228 | // 1 sig size incr, x_sig=max_sig, x_exp < max_exp | |
229 | // Set p6, result is sig=min_sig, exp++ | |
230 | // 2 sig size incr, x_sig=max_sig, x_exp >= max_exp | |
231 | // Set p7, result is inf, signal overflow | |
232 | // 3 sig size decr, x_sig=min_sig, x_exp > min_exp | |
233 | // Set p8, result is sig=max_sig, exp-- | |
234 | // 4 sig size decr, x_sig=min_sig, x_exp = min_exp | |
235 | // Set p9, result is sig=max_den_sig, exp same, signal underflow and inexact | |
236 | // 5 sig size decr, x_sig=min_den_sig, x_exp = min_exp | |
237 | // Set p10, result is zero, sign of x, signal underflow and inexact | |
0347518d | 238 | // 6 sig size decr, x_sig=min_sig, x_exp < min_exp |
d5efd131 MF |
239 | // Set p14, result is zero, sign of x, signal underflow and inexact |
240 | // | |
241 | // Form exponent of smallest float denormal (if normalized register format) | |
242 | { .mmi | |
243 | adds GR_min_den_rexp = -23, GR_min_pexp | |
244 | (p12) cmp.eq.unc p6,p0 = GR_new_sig, r0 | |
245 | (p13) cmp.eq.unc p8,p10 = GR_new_sig, GR_lden_sig ;; | |
246 | } | |
247 | ||
248 | { .mmi | |
249 | (p6) cmp.lt.unc p6,p7 = GR_x_exp, GR_max_pexp | |
250 | (p8) cmp.gt.unc p8,p9 = GR_x_exp, GR_min_pexp | |
251 | (p10) cmp.eq.unc p10,p0 = GR_new_sig, r0 ;; | |
252 | } | |
253 | ||
254 | // Create small normal in case need to generate underflow flag | |
255 | { .mfi | |
256 | (p10) cmp.le.unc p10,p0 = GR_x_exp, GR_min_pexp | |
257 | fmerge.se FR_tmp = FR_sden_exp, FR_lnorm_sig | |
258 | (p9) cmp.gt.unc p9,p14 = GR_x_exp, GR_min_den_rexp | |
259 | } | |
260 | // Branch if cases 1, 2, 3 | |
261 | { .bbb | |
262 | (p6) br.cond.spnt NEXT_EXPUP | |
263 | (p7) br.cond.spnt NEXT_OVERFLOW | |
264 | (p8) br.cond.spnt NEXT_EXPDOWN ;; | |
265 | } | |
266 | ||
267 | // Branch if cases 4, 5, 6 | |
268 | { .bbb | |
269 | (p9) br.cond.spnt NEXT_NORM_TO_DENORM | |
270 | (p10) br.cond.spnt NEXT_UNDERFLOW_TO_ZERO | |
271 | (p14) br.cond.spnt NEXT_UNDERFLOW_TO_ZERO ;; | |
272 | } | |
273 | ||
274 | // Here if no special cases | |
275 | // Set p6 if result will be a denormal, so can force underflow flag | |
276 | // Case 1: x_exp=min_exp, x_sig=unnormalized | |
277 | // Case 2: x_exp<min_exp | |
278 | { .mfi | |
279 | cmp.lt p6,p7 = GR_x_exp, GR_min_pexp | |
280 | fmerge.se f8 = FR_new_exp, FR_new_sig | |
281 | nop.i 999 ;; | |
282 | } | |
283 | ||
284 | { .mfi | |
285 | nop.m 999 | |
286 | nop.f 999 | |
287 | (p7) tbit.z p6,p0 = GR_new_sig, 63 ;; | |
288 | } | |
289 | ||
290 | NEXT_COMMON_FINISH: | |
291 | // Force underflow and inexact if denormal result | |
292 | { .mfi | |
293 | nop.m 999 | |
294 | (p6) fma.s.s0 FR_tmp = FR_tmp,FR_tmp,f0 | |
295 | nop.i 999 | |
296 | } | |
297 | { .mfb | |
298 | nop.m 999 | |
299 | fnorm.s.s0 f8 = f8 // Final normalization to result precision | |
300 | (p6) br.cond.spnt NEXT_UNDERFLOW ;; | |
301 | } | |
302 | ||
303 | { .mfb | |
304 | nop.m 999 | |
305 | nop.f 999 | |
306 | br.ret.sptk b0;; | |
307 | } | |
308 | ||
309 | //Special cases | |
310 | NEXT_EXPUP: | |
311 | { .mfb | |
312 | cmp.lt p6,p7 = GR_x_exp, GR_min_pexp | |
313 | fmerge.se f8 = FR_exp1, FR_snorm_sig | |
314 | br.cond.sptk NEXT_COMMON_FINISH ;; | |
315 | } | |
316 | ||
317 | NEXT_EXPDOWN: | |
318 | { .mfb | |
319 | cmp.lt p6,p7 = GR_x_exp, GR_min_pexp | |
320 | fmerge.se f8 = FR_exp1, FR_lnorm_sig | |
321 | br.cond.sptk NEXT_COMMON_FINISH ;; | |
322 | } | |
323 | ||
324 | NEXT_NORM_TO_DENORM: | |
325 | { .mfi | |
326 | nop.m 999 | |
327 | fmerge.se f8 = FR_new_exp, FR_lden_sig | |
328 | nop.i 999 | |
329 | } | |
330 | // Force underflow and inexact | |
331 | { .mfb | |
332 | nop.m 999 | |
333 | fma.s.s0 FR_tmp = FR_tmp,FR_tmp,f0 | |
334 | br.cond.sptk NEXT_UNDERFLOW ;; | |
335 | } | |
336 | ||
337 | NEXT_UNDERFLOW_TO_ZERO: | |
338 | { .mfb | |
339 | cmp.eq p6,p0 = r0,r0 | |
340 | fmerge.s f8 = FR_save_f8,f0 | |
341 | br.cond.sptk NEXT_COMMON_FINISH ;; | |
342 | } | |
343 | ||
0347518d | 344 | NEXT_INF: |
d5efd131 MF |
345 | // Here if f8 is +- infinity |
346 | // INF | |
347 | // if f8 is +inf, no matter what y is return largest float | |
348 | // if f8 is -inf, no matter what y is return -largest float | |
349 | ||
350 | { .mfi | |
351 | nop.m 999 | |
352 | fmerge.se FR_lnorm = FR_lnorm_exp,FR_lnorm_sig | |
353 | nop.i 999 ;; | |
354 | } | |
355 | ||
356 | { .mfb | |
357 | nop.m 999 | |
0347518d MF |
358 | fmerge.s f8 = f8,FR_lnorm |
359 | br.ret.sptk b0 ;; | |
d5efd131 MF |
360 | } |
361 | ||
0347518d | 362 | NEXT_ZERO: |
d5efd131 MF |
363 | |
364 | // Here if f8 is +- zero | |
365 | // ZERO | |
0347518d MF |
366 | // if f8 is zero and y is +, return + smallest float denormal |
367 | // if f8 is zero and y is -, return - smallest float denormal | |
d5efd131 MF |
368 | |
369 | { .mfi | |
370 | nop.m 999 | |
371 | fmerge.se FR_sden = FR_sden_exp,FR_sden_sig | |
372 | nop.i 999 ;; | |
373 | } | |
374 | ||
375 | // Create small normal to generate underflow flag | |
376 | { .mfi | |
377 | nop.m 999 | |
378 | fmerge.se FR_tmp = FR_sden_exp, FR_lnorm_sig | |
379 | nop.i 999 ;; | |
380 | } | |
381 | ||
382 | // Add correct sign from direction arg | |
383 | { .mfi | |
384 | nop.m 999 | |
0347518d | 385 | fmerge.s f8 = f9,FR_sden |
d5efd131 MF |
386 | nop.i 999 ;; |
387 | } | |
388 | ||
389 | // Force underflow and inexact flags | |
390 | { .mfb | |
391 | nop.m 999 | |
392 | fma.s.s0 FR_tmp = FR_tmp,FR_tmp,f0 | |
393 | br.cond.sptk NEXT_UNDERFLOW ;; | |
394 | } | |
395 | ||
0347518d | 396 | NEXT_UNDERFLOW: |
d5efd131 MF |
397 | // Here if result is a denorm, or input is finite and result is zero |
398 | // Call error support to report possible range error | |
399 | { .mib | |
400 | alloc r32=ar.pfs,2,2,4,0 | |
401 | mov GR_Parameter_TAG = 272 // Error code | |
402 | br.cond.sptk __libm_error_region // Branch to error call | |
403 | } | |
404 | ;; | |
405 | ||
0347518d | 406 | NEXT_OVERFLOW: |
d5efd131 MF |
407 | // Here if input is finite, but result will be infinite |
408 | // Use frcpa to generate infinity of correct sign | |
409 | // Call error support to report possible range error | |
410 | { .mfi | |
411 | alloc r32=ar.pfs,2,2,4,0 | |
412 | frcpa.s1 f8,p6 = FR_save_f8, f0 | |
413 | nop.i 999 ;; | |
414 | } | |
415 | ||
416 | // Create largest double | |
417 | { .mfi | |
418 | nop.m 999 | |
419 | fmerge.se FR_lnorm = FR_lnorm_exp,FR_lnorm_sig | |
420 | nop.i 999 ;; | |
421 | } | |
422 | ||
423 | // Force overflow and inexact flags to be set | |
424 | { .mfb | |
425 | mov GR_Parameter_TAG = 200 // Error code | |
426 | fma.s.s0 FR_tmp = FR_lnorm,FR_lnorm,f0 | |
427 | br.cond.sptk __libm_error_region // Branch to error call | |
428 | } | |
429 | ;; | |
430 | ||
431 | GLOBAL_LIBM_END(nexttowardf) | |
432 | ||
433 | ||
434 | LOCAL_LIBM_ENTRY(__libm_error_region) | |
435 | .prologue | |
436 | ||
437 | // (1) | |
438 | { .mfi | |
439 | add GR_Parameter_Y=-32,sp // Parameter 2 value | |
440 | nop.f 0 | |
441 | .save ar.pfs,GR_SAVE_PFS | |
442 | mov GR_SAVE_PFS=ar.pfs // Save ar.pfs | |
443 | } | |
444 | { .mfi | |
445 | .fframe 64 | |
446 | add sp=-64,sp // Create new stack | |
447 | nop.f 0 | |
448 | mov GR_SAVE_GP=gp // Save gp | |
449 | };; | |
450 | ||
451 | ||
452 | // (2) | |
453 | { .mmi | |
454 | stfs [GR_Parameter_Y] = f9,16 // STORE Parameter 2 on stack | |
455 | add GR_Parameter_X = 16,sp // Parameter 1 address | |
456 | .save b0, GR_SAVE_B0 | |
457 | mov GR_SAVE_B0=b0 // Save b0 | |
458 | };; | |
459 | ||
460 | .body | |
461 | // (3) | |
462 | { .mib | |
463 | stfs [GR_Parameter_X] = FR_save_f8 // STORE Parameter 1 on stack | |
464 | add GR_Parameter_RESULT = 0,GR_Parameter_Y // Parameter 3 address | |
0347518d | 465 | nop.b 0 |
d5efd131 MF |
466 | } |
467 | { .mib | |
468 | stfs [GR_Parameter_Y] = f8 // STORE Parameter 3 on stack | |
469 | add GR_Parameter_Y = -16,GR_Parameter_Y | |
470 | br.call.sptk b0=__libm_error_support# // Call error handling function | |
471 | };; | |
472 | { .mmi | |
473 | nop.m 0 | |
474 | nop.m 0 | |
475 | add GR_Parameter_RESULT = 48,sp | |
476 | };; | |
477 | ||
478 | // (4) | |
479 | { .mmi | |
480 | ldfs f8 = [GR_Parameter_RESULT] // Get return result off stack | |
481 | .restore sp | |
482 | add sp = 64,sp // Restore stack pointer | |
483 | mov b0 = GR_SAVE_B0 // Restore return address | |
484 | };; | |
485 | { .mib | |
486 | mov gp = GR_SAVE_GP // Restore gp | |
487 | mov ar.pfs = GR_SAVE_PFS // Restore ar.pfs | |
488 | br.ret.sptk b0 // Return | |
489 | };; | |
490 | ||
491 | LOCAL_LIBM_END(__libm_error_region) | |
492 | ||
493 | ||
494 | .type __libm_error_support#,@function | |
495 | .global __libm_error_support# | |
496 |