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