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 compile time. This is wrong since the
34 exceptions are not set correctly. */
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)
63 * 21-- pow(x,y) overflow
64 * 22-- pow(x,y) underflow
65 * 23-- pow(0,negative)
66 * 24-- pow(neg,non-integral)
67 * 25-- sinh(finite) overflow
75 * 33-- scalb underflow
76 * 34-- j0(|x|>X_TLOSS)
78 * 36-- j1(|x|>X_TLOSS)
80 * 38-- jn(|x|>X_TLOSS, n)
81 * 39-- yn(x>X_TLOSS, n)
82 * 40-- tgamma(finite) overflow
83 * 41-- tgamma(-integer)
88 * 47-- exp10 underflow
96 __kernel_standard(double x
, double y
, int type
)
99 #ifndef HUGE_VAL /* this is the only routine that uses HUGE_VAL */
103 SET_HIGH_WORD(inf
,0x7ff00000); /* set inf to infinite */
106 /* The SVID struct exception uses a field "char *name;". */
107 #define CSTR(func) ((char *) (type < 100 \
109 : (type < 200 ? func "f" : func "l")))
112 (void) fflush(stdout
);
122 exc
.name
= CSTR ("acos");
123 if (_LIB_VERSION
== _SVID_
)
127 if (_LIB_VERSION
== _POSIX_
)
129 else if (!matherr(&exc
)) {
130 if(_LIB_VERSION
== _SVID_
) {
131 (void) WRITE2("acos: DOMAIN error\n", 19);
141 exc
.name
= CSTR ("asin");
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
= CSTR ("atan2");
163 assert (_LIB_VERSION
== _SVID_
);
165 if(_LIB_VERSION
== _POSIX_
)
167 else if (!matherr(&exc
)) {
168 if(_LIB_VERSION
== _SVID_
) {
169 (void) WRITE2("atan2: DOMAIN error\n", 20);
177 /* hypot(finite,finite) overflow */
179 exc
.name
= CSTR ("hypot");
180 if (_LIB_VERSION
== _SVID_
)
183 exc
.retval
= HUGE_VAL
;
184 if (_LIB_VERSION
== _POSIX_
)
185 __set_errno (ERANGE
);
186 else if (!matherr(&exc
)) {
187 __set_errno (ERANGE
);
193 /* cosh(finite) overflow */
195 exc
.name
= CSTR ("cosh");
196 if (_LIB_VERSION
== _SVID_
)
199 exc
.retval
= HUGE_VAL
;
200 if (_LIB_VERSION
== _POSIX_
)
201 __set_errno (ERANGE
);
202 else if (!matherr(&exc
)) {
203 __set_errno (ERANGE
);
209 /* exp(finite) overflow */
211 exc
.name
= CSTR ("exp");
212 if (_LIB_VERSION
== _SVID_
)
215 exc
.retval
= HUGE_VAL
;
216 if (_LIB_VERSION
== _POSIX_
)
217 __set_errno (ERANGE
);
218 else if (!matherr(&exc
)) {
219 __set_errno (ERANGE
);
225 /* exp(finite) underflow */
226 exc
.type
= UNDERFLOW
;
227 exc
.name
= CSTR ("exp");
229 if (_LIB_VERSION
== _POSIX_
)
230 __set_errno (ERANGE
);
231 else if (!matherr(&exc
)) {
232 __set_errno (ERANGE
);
239 exc
.type
= DOMAIN
; /* should be SING for IEEE */
240 exc
.name
= CSTR ("y0");
241 if (_LIB_VERSION
== _SVID_
)
244 exc
.retval
= -HUGE_VAL
;
245 if (_LIB_VERSION
== _POSIX_
)
246 __set_errno (ERANGE
);
247 else if (!matherr(&exc
)) {
248 if (_LIB_VERSION
== _SVID_
) {
249 (void) WRITE2("y0: DOMAIN error\n", 17);
259 exc
.name
= CSTR ("y0");
260 if (_LIB_VERSION
== _SVID_
)
264 if (_LIB_VERSION
== _POSIX_
)
266 else if (!matherr(&exc
)) {
267 if (_LIB_VERSION
== _SVID_
) {
268 (void) WRITE2("y0: DOMAIN error\n", 17);
277 exc
.type
= DOMAIN
; /* should be SING for IEEE */
278 exc
.name
= CSTR ("y1");
279 if (_LIB_VERSION
== _SVID_
)
282 exc
.retval
= -HUGE_VAL
;
283 if (_LIB_VERSION
== _POSIX_
)
284 __set_errno (ERANGE
);
285 else if (!matherr(&exc
)) {
286 if (_LIB_VERSION
== _SVID_
) {
287 (void) WRITE2("y1: DOMAIN error\n", 17);
297 exc
.name
= CSTR ("y1");
298 if (_LIB_VERSION
== _SVID_
)
302 if (_LIB_VERSION
== _POSIX_
)
304 else if (!matherr(&exc
)) {
305 if (_LIB_VERSION
== _SVID_
) {
306 (void) WRITE2("y1: DOMAIN error\n", 17);
315 exc
.type
= DOMAIN
; /* should be SING for IEEE */
316 exc
.name
= CSTR ("yn");
317 if (_LIB_VERSION
== _SVID_
)
320 exc
.retval
= ((x
< 0 && ((int) x
& 1) != 0)
323 if (_LIB_VERSION
== _POSIX_
)
324 __set_errno (ERANGE
);
325 else if (!matherr(&exc
)) {
326 if (_LIB_VERSION
== _SVID_
) {
327 (void) WRITE2("yn: DOMAIN error\n", 17);
337 exc
.name
= CSTR ("yn");
338 if (_LIB_VERSION
== _SVID_
)
342 if (_LIB_VERSION
== _POSIX_
)
344 else if (!matherr(&exc
)) {
345 if (_LIB_VERSION
== _SVID_
) {
346 (void) WRITE2("yn: DOMAIN error\n", 17);
354 /* lgamma(finite) overflow */
356 exc
.name
= CSTR ("lgamma");
357 if (_LIB_VERSION
== _SVID_
)
360 exc
.retval
= HUGE_VAL
;
361 if (_LIB_VERSION
== _POSIX_
)
362 __set_errno (ERANGE
);
363 else if (!matherr(&exc
)) {
364 __set_errno (ERANGE
);
370 /* lgamma(-integer) or lgamma(0) */
372 exc
.name
= CSTR ("lgamma");
373 if (_LIB_VERSION
== _SVID_
)
376 exc
.retval
= HUGE_VAL
;
377 if (_LIB_VERSION
== _POSIX_
)
378 __set_errno (ERANGE
);
379 else if (!matherr(&exc
)) {
380 if (_LIB_VERSION
== _SVID_
) {
381 (void) WRITE2("lgamma: SING error\n", 19);
391 exc
.name
= CSTR ("log");
392 if (_LIB_VERSION
== _SVID_
)
395 exc
.retval
= -HUGE_VAL
;
396 if (_LIB_VERSION
== _POSIX_
)
397 __set_errno (ERANGE
);
398 else if (!matherr(&exc
)) {
399 if (_LIB_VERSION
== _SVID_
) {
400 (void) WRITE2("log: SING error\n", 16);
410 exc
.name
= CSTR ("log");
411 if (_LIB_VERSION
== _SVID_
)
415 if (_LIB_VERSION
== _POSIX_
)
417 else if (!matherr(&exc
)) {
418 if (_LIB_VERSION
== _SVID_
) {
419 (void) WRITE2("log: DOMAIN error\n", 18);
429 exc
.name
= CSTR ("log10");
430 if (_LIB_VERSION
== _SVID_
)
433 exc
.retval
= -HUGE_VAL
;
434 if (_LIB_VERSION
== _POSIX_
)
435 __set_errno (ERANGE
);
436 else if (!matherr(&exc
)) {
437 if (_LIB_VERSION
== _SVID_
) {
438 (void) WRITE2("log10: SING error\n", 18);
448 exc
.name
= CSTR ("log10");
449 if (_LIB_VERSION
== _SVID_
)
453 if (_LIB_VERSION
== _POSIX_
)
455 else if (!matherr(&exc
)) {
456 if (_LIB_VERSION
== _SVID_
) {
457 (void) WRITE2("log10: DOMAIN error\n", 20);
465 /* pow(x,y) overflow */
467 exc
.name
= CSTR ("pow");
468 if (_LIB_VERSION
== _SVID_
) {
471 if(x
<zero
&&__rint(y
)!=y
) exc
.retval
= -HUGE
;
473 exc
.retval
= HUGE_VAL
;
475 if(x
<zero
&&__rint(y
)!=y
) exc
.retval
= -HUGE_VAL
;
477 if (_LIB_VERSION
== _POSIX_
)
478 __set_errno (ERANGE
);
479 else if (!matherr(&exc
)) {
480 __set_errno (ERANGE
);
486 /* pow(x,y) underflow */
487 exc
.type
= UNDERFLOW
;
488 exc
.name
= CSTR ("pow");
491 if (x
< zero
&& __rint (y
) != y
)
493 if (_LIB_VERSION
== _POSIX_
)
494 __set_errno (ERANGE
);
495 else if (!matherr(&exc
)) {
496 __set_errno (ERANGE
);
504 exc
.name
= CSTR ("pow");
505 if (_LIB_VERSION
== _SVID_
)
508 exc
.retval
= -HUGE_VAL
;
509 if (_LIB_VERSION
== _POSIX_
)
510 __set_errno (ERANGE
);
511 else if (!matherr(&exc
)) {
512 if (_LIB_VERSION
== _SVID_
) {
513 (void) WRITE2("pow(0,neg): DOMAIN error\n", 25);
523 exc
.name
= CSTR ("pow");
524 if (_LIB_VERSION
== _SVID_
)
527 exc
.retval
= HUGE_VAL
;
528 if (_LIB_VERSION
== _POSIX_
)
529 __set_errno (ERANGE
);
530 else if (!matherr(&exc
)) {
531 if (_LIB_VERSION
== _SVID_
) {
532 (void) WRITE2("pow(0,neg): DOMAIN error\n", 25);
540 /* neg**non-integral */
542 exc
.name
= CSTR ("pow");
543 if (_LIB_VERSION
== _SVID_
)
546 exc
.retval
= zero
/zero
; /* X/Open allow NaN */
547 if (_LIB_VERSION
== _POSIX_
)
549 else if (!matherr(&exc
)) {
550 if (_LIB_VERSION
== _SVID_
) {
551 (void) WRITE2("neg**non-integral: DOMAIN error\n", 32);
559 /* sinh(finite) overflow */
561 exc
.name
= CSTR ("sinh");
562 if (_LIB_VERSION
== _SVID_
)
563 exc
.retval
= ( (x
>zero
) ? HUGE
: -HUGE
);
565 exc
.retval
= ( (x
>zero
) ? HUGE_VAL
: -HUGE_VAL
);
566 if (_LIB_VERSION
== _POSIX_
)
567 __set_errno (ERANGE
);
568 else if (!matherr(&exc
)) {
569 __set_errno (ERANGE
);
577 exc
.name
= CSTR ("sqrt");
578 if (_LIB_VERSION
== _SVID_
)
581 exc
.retval
= zero
/zero
;
582 if (_LIB_VERSION
== _POSIX_
)
584 else if (!matherr(&exc
)) {
585 if (_LIB_VERSION
== _SVID_
) {
586 (void) WRITE2("sqrt: DOMAIN error\n", 19);
596 exc
.name
= CSTR ("fmod");
597 if (_LIB_VERSION
== _SVID_
)
600 exc
.retval
= zero
/zero
;
601 if (_LIB_VERSION
== _POSIX_
)
603 else if (!matherr(&exc
)) {
604 if (_LIB_VERSION
== _SVID_
) {
605 (void) WRITE2("fmod: DOMAIN error\n", 20);
615 exc
.name
= CSTR ("remainder");
616 exc
.retval
= zero
/zero
;
617 if (_LIB_VERSION
== _POSIX_
)
619 else if (!matherr(&exc
)) {
620 if (_LIB_VERSION
== _SVID_
) {
621 (void) WRITE2("remainder: DOMAIN error\n", 24);
631 exc
.name
= CSTR ("acosh");
632 exc
.retval
= zero
/zero
;
633 if (_LIB_VERSION
== _POSIX_
)
635 else if (!matherr(&exc
)) {
636 if (_LIB_VERSION
== _SVID_
) {
637 (void) WRITE2("acosh: DOMAIN error\n", 20);
647 exc
.name
= CSTR ("atanh");
648 exc
.retval
= zero
/zero
;
649 if (_LIB_VERSION
== _POSIX_
)
651 else if (!matherr(&exc
)) {
652 if (_LIB_VERSION
== _SVID_
) {
653 (void) WRITE2("atanh: DOMAIN error\n", 20);
663 exc
.name
= CSTR ("atanh");
664 exc
.retval
= x
/zero
; /* sign(x)*inf */
665 if (_LIB_VERSION
== _POSIX_
)
666 __set_errno (ERANGE
);
667 else if (!matherr(&exc
)) {
668 if (_LIB_VERSION
== _SVID_
) {
669 (void) WRITE2("atanh: SING error\n", 18);
677 /* scalb overflow; SVID also returns +-HUGE_VAL */
679 exc
.name
= CSTR ("scalb");
680 exc
.retval
= x
> zero
? HUGE_VAL
: -HUGE_VAL
;
681 if (_LIB_VERSION
== _POSIX_
)
682 __set_errno (ERANGE
);
683 else if (!matherr(&exc
)) {
684 __set_errno (ERANGE
);
690 /* scalb underflow */
691 exc
.type
= UNDERFLOW
;
692 exc
.name
= CSTR ("scalb");
693 exc
.retval
= __copysign(zero
,x
);
694 if (_LIB_VERSION
== _POSIX_
)
695 __set_errno (ERANGE
);
696 else if (!matherr(&exc
)) {
697 __set_errno (ERANGE
);
703 /* j0(|x|>X_TLOSS) */
705 exc
.name
= CSTR ("j0");
707 if (_LIB_VERSION
== _POSIX_
)
708 __set_errno (ERANGE
);
709 else if (!matherr(&exc
)) {
710 if (_LIB_VERSION
== _SVID_
) {
711 (void) WRITE2(exc
.name
, 2);
712 (void) WRITE2(": TLOSS error\n", 14);
714 __set_errno (ERANGE
);
722 exc
.name
= CSTR ("y0");
724 if (_LIB_VERSION
== _POSIX_
)
725 __set_errno (ERANGE
);
726 else if (!matherr(&exc
)) {
727 if (_LIB_VERSION
== _SVID_
) {
728 (void) WRITE2(exc
.name
, 2);
729 (void) WRITE2(": TLOSS error\n", 14);
731 __set_errno (ERANGE
);
737 /* j1(|x|>X_TLOSS) */
739 exc
.name
= CSTR ("j1");
741 if (_LIB_VERSION
== _POSIX_
)
742 __set_errno (ERANGE
);
743 else if (!matherr(&exc
)) {
744 if (_LIB_VERSION
== _SVID_
) {
745 (void) WRITE2(exc
.name
, 2);
746 (void) WRITE2(": TLOSS error\n", 14);
748 __set_errno (ERANGE
);
756 exc
.name
= CSTR ("y1");
758 if (_LIB_VERSION
== _POSIX_
)
759 __set_errno (ERANGE
);
760 else if (!matherr(&exc
)) {
761 if (_LIB_VERSION
== _SVID_
) {
762 (void) WRITE2(exc
.name
, 2);
763 (void) WRITE2(": TLOSS error\n", 14);
765 __set_errno (ERANGE
);
771 /* jn(|x|>X_TLOSS) */
773 exc
.name
= CSTR ("jn");
775 if (_LIB_VERSION
== _POSIX_
)
776 __set_errno (ERANGE
);
777 else if (!matherr(&exc
)) {
778 if (_LIB_VERSION
== _SVID_
) {
779 (void) WRITE2(exc
.name
, 2);
780 (void) WRITE2(": TLOSS error\n", 14);
782 __set_errno (ERANGE
);
790 exc
.name
= CSTR ("yn");
792 if (_LIB_VERSION
== _POSIX_
)
793 __set_errno (ERANGE
);
794 else if (!matherr(&exc
)) {
795 if (_LIB_VERSION
== _SVID_
) {
796 (void) WRITE2(exc
.name
, 2);
797 (void) WRITE2(": TLOSS error\n", 14);
799 __set_errno (ERANGE
);
805 /* tgamma(finite) overflow */
807 exc
.name
= CSTR ("tgamma");
808 exc
.retval
= __copysign (HUGE_VAL
, x
);
809 if (_LIB_VERSION
== _POSIX_
)
810 __set_errno (ERANGE
);
811 else if (!matherr(&exc
)) {
812 __set_errno (ERANGE
);
818 /* tgamma(-integer) */
820 exc
.name
= CSTR ("tgamma");
822 if (_LIB_VERSION
== _POSIX_
)
824 else if (!matherr(&exc
)) {
825 if (_LIB_VERSION
== _SVID_
) {
826 (void) WRITE2("tgamma: SING error\n", 18);
827 exc
.retval
= HUGE_VAL
;
836 /* exp(finite) overflow */
838 exc
.name
= CSTR ("exp2");
839 if (_LIB_VERSION
== _SVID_
)
842 exc
.retval
= HUGE_VAL
;
843 if (_LIB_VERSION
== _POSIX_
)
844 __set_errno (ERANGE
);
845 else if (!matherr(&exc
)) {
846 __set_errno (ERANGE
);
852 /* exp(finite) underflow */
853 exc
.type
= UNDERFLOW
;
854 exc
.name
= CSTR ("exp2");
856 if (_LIB_VERSION
== _POSIX_
)
857 __set_errno (ERANGE
);
858 else if (!matherr(&exc
)) {
859 __set_errno (ERANGE
);
866 /* exp(finite) overflow */
868 exc
.name
= CSTR ("exp10");
869 if (_LIB_VERSION
== _SVID_
)
872 exc
.retval
= HUGE_VAL
;
873 if (_LIB_VERSION
== _POSIX_
)
874 __set_errno (ERANGE
);
875 else if (!matherr(&exc
)) {
876 __set_errno (ERANGE
);
882 /* exp(finite) underflow */
883 exc
.type
= UNDERFLOW
;
884 exc
.name
= CSTR ("exp10");
886 if (_LIB_VERSION
== _POSIX_
)
887 __set_errno (ERANGE
);
888 else if (!matherr(&exc
)) {
889 __set_errno (ERANGE
);
897 exc
.name
= CSTR ("log2");
898 if (_LIB_VERSION
== _SVID_
)
901 exc
.retval
= -HUGE_VAL
;
902 if (_LIB_VERSION
== _POSIX_
)
903 __set_errno (ERANGE
);
904 else if (!matherr(&exc
)) {
913 exc
.name
= CSTR ("log2");
914 if (_LIB_VERSION
== _SVID_
)
918 if (_LIB_VERSION
== _POSIX_
)
920 else if (!matherr(&exc
)) {
929 exc
.name
= CSTR ("tgamma");
930 exc
.retval
= __copysign (HUGE_VAL
, x
);
931 if (_LIB_VERSION
== _POSIX_
)
932 __set_errno (ERANGE
);
933 else if (!matherr(&exc
)) {
934 if (_LIB_VERSION
== _SVID_
)
935 (void) WRITE2("tgamma: SING error\n", 18);
936 __set_errno (ERANGE
);
940 /* #### Last used is 50/150/250 ### */