vfp.h 9.3 KB

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