about summary refs log tree commit diff
path: root/sysdeps/ia64/fpu/s_nexttoward.S
blob: f8fac1e07214de99ac41b4026737ec6b66a4e173 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
.file "nexttoward.s"


// Copyright (c) 2001 - 2003, Intel Corporation
// All rights reserved.
//
// Contributed 2001 by the Intel Numerics Group, Intel Corporation
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// * The name of Intel Corporation may not be used to endorse or promote
// products derived from this software without specific prior written
// permission.

// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR ITS 
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
// 
// Intel Corporation is the author of this code, and requests that all
// problem reports or change requests be submitted to it directly at 
// http://www.intel.com/software/products/opensource/libraries/num.htm.
//
// History
//==============================================================
// 08/15/01 Initial version 
// 08/23/01 Corrected error tag number
// 05/20/02 Cleaned up namespace and sf0 syntax
// 02/10/03 Reordered header: .section, .global, .proc, .align
//
// API
//==============================================================
// double nexttoward( double x, long double y );
// input  floating point f8, f9
// output floating point f8
//
// Registers used
//==============================================================
nexttoward_GR_max_pexp     = r14
nexttoward_GR_min_pexp     = r15
nexttoward_GR_exp          = r16
nexttoward_GR_sig          = r17
nexttoward_GR_lnorm_sig    = r18
nexttoward_GR_sign_mask    = r19
nexttoward_GR_exp_mask     = r20
nexttoward_GR_sden_sig     = r21
nexttoward_GR_new_sig      = r22
nexttoward_GR_new_exp      = r23
nexttoward_GR_lden_sig     = r24
nexttoward_GR_snorm_sig    = r25
nexttoward_GR_exp1         = r26
nexttoward_GR_x_exp        = r27
nexttoward_GR_min_den_rexp = r28
// r36-39 parameters for libm_error_support

GR_SAVE_B0                = r34
GR_SAVE_GP                = r35
GR_SAVE_PFS               = r32

GR_Parameter_X            = r36
GR_Parameter_Y            = r37
GR_Parameter_RESULT       = r38

NEXTTOWARD_lnorm_sig       = f10
NEXTTOWARD_lnorm_exp       = f11
NEXTTOWARD_lnorm           = f12
NEXTTOWARD_sden_sig        = f13
NEXTTOWARD_sden_exp        = f14
NEXTTOWARD_sden            = f15
NEXTTOWARD_save_f8         = f33
NEXTTOWARD_new_exp         = f34
NEXTTOWARD_new_sig         = f35
NEXTTOWARD_lden_sig        = f36
NEXTTOWARD_snorm_sig       = f37
NEXTTOWARD_exp1            = f38
NEXTTOWARD_tmp             = f39

//
// Overview of operation
//==============================================================
// nexttoward determines the next representable value 
// after x in the direction of y. 


.section .text
GLOBAL_LIBM_ENTRY(nexttoward)

// Extract signexp from x
// Is x < y ?  p10 if yes, p11 if no
// Form smallest denormal significand = ulp size
{ .mfi
      getf.exp nexttoward_GR_exp      = f8
      fcmp.lt.s1 p10,p11 = f8, f9                
      addl nexttoward_GR_sden_sig = 0x800, r0
}
// Form largest normal significand 0xfffffffffffff800
// Form smallest normal exponent
{ .mfi
      addl nexttoward_GR_lnorm_sig = -0x800,r0
      nop.f 999
      addl nexttoward_GR_min_pexp = 0x0fc01, r0 ;;
}
// Extract significand from x
// Is x=y?
// Form largest normal exponent
{ .mfi
      getf.sig nexttoward_GR_sig      = f8
      fcmp.eq.s0 p6,p0 = f8, f9                
      addl nexttoward_GR_max_pexp = 0x103fe, r0
}
// Move largest normal significand to fp reg for special cases
{ .mfi
      setf.sig NEXTTOWARD_lnorm_sig = nexttoward_GR_lnorm_sig
      nop.f 999
      addl nexttoward_GR_sign_mask = 0x20000, r0 ;;
}

// Move smallest denormal significand and signexp to fp regs
// Is x=nan?
// Set p12 and p13 based on whether significand increases or decreases
// It increases (p12 set) if x<y and x>=0 or if x>y and x<0
// It decreases (p13 set) if x<y and x<0  or if x>y and x>=0
{ .mfi
      setf.sig NEXTTOWARD_sden_sig = nexttoward_GR_sden_sig
      fclass.m  p8,p0 = f8, 0xc3           
(p10) cmp.lt p12,p13 = nexttoward_GR_exp, nexttoward_GR_sign_mask
}
{ .mfi
      setf.exp NEXTTOWARD_sden_exp = nexttoward_GR_min_pexp
(p11) cmp.ge p12,p13 = nexttoward_GR_exp, nexttoward_GR_sign_mask ;;
}

.pred.rel "mutex",p12,p13

// Form expected new significand, adding or subtracting 1 ulp increment
// If x=y set result to y
// Form smallest normal significand and largest denormal significand
{ .mfi
(p12) add nexttoward_GR_new_sig = nexttoward_GR_sig, nexttoward_GR_sden_sig
(p6)  fnorm.d.s0 f8=f9  //Normalise
      dep.z nexttoward_GR_snorm_sig = 1,63,1 // 0x8000000000000000
}
{ .mlx
(p13) sub nexttoward_GR_new_sig = nexttoward_GR_sig, nexttoward_GR_sden_sig
      movl nexttoward_GR_lden_sig = 0x7ffffffffffff800 ;;
}

// Move expected result significand and signexp to fp regs
// Is y=nan?
// Form new exponent in case result exponent needs incrementing or decrementing
{ .mfi
      setf.exp NEXTTOWARD_new_exp = nexttoward_GR_exp
      fclass.m  p9,p0 = f9, 0xc3           
(p12) add nexttoward_GR_exp1 = 1, nexttoward_GR_exp
}
{ .mib
      setf.sig NEXTTOWARD_new_sig = nexttoward_GR_new_sig
(p13) add nexttoward_GR_exp1 = -1, nexttoward_GR_exp
(p6)  br.ret.spnt    b0 ;;             // Exit if x=y
}

// Move largest normal signexp to fp reg for special cases
// Is x=zero?
{ .mfi
      setf.exp NEXTTOWARD_lnorm_exp = nexttoward_GR_max_pexp
      fclass.m  p7,p0 = f8, 0x7
      nop.i 999
}
{ .mfb
      nop.m 999
(p8)  fma.s0 f8 = f8,f1,f9                     
(p8)  br.ret.spnt    b0 ;;             // Exit if x=nan
}

// Move exp+-1 and smallest normal significand to fp regs for special cases
// Is x=inf?
{ .mfi
      setf.exp NEXTTOWARD_exp1 = nexttoward_GR_exp1
      fclass.m  p6,p0 = f8, 0x23           
      addl nexttoward_GR_exp_mask = 0x1ffff, r0
}
{ .mfb
      setf.sig NEXTTOWARD_snorm_sig = nexttoward_GR_snorm_sig
(p9)  fma.s0 f8 = f8,f1,f9                     
(p9)  br.ret.spnt    b0 ;;             // Exit if y=nan
}

// Move largest denormal significand to fp regs for special cases
// Save x
{ .mfb
      setf.sig NEXTTOWARD_lden_sig = nexttoward_GR_lden_sig
      mov NEXTTOWARD_save_f8 = f8
(p7)  br.cond.spnt NEXTTOWARD_ZERO ;;   // Exit if x=0   
}

// Mask off the sign to get x_exp
{ .mfb
      and nexttoward_GR_x_exp = nexttoward_GR_exp_mask, nexttoward_GR_exp
      nop.f 999
(p6)  br.cond.spnt NEXTTOWARD_INF ;;   // Exit if x=inf   
}

// Check 6 special cases when significand rolls over:
//  1 sig size incr, x_sig=max_sig, x_exp < max_exp
//     Set p6, result is sig=min_sig, exp++
//  2 sig size incr, x_sig=max_sig, x_exp >= max_exp
//     Set p7, result is inf, signal overflow
//  3 sig size decr, x_sig=min_sig, x_exp > min_exp
//     Set p8, result is sig=max_sig, exp--
//  4 sig size decr, x_sig=min_sig, x_exp = min_exp
//     Set p9, result is sig=max_den_sig, exp same, signal underflow and inexact
//  5 sig size decr, x_sig=min_den_sig, x_exp = min_exp
//     Set p10, result is zero, sign of x, signal underflow and inexact
//  6 sig size decr, x_sig=min_sig, x_exp < min_exp 
//     Set p14, result is zero, sign of x, signal underflow and inexact
//
// Form exponent of smallest double denormal (if normalized register format)
{ .mmi
      adds nexttoward_GR_min_den_rexp = -52, nexttoward_GR_min_pexp
(p12) cmp.eq.unc p6,p0 = nexttoward_GR_new_sig, r0
(p13) cmp.eq.unc p8,p10 = nexttoward_GR_new_sig, nexttoward_GR_lden_sig ;;
}

{ .mmi
(p6)  cmp.lt.unc p6,p7 = nexttoward_GR_x_exp, nexttoward_GR_max_pexp
(p8)  cmp.gt.unc p8,p9 = nexttoward_GR_x_exp, nexttoward_GR_min_pexp
(p10) cmp.eq.unc p10,p0 = nexttoward_GR_new_sig, r0 ;;
}

// Create small normal in case need to generate underflow flag
{ .mfi
(p10) cmp.le.unc p10,p0 = nexttoward_GR_x_exp, nexttoward_GR_min_pexp
      fmerge.se NEXTTOWARD_tmp = NEXTTOWARD_sden_exp, NEXTTOWARD_lnorm_sig
(p9)  cmp.gt.unc p9,p14 = nexttoward_GR_x_exp, nexttoward_GR_min_den_rexp
}
// Branch if cases 1, 2, 3
{ .bbb
(p6)  br.cond.spnt NEXTTOWARD_EXPUP
(p7)  br.cond.spnt NEXTTOWARD_OVERFLOW
(p8)  br.cond.spnt NEXTTOWARD_EXPDOWN ;;
}

// Branch if cases 4, 5, 6
{ .bbb
(p9)  br.cond.spnt NEXTTOWARD_NORM_TO_DENORM
(p10) br.cond.spnt NEXTTOWARD_UNDERFLOW_TO_ZERO
(p14) br.cond.spnt NEXTTOWARD_UNDERFLOW_TO_ZERO ;;
}

// Here if no special cases
// Set p6 if result will be a denormal, so can force underflow flag
//    Case 1:  x_exp=min_exp, x_sig=unnormalized
//    Case 2:  x_exp<min_exp
{ .mfi
      cmp.lt p6,p7 = nexttoward_GR_x_exp, nexttoward_GR_min_pexp
      fmerge.se f8 = NEXTTOWARD_new_exp, NEXTTOWARD_new_sig
      nop.i 999 ;;
}

{ .mfi
      nop.m 999
      nop.f 999
(p7)  tbit.z p6,p0 = nexttoward_GR_new_sig, 63 ;;
}

NEXTTOWARD_COMMON_FINISH:
// Force underflow and inexact if denormal result
{ .mfi
      nop.m 999
(p6)  fma.d.s0 NEXTTOWARD_tmp = NEXTTOWARD_tmp,NEXTTOWARD_tmp,f0
      nop.i 999 ;;
}

// Final normalization to result precision and exit
{ .mfb
      nop.m 999
      fnorm.d.s0 f8 = f8
      br.ret.sptk b0;;
}

//Special cases
NEXTTOWARD_EXPUP:
{ .mfb
      cmp.lt p6,p7 = nexttoward_GR_x_exp, nexttoward_GR_min_pexp
      fmerge.se f8 = NEXTTOWARD_exp1, NEXTTOWARD_snorm_sig
      br.cond.sptk NEXTTOWARD_COMMON_FINISH ;;
}

NEXTTOWARD_EXPDOWN:
{ .mfb
      cmp.lt p6,p7 = nexttoward_GR_x_exp, nexttoward_GR_min_pexp
      fmerge.se f8 = NEXTTOWARD_exp1, NEXTTOWARD_lnorm_sig
      br.cond.sptk NEXTTOWARD_COMMON_FINISH ;;
}

NEXTTOWARD_NORM_TO_DENORM:
{ .mfi
      nop.m 999
      fmerge.se f8 = NEXTTOWARD_new_exp, NEXTTOWARD_lden_sig
      nop.i 999
}
// Force underflow and inexact if denormal result
{ .mfb
      nop.m 999
      fma.d.s0 NEXTTOWARD_tmp = NEXTTOWARD_tmp,NEXTTOWARD_tmp,f0
      br.ret.sptk b0 ;;
}

NEXTTOWARD_UNDERFLOW_TO_ZERO:
{ .mfb
      cmp.eq p6,p0 = r0,r0
      fmerge.s f8 = NEXTTOWARD_save_f8,f0
      br.cond.sptk NEXTTOWARD_COMMON_FINISH ;;
}

NEXTTOWARD_INF: 
// Here if f8 is +- infinity
// INF
// if f8 is +inf, no matter what y is return  largest double
// if f8 is -inf, no matter what y is return -largest double

{ .mfi
      nop.m 999
      fmerge.se NEXTTOWARD_lnorm = NEXTTOWARD_lnorm_exp,NEXTTOWARD_lnorm_sig
      nop.i 999 ;;
}

{ .mfb
      nop.m 999
      fmerge.s f8 = f8,NEXTTOWARD_lnorm                
      br.ret.sptk    b0 ;;                        
}

NEXTTOWARD_ZERO: 

// Here if f8 is +- zero
// ZERO
// if f8 is zero and y is +, return + smallest double denormal 
// if f8 is zero and y is -, return - smallest double denormal 

{ .mfi
      nop.m 999
      fmerge.se NEXTTOWARD_sden = NEXTTOWARD_sden_exp,NEXTTOWARD_sden_sig
      nop.i 999 ;;
}

// Create small normal to generate underflow flag
{ .mfi
      nop.m 999
      fmerge.se NEXTTOWARD_tmp = NEXTTOWARD_sden_exp, NEXTTOWARD_lnorm_sig
      nop.i 999 ;;
}

// Add correct sign from direction arg
{ .mfi
      nop.m 999
      fmerge.s f8 = f9,NEXTTOWARD_sden                
      nop.i 999 ;;
}

// Force underflow and inexact flags
{ .mfb
      nop.m 999
      fma.d.s0 NEXTTOWARD_tmp = NEXTTOWARD_tmp,NEXTTOWARD_tmp,f0
      br.ret.sptk    b0 ;;                        
}

GLOBAL_LIBM_END(nexttoward)
// Stack operations when calling error support.
//       (1)               (2)                          (3) (call)              (4)
//   sp   -> +          psp -> +                     psp -> +                   sp -> +
//           |                 |                            |                         |
//           |                 | <- GR_Y               R3 ->| <- GR_RESULT            | -> f8
//           |                 |                            |                         |
//           | <-GR_Y      Y2->|                       Y2 ->| <- GR_Y                 |
//           |                 |                            |                         |
//           |                 | <- GR_X               X1 ->|                         |
//           |                 |                            |                         |
//  sp-64 -> +          sp ->  +                     sp ->  +                         +
//    save ar.pfs          save b0                                               restore gp
//    save gp                                                                    restore ar.pfs



LOCAL_LIBM_ENTRY(__libm_error_region)
NEXTTOWARD_OVERFLOW: 
// Here if f8 is finite, but result will be infinite
// Use frcpa to generate infinity of correct sign
// Call error support to report possible range error
.prologue

{ .mfi
      alloc          r32=ar.pfs,2,2,4,0
      frcpa.s1 f8,p6 = NEXTTOWARD_save_f8, f0
      nop.i 999 ;;
}

// Create largest double
{ .mfi
      nop.m 999
      fmerge.se NEXTTOWARD_lnorm = NEXTTOWARD_lnorm_exp,NEXTTOWARD_lnorm_sig
      nop.i 999 ;;
}

// Force overflow and inexact flags to be set
{ .mfi
      mov           r39 = 199      // Error code
      fma.d.s0 NEXTTOWARD_tmp = NEXTTOWARD_lnorm,NEXTTOWARD_lnorm,f0
      nop.i 999
}
;;

// (1)
{ .mfi
        add   GR_Parameter_Y=-32,sp             // Parameter 2 value
        nop.f 0
.save   ar.pfs,GR_SAVE_PFS
        mov  GR_SAVE_PFS=ar.pfs                 // Save ar.pfs
}
{ .mfi
.fframe 64
        add sp=-64,sp                          // Create new stack
        nop.f 0
        mov GR_SAVE_GP=gp                      // Save gp
};;


// (2)
{ .mmi
        stfd [GR_Parameter_Y] = f9,16         // STORE Parameter 2 on stack
        add GR_Parameter_X = 16,sp            // Parameter 1 address
.save   b0, GR_SAVE_B0
        mov GR_SAVE_B0=b0                     // Save b0
};;

.body
// (3)
{ .mib
        stfd [GR_Parameter_X] = NEXTTOWARD_save_f8              // STORE Parameter 1 on stack
        add   GR_Parameter_RESULT = 0,GR_Parameter_Y           // Parameter 3 address
        nop.b 0                                
}
{ .mib
        stfd [GR_Parameter_Y] = f8              // STORE Parameter 3 on stack
        add   GR_Parameter_Y = -16,GR_Parameter_Y
        br.call.sptk b0=__libm_error_support#   // Call error handling function
};;
{ .mmi
        nop.m 0
        nop.m 0
        add   GR_Parameter_RESULT = 48,sp
};;

// (4)
{ .mmi
        ldfd  f8 = [GR_Parameter_RESULT]       // Get return result off stack
.restore sp
        add   sp = 64,sp                       // Restore stack pointer
        mov   b0 = GR_SAVE_B0                  // Restore return address
};;
{ .mib
        mov   gp = GR_SAVE_GP                  // Restore gp
        mov   ar.pfs = GR_SAVE_PFS             // Restore ar.pfs
        br.ret.sptk     b0                     // Return
};;

LOCAL_LIBM_END(__libm_error_region)


.type   __libm_error_support#,@function
.global __libm_error_support#