-
Notifications
You must be signed in to change notification settings - Fork 0
/
csh_fast.c
721 lines (592 loc) · 22 KB
/
csh_fast.c
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
/*
* Correctly rounded hyperbolic sine and cosine
*
* Author : Matthieu Gallet, Florent de Dinechin
*
* This file is part of the crlibm library, developed by the Arenaire
* project at Ecole Normale Superieure de Lyon
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include "crlibm.h"
#include "crlibm_private.h"
#include "csh_fast.h"
#include "triple-double.h"
void exp13(int *exponent, double *exp_h, double *exp_m, double *exp_l, double x);
void expm1_13(double *exp_h, double *exp_m, double *exp_l, double x);
/* switches on various printfs. Default 0 */
#define DEBUG 0
static const double largest_double = 0x1.fffffffffffffp1023;
static const double tiniest_double = 0x1.0p-1074;
enum{RN,RD,RU,RZ};
static void do_cosh(double x, double* preshi, double* preslo){
int k;
db_number y;
double ch_hi, ch_lo, sh_hi, sh_lo;/* cosh(x) = (ch_hi + ch_lo)*(cosh(k*ln(2)) + (sh_hi + sh_lo)*(sinh(k*ln(2))) */
db_number table_index_float;
int table_index;
double temp_hi, temp_lo, temp;/* some temporary variables */
double b_hi, b_lo,b_ca_hi, b_ca_lo, b_sa_hi, b_sa_lo;
double ca_hi, ca_lo, sa_hi, sa_lo; /*will be the tabulated values */
double tcb_hi, tsb_hi; /*results of polynomial approximations*/
double square_b_hi;
double ch_2_pk_hi, ch_2_pk_lo, ch_2_mk_hi, ch_2_mk_lo;
double sh_2_pk_hi, sh_2_pk_lo, sh_2_mk_hi, sh_2_mk_lo;
db_number two_p_plus_k, two_p_minus_k; /* 2^(k-1) + 2^(-k-1) */
/* First range reduction*/
DOUBLE2INT(k, x * inv_ln_2.d)
if (k != 0){ /* b_hi+b_lo = x - (ln2_hi + ln2_lo) * k */
temp_hi = x - ln2_hi.d * k;
temp_lo = -ln2_lo.d * k;
Add12Cond(b_hi, b_lo, temp_hi, temp_lo);
}
else {
b_hi = x; b_lo = 0.;
}
/*we'll construct 2 constants for the last reconstruction */
two_p_plus_k.i[LO] = 0;
two_p_plus_k.i[HI] = (k-1+1023) << 20;
two_p_minus_k.i[LO] = 0;
two_p_minus_k.i[HI] = (-k-1+1023) << 20;
/* at this stage, we've done the first range reduction : we have b_hi + b_lo between -ln(2)/2 and ln(2)/2 */
/* now we can do the second range reduction */
/* we'll get the 8 leading bits of b_hi */
table_index_float.d = b_hi + two_43_44.d;
/*this add do the float equivalent of a rotation to the right, since -0.5 <= b_hi <= 0.5*/
table_index = table_index_float.i[LO];/* -89 <= table_index <= 89 */
table_index_float.d -= two_43_44.d;
table_index += bias; /* to have only positive values */
b_hi -= table_index_float.d;/* to remove the 8 leading bits*/
/* since b_hi was between -2^-1 and 2^1, we now have b_hi between -2^-9 and 2^-9 */
y.d = b_hi;
/* first, y² */
square_b_hi = b_hi * b_hi;
/* effective computation of the polynomial approximation */
if (((y.i[HI])&(0x7FFFFFFF)) < (two_minus_30.i[HI])) {
tcb_hi = 0;
tsb_hi = 0;
}
else {
/* second, cosh(y) = y² * (1/2 + y² * (1/24 + y² * 1/720)) */
tcb_hi = (square_b_hi)* (c2.d + square_b_hi * (c4.d + square_b_hi * c6.d));
tsb_hi = square_b_hi * (s3.d + square_b_hi * (s5.d + square_b_hi * s7.d));
}
if( table_index != bias) {
/* we get the tabulated the tabulated values */
ca_hi = cosh_sinh_table[table_index][0].d;
ca_lo = cosh_sinh_table[table_index][1].d;
sa_hi = cosh_sinh_table[table_index][2].d;
sa_lo = cosh_sinh_table[table_index][3].d;
/* first reconstruction of the cosh (corresponding to the second range reduction) */
Mul12(&b_sa_hi,&b_sa_lo, sa_hi, b_hi);
temp = ((((((ca_lo + (b_hi * sa_lo)) + b_lo * sa_hi) + b_sa_lo) + (b_sa_hi * tsb_hi)) + ca_hi * tcb_hi) + b_sa_hi);
Add12Cond(ch_hi, ch_lo, ca_hi, temp);
/* first reconstruction for the sinh (corresponding to the second range reduction) */
}
else {
Add12Cond(ch_hi, ch_lo, (double) 1, tcb_hi);
}
if(k != 0) {
if( table_index != bias) {
/* first reconstruction for the sinh (corresponding to the second range reduction) */
Mul12(&b_ca_hi , &b_ca_lo, ca_hi, b_hi);
temp = (((((sa_lo + (b_lo * ca_hi)) + (b_hi * ca_lo)) + b_ca_lo) + (sa_hi*tcb_hi)) + (b_ca_hi * tsb_hi));
Add12(temp_hi, temp_lo, b_ca_hi, temp);
Add22Cond(&sh_hi, &sh_lo, sa_hi, (double) 0, temp_hi, temp_lo);
}
else {
Add12Cond(sh_hi, sh_lo, b_hi, tsb_hi * b_hi + b_lo);
}
if((k < 35) && (k > -35) )
{
ch_2_pk_hi = ch_hi * two_p_plus_k.d;
ch_2_pk_lo = ch_lo * two_p_plus_k.d;
ch_2_mk_hi = ch_hi * two_p_minus_k.d;
ch_2_mk_lo = ch_lo * two_p_minus_k.d;
sh_2_pk_hi = sh_hi * two_p_plus_k.d;
sh_2_pk_lo = sh_lo * two_p_plus_k.d;
sh_2_mk_hi = - sh_hi * two_p_minus_k.d;
sh_2_mk_lo = - sh_lo * two_p_minus_k.d;
Add22Cond(preshi, preslo, ch_2_mk_hi, ch_2_mk_lo, sh_2_mk_hi, sh_2_mk_lo);
Add22Cond(&ch_2_mk_hi, &ch_2_mk_lo , sh_2_pk_hi, sh_2_pk_lo, *preshi, *preslo);
Add22Cond(preshi, preslo, ch_2_pk_hi, ch_2_pk_lo, ch_2_mk_hi, ch_2_mk_lo);
}
else if (k >= 35)
{
ch_2_pk_hi = ch_hi * two_p_plus_k.d;
ch_2_pk_lo = ch_lo * two_p_plus_k.d;
sh_2_pk_hi = sh_hi * two_p_plus_k.d;
sh_2_pk_lo = sh_lo * two_p_plus_k.d;
Add22Cond(preshi, preslo, ch_2_pk_hi, ch_2_pk_lo, sh_2_pk_hi, sh_2_pk_lo);
}
else /* if (k <= -35) */
{
ch_2_mk_hi = ch_hi * two_p_minus_k.d;
ch_2_mk_lo = ch_lo * two_p_minus_k.d;
sh_2_mk_hi = - sh_hi * two_p_minus_k.d;
sh_2_mk_lo = - sh_lo * two_p_minus_k.d;
Add22Cond(preshi, preslo, ch_2_mk_hi, ch_2_mk_lo, sh_2_mk_hi, sh_2_mk_lo);
}
}
else {
*preshi = ch_hi;
*preslo = ch_lo;
}
return;
}
static void do_cosh_accurate(int* pexponent,
double* presh, double* presm, double* presl,
double x){
double exph, expm, expl;
double expph, exppm, exppl;
int exponentm, deltaexponent;
db_number expmh, expmm, expml;
#if EVAL_PERF==1
crlibm_second_step_taken++;
#endif
if(x<0)
x=-x;
if (x > 40.0) { /* then exp(-x) < 2^-118 exp(x) */
exp13(pexponent, presh, presm, presl, x);
}
else {
exp13(pexponent, &expph, &exppm, &exppl, x);
exp13(&exponentm, &(expmh.d), &(expmm.d), &(expml.d), -x);
/* align the mantissas.
The exponent is increased but stays well below overflow threshold */
deltaexponent = exponentm - *pexponent ;
expmh.i[HI] += (deltaexponent) << 20;
expmm.i[HI] += (deltaexponent) << 20;
expml.i[HI] += (deltaexponent) << 20;
Add33(&exph, &expm, &expl, expph, exppm, exppl, expmh.d, expmm.d, expml.d);
Renormalize3(presh,presm,presl, exph, expm, expl);
}
}
double cosh_rn(double x){
db_number y;
int hx;
double rh, rl;
y.d = x;
hx = y.i[HI] & 0x7FFFFFFF;
/* Filter special cases */
if (hx > max_input_csh.i[HI]) { /* strictly greater, implies x > max_input_csh */
if (hx >= 0x7ff00000){ /* Infty or NaN */
if (((hx&0x000fffff)|y.i[LO])!=0)
return x+x; /* Nan */
else {/* otherwise the result should be +infty */
y.i[HI] = 0x7FF00000;
return (y.d);
}
}
}
if (x >= max_input_csh.d || x <= -max_input_csh.d)
return largest_double * largest_double; /* overflow */
if (hx<0x3e500000) {
if(x==0)
return 1.0; /* exact */
else
return (1.0+tiniest_double); /* to raise inexact flag */
}
do_cosh(x, &rh, &rl);
if (rh == (rh + (rl * round_cst_csh))) return rh;
else{
int exponent;
db_number res;
double resh, resm, resl;
do_cosh_accurate(&exponent, &resh,&resm, &resl, x);
RoundToNearest3(&(res.d), resh, resm, resl);
/* Now we have to set the exponent of res as exponent -1 (division
by 2). However, as res may sometimes end up infinite, we first
set the exponent to exponent -11 and then multiply by 2^10,
which will cater for overflow */
res.i[HI] += (exponent-11) << 20;
return 1024. * res.d;
}
}
double cosh_ru(double x){
db_number y;
int hx;
double rh, rl;
y.d = x;
hx = y.i[HI] & 0x7FFFFFFF;
if (hx > max_input_csh.i[HI]) {
/* if NaN, return it */
if (((hx&0x7FF00000) == 0x7FF00000) && (((y.i[HI] & 0x000FFFFF)!=0) || (y.i[LO]!=0)) )
return x;
else {/* otherwise the result should be +infty */
y.i[LO] = 0; y.i[HI] = 0x7FF00000; return (y.d);
}
}
if (x >= max_input_csh.d || x <= -max_input_csh.d)
return largest_double * largest_double; /* overflow */
if (hx<0x3e500000) { /* return the successor of 1 */
if(x==0.) return 1.0;
else{
y.l = 0x3ff0000000000001LL;
return y.d;
}
}
do_cosh(x, &rh, &rl);
TEST_AND_RETURN_RU(rh, rl, maxepsilon_csh);
/* if the previous block didn't return a value, launch accurate phase */
{
int exponent;
db_number res;
double resh, resm, resl;
do_cosh_accurate(&exponent, &resh,&resm, &resl, x);
RoundUpwards3(&(res.d), resh,resm,resl);
/* Now we have to set the exponent of res as exponent -1 (division
by 2). However, as res may sometimes end up infinite, we first
set the exponent to exponent -11 and then multiply by 2^10,
which will cater for overflow */
res.i[HI] += (exponent-11) << 20;
return 1024. * res.d;
}
}
double cosh_rd(double x){
db_number y;
int hx;
double rh, rl;
y.d = x;
hx = y.i[HI] & 0x7FFFFFFF;
if (hx > max_input_csh.i[HI]) {
if (hx >= 0x7FF00000) { /*particular cases : QNaN, SNaN, +- oo*/
if (((hx&0x7FF00000) == 0x7FF00000) && (((y.i[HI] & 0x000FFFFF)!=0) || (y.i[LO]!=0)) )
return x; /* NaN */
else { /* infinity */
y.i[HI] = hx;
return (y.d);
}
}
}
if (y.d >= max_input_csh.d || y.d <= - max_input_csh.d) { /* out of range */
y.i[LO] = 0xFFFFFFFF; y.i[HI] = 0x7FEFFFFF ; return (y.d);
}
if (hx<0x3e500000)
return (1.0);
do_cosh(x, &rh, &rl);
TEST_AND_RETURN_RD(rh, rl, maxepsilon_csh);
/* if the previous block didn't return a value, launch accurate phase */
{
int exponent;
db_number res;
double resh, resm, resl;
do_cosh_accurate(&exponent, &resh,&resm, &resl, x);
RoundDownwards3(&(res.d), resh,resm,resl);
/* Now we have to set the exponent of res as exponent -1 (division
by 2). However, as res may sometimes end up infinite, we first
set the exponent to exponent -11 and then multiply by 2^10,
which will cater for overflow */
res.i[HI] += (exponent-11) << 20;
return 1024. * res.d;
}
}
double cosh_rz(double x){
return(cosh_rd(x));/* cosh is always positive, so rounding to -infinite is equivalent to rounding to zero */
}
static void do_sinh(double x, double* prh, double* prl){
int k;
db_number y;
double temp1;
double ch_hi, ch_lo, sh_hi, sh_lo;/* cosh(x) = (sh_hi + sh_lo)*(cosh(k*ln(2)) + (ch_hi + ch_lo)*(sinh(k*ln(2))) */
db_number table_index_float;
int table_index;
double ch_2_pk_hi, ch_2_pk_lo, ch_2_mk_hi, ch_2_mk_lo;
double sh_2_pk_hi, sh_2_pk_lo, sh_2_mk_hi, sh_2_mk_lo;
double b_hi, b_lo;
double ca_b_hi, ca_b_lo, temp_hi, temp_lo, sa_b_hi, sa_b_lo;
double ca_hi, ca_lo, sa_hi, sa_lo; /*tabulated values */
double tcb_hi, tsb_hi; /*results of polynomial approximations*/
db_number two_p_plus_k, two_p_minus_k; /* 2^(k-1) + 2^(-k-1) */
double square_y_hi;
/* Now we can do the first range reduction*/
DOUBLE2INT(k, x * inv_ln_2.d)
if (k != 0){ /* b_hi + b_lo = x - (ln2_hi + ln2_lo) * k */
temp_hi = x - ln2_hi.d * k;
temp_lo = -ln2_lo.d * k;
Add12Cond(b_hi, b_lo, temp_hi, temp_lo);
}
else {
b_hi = x; b_lo = 0.;
}
/*we'll construct 2 constants for the last reconstruction */
two_p_plus_k.i[LO] = 0;
two_p_plus_k.i[HI] = (k-1+1023) << 20;
two_p_minus_k.i[LO] = 0;
two_p_minus_k.i[HI] = (-k-1+1023) << 20;
/* at this stage, we've done the first range reduction : we have b_hi + b_lo between -ln(2)/2 and ln(2)/2 */
/* now we can do the second range reduction */
/* we'll get the 8 leading bits of r_hi */
table_index_float.d = b_hi + two_43_44.d;
/*this add do the float equivalent of a rotation to the right, since -0.5 <= b_hi <= 0.5*/
table_index = table_index_float.i[LO];/* -89 <= table_index <= 89 */
table_index_float.d -= two_43_44.d;
table_index += bias; /* to have only positive values */
b_hi -= table_index_float.d;/* to remove the 8 leading bits*/
/* since b_hi was between -2^-1 and 2^1, we now have b_hi between -2^-9 and 2^-9 */
y.d = b_hi;
/* first, y² = square_y_hi + square_y_lo */
square_y_hi = b_hi * b_hi;
/* effective computation of the polyomial approximation */
if (((y.i[HI])&(0x7FFFFFFF)) <= (two_minus_30.i[HI])) {
tsb_hi = 0;
tcb_hi = 0;
}
else {
tsb_hi = square_y_hi * (s3.d + square_y_hi * (s5.d + square_y_hi * s7.d));
/* second, cosh(y) = y² * (1/2 + y² * (1/24 + y² * 1/720)) */
tcb_hi = (square_y_hi)* (c2.d + square_y_hi * (c4.d + square_y_hi * c6.d));
}
if( table_index != bias) {
/* we get the tabulated the tabulated values*/
ca_hi = cosh_sinh_table[table_index][0].d;
ca_lo = cosh_sinh_table[table_index][1].d;
sa_hi = cosh_sinh_table[table_index][2].d;
sa_lo = cosh_sinh_table[table_index][3].d;
/* first reconstruction for the sinh (corresponding to the second range reduction) */
temp1 = sa_lo;
temp1 += b_lo * ca_hi;
temp1 += b_hi * ca_lo;
Mul12(&ca_b_hi, &ca_b_lo, ca_hi, b_hi);
temp1 += ca_b_lo;
temp1 += sa_hi * tcb_hi;
temp1 += ca_b_hi * tsb_hi;
Add12Cond(temp_hi, temp_lo, ca_b_hi, temp1);
Add22Cond(&sh_hi, &sh_lo, sa_hi, (double) 0, temp_hi, temp_lo);
/* first reconstruction of the cosh (corresponding to the second range reduction) */
temp1 = ca_lo;
Mul12(&sa_b_hi,&sa_b_lo, sa_hi, b_hi);
temp1 += b_hi * sa_lo;
temp1 += b_lo * sa_hi;
temp1 += sa_b_lo;
temp1 += sa_b_hi * tsb_hi;
temp1 += ca_hi * tcb_hi;
temp1 += sa_b_hi;
Add12Cond(ch_hi, ch_lo, ca_hi, temp1);
}
else {
Add12Cond(sh_hi, sh_lo, b_hi, tsb_hi * b_hi + b_lo);
Add12Cond(ch_hi, ch_lo, (double) 1, tcb_hi);
}
if(k != 0) {
if( (k < 35) && (k > -35) ) {
ch_2_pk_hi = ch_hi * two_p_plus_k.d;
ch_2_pk_lo = ch_lo * two_p_plus_k.d;
ch_2_mk_hi = - ch_hi * two_p_minus_k.d;
ch_2_mk_lo = - ch_lo * two_p_minus_k.d;
sh_2_pk_hi = sh_hi * two_p_plus_k.d;
sh_2_pk_lo = sh_lo * two_p_plus_k.d;
sh_2_mk_hi = sh_hi * two_p_minus_k.d;
sh_2_mk_lo = sh_lo * two_p_minus_k.d;
Add22Cond(prh, prl, ch_2_mk_hi, ch_2_mk_lo, sh_2_mk_hi, sh_2_mk_lo);
Add22Cond(&ch_2_mk_hi, &ch_2_mk_lo , sh_2_pk_hi, sh_2_pk_lo, *prh, *prl);
Add22Cond(prh, prl, ch_2_pk_hi, ch_2_pk_lo, ch_2_mk_hi, ch_2_mk_lo);
}
else if (k >= 35)
{
ch_2_pk_hi = ch_hi * two_p_plus_k.d;
ch_2_pk_lo = ch_lo * two_p_plus_k.d;
sh_2_pk_hi = sh_hi * two_p_plus_k.d;
sh_2_pk_lo = sh_lo * two_p_plus_k.d;
Add22Cond(prh, prl, ch_2_pk_hi, ch_2_pk_lo, sh_2_pk_hi, sh_2_pk_lo);
}
else
{
ch_2_mk_hi = - ch_hi * two_p_minus_k.d;
ch_2_mk_lo = - ch_lo * two_p_minus_k.d;
sh_2_mk_hi = sh_hi * two_p_minus_k.d;
sh_2_mk_lo = sh_lo * two_p_minus_k.d;
Add22Cond(prh, prl, ch_2_mk_hi, ch_2_mk_lo, sh_2_mk_hi, sh_2_mk_lo);
}
}
else {
*prh = sh_hi;
*prl = sh_lo;
}
}
static void do_sinh_accurate(int* pexponent,
double* presh, double* presm, double* presl,
double x){
double exph, expm, expl;
double expph, exppm, exppl, expmh, expmm, expml;
#if EVAL_PERF==1
crlibm_second_step_taken++;
#endif
if(x > 40.0) { /* then exp(-x) < 2^-129 exp(x) */
exp13(pexponent, presh, presm, presl, x);
return;
}
if(x < -40.0) { /* then exp(x) < 2^-129 exp(-x) */
exp13(pexponent, presh, presm, presl, -x);
*presh = -*presh;
*presm = -*presm;
*presl = -*presl;
return;
}
/* Otherwise we are between -40 and 40, and we also know that |x| > 2^-25 */
if(x>0.0) {
expm1_13(&expph, &exppm, &exppl, x);
expm1_13(&expmh, &expmm, &expml, -x);
/* The following is OK because expph and -expmh have the same sign */
Add33(&exph, &expm, &expl, expph, exppm, exppl, -expmh, -expmm, -expml);
Renormalize3(presh,presm,presl, exph, expm, expl);
*pexponent=0;
return;
}
else { /* x<0 */
expm1_13(&expph, &exppm, &exppl, x);
expm1_13(&expmh, &expmm, &expml, -x);
/* The following is OK because expph and -expmh have the same sign */
Add33(&exph, &expm, &expl, -expmh, -expmm, -expml, expph, exppm, exppl);
Renormalize3(presh,presm,presl, exph, expm, expl);
*pexponent=0;
return;
}
}
double sinh_rn(double x){
db_number y;
int hx;
double rh, rl;
y.d = x;
hx = y.i[HI] & 0x7FFFFFFF;
/* Filter special cases */
if (hx > max_input_csh.i[HI]) { /* strictly greater, implies x > max_input_csh */
if (hx >= 0x7ff00000){ /* infinity or NaN */
if (((hx&0x000fffff)|y.i[LO])!=0)
return x+x; /* NaN */
else {/* otherwise the result should be +infty */
return (y.d);
}
}
if (x > max_input_csh.d)
return largest_double * largest_double; /* overflow */
if (x < -max_input_csh.d)
return -largest_double * largest_double; /* overflow */
}
if (hx<0x3e500000) {
return x; /* exact, we should find some way of raising the inexact flag */
}
do_sinh(x, &rh, &rl);
if (rh == (rh + (rl * round_cst_csh))) return rh;
else{
int exponent;
db_number res;
double resh, resm, resl;
do_sinh_accurate(&exponent, &resh,&resm, &resl, x);
RoundToNearest3(&(res.d), resh, resm, resl);
/* Now we have to set the exponent of res as exponent -1 (division
by 2). However, as res may sometimes end up infinite, we first
set the exponent to exponent -11 and then multiply by 2^10,
which will cater for overflow */
res.i[HI] += (exponent-11) << 20;
return 1024. * res.d;
}
}
double sinh_ru(double x){
db_number y;
double rh, rl;
y.d = x;
y.i[HI] = y.i[HI] & 0x7FFFFFFF; /* to get the absolute value of the input */
if ((y.i[HI] & 0x7FF00000) >= (0x7FF00000)) { /*particular cases : QNaN, SNaN, +- oo*/
return (x);
}
if (y.d > max_input_csh.d) { /* out of range */
if(x>0) {
y.i[LO] = 0; y.i[HI] = 0x7FF00000; return (y.d);
}
else {
y.i[LO] = 0xFFFFFFFF; y.i[HI] = 0xFFEFFFFF ; return (y.d);
}
}
if(y.i[HI] < 0x3e500000) /* 2^(-26) */
{ /* Add one ulp if x positive */
if(x>0) {
y.l++;
return y.d;
}
else
return x;
}
do_sinh(x, &rh, &rl);
TEST_AND_RETURN_RU(rh, rl, maxepsilon_csh);
/* if the previous block didn't return a value, launch accurate phase */
{
int exponent;
db_number res;
double resh, resm, resl;
do_sinh_accurate(&exponent, &resh,&resm, &resl, x);
RoundUpwards3(&(res.d), resh,resm,resl);
/* Now we have to set the exponent of res as exponent -1 (division
by 2). However, as res may sometimes end up infinite, we first
set the exponent to exponent -11 and then multiply by 2^10,
which will cater for overflow */
res.i[HI] += (exponent-11) << 20;
return 1024. * res.d;
}
}
double sinh_rd(double x){
db_number y;
double rh, rl;
y.d = x;
y.i[HI] = y.i[HI] & 0x7FFFFFFF; /* to get the absolute value of the input */
if ((y.i[HI] & 0x7FF00000) >= (0x7FF00000)) { /*particular cases : QNaN, SNaN, +- oo*/
y.d = x;
return (y.d);
}
if (y.d > max_input_csh.d) { /* out of range */
if(x>0) {
y.i[LO] = 0xFFFFFFFF; y.i[HI] = 0x7FEFFFFF ; return (y.d);
}
else {
y.i[LO] = 0; y.i[HI] = 0xFFF00000; return (y.d);
}
}
if(y.i[HI] < 0x3e500000) /* 2^(-26) */
{ /* Add one ulp and restore the sign if x negative */
if(x<0){
y.l = (y.l+1);
return -y.d;
}
else
return x;
}
do_sinh(x, &rh, &rl);
TEST_AND_RETURN_RD(rh, rl, maxepsilon_csh);
/* if the previous block didn't return a value, launch accurate phase */
{
int exponent;
db_number res;
double resh, resm, resl;
do_sinh_accurate(&exponent, &resh,&resm, &resl, x);
RoundDownwards3(&(res.d), resh,resm,resl);
/* Now we have to set the exponent of res as exponent -1 (division
by 2). However, as res may sometimes end up infinite, we first
set the exponent to exponent -11 and then multiply by 2^10,
which will cater for overflow */
res.i[HI] += (exponent-11) << 20;
return 1024. * res.d;
}
}
double sinh_rz(double x){
if( x > 0) {
return(sinh_rd(x));
}
else {
return(sinh_ru(x));
}
}