4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #ifndef BASE_CONVERSION_H
28 #define BASE_CONVERSION_H
30 #pragma ident "%Z%%M% %I% %E% SMI"
33 #include <floatingpoint.h>
34 #include <sys/isa_defs.h>
37 * Common constants, types, and declarations for floating point
41 /* PRIVATE CONSTANTS */
44 #define SINGLE_BIAS 127
45 #define DOUBLE_BIAS 1023
46 #define EXTENDED_BIAS 16383
47 #define QUAD_BIAS 16383
53 * Unpacked binary floating point format. The binary point lies
54 * to the right of the most significant bit in significand[0].
55 * The exponent is unbiased. The significand array is long enough
56 * that the last word never contains any bits we need to keep,
57 * just rounding information.
60 #define UNPACKED_SIZE 5
64 enum fp_class_type fpclass
;
66 unsigned significand
[UNPACKED_SIZE
];
70 * Packed binary floating point formats. The *_msw structure
71 * corresponds to the most significant word.
77 unsigned significand
:23;
83 unsigned significand
:20;
95 unsigned significand
:16;
105 unsigned significand2
;
110 unsigned significand2
;
111 unsigned significand
;
113 } extended_formatted
;
116 unsigned significand4
;
117 unsigned significand3
;
118 unsigned significand2
;
120 } quadruple_formatted
;
127 unsigned significand
:23;
132 unsigned exponent
:11;
133 unsigned significand
:20;
138 unsigned exponent
:15;
144 unsigned exponent
:15;
145 unsigned significand
:16;
154 unsigned significand2
;
159 unsigned significand
;
160 unsigned significand2
;
161 } extended_formatted
;
165 unsigned significand2
;
166 unsigned significand3
;
167 unsigned significand4
;
168 } quadruple_formatted
;
175 } single_equivalence
;
180 } double_equivalence
;
183 extended_formatted f
;
185 } extended_equivalence
;
188 quadruple_formatted f
;
190 } quadruple_equivalence
;
193 * Multiple precision floating point type. This type is suitable
194 * for representing positive floating point numbers of variable
195 * precision in either binary or decimal. The bsignificand array
196 * holds the digits of a multi-word integer, stored least significant
197 * digit first, in either radix 2^16 or 10^4. blength is the
198 * length of the significand array. bexponent is a power of two
199 * or ten, so that the value represented is
201 * 2^(bexponent) * sum (bsignificand[i] * 2^(i*16))
205 * 10^(bexponent) * sum (bsignificand[i] * 10^(i*4))
207 * if decimal, where the sum runs from i = 0 to blength - 1.
208 * (Whether the representation is binary or decimal is implied
209 * from context.) bsize indicates the size of the significand
210 * array and may be larger than _BIG_FLOAT_SIZE if storage has
211 * been allocated at runtime.
214 #define _BIG_FLOAT_SIZE (DECIMAL_STRING_LENGTH/2)
217 unsigned short bsize
;
218 unsigned short blength
;
220 unsigned short bsignificand
[_BIG_FLOAT_SIZE
];
223 /* structure for storing IEEE modes and status flags */
229 /* PRIVATE GLOBAL VARIABLES */
232 * Thread-specific flags to indicate whether any NaNs or infinities
233 * have been read or written.
235 extern int *_thrp_get_inf_read(void);
236 extern int *_thrp_get_inf_written(void);
237 extern int *_thrp_get_nan_read(void);
238 extern int *_thrp_get_nan_written(void);
240 #define __inf_read (*(int *)_thrp_get_inf_read())
241 #define __inf_written (*(int *)_thrp_get_inf_written())
242 #define __nan_read (*(int *)_thrp_get_nan_read())
243 #define __nan_written (*(int *)_thrp_get_nan_written())
246 * Powers of 5 in base 2**16 and powers of 2 in base 10**4.
248 * __tbl_10_small_digits contains
251 * 5**__TBL_10_SMALL_SIZE-1
252 * __tbl_10_big_digits contains
254 * 5**__TBL_10_SMALL_SIZE, ...
255 * 5**__TBL_10_SMALL_SIZE*(__TBL_10_BIG_SIZE-1)
256 * __tbl_10_huge_digits contains
258 * 5**__TBL_10_SMALL_SIZE*__TBL_10_BIG_SIZE, ...
259 * 5**__TBL_10_SMALL_SIZE*__TBL_10_BIG_SIZE*(__TBL_10_HUGE_SIZE-1)
261 * so that any power of 5 from 5**0 to
262 * 5**__TBL_10_SMALL_SIZE*__TBL_10_BIG_SIZE*__TBL_10_HUGE_SIZE
263 * can be represented as a product of at most three table entries.
265 * Similarly any power of 2 from 2**0 to
266 * 2**__TBL_2_SMALL_SIZE*__TBL_2_BIG_SIZE*__TBL_2_HUGE_SIZE
267 * can be represented as a product of at most three table entries.
269 * Since the powers vary greatly in size, the tables are condensed:
270 * entry i in table x is stored in
271 * x_digits[x_start[i]] (least significant)
273 * x_digits[x_start[i+1]-1] (most significant)
276 #define __TBL_10_SMALL_SIZE 64
277 #define __TBL_10_BIG_SIZE 16
278 #define __TBL_10_HUGE_SIZE 6
280 extern const unsigned short
281 __tbl_10_small_digits
[], __tbl_10_small_start
[],
282 __tbl_10_big_digits
[], __tbl_10_big_start
[],
283 __tbl_10_huge_digits
[], __tbl_10_huge_start
[];
285 #define __TBL_2_SMALL_SIZE 176
286 #define __TBL_2_BIG_SIZE 16
287 #define __TBL_2_HUGE_SIZE 6
289 extern const unsigned short
290 __tbl_2_small_digits
[], __tbl_2_small_start
[],
291 __tbl_2_big_digits
[], __tbl_2_big_start
[],
292 __tbl_2_huge_digits
[], __tbl_2_huge_start
[];
295 * Powers of ten. For i = 0, 1, ..., __TBL_TENS_MAX, __tbl_tens[i]
296 * = 10^i rounded to double precision. (10^i is representable exactly
297 * in double precision for i <= __TBL_TENS_EXACT.)
300 #define __TBL_TENS_EXACT 22
301 #define __TBL_TENS_MAX 49
303 extern const double __tbl_tens
[];
306 /* PRIVATE FUNCTIONS */
308 extern void __base_conversion_set_exception(fp_exception_field_type
);
310 extern void __four_digits_quick(unsigned short, char *);
312 extern int __fast_double_to_decimal(double *dd
, decimal_mode
*pm
,
313 decimal_record
*pd
, fp_exception_field_type
*ps
);
315 extern void __pack_single(unpacked
*, single
*, enum fp_direction_type
,
316 fp_exception_field_type
*);
317 extern void __pack_double(unpacked
*, double *, enum fp_direction_type
,
318 fp_exception_field_type
*);
319 extern void __pack_extended(unpacked
*, extended
*, enum fp_direction_type
,
320 fp_exception_field_type
*);
321 extern void __pack_quadruple(unpacked
*, quadruple
*,
322 enum fp_direction_type
, fp_exception_field_type
*);
324 extern void __infnanstring(enum fp_class_type cl
, int ndigits
, char *buf
);
326 extern void __big_float_times_power(_big_float
*pbf
, int mult
, int n
,
327 int precision
, _big_float
**pnewbf
);
329 extern void __get_ieee_flags(__ieee_flags_type
*);
330 extern void __set_ieee_flags(__ieee_flags_type
*);
332 extern double __mul_set(double, double, int *);
333 extern double __div_set(double, double, int *);
334 extern double __dabs(double *);
336 #if defined(sparc) || defined(__sparc)
337 extern enum fp_direction_type
_QgetRD(void);
340 #include "base_inlines.h"
342 #endif /* BASE_CONVERSION_H */