fp_emu.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * fp_emu.h
  3. *
  4. * Copyright Roman Zippel, 1997. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, and the entire permission notice in its entirety,
  11. * including the disclaimer of warranties.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. The name of the author may not be used to endorse or promote
  16. * products derived from this software without specific prior
  17. * written permission.
  18. *
  19. * ALTERNATIVELY, this product may be distributed under the terms of
  20. * the GNU General Public License, in which case the provisions of the GPL are
  21. * required INSTEAD OF the above restrictions. (This clause is
  22. * necessary due to a potential bad interaction between the GPL and
  23. * the restrictions contained in a BSD-style copyright.)
  24. *
  25. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  26. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  27. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  29. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  30. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  33. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  35. * OF THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #ifndef _FP_EMU_H
  38. #define _FP_EMU_H
  39. #ifdef __ASSEMBLY__
  40. #include <asm/asm-offsets.h>
  41. #endif
  42. #include <asm/math-emu.h>
  43. #ifndef __ASSEMBLY__
  44. #define IS_INF(a) ((a)->exp == 0x7fff)
  45. #define IS_ZERO(a) ((a)->mant.m64 == 0)
  46. #define fp_set_sr(bit) ({ \
  47. FPDATA->fpsr |= 1 << (bit); \
  48. })
  49. #define fp_set_quotient(quotient) ({ \
  50. FPDATA->fpsr &= 0xff00ffff; \
  51. FPDATA->fpsr |= ((quotient) & 0xff) << 16; \
  52. })
  53. /* linkage for several useful functions */
  54. /* Normalize the extended struct, return 0 for a NaN */
  55. #define fp_normalize_ext(fpreg) ({ \
  56. register struct fp_ext *reg asm ("a0") = fpreg; \
  57. register int res asm ("d0"); \
  58. \
  59. asm volatile ("jsr fp_conv_ext2ext" \
  60. : "=d" (res) : "a" (reg) \
  61. : "a1", "d1", "d2", "memory"); \
  62. res; \
  63. })
  64. #define fp_copy_ext(dest, src) ({ \
  65. *dest = *src; \
  66. })
  67. #define fp_monadic_check(dest, src) ({ \
  68. fp_copy_ext(dest, src); \
  69. if (!fp_normalize_ext(dest)) \
  70. return dest; \
  71. })
  72. #define fp_dyadic_check(dest, src) ({ \
  73. if (!fp_normalize_ext(dest)) \
  74. return dest; \
  75. if (!fp_normalize_ext(src)) { \
  76. fp_copy_ext(dest, src); \
  77. return dest; \
  78. } \
  79. })
  80. extern const struct fp_ext fp_QNaN;
  81. extern const struct fp_ext fp_Inf;
  82. #define fp_set_nan(dest) ({ \
  83. fp_set_sr(FPSR_EXC_OPERR); \
  84. *dest = fp_QNaN; \
  85. })
  86. /* TODO check rounding mode? */
  87. #define fp_set_ovrflw(dest) ({ \
  88. fp_set_sr(FPSR_EXC_OVFL); \
  89. dest->exp = 0x7fff; \
  90. dest->mant.m64 = 0; \
  91. })
  92. #define fp_conv_ext2long(src) ({ \
  93. register struct fp_ext *__src asm ("a0") = src; \
  94. register int __res asm ("d0"); \
  95. \
  96. asm volatile ("jsr fp_conv_ext2long" \
  97. : "=d" (__res) : "a" (__src) \
  98. : "a1", "d1", "d2", "memory"); \
  99. __res; \
  100. })
  101. #define fp_conv_long2ext(dest, src) ({ \
  102. register struct fp_ext *__dest asm ("a0") = dest; \
  103. register int __src asm ("d0") = src; \
  104. \
  105. asm volatile ("jsr fp_conv_ext2long" \
  106. : : "d" (__src), "a" (__dest) \
  107. : "a1", "d1", "d2", "memory"); \
  108. })
  109. #else /* __ASSEMBLY__ */
  110. /*
  111. * set, reset or clear a bit in the fp status register
  112. */
  113. .macro fp_set_sr bit
  114. bset #(\bit&7),(FPD_FPSR+3-(\bit/8),FPDATA)
  115. .endm
  116. .macro fp_clr_sr bit
  117. bclr #(\bit&7),(FPD_FPSR+3-(\bit/8),FPDATA)
  118. .endm
  119. .macro fp_tst_sr bit
  120. btst #(\bit&7),(FPD_FPSR+3-(\bit/8),FPDATA)
  121. .endm
  122. #endif /* __ASSEMBLY__ */
  123. #endif /* _FP_EMU_H */