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
97 double __kernel_standard(double x
, double y
, int type
)
99 double __kernel_standard(x
,y
,type
)
100 double x
,y
; int type
;
103 struct exception exc
;
104 #ifndef HUGE_VAL /* this is the only routine that uses HUGE_VAL */
108 SET_HIGH_WORD(inf
,0x7ff00000); /* set inf to infinite */
112 (void) fflush(stdout
);
122 exc
.name
= type
< 100 ? "acos" : (type
< 200
123 ? "acosf" : "acosl");;
124 if (_LIB_VERSION
== _SVID_
)
128 if (_LIB_VERSION
== _POSIX_
)
130 else if (!matherr(&exc
)) {
131 if(_LIB_VERSION
== _SVID_
) {
132 (void) WRITE2("acos: DOMAIN error\n", 19);
142 exc
.name
= type
< 100 ? "asin" : (type
< 200
143 ? "asinf" : "asinl");
144 if (_LIB_VERSION
== _SVID_
)
148 if(_LIB_VERSION
== _POSIX_
)
150 else if (!matherr(&exc
)) {
151 if(_LIB_VERSION
== _SVID_
) {
152 (void) WRITE2("asin: DOMAIN error\n", 19);
164 exc
.name
= type
< 100 ? "atan2" : (type
< 200
165 ? "atan2f" : "atan2l");
166 assert (_LIB_VERSION
== _SVID_
);
168 if(_LIB_VERSION
== _POSIX_
)
170 else if (!matherr(&exc
)) {
171 if(_LIB_VERSION
== _SVID_
) {
172 (void) WRITE2("atan2: DOMAIN error\n", 20);
180 /* hypot(finite,finite) overflow */
182 exc
.name
= type
< 100 ? "hypot" : (type
< 200
183 ? "hypotf" : "hypotl");
184 if (_LIB_VERSION
== _SVID_
)
187 exc
.retval
= HUGE_VAL
;
188 if (_LIB_VERSION
== _POSIX_
)
189 __set_errno (ERANGE
);
190 else if (!matherr(&exc
)) {
191 __set_errno (ERANGE
);
197 /* cosh(finite) overflow */
199 exc
.name
= type
< 100 ? "cosh" : (type
< 200
200 ? "coshf" : "coshl");
201 if (_LIB_VERSION
== _SVID_
)
204 exc
.retval
= HUGE_VAL
;
205 if (_LIB_VERSION
== _POSIX_
)
206 __set_errno (ERANGE
);
207 else if (!matherr(&exc
)) {
208 __set_errno (ERANGE
);
214 /* exp(finite) overflow */
216 exc
.name
= type
< 100 ? "exp" : (type
< 200
218 if (_LIB_VERSION
== _SVID_
)
221 exc
.retval
= HUGE_VAL
;
222 if (_LIB_VERSION
== _POSIX_
)
223 __set_errno (ERANGE
);
224 else if (!matherr(&exc
)) {
225 __set_errno (ERANGE
);
231 /* exp(finite) underflow */
232 exc
.type
= UNDERFLOW
;
233 exc
.name
= type
< 100 ? "exp" : (type
< 200
236 if (_LIB_VERSION
== _POSIX_
)
237 __set_errno (ERANGE
);
238 else if (!matherr(&exc
)) {
239 __set_errno (ERANGE
);
246 exc
.type
= DOMAIN
; /* should be SING for IEEE */
247 exc
.name
= type
< 100 ? "y0" : (type
< 200 ? "y0f" : "y0l");
248 if (_LIB_VERSION
== _SVID_
)
251 exc
.retval
= -HUGE_VAL
;
252 if (_LIB_VERSION
== _POSIX_
)
254 else if (!matherr(&exc
)) {
255 if (_LIB_VERSION
== _SVID_
) {
256 (void) WRITE2("y0: DOMAIN error\n", 17);
266 exc
.name
= type
< 100 ? "y0" : (type
< 200 ? "y0f" : "y0l");
267 if (_LIB_VERSION
== _SVID_
)
270 exc
.retval
= -HUGE_VAL
;
271 if (_LIB_VERSION
== _POSIX_
)
273 else if (!matherr(&exc
)) {
274 if (_LIB_VERSION
== _SVID_
) {
275 (void) WRITE2("y0: DOMAIN error\n", 17);
284 exc
.type
= DOMAIN
; /* should be SING for IEEE */
285 exc
.name
= type
< 100 ? "y1" : (type
< 200 ? "y1f" : "y1l");
286 if (_LIB_VERSION
== _SVID_
)
289 exc
.retval
= -HUGE_VAL
;
290 if (_LIB_VERSION
== _POSIX_
)
292 else if (!matherr(&exc
)) {
293 if (_LIB_VERSION
== _SVID_
) {
294 (void) WRITE2("y1: DOMAIN error\n", 17);
304 exc
.name
= type
< 100 ? "y1" : (type
< 200 ? "y1f" : "y1l");
305 if (_LIB_VERSION
== _SVID_
)
308 exc
.retval
= -HUGE_VAL
;
309 if (_LIB_VERSION
== _POSIX_
)
311 else if (!matherr(&exc
)) {
312 if (_LIB_VERSION
== _SVID_
) {
313 (void) WRITE2("y1: DOMAIN error\n", 17);
322 exc
.type
= DOMAIN
; /* should be SING for IEEE */
323 exc
.name
= type
< 100 ? "yn" : (type
< 200 ? "ynf" : "ynl");
324 if (_LIB_VERSION
== _SVID_
)
327 exc
.retval
= -HUGE_VAL
;
328 if (_LIB_VERSION
== _POSIX_
)
330 else if (!matherr(&exc
)) {
331 if (_LIB_VERSION
== _SVID_
) {
332 (void) WRITE2("yn: DOMAIN error\n", 17);
342 exc
.name
= type
< 100 ? "yn" : (type
< 200 ? "ynf" : "ynl");
343 if (_LIB_VERSION
== _SVID_
)
346 exc
.retval
= -HUGE_VAL
;
347 if (_LIB_VERSION
== _POSIX_
)
349 else if (!matherr(&exc
)) {
350 if (_LIB_VERSION
== _SVID_
) {
351 (void) WRITE2("yn: DOMAIN error\n", 17);
359 /* lgamma(finite) overflow */
361 exc
.name
= type
< 100 ? "lgamma" : (type
< 200
362 ? "lgammaf" : "lgammal");
363 if (_LIB_VERSION
== _SVID_
)
366 exc
.retval
= HUGE_VAL
;
367 if (_LIB_VERSION
== _POSIX_
)
368 __set_errno (ERANGE
);
369 else if (!matherr(&exc
)) {
370 __set_errno (ERANGE
);
376 /* lgamma(-integer) or lgamma(0) */
378 exc
.name
= type
< 100 ? "lgamma" : (type
< 200
379 ? "lgammaf" : "lgammal");
380 if (_LIB_VERSION
== _SVID_
)
383 exc
.retval
= HUGE_VAL
;
384 if (_LIB_VERSION
== _POSIX_
)
386 else if (!matherr(&exc
)) {
387 if (_LIB_VERSION
== _SVID_
) {
388 (void) WRITE2("lgamma: SING error\n", 19);
398 exc
.name
= type
< 100 ? "log" : (type
< 200 ? "logf" : "logl");
399 if (_LIB_VERSION
== _SVID_
)
402 exc
.retval
= -HUGE_VAL
;
403 if (_LIB_VERSION
== _POSIX_
)
404 __set_errno (ERANGE
);
405 else if (!matherr(&exc
)) {
406 if (_LIB_VERSION
== _SVID_
) {
407 (void) WRITE2("log: SING error\n", 16);
417 exc
.name
= type
< 100 ? "log" : (type
< 200 ? "logf" : "logl");
418 if (_LIB_VERSION
== _SVID_
)
422 if (_LIB_VERSION
== _POSIX_
)
424 else if (!matherr(&exc
)) {
425 if (_LIB_VERSION
== _SVID_
) {
426 (void) WRITE2("log: DOMAIN error\n", 18);
436 exc
.name
= type
< 100 ? "log10" : (type
< 200
437 ? "log10f" : "log10l");
438 if (_LIB_VERSION
== _SVID_
)
441 exc
.retval
= -HUGE_VAL
;
442 if (_LIB_VERSION
== _POSIX_
)
443 __set_errno (ERANGE
);
444 else if (!matherr(&exc
)) {
445 if (_LIB_VERSION
== _SVID_
) {
446 (void) WRITE2("log10: SING error\n", 18);
456 exc
.name
= type
< 100 ? "log10" : (type
< 200
457 ? "log10f" : "log10l");
458 if (_LIB_VERSION
== _SVID_
)
462 if (_LIB_VERSION
== _POSIX_
)
464 else if (!matherr(&exc
)) {
465 if (_LIB_VERSION
== _SVID_
) {
466 (void) WRITE2("log10: DOMAIN error\n", 20);
475 /* error only if _LIB_VERSION == _SVID_ */
477 exc
.name
= type
< 100 ? "pow" : (type
< 200 ? "powf" : "powl");
479 if (_LIB_VERSION
!= _SVID_
) exc
.retval
= 1.0;
480 else if (!matherr(&exc
)) {
481 (void) WRITE2("pow(0,0): DOMAIN error\n", 23);
488 /* pow(x,y) overflow */
490 exc
.name
= type
< 100 ? "pow" : (type
< 200 ? "powf" : "powl");
491 if (_LIB_VERSION
== _SVID_
) {
494 if(x
<zero
&&__rint(y
)!=y
) exc
.retval
= -HUGE
;
496 exc
.retval
= HUGE_VAL
;
498 if(x
<zero
&&__rint(y
)!=y
) exc
.retval
= -HUGE_VAL
;
500 if (_LIB_VERSION
== _POSIX_
)
501 __set_errno (ERANGE
);
502 else if (!matherr(&exc
)) {
503 __set_errno (ERANGE
);
509 /* pow(x,y) underflow */
510 exc
.type
= UNDERFLOW
;
511 exc
.name
= type
< 100 ? "pow" : (type
< 200 ? "powf" : "powl");
513 if (_LIB_VERSION
== _POSIX_
)
514 __set_errno (ERANGE
);
515 else if (!matherr(&exc
)) {
516 __set_errno (ERANGE
);
524 exc
.name
= type
< 100 ? "pow" : (type
< 200 ? "powf" : "powl");
525 if (_LIB_VERSION
== _SVID_
)
528 exc
.retval
= -HUGE_VAL
;
529 if (_LIB_VERSION
== _POSIX_
)
531 else if (!matherr(&exc
)) {
532 if (_LIB_VERSION
== _SVID_
) {
533 (void) WRITE2("pow(0,neg): DOMAIN error\n", 25);
543 exc
.name
= type
< 100 ? "pow" : (type
< 200 ? "powf" : "powl");
544 if (_LIB_VERSION
== _SVID_
)
547 exc
.retval
= HUGE_VAL
;
548 if (_LIB_VERSION
== _POSIX_
)
550 else if (!matherr(&exc
)) {
551 if (_LIB_VERSION
== _SVID_
) {
552 (void) WRITE2("pow(0,neg): DOMAIN error\n", 25);
560 /* neg**non-integral */
562 exc
.name
= type
< 100 ? "pow" : (type
< 200 ? "powf" : "powl");
563 if (_LIB_VERSION
== _SVID_
)
566 exc
.retval
= zero
/zero
; /* X/Open allow NaN */
567 if (_LIB_VERSION
== _POSIX_
)
569 else if (!matherr(&exc
)) {
570 if (_LIB_VERSION
== _SVID_
) {
571 (void) WRITE2("neg**non-integral: DOMAIN error\n", 32);
579 /* sinh(finite) overflow */
581 exc
.name
= type
< 100 ? "sinh" : (type
< 200
582 ? "sinhf" : "sinhl");
583 if (_LIB_VERSION
== _SVID_
)
584 exc
.retval
= ( (x
>zero
) ? HUGE
: -HUGE
);
586 exc
.retval
= ( (x
>zero
) ? HUGE_VAL
: -HUGE_VAL
);
587 if (_LIB_VERSION
== _POSIX_
)
588 __set_errno (ERANGE
);
589 else if (!matherr(&exc
)) {
590 __set_errno (ERANGE
);
598 exc
.name
= type
< 100 ? "sqrt" : (type
< 200
599 ? "sqrtf" : "sqrtl");
600 if (_LIB_VERSION
== _SVID_
)
603 exc
.retval
= zero
/zero
;
604 if (_LIB_VERSION
== _POSIX_
)
606 else if (!matherr(&exc
)) {
607 if (_LIB_VERSION
== _SVID_
) {
608 (void) WRITE2("sqrt: DOMAIN error\n", 19);
618 exc
.name
= type
< 100 ? "fmod" : (type
< 200
619 ? "fmodf" : "fmodl");
620 if (_LIB_VERSION
== _SVID_
)
623 exc
.retval
= zero
/zero
;
624 if (_LIB_VERSION
== _POSIX_
)
626 else if (!matherr(&exc
)) {
627 if (_LIB_VERSION
== _SVID_
) {
628 (void) WRITE2("fmod: DOMAIN error\n", 20);
638 exc
.name
= type
< 100 ? "remainder" : (type
< 200
641 exc
.retval
= zero
/zero
;
642 if (_LIB_VERSION
== _POSIX_
)
644 else if (!matherr(&exc
)) {
645 if (_LIB_VERSION
== _SVID_
) {
646 (void) WRITE2("remainder: DOMAIN error\n", 24);
656 exc
.name
= type
< 100 ? "acosh" : (type
< 200
657 ? "acoshf" : "acoshl");
658 exc
.retval
= zero
/zero
;
659 if (_LIB_VERSION
== _POSIX_
)
661 else if (!matherr(&exc
)) {
662 if (_LIB_VERSION
== _SVID_
) {
663 (void) WRITE2("acosh: DOMAIN error\n", 20);
673 exc
.name
= type
< 100 ? "atanh" : (type
< 200
674 ? "atanhf" : "atanhl");
675 exc
.retval
= zero
/zero
;
676 if (_LIB_VERSION
== _POSIX_
)
678 else if (!matherr(&exc
)) {
679 if (_LIB_VERSION
== _SVID_
) {
680 (void) WRITE2("atanh: DOMAIN error\n", 20);
690 exc
.name
= type
< 100 ? "atanh" : (type
< 200
691 ? "atanhf" : "atanhl");
692 exc
.retval
= x
/zero
; /* sign(x)*inf */
693 if (_LIB_VERSION
== _POSIX_
)
695 else if (!matherr(&exc
)) {
696 if (_LIB_VERSION
== _SVID_
) {
697 (void) WRITE2("atanh: SING error\n", 18);
705 /* scalb overflow; SVID also returns +-HUGE_VAL */
707 exc
.name
= type
< 100 ? "scalb" : (type
< 200
708 ? "scalbf" : "scalbl");
709 exc
.retval
= x
> zero
? HUGE_VAL
: -HUGE_VAL
;
710 if (_LIB_VERSION
== _POSIX_
)
711 __set_errno (ERANGE
);
712 else if (!matherr(&exc
)) {
713 __set_errno (ERANGE
);
719 /* scalb underflow */
720 exc
.type
= UNDERFLOW
;
721 exc
.name
= type
< 100 ? "scalb" : (type
< 200
722 ? "scalbf" : "scalbl");
723 exc
.retval
= __copysign(zero
,x
);
724 if (_LIB_VERSION
== _POSIX_
)
725 __set_errno (ERANGE
);
726 else if (!matherr(&exc
)) {
727 __set_errno (ERANGE
);
733 /* j0(|x|>X_TLOSS) */
735 exc
.name
= type
< 100 ? "j0" : (type
< 200 ? "j0f" : "j0l");
737 if (_LIB_VERSION
== _POSIX_
)
738 __set_errno (ERANGE
);
739 else if (!matherr(&exc
)) {
740 if (_LIB_VERSION
== _SVID_
) {
741 (void) WRITE2(exc
.name
, 2);
742 (void) WRITE2(": TLOSS error\n", 14);
744 __set_errno (ERANGE
);
752 exc
.name
= type
< 100 ? "y0" : (type
< 200 ? "y0f" : "y0l");
754 if (_LIB_VERSION
== _POSIX_
)
755 __set_errno (ERANGE
);
756 else if (!matherr(&exc
)) {
757 if (_LIB_VERSION
== _SVID_
) {
758 (void) WRITE2(exc
.name
, 2);
759 (void) WRITE2(": TLOSS error\n", 14);
761 __set_errno (ERANGE
);
767 /* j1(|x|>X_TLOSS) */
769 exc
.name
= type
< 100 ? "j1" : (type
< 200 ? "j1f" : "j1l");
771 if (_LIB_VERSION
== _POSIX_
)
772 __set_errno (ERANGE
);
773 else if (!matherr(&exc
)) {
774 if (_LIB_VERSION
== _SVID_
) {
775 (void) WRITE2(exc
.name
, 2);
776 (void) WRITE2(": TLOSS error\n", 14);
778 __set_errno (ERANGE
);
786 exc
.name
= type
< 100 ? "y1" : (type
< 200 ? "y1f" : "y1l");
788 if (_LIB_VERSION
== _POSIX_
)
789 __set_errno (ERANGE
);
790 else if (!matherr(&exc
)) {
791 if (_LIB_VERSION
== _SVID_
) {
792 (void) WRITE2(exc
.name
, 2);
793 (void) WRITE2(": TLOSS error\n", 14);
795 __set_errno (ERANGE
);
801 /* jn(|x|>X_TLOSS) */
803 exc
.name
= type
< 100 ? "jn" : (type
< 200 ? "jnf" : "jnl");
805 if (_LIB_VERSION
== _POSIX_
)
806 __set_errno (ERANGE
);
807 else if (!matherr(&exc
)) {
808 if (_LIB_VERSION
== _SVID_
) {
809 (void) WRITE2(exc
.name
, 2);
810 (void) WRITE2(": TLOSS error\n", 14);
812 __set_errno (ERANGE
);
820 exc
.name
= type
< 100 ? "yn" : (type
< 200 ? "ynf" : "ynl");
822 if (_LIB_VERSION
== _POSIX_
)
823 __set_errno (ERANGE
);
824 else if (!matherr(&exc
)) {
825 if (_LIB_VERSION
== _SVID_
) {
826 (void) WRITE2(exc
.name
, 2);
827 (void) WRITE2(": TLOSS error\n", 14);
829 __set_errno (ERANGE
);
835 /* gamma(finite) overflow */
837 exc
.name
= type
< 100 ? "tgamma" : (type
< 200
838 ? "tgammaf" : "tgammal");
839 exc
.retval
= HUGE_VAL
;
840 if (_LIB_VERSION
== _POSIX_
)
841 __set_errno (ERANGE
);
842 else if (!matherr(&exc
)) {
843 __set_errno (ERANGE
);
849 /* gamma(-integer) or gamma(0) */
851 exc
.name
= type
< 100 ? "tgamma" : (type
< 200
852 ? "tgammaf" : "tgammal");
854 if (_LIB_VERSION
== _POSIX_
)
856 else if (!matherr(&exc
)) {
857 if (_LIB_VERSION
== _SVID_
) {
858 (void) WRITE2("tgamma: SING error\n", 18);
859 exc
.retval
= HUGE_VAL
;
868 /* error only if _LIB_VERSION == _SVID_ & _XOPEN_ */
870 exc
.name
= type
< 100 ? "pow" : (type
< 200 ? "powf" : "powl");
872 if (_LIB_VERSION
== _IEEE_
||
873 _LIB_VERSION
== _POSIX_
) exc
.retval
= 1.0;
874 else if (!matherr(&exc
)) {
882 /* exp(finite) overflow */
884 exc
.name
= type
< 100 ? "exp2" : (type
< 200
885 ? "exp2f" : "exp2l");
886 if (_LIB_VERSION
== _SVID_
)
889 exc
.retval
= HUGE_VAL
;
890 if (_LIB_VERSION
== _POSIX_
)
891 __set_errno (ERANGE
);
892 else if (!matherr(&exc
)) {
893 __set_errno (ERANGE
);
899 /* exp(finite) underflow */
900 exc
.type
= UNDERFLOW
;
901 exc
.name
= type
< 100 ? "exp2" : (type
< 200
902 ? "exp2f" : "exp2l");
904 if (_LIB_VERSION
== _POSIX_
)
905 __set_errno (ERANGE
);
906 else if (!matherr(&exc
)) {
907 __set_errno (ERANGE
);
914 /* exp(finite) overflow */
916 exc
.name
= type
< 100 ? "exp10" : (type
< 200
917 ? "exp10f" : "exp10l");
918 if (_LIB_VERSION
== _SVID_
)
921 exc
.retval
= HUGE_VAL
;
922 if (_LIB_VERSION
== _POSIX_
)
923 __set_errno (ERANGE
);
924 else if (!matherr(&exc
)) {
925 __set_errno (ERANGE
);
931 /* exp(finite) underflow */
932 exc
.type
= UNDERFLOW
;
933 exc
.name
= type
< 100 ? "exp10" : (type
< 200
934 ? "exp10f" : "exp10l");
936 if (_LIB_VERSION
== _POSIX_
)
937 __set_errno (ERANGE
);
938 else if (!matherr(&exc
)) {
939 __set_errno (ERANGE
);
947 exc
.name
= type
< 100 ? "log2" : (type
< 200
948 ? "log2f" : "log2l");
949 if (_LIB_VERSION
== _SVID_
)
952 exc
.retval
= -HUGE_VAL
;
953 if (_LIB_VERSION
== _POSIX_
)
954 __set_errno (ERANGE
);
955 else if (!matherr(&exc
)) {
964 exc
.name
= type
< 100 ? "log2" : (type
< 200
965 ? "log2f" : "log2l");
966 if (_LIB_VERSION
== _SVID_
)
970 if (_LIB_VERSION
== _POSIX_
)
972 else if (!matherr(&exc
)) {
977 /* #### Last used is 49/149/249 ### */