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
98 double __kernel_standard(double x
, double y
, int type
)
100 double __kernel_standard(x
,y
,type
)
101 double x
,y
; int type
;
104 struct exception exc
;
105 #ifndef HUGE_VAL /* this is the only routine that uses HUGE_VAL */
109 SET_HIGH_WORD(inf
,0x7ff00000); /* set inf to infinite */
113 (void) fflush(stdout
);
123 exc
.name
= type
< 100 ? "acos" : (type
< 200
124 ? "acosf" : "acosl");;
125 if (_LIB_VERSION
== _SVID_
)
129 if (_LIB_VERSION
== _POSIX_
)
131 else if (!matherr(&exc
)) {
132 if(_LIB_VERSION
== _SVID_
) {
133 (void) WRITE2("acos: DOMAIN error\n", 19);
143 exc
.name
= type
< 100 ? "asin" : (type
< 200
144 ? "asinf" : "asinl");
145 if (_LIB_VERSION
== _SVID_
)
149 if(_LIB_VERSION
== _POSIX_
)
151 else if (!matherr(&exc
)) {
152 if(_LIB_VERSION
== _SVID_
) {
153 (void) WRITE2("asin: DOMAIN error\n", 19);
165 exc
.name
= type
< 100 ? "atan2" : (type
< 200
166 ? "atan2f" : "atan2l");
167 assert (_LIB_VERSION
== _SVID_
);
169 if(_LIB_VERSION
== _POSIX_
)
171 else if (!matherr(&exc
)) {
172 if(_LIB_VERSION
== _SVID_
) {
173 (void) WRITE2("atan2: DOMAIN error\n", 20);
181 /* hypot(finite,finite) overflow */
183 exc
.name
= type
< 100 ? "hypot" : (type
< 200
184 ? "hypotf" : "hypotl");
185 if (_LIB_VERSION
== _SVID_
)
188 exc
.retval
= HUGE_VAL
;
189 if (_LIB_VERSION
== _POSIX_
)
190 __set_errno (ERANGE
);
191 else if (!matherr(&exc
)) {
192 __set_errno (ERANGE
);
198 /* cosh(finite) overflow */
200 exc
.name
= type
< 100 ? "cosh" : (type
< 200
201 ? "coshf" : "coshl");
202 if (_LIB_VERSION
== _SVID_
)
205 exc
.retval
= HUGE_VAL
;
206 if (_LIB_VERSION
== _POSIX_
)
207 __set_errno (ERANGE
);
208 else if (!matherr(&exc
)) {
209 __set_errno (ERANGE
);
215 /* exp(finite) overflow */
217 exc
.name
= type
< 100 ? "exp" : (type
< 200
219 if (_LIB_VERSION
== _SVID_
)
222 exc
.retval
= HUGE_VAL
;
223 if (_LIB_VERSION
== _POSIX_
)
224 __set_errno (ERANGE
);
225 else if (!matherr(&exc
)) {
226 __set_errno (ERANGE
);
232 /* exp(finite) underflow */
233 exc
.type
= UNDERFLOW
;
234 exc
.name
= type
< 100 ? "exp" : (type
< 200
237 if (_LIB_VERSION
== _POSIX_
)
238 __set_errno (ERANGE
);
239 else if (!matherr(&exc
)) {
240 __set_errno (ERANGE
);
247 exc
.type
= DOMAIN
; /* should be SING for IEEE */
248 exc
.name
= type
< 100 ? "y0" : (type
< 200 ? "y0f" : "y0l");
249 if (_LIB_VERSION
== _SVID_
)
252 exc
.retval
= -HUGE_VAL
;
253 if (_LIB_VERSION
== _POSIX_
)
255 else if (!matherr(&exc
)) {
256 if (_LIB_VERSION
== _SVID_
) {
257 (void) WRITE2("y0: DOMAIN error\n", 17);
267 exc
.name
= type
< 100 ? "y0" : (type
< 200 ? "y0f" : "y0l");
268 if (_LIB_VERSION
== _SVID_
)
271 exc
.retval
= -HUGE_VAL
;
272 if (_LIB_VERSION
== _POSIX_
)
274 else if (!matherr(&exc
)) {
275 if (_LIB_VERSION
== _SVID_
) {
276 (void) WRITE2("y0: DOMAIN error\n", 17);
285 exc
.type
= DOMAIN
; /* should be SING for IEEE */
286 exc
.name
= type
< 100 ? "y1" : (type
< 200 ? "y1f" : "y1l");
287 if (_LIB_VERSION
== _SVID_
)
290 exc
.retval
= -HUGE_VAL
;
291 if (_LIB_VERSION
== _POSIX_
)
293 else if (!matherr(&exc
)) {
294 if (_LIB_VERSION
== _SVID_
) {
295 (void) WRITE2("y1: DOMAIN error\n", 17);
305 exc
.name
= type
< 100 ? "y1" : (type
< 200 ? "y1f" : "y1l");
306 if (_LIB_VERSION
== _SVID_
)
309 exc
.retval
= -HUGE_VAL
;
310 if (_LIB_VERSION
== _POSIX_
)
312 else if (!matherr(&exc
)) {
313 if (_LIB_VERSION
== _SVID_
) {
314 (void) WRITE2("y1: DOMAIN error\n", 17);
323 exc
.type
= DOMAIN
; /* should be SING for IEEE */
324 exc
.name
= type
< 100 ? "yn" : (type
< 200 ? "ynf" : "ynl");
325 if (_LIB_VERSION
== _SVID_
)
328 exc
.retval
= -HUGE_VAL
;
329 if (_LIB_VERSION
== _POSIX_
)
331 else if (!matherr(&exc
)) {
332 if (_LIB_VERSION
== _SVID_
) {
333 (void) WRITE2("yn: DOMAIN error\n", 17);
343 exc
.name
= type
< 100 ? "yn" : (type
< 200 ? "ynf" : "ynl");
344 if (_LIB_VERSION
== _SVID_
)
347 exc
.retval
= -HUGE_VAL
;
348 if (_LIB_VERSION
== _POSIX_
)
350 else if (!matherr(&exc
)) {
351 if (_LIB_VERSION
== _SVID_
) {
352 (void) WRITE2("yn: DOMAIN error\n", 17);
360 /* lgamma(finite) overflow */
362 exc
.name
= type
< 100 ? "lgamma" : (type
< 200
363 ? "lgammaf" : "lgammal");
364 if (_LIB_VERSION
== _SVID_
)
367 exc
.retval
= HUGE_VAL
;
368 if (_LIB_VERSION
== _POSIX_
)
369 __set_errno (ERANGE
);
370 else if (!matherr(&exc
)) {
371 __set_errno (ERANGE
);
377 /* lgamma(-integer) or lgamma(0) */
379 exc
.name
= type
< 100 ? "lgamma" : (type
< 200
380 ? "lgammaf" : "lgammal");
381 if (_LIB_VERSION
== _SVID_
)
384 exc
.retval
= HUGE_VAL
;
385 if (_LIB_VERSION
== _POSIX_
)
386 __set_errno (ERANGE
);
387 else if (!matherr(&exc
)) {
388 if (_LIB_VERSION
== _SVID_
) {
389 (void) WRITE2("lgamma: SING error\n", 19);
399 exc
.name
= type
< 100 ? "log" : (type
< 200 ? "logf" : "logl");
400 if (_LIB_VERSION
== _SVID_
)
403 exc
.retval
= -HUGE_VAL
;
404 if (_LIB_VERSION
== _POSIX_
)
405 __set_errno (ERANGE
);
406 else if (!matherr(&exc
)) {
407 if (_LIB_VERSION
== _SVID_
) {
408 (void) WRITE2("log: SING error\n", 16);
418 exc
.name
= type
< 100 ? "log" : (type
< 200 ? "logf" : "logl");
419 if (_LIB_VERSION
== _SVID_
)
423 if (_LIB_VERSION
== _POSIX_
)
425 else if (!matherr(&exc
)) {
426 if (_LIB_VERSION
== _SVID_
) {
427 (void) WRITE2("log: DOMAIN error\n", 18);
437 exc
.name
= type
< 100 ? "log10" : (type
< 200
438 ? "log10f" : "log10l");
439 if (_LIB_VERSION
== _SVID_
)
442 exc
.retval
= -HUGE_VAL
;
443 if (_LIB_VERSION
== _POSIX_
)
444 __set_errno (ERANGE
);
445 else if (!matherr(&exc
)) {
446 if (_LIB_VERSION
== _SVID_
) {
447 (void) WRITE2("log10: SING error\n", 18);
457 exc
.name
= type
< 100 ? "log10" : (type
< 200
458 ? "log10f" : "log10l");
459 if (_LIB_VERSION
== _SVID_
)
463 if (_LIB_VERSION
== _POSIX_
)
465 else if (!matherr(&exc
)) {
466 if (_LIB_VERSION
== _SVID_
) {
467 (void) WRITE2("log10: DOMAIN error\n", 20);
476 /* error only if _LIB_VERSION == _SVID_ */
478 exc
.name
= type
< 100 ? "pow" : (type
< 200 ? "powf" : "powl");
480 if (_LIB_VERSION
!= _SVID_
) exc
.retval
= 1.0;
481 else if (!matherr(&exc
)) {
482 (void) WRITE2("pow(0,0): DOMAIN error\n", 23);
489 /* pow(x,y) overflow */
491 exc
.name
= type
< 100 ? "pow" : (type
< 200 ? "powf" : "powl");
492 if (_LIB_VERSION
== _SVID_
) {
495 if(x
<zero
&&__rint(y
)!=y
) exc
.retval
= -HUGE
;
497 exc
.retval
= HUGE_VAL
;
499 if(x
<zero
&&__rint(y
)!=y
) exc
.retval
= -HUGE_VAL
;
501 if (_LIB_VERSION
== _POSIX_
)
502 __set_errno (ERANGE
);
503 else if (!matherr(&exc
)) {
504 __set_errno (ERANGE
);
510 /* pow(x,y) underflow */
511 exc
.type
= UNDERFLOW
;
512 exc
.name
= type
< 100 ? "pow" : (type
< 200 ? "powf" : "powl");
514 if (_LIB_VERSION
== _POSIX_
)
515 __set_errno (ERANGE
);
516 else if (!matherr(&exc
)) {
517 __set_errno (ERANGE
);
525 exc
.name
= type
< 100 ? "pow" : (type
< 200 ? "powf" : "powl");
526 if (_LIB_VERSION
== _SVID_
)
529 exc
.retval
= -HUGE_VAL
;
530 if (_LIB_VERSION
== _POSIX_
)
531 __set_errno (ERANGE
);
532 else if (!matherr(&exc
)) {
533 if (_LIB_VERSION
== _SVID_
) {
534 (void) WRITE2("pow(0,neg): DOMAIN error\n", 25);
544 exc
.name
= type
< 100 ? "pow" : (type
< 200 ? "powf" : "powl");
545 if (_LIB_VERSION
== _SVID_
)
548 exc
.retval
= HUGE_VAL
;
549 if (_LIB_VERSION
== _POSIX_
)
550 __set_errno (ERANGE
);
551 else if (!matherr(&exc
)) {
552 if (_LIB_VERSION
== _SVID_
) {
553 (void) WRITE2("pow(0,neg): DOMAIN error\n", 25);
561 /* neg**non-integral */
563 exc
.name
= type
< 100 ? "pow" : (type
< 200 ? "powf" : "powl");
564 if (_LIB_VERSION
== _SVID_
)
567 exc
.retval
= zero
/zero
; /* X/Open allow NaN */
568 if (_LIB_VERSION
== _POSIX_
)
570 else if (!matherr(&exc
)) {
571 if (_LIB_VERSION
== _SVID_
) {
572 (void) WRITE2("neg**non-integral: DOMAIN error\n", 32);
580 /* sinh(finite) overflow */
582 exc
.name
= type
< 100 ? "sinh" : (type
< 200
583 ? "sinhf" : "sinhl");
584 if (_LIB_VERSION
== _SVID_
)
585 exc
.retval
= ( (x
>zero
) ? HUGE
: -HUGE
);
587 exc
.retval
= ( (x
>zero
) ? HUGE_VAL
: -HUGE_VAL
);
588 if (_LIB_VERSION
== _POSIX_
)
589 __set_errno (ERANGE
);
590 else if (!matherr(&exc
)) {
591 __set_errno (ERANGE
);
599 exc
.name
= type
< 100 ? "sqrt" : (type
< 200
600 ? "sqrtf" : "sqrtl");
601 if (_LIB_VERSION
== _SVID_
)
604 exc
.retval
= zero
/zero
;
605 if (_LIB_VERSION
== _POSIX_
)
607 else if (!matherr(&exc
)) {
608 if (_LIB_VERSION
== _SVID_
) {
609 (void) WRITE2("sqrt: DOMAIN error\n", 19);
619 exc
.name
= type
< 100 ? "fmod" : (type
< 200
620 ? "fmodf" : "fmodl");
621 if (_LIB_VERSION
== _SVID_
)
624 exc
.retval
= zero
/zero
;
625 if (_LIB_VERSION
== _POSIX_
)
627 else if (!matherr(&exc
)) {
628 if (_LIB_VERSION
== _SVID_
) {
629 (void) WRITE2("fmod: DOMAIN error\n", 20);
639 exc
.name
= type
< 100 ? "remainder" : (type
< 200
642 exc
.retval
= zero
/zero
;
643 if (_LIB_VERSION
== _POSIX_
)
645 else if (!matherr(&exc
)) {
646 if (_LIB_VERSION
== _SVID_
) {
647 (void) WRITE2("remainder: DOMAIN error\n", 24);
657 exc
.name
= type
< 100 ? "acosh" : (type
< 200
658 ? "acoshf" : "acoshl");
659 exc
.retval
= zero
/zero
;
660 if (_LIB_VERSION
== _POSIX_
)
662 else if (!matherr(&exc
)) {
663 if (_LIB_VERSION
== _SVID_
) {
664 (void) WRITE2("acosh: DOMAIN error\n", 20);
674 exc
.name
= type
< 100 ? "atanh" : (type
< 200
675 ? "atanhf" : "atanhl");
676 exc
.retval
= zero
/zero
;
677 if (_LIB_VERSION
== _POSIX_
)
679 else if (!matherr(&exc
)) {
680 if (_LIB_VERSION
== _SVID_
) {
681 (void) WRITE2("atanh: DOMAIN error\n", 20);
691 exc
.name
= type
< 100 ? "atanh" : (type
< 200
692 ? "atanhf" : "atanhl");
693 exc
.retval
= x
/zero
; /* sign(x)*inf */
694 if (_LIB_VERSION
== _POSIX_
)
695 __set_errno (ERANGE
);
696 else if (!matherr(&exc
)) {
697 if (_LIB_VERSION
== _SVID_
) {
698 (void) WRITE2("atanh: SING error\n", 18);
706 /* scalb overflow; SVID also returns +-HUGE_VAL */
708 exc
.name
= type
< 100 ? "scalb" : (type
< 200
709 ? "scalbf" : "scalbl");
710 exc
.retval
= x
> zero
? HUGE_VAL
: -HUGE_VAL
;
711 if (_LIB_VERSION
== _POSIX_
)
712 __set_errno (ERANGE
);
713 else if (!matherr(&exc
)) {
714 __set_errno (ERANGE
);
720 /* scalb underflow */
721 exc
.type
= UNDERFLOW
;
722 exc
.name
= type
< 100 ? "scalb" : (type
< 200
723 ? "scalbf" : "scalbl");
724 exc
.retval
= __copysign(zero
,x
);
725 if (_LIB_VERSION
== _POSIX_
)
726 __set_errno (ERANGE
);
727 else if (!matherr(&exc
)) {
728 __set_errno (ERANGE
);
734 /* j0(|x|>X_TLOSS) */
736 exc
.name
= type
< 100 ? "j0" : (type
< 200 ? "j0f" : "j0l");
738 if (_LIB_VERSION
== _POSIX_
)
739 __set_errno (ERANGE
);
740 else if (!matherr(&exc
)) {
741 if (_LIB_VERSION
== _SVID_
) {
742 (void) WRITE2(exc
.name
, 2);
743 (void) WRITE2(": TLOSS error\n", 14);
745 __set_errno (ERANGE
);
753 exc
.name
= type
< 100 ? "y0" : (type
< 200 ? "y0f" : "y0l");
755 if (_LIB_VERSION
== _POSIX_
)
756 __set_errno (ERANGE
);
757 else if (!matherr(&exc
)) {
758 if (_LIB_VERSION
== _SVID_
) {
759 (void) WRITE2(exc
.name
, 2);
760 (void) WRITE2(": TLOSS error\n", 14);
762 __set_errno (ERANGE
);
768 /* j1(|x|>X_TLOSS) */
770 exc
.name
= type
< 100 ? "j1" : (type
< 200 ? "j1f" : "j1l");
772 if (_LIB_VERSION
== _POSIX_
)
773 __set_errno (ERANGE
);
774 else if (!matherr(&exc
)) {
775 if (_LIB_VERSION
== _SVID_
) {
776 (void) WRITE2(exc
.name
, 2);
777 (void) WRITE2(": TLOSS error\n", 14);
779 __set_errno (ERANGE
);
787 exc
.name
= type
< 100 ? "y1" : (type
< 200 ? "y1f" : "y1l");
789 if (_LIB_VERSION
== _POSIX_
)
790 __set_errno (ERANGE
);
791 else if (!matherr(&exc
)) {
792 if (_LIB_VERSION
== _SVID_
) {
793 (void) WRITE2(exc
.name
, 2);
794 (void) WRITE2(": TLOSS error\n", 14);
796 __set_errno (ERANGE
);
802 /* jn(|x|>X_TLOSS) */
804 exc
.name
= type
< 100 ? "jn" : (type
< 200 ? "jnf" : "jnl");
806 if (_LIB_VERSION
== _POSIX_
)
807 __set_errno (ERANGE
);
808 else if (!matherr(&exc
)) {
809 if (_LIB_VERSION
== _SVID_
) {
810 (void) WRITE2(exc
.name
, 2);
811 (void) WRITE2(": TLOSS error\n", 14);
813 __set_errno (ERANGE
);
821 exc
.name
= type
< 100 ? "yn" : (type
< 200 ? "ynf" : "ynl");
823 if (_LIB_VERSION
== _POSIX_
)
824 __set_errno (ERANGE
);
825 else if (!matherr(&exc
)) {
826 if (_LIB_VERSION
== _SVID_
) {
827 (void) WRITE2(exc
.name
, 2);
828 (void) WRITE2(": TLOSS error\n", 14);
830 __set_errno (ERANGE
);
836 /* tgamma(finite) overflow */
838 exc
.name
= type
< 100 ? "tgamma" : (type
< 200
839 ? "tgammaf" : "tgammal");
840 exc
.retval
= HUGE_VAL
;
841 if (_LIB_VERSION
== _POSIX_
)
842 __set_errno (ERANGE
);
843 else if (!matherr(&exc
)) {
844 __set_errno (ERANGE
);
850 /* tgamma(-integer) */
852 exc
.name
= type
< 100 ? "tgamma" : (type
< 200
853 ? "tgammaf" : "tgammal");
855 if (_LIB_VERSION
== _POSIX_
)
857 else if (!matherr(&exc
)) {
858 if (_LIB_VERSION
== _SVID_
) {
859 (void) WRITE2("tgamma: SING error\n", 18);
860 exc
.retval
= HUGE_VAL
;
869 /* error only if _LIB_VERSION == _SVID_ & _XOPEN_ */
871 exc
.name
= type
< 100 ? "pow" : (type
< 200 ? "powf" : "powl");
873 if (_LIB_VERSION
== _IEEE_
||
874 _LIB_VERSION
== _POSIX_
) exc
.retval
= 1.0;
875 else if (!matherr(&exc
)) {
883 /* exp(finite) overflow */
885 exc
.name
= type
< 100 ? "exp2" : (type
< 200
886 ? "exp2f" : "exp2l");
887 if (_LIB_VERSION
== _SVID_
)
890 exc
.retval
= HUGE_VAL
;
891 if (_LIB_VERSION
== _POSIX_
)
892 __set_errno (ERANGE
);
893 else if (!matherr(&exc
)) {
894 __set_errno (ERANGE
);
900 /* exp(finite) underflow */
901 exc
.type
= UNDERFLOW
;
902 exc
.name
= type
< 100 ? "exp2" : (type
< 200
903 ? "exp2f" : "exp2l");
905 if (_LIB_VERSION
== _POSIX_
)
906 __set_errno (ERANGE
);
907 else if (!matherr(&exc
)) {
908 __set_errno (ERANGE
);
915 /* exp(finite) overflow */
917 exc
.name
= type
< 100 ? "exp10" : (type
< 200
918 ? "exp10f" : "exp10l");
919 if (_LIB_VERSION
== _SVID_
)
922 exc
.retval
= HUGE_VAL
;
923 if (_LIB_VERSION
== _POSIX_
)
924 __set_errno (ERANGE
);
925 else if (!matherr(&exc
)) {
926 __set_errno (ERANGE
);
932 /* exp(finite) underflow */
933 exc
.type
= UNDERFLOW
;
934 exc
.name
= type
< 100 ? "exp10" : (type
< 200
935 ? "exp10f" : "exp10l");
937 if (_LIB_VERSION
== _POSIX_
)
938 __set_errno (ERANGE
);
939 else if (!matherr(&exc
)) {
940 __set_errno (ERANGE
);
948 exc
.name
= type
< 100 ? "log2" : (type
< 200
949 ? "log2f" : "log2l");
950 if (_LIB_VERSION
== _SVID_
)
953 exc
.retval
= -HUGE_VAL
;
954 if (_LIB_VERSION
== _POSIX_
)
955 __set_errno (ERANGE
);
956 else if (!matherr(&exc
)) {
965 exc
.name
= type
< 100 ? "log2" : (type
< 200
966 ? "log2f" : "log2l");
967 if (_LIB_VERSION
== _SVID_
)
971 if (_LIB_VERSION
== _POSIX_
)
973 else if (!matherr(&exc
)) {
982 exc
.name
= type
< 100 ? "tgamma" : (type
< 200
983 ? "tgammaf" : "tgammal");
984 exc
.retval
= __copysign (HUGE_VAL
, x
);
985 if (_LIB_VERSION
== _POSIX_
)
986 __set_errno (ERANGE
);
987 else if (!matherr(&exc
)) {
988 if (_LIB_VERSION
== _SVID_
)
989 (void) WRITE2("tgamma: SING error\n", 18);
990 __set_errno (ERANGE
);
994 /* #### Last used is 50/150/250 ### */