1 /* @(#)k_standard.c 5.1 93/09/24 */
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6 * Developed at SunPro, a Sun Microsystems, Inc. business.
7 * Permission to use, copy, modify, and distribute this
8 * software is freely granted, provided that this notice
10 * ====================================================
13 #if defined(LIBM_SCCS) && !defined(lint)
14 static char rcsid
[] = "$NetBSD: k_standard.c,v 1.6 1995/05/10 20:46:35 jtc Exp $";
18 #include "math_private.h"
24 #include <stdio.h> /* fputs(), stderr */
25 #define WRITE2(u,v) fputs(u, stderr)
26 #else /* !defined(_USE_WRITE) */
27 #include <unistd.h> /* write */
28 #define WRITE2(u,v) write(2, u, v)
30 #endif /* !defined(_USE_WRITE) */
32 /* XXX gcc versions until now don't delay the 0.0/0.0 division until
33 runtime but produce NaN at copile time. This is wrong since the
34 exceptions are not set correctly. */
35 #if 0 && defined __STDC__
36 static const double zero
= 0.0; /* used as const */
38 static double zero
= 0.0; /* used as const */
42 * Standard conformance (non-IEEE) on exception cases.
57 * 14-- lgamma(finite) overflow
58 * 15-- lgamma(-integer)
64 * 21-- pow(x,y) overflow
65 * 22-- pow(x,y) underflow
66 * 23-- pow(0,negative)
67 * 24-- pow(neg,non-integral)
68 * 25-- sinh(finite) overflow
76 * 33-- scalb underflow
77 * 34-- j0(|x|>X_TLOSS)
79 * 36-- j1(|x|>X_TLOSS)
81 * 38-- jn(|x|>X_TLOSS, n)
82 * 39-- yn(x>X_TLOSS, n)
83 * 40-- tgamma(finite) overflow
84 * 41-- tgamma(-integer)
90 * 47-- exp10 underflow
95 double __kernel_standard(double x
, double y
, int type
)
97 double __kernel_standard(x
,y
,type
)
101 struct exception exc
;
102 #ifndef HUGE_VAL /* this is the only routine that uses HUGE_VAL */
106 SET_HIGH_WORD(inf
,0x7ff00000); /* set inf to infinite */
110 (void) fflush(stdout
);
120 exc
.name
= type
< 100 ? "acos" : (type
< 200
121 ? "acosf" : "acosl");;
122 if (_LIB_VERSION
== _SVID_
)
126 if (_LIB_VERSION
== _POSIX_
)
128 else if (!matherr(&exc
)) {
129 if(_LIB_VERSION
== _SVID_
) {
130 (void) WRITE2("acos: DOMAIN error\n", 19);
140 exc
.name
= type
< 100 ? "asin" : (type
< 200
141 ? "asinf" : "asinl");
142 if (_LIB_VERSION
== _SVID_
)
146 if(_LIB_VERSION
== _POSIX_
)
148 else if (!matherr(&exc
)) {
149 if(_LIB_VERSION
== _SVID_
) {
150 (void) WRITE2("asin: DOMAIN error\n", 19);
162 exc
.name
= type
< 100 ? "atan2" : (type
< 200
163 ? "atan2f" : "atan2l");
164 assert (_LIB_VERSION
== _SVID_
);
166 if(_LIB_VERSION
== _POSIX_
)
168 else if (!matherr(&exc
)) {
169 if(_LIB_VERSION
== _SVID_
) {
170 (void) WRITE2("atan2: DOMAIN error\n", 20);
178 /* hypot(finite,finite) overflow */
180 exc
.name
= type
< 100 ? "hypot" : (type
< 200
181 ? "hypotf" : "hypotl");
182 if (_LIB_VERSION
== _SVID_
)
185 exc
.retval
= HUGE_VAL
;
186 if (_LIB_VERSION
== _POSIX_
)
187 __set_errno (ERANGE
);
188 else if (!matherr(&exc
)) {
189 __set_errno (ERANGE
);
195 /* cosh(finite) overflow */
197 exc
.name
= type
< 100 ? "cosh" : (type
< 200
198 ? "coshf" : "coshl");
199 if (_LIB_VERSION
== _SVID_
)
202 exc
.retval
= HUGE_VAL
;
203 if (_LIB_VERSION
== _POSIX_
)
204 __set_errno (ERANGE
);
205 else if (!matherr(&exc
)) {
206 __set_errno (ERANGE
);
212 /* exp(finite) overflow */
214 exc
.name
= type
< 100 ? "exp" : (type
< 200
216 if (_LIB_VERSION
== _SVID_
)
219 exc
.retval
= HUGE_VAL
;
220 if (_LIB_VERSION
== _POSIX_
)
221 __set_errno (ERANGE
);
222 else if (!matherr(&exc
)) {
223 __set_errno (ERANGE
);
229 /* exp(finite) underflow */
230 exc
.type
= UNDERFLOW
;
231 exc
.name
= type
< 100 ? "exp" : (type
< 200
234 if (_LIB_VERSION
== _POSIX_
)
235 __set_errno (ERANGE
);
236 else if (!matherr(&exc
)) {
237 __set_errno (ERANGE
);
244 exc
.type
= DOMAIN
; /* should be SING for IEEE */
245 exc
.name
= type
< 100 ? "y0" : (type
< 200 ? "y0f" : "y0l");
246 if (_LIB_VERSION
== _SVID_
)
249 exc
.retval
= -HUGE_VAL
;
250 if (_LIB_VERSION
== _POSIX_
)
252 else if (!matherr(&exc
)) {
253 if (_LIB_VERSION
== _SVID_
) {
254 (void) WRITE2("y0: DOMAIN error\n", 17);
264 exc
.name
= type
< 100 ? "y0" : (type
< 200 ? "y0f" : "y0l");
265 if (_LIB_VERSION
== _SVID_
)
268 exc
.retval
= -HUGE_VAL
;
269 if (_LIB_VERSION
== _POSIX_
)
271 else if (!matherr(&exc
)) {
272 if (_LIB_VERSION
== _SVID_
) {
273 (void) WRITE2("y0: DOMAIN error\n", 17);
282 exc
.type
= DOMAIN
; /* should be SING for IEEE */
283 exc
.name
= type
< 100 ? "y1" : (type
< 200 ? "y1f" : "y1l");
284 if (_LIB_VERSION
== _SVID_
)
287 exc
.retval
= -HUGE_VAL
;
288 if (_LIB_VERSION
== _POSIX_
)
290 else if (!matherr(&exc
)) {
291 if (_LIB_VERSION
== _SVID_
) {
292 (void) WRITE2("y1: DOMAIN error\n", 17);
302 exc
.name
= type
< 100 ? "y1" : (type
< 200 ? "y1f" : "y1l");
303 if (_LIB_VERSION
== _SVID_
)
306 exc
.retval
= -HUGE_VAL
;
307 if (_LIB_VERSION
== _POSIX_
)
309 else if (!matherr(&exc
)) {
310 if (_LIB_VERSION
== _SVID_
) {
311 (void) WRITE2("y1: DOMAIN error\n", 17);
320 exc
.type
= DOMAIN
; /* should be SING for IEEE */
321 exc
.name
= type
< 100 ? "yn" : (type
< 200 ? "ynf" : "ynl");
322 if (_LIB_VERSION
== _SVID_
)
325 exc
.retval
= -HUGE_VAL
;
326 if (_LIB_VERSION
== _POSIX_
)
328 else if (!matherr(&exc
)) {
329 if (_LIB_VERSION
== _SVID_
) {
330 (void) WRITE2("yn: DOMAIN error\n", 17);
340 exc
.name
= type
< 100 ? "yn" : (type
< 200 ? "ynf" : "ynl");
341 if (_LIB_VERSION
== _SVID_
)
344 exc
.retval
= -HUGE_VAL
;
345 if (_LIB_VERSION
== _POSIX_
)
347 else if (!matherr(&exc
)) {
348 if (_LIB_VERSION
== _SVID_
) {
349 (void) WRITE2("yn: DOMAIN error\n", 17);
357 /* lgamma(finite) overflow */
359 exc
.name
= type
< 100 ? "lgamma" : (type
< 200
360 ? "lgammaf" : "lgammal");
361 if (_LIB_VERSION
== _SVID_
)
364 exc
.retval
= HUGE_VAL
;
365 if (_LIB_VERSION
== _POSIX_
)
366 __set_errno (ERANGE
);
367 else if (!matherr(&exc
)) {
368 __set_errno (ERANGE
);
374 /* lgamma(-integer) or lgamma(0) */
376 exc
.name
= type
< 100 ? "lgamma" : (type
< 200
377 ? "lgammaf" : "lgammal");
378 if (_LIB_VERSION
== _SVID_
)
381 exc
.retval
= HUGE_VAL
;
382 if (_LIB_VERSION
== _POSIX_
)
384 else if (!matherr(&exc
)) {
385 if (_LIB_VERSION
== _SVID_
) {
386 (void) WRITE2("lgamma: SING error\n", 19);
396 exc
.name
= type
< 100 ? "log" : (type
< 200 ? "logf" : "logl");
397 if (_LIB_VERSION
== _SVID_
)
400 exc
.retval
= -HUGE_VAL
;
401 if (_LIB_VERSION
== _POSIX_
)
402 __set_errno (ERANGE
);
403 else if (!matherr(&exc
)) {
404 if (_LIB_VERSION
== _SVID_
) {
405 (void) WRITE2("log: SING error\n", 16);
415 exc
.name
= type
< 100 ? "log" : (type
< 200 ? "logf" : "logl");
416 if (_LIB_VERSION
== _SVID_
)
420 if (_LIB_VERSION
== _POSIX_
)
422 else if (!matherr(&exc
)) {
423 if (_LIB_VERSION
== _SVID_
) {
424 (void) WRITE2("log: DOMAIN error\n", 18);
434 exc
.name
= type
< 100 ? "log10" : (type
< 200
435 ? "log10f" : "log10l");
436 if (_LIB_VERSION
== _SVID_
)
439 exc
.retval
= -HUGE_VAL
;
440 if (_LIB_VERSION
== _POSIX_
)
441 __set_errno (ERANGE
);
442 else if (!matherr(&exc
)) {
443 if (_LIB_VERSION
== _SVID_
) {
444 (void) WRITE2("log10: SING error\n", 18);
454 exc
.name
= type
< 100 ? "log10" : (type
< 200
455 ? "log10f" : "log10l");
456 if (_LIB_VERSION
== _SVID_
)
460 if (_LIB_VERSION
== _POSIX_
)
462 else if (!matherr(&exc
)) {
463 if (_LIB_VERSION
== _SVID_
) {
464 (void) WRITE2("log10: DOMAIN error\n", 20);
473 /* error only if _LIB_VERSION == _SVID_ */
475 exc
.name
= type
< 100 ? "pow" : (type
< 200 ? "powf" : "powl");
477 if (_LIB_VERSION
!= _SVID_
) exc
.retval
= 1.0;
478 else if (!matherr(&exc
)) {
479 (void) WRITE2("pow(0,0): DOMAIN error\n", 23);
486 /* pow(x,y) overflow */
488 exc
.name
= type
< 100 ? "pow" : (type
< 200 ? "powf" : "powl");
489 if (_LIB_VERSION
== _SVID_
) {
492 if(x
<zero
&&__rint(y
)!=y
) exc
.retval
= -HUGE
;
494 exc
.retval
= HUGE_VAL
;
496 if(x
<zero
&&__rint(y
)!=y
) exc
.retval
= -HUGE_VAL
;
498 if (_LIB_VERSION
== _POSIX_
)
499 __set_errno (ERANGE
);
500 else if (!matherr(&exc
)) {
501 __set_errno (ERANGE
);
507 /* pow(x,y) underflow */
508 exc
.type
= UNDERFLOW
;
509 exc
.name
= type
< 100 ? "pow" : (type
< 200 ? "powf" : "powl");
511 if (_LIB_VERSION
== _POSIX_
)
512 __set_errno (ERANGE
);
513 else if (!matherr(&exc
)) {
514 __set_errno (ERANGE
);
522 exc
.name
= type
< 100 ? "pow" : (type
< 200 ? "powf" : "powl");
523 if (_LIB_VERSION
== _SVID_
)
526 exc
.retval
= -HUGE_VAL
;
527 if (_LIB_VERSION
== _POSIX_
)
529 else if (!matherr(&exc
)) {
530 if (_LIB_VERSION
== _SVID_
) {
531 (void) WRITE2("pow(0,neg): DOMAIN error\n", 25);
541 exc
.name
= type
< 100 ? "pow" : (type
< 200 ? "powf" : "powl");
542 if (_LIB_VERSION
== _SVID_
)
545 exc
.retval
= HUGE_VAL
;
546 if (_LIB_VERSION
== _POSIX_
)
548 else if (!matherr(&exc
)) {
549 if (_LIB_VERSION
== _SVID_
) {
550 (void) WRITE2("pow(0,neg): DOMAIN error\n", 25);
558 /* neg**non-integral */
560 exc
.name
= type
< 100 ? "pow" : (type
< 200 ? "powf" : "powl");
561 if (_LIB_VERSION
== _SVID_
)
564 exc
.retval
= zero
/zero
; /* X/Open allow NaN */
565 if (_LIB_VERSION
== _POSIX_
)
567 else if (!matherr(&exc
)) {
568 if (_LIB_VERSION
== _SVID_
) {
569 (void) WRITE2("neg**non-integral: DOMAIN error\n", 32);
577 /* sinh(finite) overflow */
579 exc
.name
= type
< 100 ? "sinh" : (type
< 200
580 ? "sinhf" : "sinhl");
581 if (_LIB_VERSION
== _SVID_
)
582 exc
.retval
= ( (x
>zero
) ? HUGE
: -HUGE
);
584 exc
.retval
= ( (x
>zero
) ? HUGE_VAL
: -HUGE_VAL
);
585 if (_LIB_VERSION
== _POSIX_
)
586 __set_errno (ERANGE
);
587 else if (!matherr(&exc
)) {
588 __set_errno (ERANGE
);
596 exc
.name
= type
< 100 ? "sqrt" : (type
< 200
597 ? "sqrtf" : "sqrtl");
598 if (_LIB_VERSION
== _SVID_
)
601 exc
.retval
= zero
/zero
;
602 if (_LIB_VERSION
== _POSIX_
)
604 else if (!matherr(&exc
)) {
605 if (_LIB_VERSION
== _SVID_
) {
606 (void) WRITE2("sqrt: DOMAIN error\n", 19);
616 exc
.name
= type
< 100 ? "fmod" : (type
< 200
617 ? "fmodf" : "fmodl");
618 if (_LIB_VERSION
== _SVID_
)
621 exc
.retval
= zero
/zero
;
622 if (_LIB_VERSION
== _POSIX_
)
624 else if (!matherr(&exc
)) {
625 if (_LIB_VERSION
== _SVID_
) {
626 (void) WRITE2("fmod: DOMAIN error\n", 20);
636 exc
.name
= type
< 100 ? "remainder" : (type
< 200
639 exc
.retval
= zero
/zero
;
640 if (_LIB_VERSION
== _POSIX_
)
642 else if (!matherr(&exc
)) {
643 if (_LIB_VERSION
== _SVID_
) {
644 (void) WRITE2("remainder: DOMAIN error\n", 24);
654 exc
.name
= type
< 100 ? "acosh" : (type
< 200
655 ? "acoshf" : "acoshl");
656 exc
.retval
= zero
/zero
;
657 if (_LIB_VERSION
== _POSIX_
)
659 else if (!matherr(&exc
)) {
660 if (_LIB_VERSION
== _SVID_
) {
661 (void) WRITE2("acosh: DOMAIN error\n", 20);
671 exc
.name
= type
< 100 ? "atanh" : (type
< 200
672 ? "atanhf" : "atanhl");
673 exc
.retval
= zero
/zero
;
674 if (_LIB_VERSION
== _POSIX_
)
676 else if (!matherr(&exc
)) {
677 if (_LIB_VERSION
== _SVID_
) {
678 (void) WRITE2("atanh: DOMAIN error\n", 20);
688 exc
.name
= type
< 100 ? "atanh" : (type
< 200
689 ? "atanhf" : "atanhl");
690 exc
.retval
= x
/zero
; /* sign(x)*inf */
691 if (_LIB_VERSION
== _POSIX_
)
693 else if (!matherr(&exc
)) {
694 if (_LIB_VERSION
== _SVID_
) {
695 (void) WRITE2("atanh: SING error\n", 18);
703 /* scalb overflow; SVID also returns +-HUGE_VAL */
705 exc
.name
= type
< 100 ? "scalb" : (type
< 200
706 ? "scalbf" : "scalbl");
707 exc
.retval
= x
> zero
? HUGE_VAL
: -HUGE_VAL
;
708 if (_LIB_VERSION
== _POSIX_
)
709 __set_errno (ERANGE
);
710 else if (!matherr(&exc
)) {
711 __set_errno (ERANGE
);
717 /* scalb underflow */
718 exc
.type
= UNDERFLOW
;
719 exc
.name
= type
< 100 ? "scalb" : (type
< 200
720 ? "scalbf" : "scalbl");
721 exc
.retval
= __copysign(zero
,x
);
722 if (_LIB_VERSION
== _POSIX_
)
723 __set_errno (ERANGE
);
724 else if (!matherr(&exc
)) {
725 __set_errno (ERANGE
);
731 /* j0(|x|>X_TLOSS) */
733 exc
.name
= type
< 100 ? "j0" : (type
< 200 ? "j0f" : "j0l");
735 if (_LIB_VERSION
== _POSIX_
)
736 __set_errno (ERANGE
);
737 else if (!matherr(&exc
)) {
738 if (_LIB_VERSION
== _SVID_
) {
739 (void) WRITE2(exc
.name
, 2);
740 (void) WRITE2(": TLOSS error\n", 14);
742 __set_errno (ERANGE
);
750 exc
.name
= type
< 100 ? "y0" : (type
< 200 ? "y0f" : "y0l");
752 if (_LIB_VERSION
== _POSIX_
)
753 __set_errno (ERANGE
);
754 else if (!matherr(&exc
)) {
755 if (_LIB_VERSION
== _SVID_
) {
756 (void) WRITE2(exc
.name
, 2);
757 (void) WRITE2(": TLOSS error\n", 14);
759 __set_errno (ERANGE
);
765 /* j1(|x|>X_TLOSS) */
767 exc
.name
= type
< 100 ? "j1" : (type
< 200 ? "j1f" : "j1l");
769 if (_LIB_VERSION
== _POSIX_
)
770 __set_errno (ERANGE
);
771 else if (!matherr(&exc
)) {
772 if (_LIB_VERSION
== _SVID_
) {
773 (void) WRITE2(exc
.name
, 2);
774 (void) WRITE2(": TLOSS error\n", 14);
776 __set_errno (ERANGE
);
784 exc
.name
= type
< 100 ? "y1" : (type
< 200 ? "y1f" : "y1l");
786 if (_LIB_VERSION
== _POSIX_
)
787 __set_errno (ERANGE
);
788 else if (!matherr(&exc
)) {
789 if (_LIB_VERSION
== _SVID_
) {
790 (void) WRITE2(exc
.name
, 2);
791 (void) WRITE2(": TLOSS error\n", 14);
793 __set_errno (ERANGE
);
799 /* jn(|x|>X_TLOSS) */
801 exc
.name
= type
< 100 ? "jn" : (type
< 200 ? "jnf" : "jnl");
803 if (_LIB_VERSION
== _POSIX_
)
804 __set_errno (ERANGE
);
805 else if (!matherr(&exc
)) {
806 if (_LIB_VERSION
== _SVID_
) {
807 (void) WRITE2(exc
.name
, 2);
808 (void) WRITE2(": TLOSS error\n", 14);
810 __set_errno (ERANGE
);
818 exc
.name
= type
< 100 ? "yn" : (type
< 200 ? "ynf" : "ynl");
820 if (_LIB_VERSION
== _POSIX_
)
821 __set_errno (ERANGE
);
822 else if (!matherr(&exc
)) {
823 if (_LIB_VERSION
== _SVID_
) {
824 (void) WRITE2(exc
.name
, 2);
825 (void) WRITE2(": TLOSS error\n", 14);
827 __set_errno (ERANGE
);
833 /* gamma(finite) overflow */
835 exc
.name
= type
< 100 ? "tgamma" : (type
< 200
836 ? "tgammaf" : "tgammal");
837 exc
.retval
= HUGE_VAL
;
838 if (_LIB_VERSION
== _POSIX_
)
839 __set_errno (ERANGE
);
840 else if (!matherr(&exc
)) {
841 __set_errno (ERANGE
);
847 /* gamma(-integer) or gamma(0) */
849 exc
.name
= type
< 100 ? "tgamma" : (type
< 200
850 ? "tgammaf" : "tgammal");
852 if (_LIB_VERSION
== _POSIX_
)
854 else if (!matherr(&exc
)) {
855 if (_LIB_VERSION
== _SVID_
) {
856 (void) WRITE2("tgamma: SING error\n", 18);
857 exc
.retval
= HUGE_VAL
;
866 /* error only if _LIB_VERSION == _SVID_ & _XOPEN_ */
868 exc
.name
= type
< 100 ? "pow" : (type
< 200 ? "powf" : "powl");
870 if (_LIB_VERSION
== _IEEE_
||
871 _LIB_VERSION
== _POSIX_
) exc
.retval
= 1.0;
872 else if (!matherr(&exc
)) {
880 /* exp(finite) overflow */
882 exc
.name
= type
< 100 ? "exp2" : (type
< 200
883 ? "exp2f" : "exp2l");
884 if (_LIB_VERSION
== _SVID_
)
887 exc
.retval
= HUGE_VAL
;
888 if (_LIB_VERSION
== _POSIX_
)
889 __set_errno (ERANGE
);
890 else if (!matherr(&exc
)) {
891 __set_errno (ERANGE
);
897 /* exp(finite) underflow */
898 exc
.type
= UNDERFLOW
;
899 exc
.name
= type
< 100 ? "exp2" : (type
< 200
900 ? "exp2f" : "exp2l");
902 if (_LIB_VERSION
== _POSIX_
)
903 __set_errno (ERANGE
);
904 else if (!matherr(&exc
)) {
905 __set_errno (ERANGE
);
912 /* exp(finite) overflow */
914 exc
.name
= type
< 100 ? "exp10" : (type
< 200
915 ? "exp10f" : "exp10l");
916 if (_LIB_VERSION
== _SVID_
)
919 exc
.retval
= HUGE_VAL
;
920 if (_LIB_VERSION
== _POSIX_
)
921 __set_errno (ERANGE
);
922 else if (!matherr(&exc
)) {
923 __set_errno (ERANGE
);
929 /* exp(finite) underflow */
930 exc
.type
= UNDERFLOW
;
931 exc
.name
= type
< 100 ? "exp10" : (type
< 200
932 ? "exp10f" : "exp10l");
934 if (_LIB_VERSION
== _POSIX_
)
935 __set_errno (ERANGE
);
936 else if (!matherr(&exc
)) {
937 __set_errno (ERANGE
);
940 /* #### Last used is 47/147/247 ### */