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 5*7257d1b4Sraf * Common Development and Distribution License (the "License"). 6*7257d1b4Sraf * 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 */ 210ec57554Sraf 227c478bd9Sstevel@tonic-gate /* 23*7257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 270ec57554Sraf #ifndef BASE_CONVERSION_H 280ec57554Sraf #define BASE_CONVERSION_H 290ec57554Sraf 307c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate #include <errno.h> 337c478bd9Sstevel@tonic-gate #include <floatingpoint.h> 347c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h> 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate /* 377c478bd9Sstevel@tonic-gate * Common constants, types, and declarations for floating point 387c478bd9Sstevel@tonic-gate * base conversion 397c478bd9Sstevel@tonic-gate */ 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate /* PRIVATE CONSTANTS */ 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate /* exponent bias */ 447c478bd9Sstevel@tonic-gate #define SINGLE_BIAS 127 457c478bd9Sstevel@tonic-gate #define DOUBLE_BIAS 1023 467c478bd9Sstevel@tonic-gate #define EXTENDED_BIAS 16383 477c478bd9Sstevel@tonic-gate #define QUAD_BIAS 16383 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate /* PRIVATE TYPES */ 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate /* 537c478bd9Sstevel@tonic-gate * Unpacked binary floating point format. The binary point lies 547c478bd9Sstevel@tonic-gate * to the right of the most significant bit in significand[0]. 557c478bd9Sstevel@tonic-gate * The exponent is unbiased. The significand array is long enough 567c478bd9Sstevel@tonic-gate * that the last word never contains any bits we need to keep, 577c478bd9Sstevel@tonic-gate * just rounding information. 587c478bd9Sstevel@tonic-gate */ 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate #define UNPACKED_SIZE 5 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate typedef struct { 637c478bd9Sstevel@tonic-gate int sign; 647c478bd9Sstevel@tonic-gate enum fp_class_type fpclass; 657c478bd9Sstevel@tonic-gate int exponent; 667c478bd9Sstevel@tonic-gate unsigned significand[UNPACKED_SIZE]; 677c478bd9Sstevel@tonic-gate } unpacked; 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate /* 707c478bd9Sstevel@tonic-gate * Packed binary floating point formats. The *_msw structure 717c478bd9Sstevel@tonic-gate * corresponds to the most significant word. 727c478bd9Sstevel@tonic-gate */ 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate typedef struct { 777c478bd9Sstevel@tonic-gate unsigned significand:23; 787c478bd9Sstevel@tonic-gate unsigned exponent:8; 797c478bd9Sstevel@tonic-gate unsigned sign:1; 807c478bd9Sstevel@tonic-gate } single_msw; 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate typedef struct { 837c478bd9Sstevel@tonic-gate unsigned significand:20; 847c478bd9Sstevel@tonic-gate unsigned exponent:11; 857c478bd9Sstevel@tonic-gate unsigned sign:1; 867c478bd9Sstevel@tonic-gate } double_msw; 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate typedef struct { 897c478bd9Sstevel@tonic-gate unsigned exponent:15; 907c478bd9Sstevel@tonic-gate unsigned sign:1; 917c478bd9Sstevel@tonic-gate unsigned unused:16; 927c478bd9Sstevel@tonic-gate } extended_msw; 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate typedef struct { 957c478bd9Sstevel@tonic-gate unsigned significand:16; 967c478bd9Sstevel@tonic-gate unsigned exponent:15; 977c478bd9Sstevel@tonic-gate unsigned sign:1; 987c478bd9Sstevel@tonic-gate } quadruple_msw; 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate typedef struct { 1017c478bd9Sstevel@tonic-gate single_msw msw; 1027c478bd9Sstevel@tonic-gate } single_formatted; 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate typedef struct { 1057c478bd9Sstevel@tonic-gate unsigned significand2; 1067c478bd9Sstevel@tonic-gate double_msw msw; 1077c478bd9Sstevel@tonic-gate } double_formatted; 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate typedef struct { 1107c478bd9Sstevel@tonic-gate unsigned significand2; 1117c478bd9Sstevel@tonic-gate unsigned significand; 1127c478bd9Sstevel@tonic-gate extended_msw msw; 1137c478bd9Sstevel@tonic-gate } extended_formatted; 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate typedef struct { 1167c478bd9Sstevel@tonic-gate unsigned significand4; 1177c478bd9Sstevel@tonic-gate unsigned significand3; 1187c478bd9Sstevel@tonic-gate unsigned significand2; 1197c478bd9Sstevel@tonic-gate quadruple_msw msw; 1207c478bd9Sstevel@tonic-gate } quadruple_formatted; 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate #else 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate typedef struct { 1257c478bd9Sstevel@tonic-gate unsigned sign:1; 1267c478bd9Sstevel@tonic-gate unsigned exponent:8; 1277c478bd9Sstevel@tonic-gate unsigned significand:23; 1287c478bd9Sstevel@tonic-gate } single_msw; 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate typedef struct { 1317c478bd9Sstevel@tonic-gate unsigned sign:1; 1327c478bd9Sstevel@tonic-gate unsigned exponent:11; 1337c478bd9Sstevel@tonic-gate unsigned significand:20; 1347c478bd9Sstevel@tonic-gate } double_msw; 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate typedef struct { 1377c478bd9Sstevel@tonic-gate unsigned sign:1; 1387c478bd9Sstevel@tonic-gate unsigned exponent:15; 1397c478bd9Sstevel@tonic-gate unsigned unused:16; 1407c478bd9Sstevel@tonic-gate } extended_msw; 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate typedef struct { 1437c478bd9Sstevel@tonic-gate unsigned sign:1; 1447c478bd9Sstevel@tonic-gate unsigned exponent:15; 1457c478bd9Sstevel@tonic-gate unsigned significand:16; 1467c478bd9Sstevel@tonic-gate } quadruple_msw; 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate typedef struct { 1497c478bd9Sstevel@tonic-gate single_msw msw; 1507c478bd9Sstevel@tonic-gate } single_formatted; 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate typedef struct { 1537c478bd9Sstevel@tonic-gate double_msw msw; 1547c478bd9Sstevel@tonic-gate unsigned significand2; 1557c478bd9Sstevel@tonic-gate } double_formatted; 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate typedef struct { 1587c478bd9Sstevel@tonic-gate extended_msw msw; 1597c478bd9Sstevel@tonic-gate unsigned significand; 1607c478bd9Sstevel@tonic-gate unsigned significand2; 1617c478bd9Sstevel@tonic-gate } extended_formatted; 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate typedef struct { 1647c478bd9Sstevel@tonic-gate quadruple_msw msw; 1657c478bd9Sstevel@tonic-gate unsigned significand2; 1667c478bd9Sstevel@tonic-gate unsigned significand3; 1677c478bd9Sstevel@tonic-gate unsigned significand4; 1687c478bd9Sstevel@tonic-gate } quadruple_formatted; 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate #endif 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate typedef union { 1737c478bd9Sstevel@tonic-gate single_formatted f; 1747c478bd9Sstevel@tonic-gate single x; 1757c478bd9Sstevel@tonic-gate } single_equivalence; 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate typedef union { 1787c478bd9Sstevel@tonic-gate double_formatted f; 1797c478bd9Sstevel@tonic-gate double x; 1807c478bd9Sstevel@tonic-gate } double_equivalence; 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate typedef union { 1837c478bd9Sstevel@tonic-gate extended_formatted f; 1847c478bd9Sstevel@tonic-gate extended x; 1857c478bd9Sstevel@tonic-gate } extended_equivalence; 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate typedef union { 1887c478bd9Sstevel@tonic-gate quadruple_formatted f; 1897c478bd9Sstevel@tonic-gate quadruple x; 1907c478bd9Sstevel@tonic-gate } quadruple_equivalence; 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate /* 1937c478bd9Sstevel@tonic-gate * Multiple precision floating point type. This type is suitable 1947c478bd9Sstevel@tonic-gate * for representing positive floating point numbers of variable 1957c478bd9Sstevel@tonic-gate * precision in either binary or decimal. The bsignificand array 1967c478bd9Sstevel@tonic-gate * holds the digits of a multi-word integer, stored least significant 1977c478bd9Sstevel@tonic-gate * digit first, in either radix 2^16 or 10^4. blength is the 1987c478bd9Sstevel@tonic-gate * length of the significand array. bexponent is a power of two 1997c478bd9Sstevel@tonic-gate * or ten, so that the value represented is 2007c478bd9Sstevel@tonic-gate * 2017c478bd9Sstevel@tonic-gate * 2^(bexponent) * sum (bsignificand[i] * 2^(i*16)) 2027c478bd9Sstevel@tonic-gate * 2037c478bd9Sstevel@tonic-gate * if binary, or 2047c478bd9Sstevel@tonic-gate * 2057c478bd9Sstevel@tonic-gate * 10^(bexponent) * sum (bsignificand[i] * 10^(i*4)) 2067c478bd9Sstevel@tonic-gate * 2077c478bd9Sstevel@tonic-gate * if decimal, where the sum runs from i = 0 to blength - 1. 2087c478bd9Sstevel@tonic-gate * (Whether the representation is binary or decimal is implied 2097c478bd9Sstevel@tonic-gate * from context.) bsize indicates the size of the significand 2107c478bd9Sstevel@tonic-gate * array and may be larger than _BIG_FLOAT_SIZE if storage has 2117c478bd9Sstevel@tonic-gate * been allocated at runtime. 2127c478bd9Sstevel@tonic-gate */ 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate #define _BIG_FLOAT_SIZE (DECIMAL_STRING_LENGTH/2) 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate typedef struct { 2177c478bd9Sstevel@tonic-gate unsigned short bsize; 2187c478bd9Sstevel@tonic-gate unsigned short blength; 2197c478bd9Sstevel@tonic-gate short int bexponent; 2207c478bd9Sstevel@tonic-gate unsigned short bsignificand[_BIG_FLOAT_SIZE]; 2217c478bd9Sstevel@tonic-gate } _big_float; 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate /* structure for storing IEEE modes and status flags */ 2247c478bd9Sstevel@tonic-gate typedef struct { 2257c478bd9Sstevel@tonic-gate int status, mode; 2267c478bd9Sstevel@tonic-gate } __ieee_flags_type; 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate /* PRIVATE GLOBAL VARIABLES */ 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate /* 232*7257d1b4Sraf * Thread-specific flags to indicate whether any NaNs or infinities 233*7257d1b4Sraf * have been read or written. 2347c478bd9Sstevel@tonic-gate */ 235*7257d1b4Sraf extern int *_thrp_get_inf_read(void); 236*7257d1b4Sraf extern int *_thrp_get_inf_written(void); 237*7257d1b4Sraf extern int *_thrp_get_nan_read(void); 238*7257d1b4Sraf extern int *_thrp_get_nan_written(void); 2397c478bd9Sstevel@tonic-gate 240*7257d1b4Sraf #define __inf_read (*(int *)_thrp_get_inf_read()) 241*7257d1b4Sraf #define __inf_written (*(int *)_thrp_get_inf_written()) 242*7257d1b4Sraf #define __nan_read (*(int *)_thrp_get_nan_read()) 243*7257d1b4Sraf #define __nan_written (*(int *)_thrp_get_nan_written()) 2447c478bd9Sstevel@tonic-gate 2457c478bd9Sstevel@tonic-gate /* 2467c478bd9Sstevel@tonic-gate * Powers of 5 in base 2**16 and powers of 2 in base 10**4. 2477c478bd9Sstevel@tonic-gate * 2487c478bd9Sstevel@tonic-gate * __tbl_10_small_digits contains 2497c478bd9Sstevel@tonic-gate * 5**0, 2507c478bd9Sstevel@tonic-gate * 5**1, ... 2517c478bd9Sstevel@tonic-gate * 5**__TBL_10_SMALL_SIZE-1 2527c478bd9Sstevel@tonic-gate * __tbl_10_big_digits contains 2537c478bd9Sstevel@tonic-gate * 5**0, 2547c478bd9Sstevel@tonic-gate * 5**__TBL_10_SMALL_SIZE, ... 2557c478bd9Sstevel@tonic-gate * 5**__TBL_10_SMALL_SIZE*(__TBL_10_BIG_SIZE-1) 2567c478bd9Sstevel@tonic-gate * __tbl_10_huge_digits contains 2577c478bd9Sstevel@tonic-gate * 5**0, 2587c478bd9Sstevel@tonic-gate * 5**__TBL_10_SMALL_SIZE*__TBL_10_BIG_SIZE, ... 2597c478bd9Sstevel@tonic-gate * 5**__TBL_10_SMALL_SIZE*__TBL_10_BIG_SIZE*(__TBL_10_HUGE_SIZE-1) 2607c478bd9Sstevel@tonic-gate * 2617c478bd9Sstevel@tonic-gate * so that any power of 5 from 5**0 to 2627c478bd9Sstevel@tonic-gate * 5**__TBL_10_SMALL_SIZE*__TBL_10_BIG_SIZE*__TBL_10_HUGE_SIZE 2637c478bd9Sstevel@tonic-gate * can be represented as a product of at most three table entries. 2647c478bd9Sstevel@tonic-gate * 2657c478bd9Sstevel@tonic-gate * Similarly any power of 2 from 2**0 to 2667c478bd9Sstevel@tonic-gate * 2**__TBL_2_SMALL_SIZE*__TBL_2_BIG_SIZE*__TBL_2_HUGE_SIZE 2677c478bd9Sstevel@tonic-gate * can be represented as a product of at most three table entries. 2687c478bd9Sstevel@tonic-gate * 2697c478bd9Sstevel@tonic-gate * Since the powers vary greatly in size, the tables are condensed: 2707c478bd9Sstevel@tonic-gate * entry i in table x is stored in 2717c478bd9Sstevel@tonic-gate * x_digits[x_start[i]] (least significant) 2727c478bd9Sstevel@tonic-gate * through 2737c478bd9Sstevel@tonic-gate * x_digits[x_start[i+1]-1] (most significant) 2747c478bd9Sstevel@tonic-gate */ 2757c478bd9Sstevel@tonic-gate 2767c478bd9Sstevel@tonic-gate #define __TBL_10_SMALL_SIZE 64 2777c478bd9Sstevel@tonic-gate #define __TBL_10_BIG_SIZE 16 2787c478bd9Sstevel@tonic-gate #define __TBL_10_HUGE_SIZE 6 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate extern const unsigned short 2817c478bd9Sstevel@tonic-gate __tbl_10_small_digits[], __tbl_10_small_start[], 2827c478bd9Sstevel@tonic-gate __tbl_10_big_digits[], __tbl_10_big_start[], 2837c478bd9Sstevel@tonic-gate __tbl_10_huge_digits[], __tbl_10_huge_start[]; 2847c478bd9Sstevel@tonic-gate 2857c478bd9Sstevel@tonic-gate #define __TBL_2_SMALL_SIZE 176 2867c478bd9Sstevel@tonic-gate #define __TBL_2_BIG_SIZE 16 2877c478bd9Sstevel@tonic-gate #define __TBL_2_HUGE_SIZE 6 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate extern const unsigned short 2907c478bd9Sstevel@tonic-gate __tbl_2_small_digits[], __tbl_2_small_start[], 2917c478bd9Sstevel@tonic-gate __tbl_2_big_digits[], __tbl_2_big_start[], 2927c478bd9Sstevel@tonic-gate __tbl_2_huge_digits[], __tbl_2_huge_start[]; 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate /* 2957c478bd9Sstevel@tonic-gate * Powers of ten. For i = 0, 1, ..., __TBL_TENS_MAX, __tbl_tens[i] 2967c478bd9Sstevel@tonic-gate * = 10^i rounded to double precision. (10^i is representable exactly 2977c478bd9Sstevel@tonic-gate * in double precision for i <= __TBL_TENS_EXACT.) 2987c478bd9Sstevel@tonic-gate */ 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate #define __TBL_TENS_EXACT 22 3017c478bd9Sstevel@tonic-gate #define __TBL_TENS_MAX 49 3027c478bd9Sstevel@tonic-gate 3037c478bd9Sstevel@tonic-gate extern const double __tbl_tens[]; 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate /* PRIVATE FUNCTIONS */ 3077c478bd9Sstevel@tonic-gate 3087c478bd9Sstevel@tonic-gate extern void __base_conversion_set_exception(fp_exception_field_type); 3097c478bd9Sstevel@tonic-gate 3107c478bd9Sstevel@tonic-gate extern void __four_digits_quick(unsigned short, char *); 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate extern int __fast_double_to_decimal(double *dd, decimal_mode *pm, 3137c478bd9Sstevel@tonic-gate decimal_record *pd, fp_exception_field_type *ps); 3147c478bd9Sstevel@tonic-gate 3157c478bd9Sstevel@tonic-gate extern void __pack_single(unpacked *, single *, enum fp_direction_type, 3167c478bd9Sstevel@tonic-gate fp_exception_field_type *); 3177c478bd9Sstevel@tonic-gate extern void __pack_double(unpacked *, double *, enum fp_direction_type, 3187c478bd9Sstevel@tonic-gate fp_exception_field_type *); 3197c478bd9Sstevel@tonic-gate extern void __pack_extended(unpacked *, extended *, enum fp_direction_type, 3207c478bd9Sstevel@tonic-gate fp_exception_field_type *); 3217c478bd9Sstevel@tonic-gate extern void __pack_quadruple(unpacked *, quadruple *, 3227c478bd9Sstevel@tonic-gate enum fp_direction_type, fp_exception_field_type *); 3237c478bd9Sstevel@tonic-gate 3247c478bd9Sstevel@tonic-gate extern void __infnanstring(enum fp_class_type cl, int ndigits, char *buf); 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate extern void __big_float_times_power(_big_float *pbf, int mult, int n, 3277c478bd9Sstevel@tonic-gate int precision, _big_float **pnewbf); 3287c478bd9Sstevel@tonic-gate 3297c478bd9Sstevel@tonic-gate extern void __get_ieee_flags(__ieee_flags_type *); 3307c478bd9Sstevel@tonic-gate extern void __set_ieee_flags(__ieee_flags_type *); 3317c478bd9Sstevel@tonic-gate 3327c478bd9Sstevel@tonic-gate extern double __mul_set(double, double, int *); 3337c478bd9Sstevel@tonic-gate extern double __div_set(double, double, int *); 3347c478bd9Sstevel@tonic-gate extern double __dabs(double *); 3357c478bd9Sstevel@tonic-gate 3367c478bd9Sstevel@tonic-gate #if defined(sparc) || defined(__sparc) 3377c478bd9Sstevel@tonic-gate extern enum fp_direction_type _QgetRD(void); 3387c478bd9Sstevel@tonic-gate #endif 3390ec57554Sraf 3400ec57554Sraf #include "base_inlines.h" 3410ec57554Sraf 3420ec57554Sraf #endif /* BASE_CONVERSION_H */ 343