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