xref: /illumos-gate/usr/src/lib/libc/port/fp/_base_sup.c (revision d3b5f563)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57257d1b4Sraf  * Common Development and Distribution License (the "License").
67257d1b4Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217257d1b4Sraf 
227c478bd9Sstevel@tonic-gate /*
237257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27*d3b5f563SJohn Levon /*
28*d3b5f563SJohn Levon  * Copyright 2019 Joyent, Inc.
29*d3b5f563SJohn Levon  */
30*d3b5f563SJohn Levon 
317257d1b4Sraf #include "lint.h"
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include "base_conversion.h"
347c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * Miscellaneous support routines used in base conversion
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate static const union {
417c478bd9Sstevel@tonic-gate 	unsigned int	u[2];
427c478bd9Sstevel@tonic-gate 	double		d;
437c478bd9Sstevel@tonic-gate } C[] = {
447c478bd9Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
457c478bd9Sstevel@tonic-gate 	{ 0x00000000u, 0x00100000u },
467c478bd9Sstevel@tonic-gate 	{ 0x00000001u, 0x7ff00000u }
477c478bd9Sstevel@tonic-gate #else
487c478bd9Sstevel@tonic-gate 	{ 0x00100000u, 0x00000000u },
497c478bd9Sstevel@tonic-gate 	{ 0x7ff00000u, 0x00000001u }
507c478bd9Sstevel@tonic-gate #endif
517c478bd9Sstevel@tonic-gate };
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #define	minnormal	C[0].d
547c478bd9Sstevel@tonic-gate #define	signalingnan	C[1].d
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /* raise the floating point exceptions indicated by ef */
577c478bd9Sstevel@tonic-gate void
__base_conversion_set_exception(fp_exception_field_type ef)587c478bd9Sstevel@tonic-gate __base_conversion_set_exception(fp_exception_field_type ef)
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate 	double	t;
619c3b8506SToomas Soome 	volatile double tstored __unused;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	if (ef == (1 << fp_inexact)) {
647c478bd9Sstevel@tonic-gate 		t = 9.999999962747097015E-1;
657c478bd9Sstevel@tonic-gate 		/*
667c478bd9Sstevel@tonic-gate 		 * 28 sig bits so product isn't inexact in extended
677c478bd9Sstevel@tonic-gate 		 * accumulator, causing two inexact traps.
687c478bd9Sstevel@tonic-gate 		 */
697c478bd9Sstevel@tonic-gate 	} else if ((ef & (1 << fp_invalid)) != 0) {
707c478bd9Sstevel@tonic-gate 		t = signalingnan;
717c478bd9Sstevel@tonic-gate 	} else if ((ef & (1 << fp_overflow)) != 0) {
727c478bd9Sstevel@tonic-gate 		t = 4.149515553422842866E+180;
737c478bd9Sstevel@tonic-gate 		/*
747c478bd9Sstevel@tonic-gate 		 * 28 sig bits so product isn't inexact in extended
757c478bd9Sstevel@tonic-gate 		 * accumulator, causing inexact trap prior to overflow trap
767c478bd9Sstevel@tonic-gate 		 * on store.
777c478bd9Sstevel@tonic-gate 		 */
787c478bd9Sstevel@tonic-gate 	} else if ((ef & (1 << fp_underflow)) != 0) {
797c478bd9Sstevel@tonic-gate 		t = minnormal;
807c478bd9Sstevel@tonic-gate 	} else
817c478bd9Sstevel@tonic-gate 		return;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	/* Storage forces exception */
847c478bd9Sstevel@tonic-gate 	tstored = t * t;
857c478bd9Sstevel@tonic-gate #if defined(__lint)
867c478bd9Sstevel@tonic-gate 	tstored = tstored;
877c478bd9Sstevel@tonic-gate #endif
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate  * The following routine is no longer used in libc, but we have
927c478bd9Sstevel@tonic-gate  * to leave it for now because it's still used by Sun's old Fortran
937c478bd9Sstevel@tonic-gate  * runtime libraries.  Today this is a bug; in the days of SunOS 4.x,
947c478bd9Sstevel@tonic-gate  * when the relevant design decisions were made, it was a feature.
95*d3b5f563SJohn Levon  *
96*d3b5f563SJohn Levon  * Regardless, on 32-bit, 'quadruple' under GCC is not 128 bits, so it
97*d3b5f563SJohn Levon  * uses uninitialized memory...
987c478bd9Sstevel@tonic-gate  */
99*d3b5f563SJohn Levon #pragma GCC diagnostic ignored "-Wuninitialized"
1007c478bd9Sstevel@tonic-gate enum fp_class_type
__class_quadruple(quadruple * x)1017c478bd9Sstevel@tonic-gate __class_quadruple(quadruple *x)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	quadruple_equivalence kluge;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	kluge.x = *x;
1067c478bd9Sstevel@tonic-gate 	if (kluge.f.msw.exponent == 0) {	/* 0 or sub */
1077c478bd9Sstevel@tonic-gate 		if ((kluge.f.msw.significand == 0) &&
1087c478bd9Sstevel@tonic-gate 		    (kluge.f.significand2 == 0) &&
1097c478bd9Sstevel@tonic-gate 		    (kluge.f.significand3 == 0) &&
1107c478bd9Sstevel@tonic-gate 		    (kluge.f.significand4 == 0))
1117c478bd9Sstevel@tonic-gate 			return (fp_zero);
1127c478bd9Sstevel@tonic-gate 		else
1137c478bd9Sstevel@tonic-gate 			return (fp_subnormal);
1147c478bd9Sstevel@tonic-gate 	} else if (kluge.f.msw.exponent == 0x7fff) {	/* inf or nan */
1157c478bd9Sstevel@tonic-gate 		if ((kluge.f.msw.significand == 0) &&
1167c478bd9Sstevel@tonic-gate 		    (kluge.f.significand2 == 0) &&
1177c478bd9Sstevel@tonic-gate 		    (kluge.f.significand3 == 0) &&
1187c478bd9Sstevel@tonic-gate 		    (kluge.f.significand4 == 0))
1197c478bd9Sstevel@tonic-gate 			return (fp_infinity);
1207c478bd9Sstevel@tonic-gate 		else if ((kluge.f.msw.significand & 0xffff) >=
1217257d1b4Sraf 		    (unsigned int)0x8000)
1227c478bd9Sstevel@tonic-gate 			return (fp_quiet);
1237c478bd9Sstevel@tonic-gate 		else
1247c478bd9Sstevel@tonic-gate 			return (fp_signaling);
1257c478bd9Sstevel@tonic-gate 	} else
1267c478bd9Sstevel@tonic-gate 		return (fp_normal);
1277c478bd9Sstevel@tonic-gate }
128