vfp.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * linux/arch/arm/vfp/vfp.h
  3. *
  4. * Copyright (C) 2004 ARM Limited.
  5. * Written by Deep Blue Solutions Limited.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. static inline u32 vfp_shiftright32jamming(u32 val, unsigned int shift)
  12. {
  13. if (shift) {
  14. if (shift < 32)
  15. val = val >> shift | ((val << (32 - shift)) != 0);
  16. else
  17. val = val != 0;
  18. }
  19. return val;
  20. }
  21. static inline u64 vfp_shiftright64jamming(u64 val, unsigned int shift)
  22. {
  23. if (shift) {
  24. if (shift < 64)
  25. val = val >> shift | ((val << (64 - shift)) != 0);
  26. else
  27. val = val != 0;
  28. }
  29. return val;
  30. }
  31. static inline u32 vfp_hi64to32jamming(u64 val)
  32. {
  33. u32 v;
  34. asm(
  35. "cmp %Q1, #1 @ vfp_hi64to32jamming\n\t"
  36. "movcc %0, %R1\n\t"
  37. "orrcs %0, %R1, #1"
  38. : "=r" (v) : "r" (val) : "cc");
  39. return v;
  40. }
  41. static inline void add128(u64 *resh, u64 *resl, u64 nh, u64 nl, u64 mh, u64 ml)
  42. {
  43. asm( "adds %Q0, %Q2, %Q4\n\t"
  44. "adcs %R0, %R2, %R4\n\t"
  45. "adcs %Q1, %Q3, %Q5\n\t"
  46. "adc %R1, %R3, %R5"
  47. : "=r" (nl), "=r" (nh)
  48. : "0" (nl), "1" (nh), "r" (ml), "r" (mh)
  49. : "cc");
  50. *resh = nh;
  51. *resl = nl;
  52. }
  53. static inline void sub128(u64 *resh, u64 *resl, u64 nh, u64 nl, u64 mh, u64 ml)
  54. {
  55. asm( "subs %Q0, %Q2, %Q4\n\t"
  56. "sbcs %R0, %R2, %R4\n\t"
  57. "sbcs %Q1, %Q3, %Q5\n\t"
  58. "sbc %R1, %R3, %R5\n\t"
  59. : "=r" (nl), "=r" (nh)
  60. : "0" (nl), "1" (nh), "r" (ml), "r" (mh)
  61. : "cc");
  62. *resh = nh;
  63. *resl = nl;
  64. }
  65. static inline void mul64to128(u64 *resh, u64 *resl, u64 n, u64 m)
  66. {
  67. u32 nh, nl, mh, ml;
  68. u64 rh, rma, rmb, rl;
  69. nl = n;
  70. ml = m;
  71. rl = (u64)nl * ml;
  72. nh = n >> 32;
  73. rma = (u64)nh * ml;
  74. mh = m >> 32;
  75. rmb = (u64)nl * mh;
  76. rma += rmb;
  77. rh = (u64)nh * mh;
  78. rh += ((u64)(rma < rmb) << 32) + (rma >> 32);
  79. rma <<= 32;
  80. rl += rma;
  81. rh += (rl < rma);
  82. *resl = rl;
  83. *resh = rh;
  84. }
  85. static inline void shift64left(u64 *resh, u64 *resl, u64 n)
  86. {
  87. *resh = n >> 63;
  88. *resl = n << 1;
  89. }
  90. static inline u64 vfp_hi64multiply64(u64 n, u64 m)
  91. {
  92. u64 rh, rl;
  93. mul64to128(&rh, &rl, n, m);
  94. return rh | (rl != 0);
  95. }
  96. static inline u64 vfp_estimate_div128to64(u64 nh, u64 nl, u64 m)
  97. {
  98. u64 mh, ml, remh, reml, termh, terml, z;
  99. if (nh >= m)
  100. return ~0ULL;
  101. mh = m >> 32;
  102. z = (mh << 32 <= nh) ? 0xffffffff00000000ULL : (nh / mh) << 32;
  103. mul64to128(&termh, &terml, m, z);
  104. sub128(&remh, &reml, nh, nl, termh, terml);
  105. ml = m << 32;
  106. while ((s64)remh < 0) {
  107. z -= 0x100000000ULL;
  108. add128(&remh, &reml, remh, reml, mh, ml);
  109. }
  110. remh = (remh << 32) | (reml >> 32);
  111. z |= (mh << 32 <= remh) ? 0xffffffff : remh / mh;
  112. return z;
  113. }
  114. /*
  115. * Operations on unpacked elements
  116. */
  117. #define vfp_sign_negate(sign) (sign ^ 0x8000)
  118. /*
  119. * Single-precision
  120. */
  121. struct vfp_single {
  122. s16 exponent;
  123. u16 sign;
  124. u32 significand;
  125. };
  126. extern s32 vfp_get_float(unsigned int reg);
  127. extern void vfp_put_float(unsigned int reg, s32 val);
  128. /*
  129. * VFP_SINGLE_MANTISSA_BITS - number of bits in the mantissa
  130. * VFP_SINGLE_EXPONENT_BITS - number of bits in the exponent
  131. * VFP_SINGLE_LOW_BITS - number of low bits in the unpacked significand
  132. * which are not propagated to the float upon packing.
  133. */
  134. #define VFP_SINGLE_MANTISSA_BITS (23)
  135. #define VFP_SINGLE_EXPONENT_BITS (8)
  136. #define VFP_SINGLE_LOW_BITS (32 - VFP_SINGLE_MANTISSA_BITS - 2)
  137. #define VFP_SINGLE_LOW_BITS_MASK ((1 << VFP_SINGLE_LOW_BITS) - 1)
  138. /*
  139. * The bit in an unpacked float which indicates that it is a quiet NaN
  140. */
  141. #define VFP_SINGLE_SIGNIFICAND_QNAN (1 << (VFP_SINGLE_MANTISSA_BITS - 1 + VFP_SINGLE_LOW_BITS))
  142. /*
  143. * Operations on packed single-precision numbers
  144. */
  145. #define vfp_single_packed_sign(v) ((v) & 0x80000000)
  146. #define vfp_single_packed_negate(v) ((v) ^ 0x80000000)
  147. #define vfp_single_packed_abs(v) ((v) & ~0x80000000)
  148. #define vfp_single_packed_exponent(v) (((v) >> VFP_SINGLE_MANTISSA_BITS) & ((1 << VFP_SINGLE_EXPONENT_BITS) - 1))
  149. #define vfp_single_packed_mantissa(v) ((v) & ((1 << VFP_SINGLE_MANTISSA_BITS) - 1))
  150. /*
  151. * Unpack a single-precision float. Note that this returns the magnitude
  152. * of the single-precision float mantissa with the 1. if necessary,
  153. * aligned to bit 30.
  154. */
  155. static inline void vfp_single_unpack(struct vfp_single *s, s32 val)
  156. {
  157. u32 significand;
  158. s->sign = vfp_single_packed_sign(val) >> 16,
  159. s->exponent = vfp_single_packed_exponent(val);
  160. significand = (u32) val;
  161. significand = (significand << (32 - VFP_SINGLE_MANTISSA_BITS)) >> 2;
  162. if (s->exponent && s->exponent != 255)
  163. significand |= 0x40000000;
  164. s->significand = significand;
  165. }
  166. /*
  167. * Re-pack a single-precision float. This assumes that the float is
  168. * already normalised such that the MSB is bit 30, _not_ bit 31.
  169. */
  170. static inline s32 vfp_single_pack(struct vfp_single *s)
  171. {
  172. u32 val;
  173. val = (s->sign << 16) +
  174. (s->exponent << VFP_SINGLE_MANTISSA_BITS) +
  175. (s->significand >> VFP_SINGLE_LOW_BITS);
  176. return (s32)val;
  177. }
  178. #define VFP_NUMBER (1<<0)
  179. #define VFP_ZERO (1<<1)
  180. #define VFP_DENORMAL (1<<2)
  181. #define VFP_INFINITY (1<<3)
  182. #define VFP_NAN (1<<4)
  183. #define VFP_NAN_SIGNAL (1<<5)
  184. #define VFP_QNAN (VFP_NAN)
  185. #define VFP_SNAN (VFP_NAN|VFP_NAN_SIGNAL)
  186. static inline int vfp_single_type(struct vfp_single *s)
  187. {
  188. int type = VFP_NUMBER;
  189. if (s->exponent == 255) {
  190. if (s->significand == 0)
  191. type = VFP_INFINITY;
  192. else if (s->significand & VFP_SINGLE_SIGNIFICAND_QNAN)
  193. type = VFP_QNAN;
  194. else
  195. type = VFP_SNAN;
  196. } else if (s->exponent == 0) {
  197. if (s->significand == 0)
  198. type |= VFP_ZERO;
  199. else
  200. type |= VFP_DENORMAL;
  201. }
  202. return type;
  203. }
  204. #ifndef DEBUG
  205. #define vfp_single_normaliseround(sd,vsd,fpscr,except,func) __vfp_single_normaliseround(sd,vsd,fpscr,except)
  206. u32 __vfp_single_normaliseround(int sd, struct vfp_single *vs, u32 fpscr, u32 exceptions);
  207. #else
  208. u32 vfp_single_normaliseround(int sd, struct vfp_single *vs, u32 fpscr, u32 exceptions, const char *func);
  209. #endif
  210. /*
  211. * Double-precision
  212. */
  213. struct vfp_double {
  214. s16 exponent;
  215. u16 sign;
  216. u64 significand;
  217. };
  218. /*
  219. * VFP_REG_ZERO is a special register number for vfp_get_double
  220. * which returns (double)0.0. This is useful for the compare with
  221. * zero instructions.
  222. */
  223. #define VFP_REG_ZERO 16
  224. extern u64 vfp_get_double(unsigned int reg);
  225. extern void vfp_put_double(unsigned int reg, u64 val);
  226. #define VFP_DOUBLE_MANTISSA_BITS (52)
  227. #define VFP_DOUBLE_EXPONENT_BITS (11)
  228. #define VFP_DOUBLE_LOW_BITS (64 - VFP_DOUBLE_MANTISSA_BITS - 2)
  229. #define VFP_DOUBLE_LOW_BITS_MASK ((1 << VFP_DOUBLE_LOW_BITS) - 1)
  230. /*
  231. * The bit in an unpacked double which indicates that it is a quiet NaN
  232. */
  233. #define VFP_DOUBLE_SIGNIFICAND_QNAN (1ULL << (VFP_DOUBLE_MANTISSA_BITS - 1 + VFP_DOUBLE_LOW_BITS))
  234. /*
  235. * Operations on packed single-precision numbers
  236. */
  237. #define vfp_double_packed_sign(v) ((v) & (1ULL << 63))
  238. #define vfp_double_packed_negate(v) ((v) ^ (1ULL << 63))
  239. #define vfp_double_packed_abs(v) ((v) & ~(1ULL << 63))
  240. #define vfp_double_packed_exponent(v) (((v) >> VFP_DOUBLE_MANTISSA_BITS) & ((1 << VFP_DOUBLE_EXPONENT_BITS) - 1))
  241. #define vfp_double_packed_mantissa(v) ((v) & ((1ULL << VFP_DOUBLE_MANTISSA_BITS) - 1))
  242. /*
  243. * Unpack a double-precision float. Note that this returns the magnitude
  244. * of the double-precision float mantissa with the 1. if necessary,
  245. * aligned to bit 62.
  246. */
  247. static inline void vfp_double_unpack(struct vfp_double *s, s64 val)
  248. {
  249. u64 significand;
  250. s->sign = vfp_double_packed_sign(val) >> 48;
  251. s->exponent = vfp_double_packed_exponent(val);
  252. significand = (u64) val;
  253. significand = (significand << (64 - VFP_DOUBLE_MANTISSA_BITS)) >> 2;
  254. if (s->exponent && s->exponent != 2047)
  255. significand |= (1ULL << 62);
  256. s->significand = significand;
  257. }
  258. /*
  259. * Re-pack a double-precision float. This assumes that the float is
  260. * already normalised such that the MSB is bit 30, _not_ bit 31.
  261. */
  262. static inline s64 vfp_double_pack(struct vfp_double *s)
  263. {
  264. u64 val;
  265. val = ((u64)s->sign << 48) +
  266. ((u64)s->exponent << VFP_DOUBLE_MANTISSA_BITS) +
  267. (s->significand >> VFP_DOUBLE_LOW_BITS);
  268. return (s64)val;
  269. }
  270. static inline int vfp_double_type(struct vfp_double *s)
  271. {
  272. int type = VFP_NUMBER;
  273. if (s->exponent == 2047) {
  274. if (s->significand == 0)
  275. type = VFP_INFINITY;
  276. else if (s->significand & VFP_DOUBLE_SIGNIFICAND_QNAN)
  277. type = VFP_QNAN;
  278. else
  279. type = VFP_SNAN;
  280. } else if (s->exponent == 0) {
  281. if (s->significand == 0)
  282. type |= VFP_ZERO;
  283. else
  284. type |= VFP_DENORMAL;
  285. }
  286. return type;
  287. }
  288. u32 vfp_double_normaliseround(int dd, struct vfp_double *vd, u32 fpscr, u32 exceptions, const char *func);
  289. /*
  290. * System registers
  291. */
  292. extern u32 vfp_get_sys(unsigned int reg);
  293. extern void vfp_put_sys(unsigned int reg, u32 val);
  294. u32 vfp_estimate_sqrt_significand(u32 exponent, u32 significand);
  295. /*
  296. * A special flag to tell the normalisation code not to normalise.
  297. */
  298. #define VFP_NAN_FLAG 0x100