2 * \file libyasm/floatnum.h
3 * \brief YASM floating point (IEEE) interface.
6 * Copyright (C) 2001-2007 Peter Johnson
8 * Based on public-domain x86 assembly code by Randall Hyde (8/28/91).
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * - Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #ifndef YASM_FLOATNUM_H
33 #define YASM_FLOATNUM_H
39 /** Initialize floatnum internal data structures. */
41 void yasm_floatnum_initialize(void);
43 /** Clean up internal floatnum allocations. */
45 void yasm_floatnum_cleanup(void);
47 /** Create a new floatnum from a decimal string. The input string must be in
48 * standard C representation ([+-]123.456e[-+]789).
49 * \param str floating point decimal string
50 * \return Newly allocated floatnum.
53 /*@only@*/ yasm_floatnum *yasm_floatnum_create(const char *str);
55 /** Duplicate a floatnum.
57 * \return Newly allocated floatnum with the same value as flt.
60 /*@only@*/ yasm_floatnum *yasm_floatnum_copy(const yasm_floatnum *flt);
62 /** Destroy (free allocated memory for) a floatnum.
66 void yasm_floatnum_destroy(/*@only@*/ yasm_floatnum *flt);
68 /** Floating point calculation function: acc = acc op operand.
69 * \note Not all operations in yasm_expr_op may be supported; unsupported
70 * operations will result in an error.
71 * \param acc floatnum accumulator
73 * \param operand floatnum operand
74 * \return Nonzero on error.
77 int yasm_floatnum_calc(yasm_floatnum *acc, yasm_expr_op op,
78 yasm_floatnum *operand);
80 /** Convert a floatnum to single-precision and return as 32-bit value.
81 * The 32-bit value is a "standard" C value (eg, of unknown endian).
83 * \param ret_val pointer to storage for 32-bit output
84 * \return Nonzero if flt can't fit into single precision: -1 if underflow
85 * occurred, 1 if overflow occurred.
88 int yasm_floatnum_get_int(const yasm_floatnum *flt,
89 /*@out@*/ unsigned long *ret_val);
91 /** Output a #yasm_floatnum to buffer in little-endian or big-endian. Puts the
92 * value into the least significant bits of the destination, or may be shifted
93 * into more significant bits by the shift parameter. The destination bits are
94 * cleared before being set. [0] should be the first byte output to the file.
95 * \note Not all sizes are valid. Currently, only 32 (single-precision), 64
96 * (double-precision), and 80 (extended-precision) are valid sizes.
97 * Use yasm_floatnum_check_size() to check for supported sizes.
99 * \param ptr pointer to storage for size bytes of output
100 * \param destsize destination size (in bytes)
101 * \param valsize size (in bits)
102 * \param shift left shift (in bits)
103 * \param bigendian endianness (nonzero=big, zero=little)
104 * \param warn enables standard overflow/underflow warnings
105 * \return Nonzero if flt can't fit into the specified precision: -1 if
106 * underflow occurred, 1 if overflow occurred.
109 int yasm_floatnum_get_sized(const yasm_floatnum *flt, unsigned char *ptr,
110 size_t destsize, size_t valsize, size_t shift,
111 int bigendian, int warn);
113 /** Basic check to see if size is valid for flt conversion (using
114 * yasm_floatnum_get_sized()). Doesn't actually check for underflow/overflow
115 * but rather checks for size=32,64,80
117 * \param flt floatnum
118 * \param size number of bits of output space
119 * \return 1 if valid size, 0 if invalid size.
122 int yasm_floatnum_check_size(const yasm_floatnum *flt, size_t size);
124 /** Print various representations of a floatnum. For debugging purposes only.
126 * \param flt floatnum
129 void yasm_floatnum_print(const yasm_floatnum *flt, FILE *f);