1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3
* ***** BEGIN LICENSE BLOCK *****
4
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
6
* The contents of this file are subject to the Mozilla Public License Version
7
* 1.1 (the "License"); you may not use this file except in compliance with
8
* the License. You may obtain a copy of the License at
9
* http://www.mozilla.org/MPL/
11
* Software distributed under the License is distributed on an "AS IS" basis,
12
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13
* for the specific language governing rights and limitations under the
16
* The Original Code is Mozilla Communicator client code, released
19
* The Initial Developer of the Original Code is
20
* Sun Microsystems, Inc.
21
* Portions created by the Initial Developer are Copyright (C) 1998
22
* the Initial Developer. All Rights Reserved.
26
* Alternatively, the contents of this file may be used under the terms of
27
* either of the GNU General Public License Version 2 or later (the "GPL"),
28
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29
* in which case the provisions of the GPL or the LGPL are applicable instead
30
* of those above. If you wish to allow use of your version of this file only
31
* under the terms of either the GPL or the LGPL, and not to allow others to
32
* use your version of this file under the terms of the MPL, indicate your
33
* decision by deleting the provisions above and replace them with the notice
34
* and other provisions required by the GPL or the LGPL. If you do not delete
35
* the provisions above, a recipient may use your version of this file under
36
* the terms of any one of the MPL, the GPL or the LGPL.
38
* ***** END LICENSE BLOCK ***** */
40
/* @(#)k_standard.c 1.3 95/01/18 */
42
* ====================================================
43
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
45
* Developed at SunSoft, a Sun Microsystems, Inc. business.
46
* Permission to use, copy, modify, and distribute this
47
* software is freely granted, provided that this notice
49
* ====================================================
55
/* XXX ugly hack to get msvc to link without error. */
56
#if _LIB_VERSION == _IEEE_ && !(defined(DARWIN) || defined(XP_MACOSX))
66
#include <stdio.h> /* fputs(), stderr */
67
#define WRITE2(u,v) fputs(u, stderr)
68
#else /* !defined(_USE_WRITE) */
69
#include <unistd.h> /* write */
70
#define WRITE2(u,v) write(2, u, v)
72
#endif /* !defined(_USE_WRITE) */
74
static double zero = 0.0; /* used as const */
77
* Standard conformance (non-IEEE) on exception cases.
92
* 14-- lgamma(finite) overflow
93
* 15-- lgamma(-integer)
99
* 21-- pow(x,y) overflow
100
* 22-- pow(x,y) underflow
101
* 23-- pow(0,negative)
102
* 24-- pow(neg,non-integral)
103
* 25-- sinh(finite) overflow
104
* 26-- sqrt(negative)
106
* 28-- remainder(x,0)
110
* 32-- scalb overflow
111
* 33-- scalb underflow
112
* 34-- j0(|x|>X_TLOSS)
114
* 36-- j1(|x|>X_TLOSS)
116
* 38-- jn(|x|>X_TLOSS, n)
117
* 39-- yn(x>X_TLOSS, n)
118
* 40-- gamma(finite) overflow
119
* 41-- gamma(-integer)
125
double __kernel_standard(double x, double y, int type, int *err)
127
double __kernel_standard(x,y,type, err)
128
double x,y; int type;int *err;
131
struct exception exc;
132
#ifndef HUGE_VAL /* this is the only routine that uses HUGE_VAL */
138
__HI(u) = 0x7ff00000; /* set inf to infinite */
145
(void) fflush(stdout);
155
if (_LIB_VERSION == _POSIX_)
157
else if (!fd_matherr(&exc)) {
158
if(_LIB_VERSION == _SVID_) {
159
(void) WRITE2("acos: DOMAIN error\n", 19);
169
if(_LIB_VERSION == _POSIX_)
171
else if (!fd_matherr(&exc)) {
172
if(_LIB_VERSION == _SVID_) {
173
(void) WRITE2("asin: DOMAIN error\n", 19);
185
if(_LIB_VERSION == _POSIX_)
187
else if (!fd_matherr(&exc)) {
188
if(_LIB_VERSION == _SVID_) {
189
(void) WRITE2("atan2: DOMAIN error\n", 20);
195
/* hypot(finite,finite) overflow */
198
if (_LIB_VERSION == _SVID_)
201
exc.retval = HUGE_VAL;
202
if (_LIB_VERSION == _POSIX_)
204
else if (!fd_matherr(&exc)) {
209
/* cosh(finite) overflow */
212
if (_LIB_VERSION == _SVID_)
215
exc.retval = HUGE_VAL;
216
if (_LIB_VERSION == _POSIX_)
218
else if (!fd_matherr(&exc)) {
223
/* exp(finite) overflow */
226
if (_LIB_VERSION == _SVID_)
229
exc.retval = HUGE_VAL;
230
if (_LIB_VERSION == _POSIX_)
232
else if (!fd_matherr(&exc)) {
237
/* exp(finite) underflow */
238
exc.type = UNDERFLOW;
241
if (_LIB_VERSION == _POSIX_)
243
else if (!fd_matherr(&exc)) {
249
exc.type = DOMAIN; /* should be SING for IEEE */
251
if (_LIB_VERSION == _SVID_)
254
exc.retval = -HUGE_VAL;
255
if (_LIB_VERSION == _POSIX_)
257
else if (!fd_matherr(&exc)) {
258
if (_LIB_VERSION == _SVID_) {
259
(void) WRITE2("y0: DOMAIN error\n", 17);
268
if (_LIB_VERSION == _SVID_)
271
exc.retval = -HUGE_VAL;
272
if (_LIB_VERSION == _POSIX_)
274
else if (!fd_matherr(&exc)) {
275
if (_LIB_VERSION == _SVID_) {
276
(void) WRITE2("y0: DOMAIN error\n", 17);
283
exc.type = DOMAIN; /* should be SING for IEEE */
285
if (_LIB_VERSION == _SVID_)
288
exc.retval = -HUGE_VAL;
289
if (_LIB_VERSION == _POSIX_)
291
else if (!fd_matherr(&exc)) {
292
if (_LIB_VERSION == _SVID_) {
293
(void) WRITE2("y1: DOMAIN error\n", 17);
302
if (_LIB_VERSION == _SVID_)
305
exc.retval = -HUGE_VAL;
306
if (_LIB_VERSION == _POSIX_)
308
else if (!fd_matherr(&exc)) {
309
if (_LIB_VERSION == _SVID_) {
310
(void) WRITE2("y1: DOMAIN error\n", 17);
317
exc.type = DOMAIN; /* should be SING for IEEE */
319
if (_LIB_VERSION == _SVID_)
322
exc.retval = -HUGE_VAL;
323
if (_LIB_VERSION == _POSIX_)
325
else if (!fd_matherr(&exc)) {
326
if (_LIB_VERSION == _SVID_) {
327
(void) WRITE2("yn: DOMAIN error\n", 17);
336
if (_LIB_VERSION == _SVID_)
339
exc.retval = -HUGE_VAL;
340
if (_LIB_VERSION == _POSIX_)
342
else if (!fd_matherr(&exc)) {
343
if (_LIB_VERSION == _SVID_) {
344
(void) WRITE2("yn: DOMAIN error\n", 17);
350
/* lgamma(finite) overflow */
353
if (_LIB_VERSION == _SVID_)
356
exc.retval = HUGE_VAL;
357
if (_LIB_VERSION == _POSIX_)
359
else if (!fd_matherr(&exc)) {
364
/* lgamma(-integer) or lgamma(0) */
367
if (_LIB_VERSION == _SVID_)
370
exc.retval = HUGE_VAL;
371
if (_LIB_VERSION == _POSIX_)
373
else if (!fd_matherr(&exc)) {
374
if (_LIB_VERSION == _SVID_) {
375
(void) WRITE2("lgamma: SING error\n", 19);
384
if (_LIB_VERSION == _SVID_)
387
exc.retval = -HUGE_VAL;
388
if (_LIB_VERSION == _POSIX_)
390
else if (!fd_matherr(&exc)) {
391
if (_LIB_VERSION == _SVID_) {
392
(void) WRITE2("log: SING error\n", 16);
401
if (_LIB_VERSION == _SVID_)
404
exc.retval = -HUGE_VAL;
405
if (_LIB_VERSION == _POSIX_)
407
else if (!fd_matherr(&exc)) {
408
if (_LIB_VERSION == _SVID_) {
409
(void) WRITE2("log: DOMAIN error\n", 18);
418
if (_LIB_VERSION == _SVID_)
421
exc.retval = -HUGE_VAL;
422
if (_LIB_VERSION == _POSIX_)
424
else if (!fd_matherr(&exc)) {
425
if (_LIB_VERSION == _SVID_) {
426
(void) WRITE2("log10: SING error\n", 18);
435
if (_LIB_VERSION == _SVID_)
438
exc.retval = -HUGE_VAL;
439
if (_LIB_VERSION == _POSIX_)
441
else if (!fd_matherr(&exc)) {
442
if (_LIB_VERSION == _SVID_) {
443
(void) WRITE2("log10: DOMAIN error\n", 20);
450
/* error only if _LIB_VERSION == _SVID_ */
454
if (_LIB_VERSION != _SVID_) exc.retval = 1.0;
455
else if (!fd_matherr(&exc)) {
456
(void) WRITE2("pow(0,0): DOMAIN error\n", 23);
461
/* pow(x,y) overflow */
464
if (_LIB_VERSION == _SVID_) {
467
if(x<zero&&fd_rint(y)!=y) exc.retval = -HUGE;
469
exc.retval = HUGE_VAL;
471
if(x<zero&&fd_rint(y)!=y) exc.retval = -HUGE_VAL;
473
if (_LIB_VERSION == _POSIX_)
475
else if (!fd_matherr(&exc)) {
480
/* pow(x,y) underflow */
481
exc.type = UNDERFLOW;
484
if (_LIB_VERSION == _POSIX_)
486
else if (!fd_matherr(&exc)) {
494
if (_LIB_VERSION == _SVID_)
497
exc.retval = -HUGE_VAL;
498
if (_LIB_VERSION == _POSIX_)
500
else if (!fd_matherr(&exc)) {
501
if (_LIB_VERSION == _SVID_) {
502
(void) WRITE2("pow(0,neg): DOMAIN error\n", 25);
508
/* neg**non-integral */
511
if (_LIB_VERSION == _SVID_)
514
exc.retval = zero/zero; /* X/Open allow NaN */
515
if (_LIB_VERSION == _POSIX_)
517
else if (!fd_matherr(&exc)) {
518
if (_LIB_VERSION == _SVID_) {
519
(void) WRITE2("neg**non-integral: DOMAIN error\n", 32);
525
/* sinh(finite) overflow */
528
if (_LIB_VERSION == _SVID_)
529
exc.retval = ( (x>zero) ? HUGE : -HUGE);
531
exc.retval = ( (x>zero) ? HUGE_VAL : -HUGE_VAL);
532
if (_LIB_VERSION == _POSIX_)
534
else if (!fd_matherr(&exc)) {
542
if (_LIB_VERSION == _SVID_)
545
exc.retval = zero/zero;
546
if (_LIB_VERSION == _POSIX_)
548
else if (!fd_matherr(&exc)) {
549
if (_LIB_VERSION == _SVID_) {
550
(void) WRITE2("sqrt: DOMAIN error\n", 19);
559
if (_LIB_VERSION == _SVID_)
562
exc.retval = zero/zero;
563
if (_LIB_VERSION == _POSIX_)
565
else if (!fd_matherr(&exc)) {
566
if (_LIB_VERSION == _SVID_) {
567
(void) WRITE2("fmod: DOMAIN error\n", 20);
575
exc.name = "remainder";
576
exc.retval = zero/zero;
577
if (_LIB_VERSION == _POSIX_)
579
else if (!fd_matherr(&exc)) {
580
if (_LIB_VERSION == _SVID_) {
581
(void) WRITE2("remainder: DOMAIN error\n", 24);
590
exc.retval = zero/zero;
591
if (_LIB_VERSION == _POSIX_)
593
else if (!fd_matherr(&exc)) {
594
if (_LIB_VERSION == _SVID_) {
595
(void) WRITE2("acosh: DOMAIN error\n", 20);
604
exc.retval = zero/zero;
605
if (_LIB_VERSION == _POSIX_)
607
else if (!fd_matherr(&exc)) {
608
if (_LIB_VERSION == _SVID_) {
609
(void) WRITE2("atanh: DOMAIN error\n", 20);
618
exc.retval = x/zero; /* sign(x)*inf */
619
if (_LIB_VERSION == _POSIX_)
621
else if (!fd_matherr(&exc)) {
622
if (_LIB_VERSION == _SVID_) {
623
(void) WRITE2("atanh: SING error\n", 18);
629
/* scalb overflow; SVID also returns +-HUGE_VAL */
632
exc.retval = x > zero ? HUGE_VAL : -HUGE_VAL;
633
if (_LIB_VERSION == _POSIX_)
635
else if (!fd_matherr(&exc)) {
640
/* scalb underflow */
641
exc.type = UNDERFLOW;
643
exc.retval = fd_copysign(zero,x);
644
if (_LIB_VERSION == _POSIX_)
646
else if (!fd_matherr(&exc)) {
651
/* j0(|x|>X_TLOSS) */
655
if (_LIB_VERSION == _POSIX_)
657
else if (!fd_matherr(&exc)) {
658
if (_LIB_VERSION == _SVID_) {
659
(void) WRITE2(exc.name, 2);
660
(void) WRITE2(": TLOSS error\n", 14);
670
if (_LIB_VERSION == _POSIX_)
672
else if (!fd_matherr(&exc)) {
673
if (_LIB_VERSION == _SVID_) {
674
(void) WRITE2(exc.name, 2);
675
(void) WRITE2(": TLOSS error\n", 14);
681
/* j1(|x|>X_TLOSS) */
685
if (_LIB_VERSION == _POSIX_)
687
else if (!fd_matherr(&exc)) {
688
if (_LIB_VERSION == _SVID_) {
689
(void) WRITE2(exc.name, 2);
690
(void) WRITE2(": TLOSS error\n", 14);
700
if (_LIB_VERSION == _POSIX_)
702
else if (!fd_matherr(&exc)) {
703
if (_LIB_VERSION == _SVID_) {
704
(void) WRITE2(exc.name, 2);
705
(void) WRITE2(": TLOSS error\n", 14);
711
/* jn(|x|>X_TLOSS) */
715
if (_LIB_VERSION == _POSIX_)
717
else if (!fd_matherr(&exc)) {
718
if (_LIB_VERSION == _SVID_) {
719
(void) WRITE2(exc.name, 2);
720
(void) WRITE2(": TLOSS error\n", 14);
730
if (_LIB_VERSION == _POSIX_)
732
else if (!fd_matherr(&exc)) {
733
if (_LIB_VERSION == _SVID_) {
734
(void) WRITE2(exc.name, 2);
735
(void) WRITE2(": TLOSS error\n", 14);
741
/* gamma(finite) overflow */
744
if (_LIB_VERSION == _SVID_)
747
exc.retval = HUGE_VAL;
748
if (_LIB_VERSION == _POSIX_)
750
else if (!fd_matherr(&exc)) {
755
/* gamma(-integer) or gamma(0) */
758
if (_LIB_VERSION == _SVID_)
761
exc.retval = HUGE_VAL;
762
if (_LIB_VERSION == _POSIX_)
764
else if (!fd_matherr(&exc)) {
765
if (_LIB_VERSION == _SVID_) {
766
(void) WRITE2("gamma: SING error\n", 18);
773
/* error only if _LIB_VERSION == _SVID_ & _XOPEN_ */
777
if (_LIB_VERSION == _IEEE_ ||
778
_LIB_VERSION == _POSIX_) exc.retval = 1.0;
779
else if (!fd_matherr(&exc)) {