fpu.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #ifndef __ASM_ALPHA_FPU_H
  2. #define __ASM_ALPHA_FPU_H
  3. #ifdef __KERNEL__
  4. #include <asm/special_insns.h>
  5. #endif
  6. /*
  7. * Alpha floating-point control register defines:
  8. */
  9. #define FPCR_DNOD (1UL<<47) /* denorm INV trap disable */
  10. #define FPCR_DNZ (1UL<<48) /* denorms to zero */
  11. #define FPCR_INVD (1UL<<49) /* invalid op disable (opt.) */
  12. #define FPCR_DZED (1UL<<50) /* division by zero disable (opt.) */
  13. #define FPCR_OVFD (1UL<<51) /* overflow disable (optional) */
  14. #define FPCR_INV (1UL<<52) /* invalid operation */
  15. #define FPCR_DZE (1UL<<53) /* division by zero */
  16. #define FPCR_OVF (1UL<<54) /* overflow */
  17. #define FPCR_UNF (1UL<<55) /* underflow */
  18. #define FPCR_INE (1UL<<56) /* inexact */
  19. #define FPCR_IOV (1UL<<57) /* integer overflow */
  20. #define FPCR_UNDZ (1UL<<60) /* underflow to zero (opt.) */
  21. #define FPCR_UNFD (1UL<<61) /* underflow disable (opt.) */
  22. #define FPCR_INED (1UL<<62) /* inexact disable (opt.) */
  23. #define FPCR_SUM (1UL<<63) /* summary bit */
  24. #define FPCR_DYN_SHIFT 58 /* first dynamic rounding mode bit */
  25. #define FPCR_DYN_CHOPPED (0x0UL << FPCR_DYN_SHIFT) /* towards 0 */
  26. #define FPCR_DYN_MINUS (0x1UL << FPCR_DYN_SHIFT) /* towards -INF */
  27. #define FPCR_DYN_NORMAL (0x2UL << FPCR_DYN_SHIFT) /* towards nearest */
  28. #define FPCR_DYN_PLUS (0x3UL << FPCR_DYN_SHIFT) /* towards +INF */
  29. #define FPCR_DYN_MASK (0x3UL << FPCR_DYN_SHIFT)
  30. #define FPCR_MASK 0xffff800000000000L
  31. /*
  32. * IEEE trap enables are implemented in software. These per-thread
  33. * bits are stored in the "ieee_state" field of "struct thread_info".
  34. * Thus, the bits are defined so as not to conflict with the
  35. * floating-point enable bit (which is architected). On top of that,
  36. * we want to make these bits compatible with OSF/1 so
  37. * ieee_set_fp_control() etc. can be implemented easily and
  38. * compatibly. The corresponding definitions are in
  39. * /usr/include/machine/fpu.h under OSF/1.
  40. */
  41. #define IEEE_TRAP_ENABLE_INV (1UL<<1) /* invalid op */
  42. #define IEEE_TRAP_ENABLE_DZE (1UL<<2) /* division by zero */
  43. #define IEEE_TRAP_ENABLE_OVF (1UL<<3) /* overflow */
  44. #define IEEE_TRAP_ENABLE_UNF (1UL<<4) /* underflow */
  45. #define IEEE_TRAP_ENABLE_INE (1UL<<5) /* inexact */
  46. #define IEEE_TRAP_ENABLE_DNO (1UL<<6) /* denorm */
  47. #define IEEE_TRAP_ENABLE_MASK (IEEE_TRAP_ENABLE_INV | IEEE_TRAP_ENABLE_DZE |\
  48. IEEE_TRAP_ENABLE_OVF | IEEE_TRAP_ENABLE_UNF |\
  49. IEEE_TRAP_ENABLE_INE | IEEE_TRAP_ENABLE_DNO)
  50. /* Denorm and Underflow flushing */
  51. #define IEEE_MAP_DMZ (1UL<<12) /* Map denorm inputs to zero */
  52. #define IEEE_MAP_UMZ (1UL<<13) /* Map underflowed outputs to zero */
  53. #define IEEE_MAP_MASK (IEEE_MAP_DMZ | IEEE_MAP_UMZ)
  54. /* status bits coming from fpcr: */
  55. #define IEEE_STATUS_INV (1UL<<17)
  56. #define IEEE_STATUS_DZE (1UL<<18)
  57. #define IEEE_STATUS_OVF (1UL<<19)
  58. #define IEEE_STATUS_UNF (1UL<<20)
  59. #define IEEE_STATUS_INE (1UL<<21)
  60. #define IEEE_STATUS_DNO (1UL<<22)
  61. #define IEEE_STATUS_MASK (IEEE_STATUS_INV | IEEE_STATUS_DZE | \
  62. IEEE_STATUS_OVF | IEEE_STATUS_UNF | \
  63. IEEE_STATUS_INE | IEEE_STATUS_DNO)
  64. #define IEEE_SW_MASK (IEEE_TRAP_ENABLE_MASK | \
  65. IEEE_STATUS_MASK | IEEE_MAP_MASK)
  66. #define IEEE_CURRENT_RM_SHIFT 32
  67. #define IEEE_CURRENT_RM_MASK (3UL<<IEEE_CURRENT_RM_SHIFT)
  68. #define IEEE_STATUS_TO_EXCSUM_SHIFT 16
  69. #define IEEE_INHERIT (1UL<<63) /* inherit on thread create? */
  70. /*
  71. * Convert the software IEEE trap enable and status bits into the
  72. * hardware fpcr format.
  73. *
  74. * Digital Unix engineers receive my thanks for not defining the
  75. * software bits identical to the hardware bits. The chip designers
  76. * receive my thanks for making all the not-implemented fpcr bits
  77. * RAZ forcing us to use system calls to read/write this value.
  78. */
  79. static inline unsigned long
  80. ieee_swcr_to_fpcr(unsigned long sw)
  81. {
  82. unsigned long fp;
  83. fp = (sw & IEEE_STATUS_MASK) << 35;
  84. fp |= (sw & IEEE_MAP_DMZ) << 36;
  85. fp |= (sw & IEEE_STATUS_MASK ? FPCR_SUM : 0);
  86. fp |= (~sw & (IEEE_TRAP_ENABLE_INV
  87. | IEEE_TRAP_ENABLE_DZE
  88. | IEEE_TRAP_ENABLE_OVF)) << 48;
  89. fp |= (~sw & (IEEE_TRAP_ENABLE_UNF | IEEE_TRAP_ENABLE_INE)) << 57;
  90. fp |= (sw & IEEE_MAP_UMZ ? FPCR_UNDZ | FPCR_UNFD : 0);
  91. fp |= (~sw & IEEE_TRAP_ENABLE_DNO) << 41;
  92. return fp;
  93. }
  94. static inline unsigned long
  95. ieee_fpcr_to_swcr(unsigned long fp)
  96. {
  97. unsigned long sw;
  98. sw = (fp >> 35) & IEEE_STATUS_MASK;
  99. sw |= (fp >> 36) & IEEE_MAP_DMZ;
  100. sw |= (~fp >> 48) & (IEEE_TRAP_ENABLE_INV
  101. | IEEE_TRAP_ENABLE_DZE
  102. | IEEE_TRAP_ENABLE_OVF);
  103. sw |= (~fp >> 57) & (IEEE_TRAP_ENABLE_UNF | IEEE_TRAP_ENABLE_INE);
  104. sw |= (fp >> 47) & IEEE_MAP_UMZ;
  105. sw |= (~fp >> 41) & IEEE_TRAP_ENABLE_DNO;
  106. return sw;
  107. }
  108. #ifdef __KERNEL__
  109. /* The following two functions don't need trapb/excb instructions
  110. around the mf_fpcr/mt_fpcr instructions because (a) the kernel
  111. never generates arithmetic faults and (b) call_pal instructions
  112. are implied trap barriers. */
  113. static inline unsigned long
  114. rdfpcr(void)
  115. {
  116. unsigned long tmp, ret;
  117. #if defined(CONFIG_ALPHA_EV6) || defined(CONFIG_ALPHA_EV67)
  118. __asm__ __volatile__ (
  119. "ftoit $f0,%0\n\t"
  120. "mf_fpcr $f0\n\t"
  121. "ftoit $f0,%1\n\t"
  122. "itoft %0,$f0"
  123. : "=r"(tmp), "=r"(ret));
  124. #else
  125. __asm__ __volatile__ (
  126. "stt $f0,%0\n\t"
  127. "mf_fpcr $f0\n\t"
  128. "stt $f0,%1\n\t"
  129. "ldt $f0,%0"
  130. : "=m"(tmp), "=m"(ret));
  131. #endif
  132. return ret;
  133. }
  134. static inline void
  135. wrfpcr(unsigned long val)
  136. {
  137. unsigned long tmp;
  138. #if defined(CONFIG_ALPHA_EV6) || defined(CONFIG_ALPHA_EV67)
  139. __asm__ __volatile__ (
  140. "ftoit $f0,%0\n\t"
  141. "itoft %1,$f0\n\t"
  142. "mt_fpcr $f0\n\t"
  143. "itoft %0,$f0"
  144. : "=&r"(tmp) : "r"(val));
  145. #else
  146. __asm__ __volatile__ (
  147. "stt $f0,%0\n\t"
  148. "ldt $f0,%1\n\t"
  149. "mt_fpcr $f0\n\t"
  150. "ldt $f0,%0"
  151. : "=m"(tmp) : "m"(val));
  152. #endif
  153. }
  154. static inline unsigned long
  155. swcr_update_status(unsigned long swcr, unsigned long fpcr)
  156. {
  157. /* EV6 implements most of the bits in hardware. Collect
  158. the acrued exception bits from the real fpcr. */
  159. if (implver() == IMPLVER_EV6) {
  160. swcr &= ~IEEE_STATUS_MASK;
  161. swcr |= (fpcr >> 35) & IEEE_STATUS_MASK;
  162. }
  163. return swcr;
  164. }
  165. extern unsigned long alpha_read_fp_reg (unsigned long reg);
  166. extern void alpha_write_fp_reg (unsigned long reg, unsigned long val);
  167. extern unsigned long alpha_read_fp_reg_s (unsigned long reg);
  168. extern void alpha_write_fp_reg_s (unsigned long reg, unsigned long val);
  169. #endif /* __KERNEL__ */
  170. #endif /* __ASM_ALPHA_FPU_H */