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 #include <sys/cdefs.h>
14 #if defined(LIBM_SCCS) && !defined(lint)
15 __RCSID("$NetBSD: k_standard.c,v 1.16 2010/09/01 10:44:28 drochner Exp $");
19 #include "math_private.h"
23 #include <stdio.h> /* fputs(), stderr */
24 #define WRITE2(u,v) fputs(u, stderr)
25 #else /* !defined(_USE_WRITE) */
26 #include <unistd.h> /* write */
27 #define WRITE2(u,v) write(2, u, v)
29 #endif /* !defined(_USE_WRITE) */
31 static const double zero
= 0.0; /* used as const */
34 * Standard conformance (non-IEEE) on exception cases.
49 * 14-- lgamma(finite) overflow
50 * 15-- lgamma(-integer)
56 * 21-- pow(x,y) overflow
57 * 22-- pow(x,y) underflow
58 * 23-- pow(0,negative)
59 * 24-- pow(neg,non-integral)
60 * 25-- sinh(finite) overflow
68 * 33-- scalb underflow
69 * 34-- j0(|x|>X_TLOSS)
71 * 36-- j1(|x|>X_TLOSS)
73 * 38-- jn(|x|>X_TLOSS, n)
74 * 39-- yn(x>X_TLOSS, n)
75 * 40-- gamma(finite) overflow
76 * 41-- gamma(-integer)
84 __kernel_standard(double x
, double y
, int type
)
87 #ifndef HUGE_VAL /* this is the only routine that uses HUGE_VAL */
91 SET_HIGH_WORD(inf
,0x7ff00000); /* set inf to infinite */
95 (void) fflush(stdout
);
104 exc
.name
= type
< 100 ? "acos" : "acosf";
106 if (_LIB_VERSION
== _POSIX_
) {
107 exc
.retval
= zero
/zero
;
109 } else if (!matherr(&exc
)) {
110 if(_LIB_VERSION
== _SVID_
) {
111 (void) WRITE2("acos: DOMAIN error\n", 19);
120 exc
.name
= type
< 100 ? "asin" : "asinf";
122 if(_LIB_VERSION
== _POSIX_
) {
123 exc
.retval
= zero
/zero
;
125 } else if (!matherr(&exc
)) {
126 if(_LIB_VERSION
== _SVID_
) {
127 (void) WRITE2("asin: DOMAIN error\n", 19);
138 exc
.name
= type
< 100 ? "atan2" : "atan2f";
140 if(_LIB_VERSION
== _POSIX_
) {
141 exc
.retval
= copysign(signbit(y
) ? M_PI
: zero
, x
);
142 } else if (!matherr(&exc
)) {
143 if(_LIB_VERSION
== _SVID_
) {
144 (void) WRITE2("atan2: DOMAIN error\n", 20);
151 /* hypot(finite,finite) overflow */
153 exc
.name
= type
< 100 ? "hypot" : "hypotf";
154 if (_LIB_VERSION
== _SVID_
)
157 exc
.retval
= HUGE_VAL
;
158 if (_LIB_VERSION
== _POSIX_
)
160 else if (!matherr(&exc
)) {
166 /* cosh(finite) overflow */
168 exc
.name
= type
< 100 ? "cosh" : "coshf";
169 if (_LIB_VERSION
== _SVID_
)
172 exc
.retval
= HUGE_VAL
;
173 if (_LIB_VERSION
== _POSIX_
)
175 else if (!matherr(&exc
)) {
181 /* exp(finite) overflow */
183 exc
.name
= type
< 100 ? "exp" : "expf";
184 if (_LIB_VERSION
== _SVID_
)
187 exc
.retval
= HUGE_VAL
;
188 if (_LIB_VERSION
== _POSIX_
)
190 else if (!matherr(&exc
)) {
196 /* exp(finite) underflow */
197 exc
.type
= UNDERFLOW
;
198 exc
.name
= type
< 100 ? "exp" : "expf";
200 if (_LIB_VERSION
== _POSIX_
)
202 else if (!matherr(&exc
)) {
209 exc
.type
= DOMAIN
; /* should be SING for IEEE */
210 exc
.name
= type
< 100 ? "y0" : "y0f";
211 if (_LIB_VERSION
== _SVID_
)
214 exc
.retval
= -HUGE_VAL
;
215 if (_LIB_VERSION
== _POSIX_
)
217 else if (!matherr(&exc
)) {
218 if (_LIB_VERSION
== _SVID_
) {
219 (void) WRITE2("y0: DOMAIN error\n", 17);
228 exc
.name
= type
< 100 ? "y0" : "y0f";
229 if (_LIB_VERSION
== _SVID_
)
232 exc
.retval
= -HUGE_VAL
;
233 if (_LIB_VERSION
== _POSIX_
)
235 else if (!matherr(&exc
)) {
236 if (_LIB_VERSION
== _SVID_
) {
237 (void) WRITE2("y0: DOMAIN error\n", 17);
245 exc
.type
= DOMAIN
; /* should be SING for IEEE */
246 exc
.name
= type
< 100 ? "y1" : "y1f";
247 if (_LIB_VERSION
== _SVID_
)
250 exc
.retval
= -HUGE_VAL
;
251 if (_LIB_VERSION
== _POSIX_
)
253 else if (!matherr(&exc
)) {
254 if (_LIB_VERSION
== _SVID_
) {
255 (void) WRITE2("y1: DOMAIN error\n", 17);
264 exc
.name
= type
< 100 ? "y1" : "y1f";
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("y1: DOMAIN error\n", 17);
281 exc
.type
= DOMAIN
; /* should be SING for IEEE */
282 exc
.name
= type
< 100 ? "yn" : "ynf";
283 if (_LIB_VERSION
== _SVID_
)
286 exc
.retval
= -HUGE_VAL
;
287 if (_LIB_VERSION
== _POSIX_
)
289 else if (!matherr(&exc
)) {
290 if (_LIB_VERSION
== _SVID_
) {
291 (void) WRITE2("yn: DOMAIN error\n", 17);
300 exc
.name
= type
< 100 ? "yn" : "ynf";
301 if (_LIB_VERSION
== _SVID_
)
304 exc
.retval
= -HUGE_VAL
;
305 if (_LIB_VERSION
== _POSIX_
)
307 else if (!matherr(&exc
)) {
308 if (_LIB_VERSION
== _SVID_
) {
309 (void) WRITE2("yn: DOMAIN error\n", 17);
316 /* lgamma(finite) overflow */
318 exc
.name
= type
< 100 ? "lgamma" : "lgammaf";
319 if (_LIB_VERSION
== _SVID_
)
322 exc
.retval
= HUGE_VAL
;
323 if (_LIB_VERSION
== _POSIX_
)
325 else if (!matherr(&exc
)) {
331 /* lgamma(-integer) or lgamma(0) */
333 exc
.name
= type
< 100 ? "lgamma" : "lgammaf";
334 if (_LIB_VERSION
== _SVID_
)
337 exc
.retval
= HUGE_VAL
;
338 if (_LIB_VERSION
== _POSIX_
)
340 else if (!matherr(&exc
)) {
341 if (_LIB_VERSION
== _SVID_
) {
342 (void) WRITE2("lgamma: SING error\n", 19);
351 exc
.name
= type
< 100 ? "log" : "logf";
352 if (_LIB_VERSION
== _SVID_
)
355 exc
.retval
= -HUGE_VAL
;
356 if (_LIB_VERSION
== _POSIX_
)
358 else if (!matherr(&exc
)) {
359 if (_LIB_VERSION
== _SVID_
) {
360 (void) WRITE2("log: SING error\n", 16);
369 exc
.name
= type
< 100 ? "log" : "logf";
370 if (_LIB_VERSION
== _SVID_
)
373 exc
.retval
= -HUGE_VAL
;
374 if (_LIB_VERSION
== _POSIX_
)
376 else if (!matherr(&exc
)) {
377 if (_LIB_VERSION
== _SVID_
) {
378 (void) WRITE2("log: DOMAIN error\n", 18);
387 exc
.name
= type
< 100 ? "log10" : "log10f";
388 if (_LIB_VERSION
== _SVID_
)
391 exc
.retval
= -HUGE_VAL
;
392 if (_LIB_VERSION
== _POSIX_
)
394 else if (!matherr(&exc
)) {
395 if (_LIB_VERSION
== _SVID_
) {
396 (void) WRITE2("log10: SING error\n", 18);
405 exc
.name
= type
< 100 ? "log10" : "log10f";
406 if (_LIB_VERSION
== _SVID_
)
409 exc
.retval
= -HUGE_VAL
;
410 if (_LIB_VERSION
== _POSIX_
)
412 else if (!matherr(&exc
)) {
413 if (_LIB_VERSION
== _SVID_
) {
414 (void) WRITE2("log10: DOMAIN error\n", 20);
422 /* error only if _LIB_VERSION == _SVID_ */
424 exc
.name
= type
< 100 ? "pow" : "powf";
426 if (_LIB_VERSION
!= _SVID_
) exc
.retval
= 1.0;
427 else if (!matherr(&exc
)) {
428 (void) WRITE2("pow(0,0): DOMAIN error\n", 23);
434 /* pow(x,y) overflow */
436 exc
.name
= type
< 100 ? "pow" : "powf";
437 if (_LIB_VERSION
== _SVID_
) {
440 if(x
<zero
&&rint(y
)!=y
) exc
.retval
= -HUGE
;
442 exc
.retval
= HUGE_VAL
;
444 if(x
<zero
&&rint(y
)!=y
) exc
.retval
= -HUGE_VAL
;
446 if (_LIB_VERSION
== _POSIX_
)
448 else if (!matherr(&exc
)) {
454 /* pow(x,y) underflow */
455 exc
.type
= UNDERFLOW
;
456 exc
.name
= type
< 100 ? "pow" : "powf";
458 if (_LIB_VERSION
== _POSIX_
)
460 else if (!matherr(&exc
)) {
468 exc
.name
= type
< 100 ? "pow" : "powf";
469 if (_LIB_VERSION
== _SVID_
)
472 exc
.retval
= -HUGE_VAL
;
473 if (_LIB_VERSION
== _POSIX_
)
475 else if (!matherr(&exc
)) {
476 if (_LIB_VERSION
== _SVID_
) {
477 (void) WRITE2("pow(0,neg): DOMAIN error\n", 25);
484 /* neg**non-integral */
486 exc
.name
= type
< 100 ? "pow" : "powf";
487 if (_LIB_VERSION
== _SVID_
)
490 exc
.retval
= zero
/zero
; /* X/Open allow NaN */
491 if (_LIB_VERSION
== _POSIX_
)
493 else if (!matherr(&exc
)) {
494 if (_LIB_VERSION
== _SVID_
) {
495 (void) WRITE2("neg**non-integral: DOMAIN error\n", 32);
502 /* sinh(finite) overflow */
504 exc
.name
= type
< 100 ? "sinh" : "sinhf";
505 if (_LIB_VERSION
== _SVID_
)
506 exc
.retval
= ( (x
>zero
) ? HUGE
: -HUGE
);
508 exc
.retval
= ( (x
>zero
) ? HUGE_VAL
: -HUGE_VAL
);
509 if (_LIB_VERSION
== _POSIX_
)
511 else if (!matherr(&exc
)) {
519 exc
.name
= type
< 100 ? "sqrt" : "sqrtf";
520 if (_LIB_VERSION
== _SVID_
)
523 exc
.retval
= zero
/zero
;
524 if (_LIB_VERSION
== _POSIX_
)
526 else if (!matherr(&exc
)) {
527 if (_LIB_VERSION
== _SVID_
) {
528 (void) WRITE2("sqrt: DOMAIN error\n", 19);
537 exc
.name
= type
< 100 ? "fmod" : "fmodf";
538 if (_LIB_VERSION
== _SVID_
)
541 exc
.retval
= zero
/zero
;
542 if (_LIB_VERSION
== _POSIX_
)
544 else if (!matherr(&exc
)) {
545 if (_LIB_VERSION
== _SVID_
) {
546 (void) WRITE2("fmod: DOMAIN error\n", 20);
555 exc
.name
= type
< 100 ? "remainder" : "remainderf";
556 exc
.retval
= zero
/zero
;
557 if (_LIB_VERSION
== _POSIX_
)
559 else if (!matherr(&exc
)) {
560 if (_LIB_VERSION
== _SVID_
) {
561 (void) WRITE2("remainder: DOMAIN error\n", 24);
570 exc
.name
= type
< 100 ? "acosh" : "acoshf";
571 exc
.retval
= zero
/zero
;
572 if (_LIB_VERSION
== _POSIX_
)
574 else if (!matherr(&exc
)) {
575 if (_LIB_VERSION
== _SVID_
) {
576 (void) WRITE2("acosh: DOMAIN error\n", 20);
585 exc
.name
= type
< 100 ? "atanh" : "atanhf";
586 exc
.retval
= zero
/zero
;
587 if (_LIB_VERSION
== _POSIX_
)
589 else if (!matherr(&exc
)) {
590 if (_LIB_VERSION
== _SVID_
) {
591 (void) WRITE2("atanh: DOMAIN error\n", 20);
600 exc
.name
= type
< 100 ? "atanh" : "atanhf";
601 exc
.retval
= x
/zero
; /* sign(x)*inf */
602 if (_LIB_VERSION
== _POSIX_
)
604 else if (!matherr(&exc
)) {
605 if (_LIB_VERSION
== _SVID_
) {
606 (void) WRITE2("atanh: SING error\n", 18);
613 /* scalb overflow; SVID also returns +-HUGE_VAL */
615 exc
.name
= type
< 100 ? "scalb" : "scalbf";
616 exc
.retval
= x
> zero
? HUGE_VAL
: -HUGE_VAL
;
617 if (_LIB_VERSION
== _POSIX_
)
619 else if (!matherr(&exc
)) {
625 /* scalb underflow */
626 exc
.type
= UNDERFLOW
;
627 exc
.name
= type
< 100 ? "scalb" : "scalbf";
628 exc
.retval
= copysign(zero
,x
);
629 if (_LIB_VERSION
== _POSIX_
)
631 else if (!matherr(&exc
)) {
637 /* j0(|x|>X_TLOSS) */
639 exc
.name
= type
< 100 ? "j0" : "j0f";
641 if (_LIB_VERSION
== _POSIX_
)
643 else if (!matherr(&exc
)) {
644 if (_LIB_VERSION
== _SVID_
) {
645 (void) WRITE2(exc
.name
, 2);
646 (void) WRITE2(": TLOSS error\n", 14);
655 exc
.name
= type
< 100 ? "y0" : "y0f";
657 if (_LIB_VERSION
== _POSIX_
)
659 else if (!matherr(&exc
)) {
660 if (_LIB_VERSION
== _SVID_
) {
661 (void) WRITE2(exc
.name
, 2);
662 (void) WRITE2(": TLOSS error\n", 14);
669 /* j1(|x|>X_TLOSS) */
671 exc
.name
= type
< 100 ? "j1" : "j1f";
673 if (_LIB_VERSION
== _POSIX_
)
675 else if (!matherr(&exc
)) {
676 if (_LIB_VERSION
== _SVID_
) {
677 (void) WRITE2(exc
.name
, 2);
678 (void) WRITE2(": TLOSS error\n", 14);
687 exc
.name
= type
< 100 ? "y1" : "y1f";
689 if (_LIB_VERSION
== _POSIX_
)
691 else if (!matherr(&exc
)) {
692 if (_LIB_VERSION
== _SVID_
) {
693 (void) WRITE2(exc
.name
, 2);
694 (void) WRITE2(": TLOSS error\n", 14);
701 /* jn(|x|>X_TLOSS) */
703 exc
.name
= type
< 100 ? "jn" : "jnf";
705 if (_LIB_VERSION
== _POSIX_
)
707 else if (!matherr(&exc
)) {
708 if (_LIB_VERSION
== _SVID_
) {
709 (void) WRITE2(exc
.name
, 2);
710 (void) WRITE2(": TLOSS error\n", 14);
719 exc
.name
= type
< 100 ? "yn" : "ynf";
721 if (_LIB_VERSION
== _POSIX_
)
723 else if (!matherr(&exc
)) {
724 if (_LIB_VERSION
== _SVID_
) {
725 (void) WRITE2(exc
.name
, 2);
726 (void) WRITE2(": TLOSS error\n", 14);
733 /* gamma(finite) overflow */
735 exc
.name
= type
< 100 ? "gamma" : "gammaf";
736 if (_LIB_VERSION
== _SVID_
)
739 exc
.retval
= HUGE_VAL
;
740 if (_LIB_VERSION
== _POSIX_
)
742 else if (!matherr(&exc
)) {
748 /* gamma(-integer) or gamma(0) */
750 exc
.name
= type
< 100 ? "gamma" : "gammaf";
751 if (_LIB_VERSION
== _SVID_
)
754 exc
.retval
= HUGE_VAL
;
755 if (_LIB_VERSION
== _POSIX_
)
757 else if (!matherr(&exc
)) {
758 if (_LIB_VERSION
== _SVID_
) {
759 (void) WRITE2("gamma: SING error\n", 18);
767 /* error only if _LIB_VERSION == _SVID_ & _XOPEN_ */
769 exc
.name
= type
< 100 ? "pow" : "powf";
771 if (_LIB_VERSION
== _IEEE_
||
772 _LIB_VERSION
== _POSIX_
) exc
.retval
= 1.0;
773 else if (!matherr(&exc
)) {
781 exc
.name
= type
< 100 ? "log2" : "log2f";
782 if (_LIB_VERSION
== _SVID_
)
785 exc
.retval
= -HUGE_VAL
;
786 if (_LIB_VERSION
== _POSIX_
)
788 else if (!matherr(&exc
)) {
789 if (_LIB_VERSION
== _SVID_
) {
790 (void) WRITE2("log2: SING error\n", 18);
799 exc
.name
= type
< 100 ? "log2" : "log2f";
800 if (_LIB_VERSION
== _SVID_
)
803 exc
.retval
= -HUGE_VAL
;
804 if (_LIB_VERSION
== _POSIX_
)
806 else if (!matherr(&exc
)) {
807 if (_LIB_VERSION
== _SVID_
) {
808 (void) WRITE2("log2: DOMAIN error\n", 20);