floatnum.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * \file libyasm/floatnum.h
  3. * \brief YASM floating point (IEEE) interface.
  4. *
  5. * \license
  6. * Copyright (C) 2001-2007 Peter Johnson
  7. *
  8. * Based on public-domain x86 assembly code by Randall Hyde (8/28/91).
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  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.
  18. *
  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.
  30. * \endlicense
  31. */
  32. #ifndef YASM_FLOATNUM_H
  33. #define YASM_FLOATNUM_H
  34. #ifndef YASM_LIB_DECL
  35. #define YASM_LIB_DECL
  36. #endif
  37. /** Initialize floatnum internal data structures. */
  38. YASM_LIB_DECL
  39. void yasm_floatnum_initialize(void);
  40. /** Clean up internal floatnum allocations. */
  41. YASM_LIB_DECL
  42. void yasm_floatnum_cleanup(void);
  43. /** Create a new floatnum from a decimal string. The input string must be in
  44. * standard C representation ([+-]123.456e[-+]789).
  45. * \param str floating point decimal string
  46. * \return Newly allocated floatnum.
  47. */
  48. YASM_LIB_DECL
  49. /*@only@*/ yasm_floatnum *yasm_floatnum_create(const char *str);
  50. /** Duplicate a floatnum.
  51. * \param flt floatnum
  52. * \return Newly allocated floatnum with the same value as flt.
  53. */
  54. YASM_LIB_DECL
  55. /*@only@*/ yasm_floatnum *yasm_floatnum_copy(const yasm_floatnum *flt);
  56. /** Destroy (free allocated memory for) a floatnum.
  57. * \param flt floatnum
  58. */
  59. YASM_LIB_DECL
  60. void yasm_floatnum_destroy(/*@only@*/ yasm_floatnum *flt);
  61. /** Floating point calculation function: acc = acc op operand.
  62. * \note Not all operations in yasm_expr_op may be supported; unsupported
  63. * operations will result in an error.
  64. * \param acc floatnum accumulator
  65. * \param op operation
  66. * \param operand floatnum operand
  67. * \return Nonzero on error.
  68. */
  69. YASM_LIB_DECL
  70. int yasm_floatnum_calc(yasm_floatnum *acc, yasm_expr_op op,
  71. yasm_floatnum *operand);
  72. /** Convert a floatnum to single-precision and return as 32-bit value.
  73. * The 32-bit value is a "standard" C value (eg, of unknown endian).
  74. * \param flt floatnum
  75. * \param ret_val pointer to storage for 32-bit output
  76. * \return Nonzero if flt can't fit into single precision: -1 if underflow
  77. * occurred, 1 if overflow occurred.
  78. */
  79. YASM_LIB_DECL
  80. int yasm_floatnum_get_int(const yasm_floatnum *flt,
  81. /*@out@*/ unsigned long *ret_val);
  82. /** Output a #yasm_floatnum to buffer in little-endian or big-endian. Puts the
  83. * value into the least significant bits of the destination, or may be shifted
  84. * into more significant bits by the shift parameter. The destination bits are
  85. * cleared before being set. [0] should be the first byte output to the file.
  86. * \note Not all sizes are valid. Currently, only 32 (single-precision), 64
  87. * (double-precision), and 80 (extended-precision) are valid sizes.
  88. * Use yasm_floatnum_check_size() to check for supported sizes.
  89. * \param flt floatnum
  90. * \param ptr pointer to storage for size bytes of output
  91. * \param destsize destination size (in bytes)
  92. * \param valsize size (in bits)
  93. * \param shift left shift (in bits)
  94. * \param bigendian endianness (nonzero=big, zero=little)
  95. * \param warn enables standard overflow/underflow warnings
  96. * \return Nonzero if flt can't fit into the specified precision: -1 if
  97. * underflow occurred, 1 if overflow occurred.
  98. */
  99. YASM_LIB_DECL
  100. int yasm_floatnum_get_sized(const yasm_floatnum *flt, unsigned char *ptr,
  101. size_t destsize, size_t valsize, size_t shift,
  102. int bigendian, int warn);
  103. /** Basic check to see if size is valid for flt conversion (using
  104. * yasm_floatnum_get_sized()). Doesn't actually check for underflow/overflow
  105. * but rather checks for size=32,64,80
  106. * (at present).
  107. * \param flt floatnum
  108. * \param size number of bits of output space
  109. * \return 1 if valid size, 0 if invalid size.
  110. */
  111. YASM_LIB_DECL
  112. int yasm_floatnum_check_size(const yasm_floatnum *flt, size_t size);
  113. /** Print various representations of a floatnum. For debugging purposes only.
  114. * \param f file
  115. * \param flt floatnum
  116. */
  117. YASM_LIB_DECL
  118. void yasm_floatnum_print(const yasm_floatnum *flt, FILE *f);
  119. #endif