vfpdouble.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. /*
  2. * linux/arch/arm/vfp/vfpdouble.c
  3. *
  4. * This code is derived in part from John R. Housers softfloat library, which
  5. * carries the following notice:
  6. *
  7. * ===========================================================================
  8. * This C source file is part of the SoftFloat IEC/IEEE Floating-point
  9. * Arithmetic Package, Release 2.
  10. *
  11. * Written by John R. Hauser. This work was made possible in part by the
  12. * International Computer Science Institute, located at Suite 600, 1947 Center
  13. * Street, Berkeley, California 94704. Funding was partially provided by the
  14. * National Science Foundation under grant MIP-9311980. The original version
  15. * of this code was written as part of a project to build a fixed-point vector
  16. * processor in collaboration with the University of California at Berkeley,
  17. * overseen by Profs. Nelson Morgan and John Wawrzynek. More information
  18. * is available through the web page `http://HTTP.CS.Berkeley.EDU/~jhauser/
  19. * arithmetic/softfloat.html'.
  20. *
  21. * THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort
  22. * has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
  23. * TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO
  24. * PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
  25. * AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
  26. *
  27. * Derivative works are acceptable, even for commercial purposes, so long as
  28. * (1) they include prominent notice that the work is derivative, and (2) they
  29. * include prominent notice akin to these three paragraphs for those parts of
  30. * this code that are retained.
  31. * ===========================================================================
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/bitops.h>
  35. #include <asm/ptrace.h>
  36. #include <asm/vfp.h>
  37. #include "vfpinstr.h"
  38. #include "vfp.h"
  39. static struct vfp_double vfp_double_default_qnan = {
  40. .exponent = 2047,
  41. .sign = 0,
  42. .significand = VFP_DOUBLE_SIGNIFICAND_QNAN,
  43. };
  44. static void vfp_double_dump(const char *str, struct vfp_double *d)
  45. {
  46. pr_debug("VFP: %s: sign=%d exponent=%d significand=%016llx\n",
  47. str, d->sign != 0, d->exponent, d->significand);
  48. }
  49. static void vfp_double_normalise_denormal(struct vfp_double *vd)
  50. {
  51. int bits = 31 - fls(vd->significand >> 32);
  52. if (bits == 31)
  53. bits = 62 - fls(vd->significand);
  54. vfp_double_dump("normalise_denormal: in", vd);
  55. if (bits) {
  56. vd->exponent -= bits - 1;
  57. vd->significand <<= bits;
  58. }
  59. vfp_double_dump("normalise_denormal: out", vd);
  60. }
  61. u32 vfp_double_normaliseround(int dd, struct vfp_double *vd, u32 fpscr, u32 exceptions, const char *func)
  62. {
  63. u64 significand, incr;
  64. int exponent, shift, underflow;
  65. u32 rmode;
  66. vfp_double_dump("pack: in", vd);
  67. /*
  68. * Infinities and NaNs are a special case.
  69. */
  70. if (vd->exponent == 2047 && (vd->significand == 0 || exceptions))
  71. goto pack;
  72. /*
  73. * Special-case zero.
  74. */
  75. if (vd->significand == 0) {
  76. vd->exponent = 0;
  77. goto pack;
  78. }
  79. exponent = vd->exponent;
  80. significand = vd->significand;
  81. shift = 32 - fls(significand >> 32);
  82. if (shift == 32)
  83. shift = 64 - fls(significand);
  84. if (shift) {
  85. exponent -= shift;
  86. significand <<= shift;
  87. }
  88. #ifdef DEBUG
  89. vd->exponent = exponent;
  90. vd->significand = significand;
  91. vfp_double_dump("pack: normalised", vd);
  92. #endif
  93. /*
  94. * Tiny number?
  95. */
  96. underflow = exponent < 0;
  97. if (underflow) {
  98. significand = vfp_shiftright64jamming(significand, -exponent);
  99. exponent = 0;
  100. #ifdef DEBUG
  101. vd->exponent = exponent;
  102. vd->significand = significand;
  103. vfp_double_dump("pack: tiny number", vd);
  104. #endif
  105. if (!(significand & ((1ULL << (VFP_DOUBLE_LOW_BITS + 1)) - 1)))
  106. underflow = 0;
  107. }
  108. /*
  109. * Select rounding increment.
  110. */
  111. incr = 0;
  112. rmode = fpscr & FPSCR_RMODE_MASK;
  113. if (rmode == FPSCR_ROUND_NEAREST) {
  114. incr = 1ULL << VFP_DOUBLE_LOW_BITS;
  115. if ((significand & (1ULL << (VFP_DOUBLE_LOW_BITS + 1))) == 0)
  116. incr -= 1;
  117. } else if (rmode == FPSCR_ROUND_TOZERO) {
  118. incr = 0;
  119. } else if ((rmode == FPSCR_ROUND_PLUSINF) ^ (vd->sign != 0))
  120. incr = (1ULL << (VFP_DOUBLE_LOW_BITS + 1)) - 1;
  121. pr_debug("VFP: rounding increment = 0x%08llx\n", incr);
  122. /*
  123. * Is our rounding going to overflow?
  124. */
  125. if ((significand + incr) < significand) {
  126. exponent += 1;
  127. significand = (significand >> 1) | (significand & 1);
  128. incr >>= 1;
  129. #ifdef DEBUG
  130. vd->exponent = exponent;
  131. vd->significand = significand;
  132. vfp_double_dump("pack: overflow", vd);
  133. #endif
  134. }
  135. /*
  136. * If any of the low bits (which will be shifted out of the
  137. * number) are non-zero, the result is inexact.
  138. */
  139. if (significand & ((1 << (VFP_DOUBLE_LOW_BITS + 1)) - 1))
  140. exceptions |= FPSCR_IXC;
  141. /*
  142. * Do our rounding.
  143. */
  144. significand += incr;
  145. /*
  146. * Infinity?
  147. */
  148. if (exponent >= 2046) {
  149. exceptions |= FPSCR_OFC | FPSCR_IXC;
  150. if (incr == 0) {
  151. vd->exponent = 2045;
  152. vd->significand = 0x7fffffffffffffffULL;
  153. } else {
  154. vd->exponent = 2047; /* infinity */
  155. vd->significand = 0;
  156. }
  157. } else {
  158. if (significand >> (VFP_DOUBLE_LOW_BITS + 1) == 0)
  159. exponent = 0;
  160. if (exponent || significand > 0x8000000000000000ULL)
  161. underflow = 0;
  162. if (underflow)
  163. exceptions |= FPSCR_UFC;
  164. vd->exponent = exponent;
  165. vd->significand = significand >> 1;
  166. }
  167. pack:
  168. vfp_double_dump("pack: final", vd);
  169. {
  170. s64 d = vfp_double_pack(vd);
  171. pr_debug("VFP: %s: d(d%d)=%016llx exceptions=%08x\n", func,
  172. dd, d, exceptions);
  173. vfp_put_double(dd, d);
  174. }
  175. return exceptions & ~VFP_NAN_FLAG;
  176. }
  177. /*
  178. * Propagate the NaN, setting exceptions if it is signalling.
  179. * 'n' is always a NaN. 'm' may be a number, NaN or infinity.
  180. */
  181. static u32
  182. vfp_propagate_nan(struct vfp_double *vdd, struct vfp_double *vdn,
  183. struct vfp_double *vdm, u32 fpscr)
  184. {
  185. struct vfp_double *nan;
  186. int tn, tm = 0;
  187. tn = vfp_double_type(vdn);
  188. if (vdm)
  189. tm = vfp_double_type(vdm);
  190. if (fpscr & FPSCR_DEFAULT_NAN)
  191. /*
  192. * Default NaN mode - always returns a quiet NaN
  193. */
  194. nan = &vfp_double_default_qnan;
  195. else {
  196. /*
  197. * Contemporary mode - select the first signalling
  198. * NAN, or if neither are signalling, the first
  199. * quiet NAN.
  200. */
  201. if (tn == VFP_SNAN || (tm != VFP_SNAN && tn == VFP_QNAN))
  202. nan = vdn;
  203. else
  204. nan = vdm;
  205. /*
  206. * Make the NaN quiet.
  207. */
  208. nan->significand |= VFP_DOUBLE_SIGNIFICAND_QNAN;
  209. }
  210. *vdd = *nan;
  211. /*
  212. * If one was a signalling NAN, raise invalid operation.
  213. */
  214. return tn == VFP_SNAN || tm == VFP_SNAN ? FPSCR_IOC : VFP_NAN_FLAG;
  215. }
  216. /*
  217. * Extended operations
  218. */
  219. static u32 vfp_double_fabs(int dd, int unused, int dm, u32 fpscr)
  220. {
  221. vfp_put_double(dd, vfp_double_packed_abs(vfp_get_double(dm)));
  222. return 0;
  223. }
  224. static u32 vfp_double_fcpy(int dd, int unused, int dm, u32 fpscr)
  225. {
  226. vfp_put_double(dd, vfp_get_double(dm));
  227. return 0;
  228. }
  229. static u32 vfp_double_fneg(int dd, int unused, int dm, u32 fpscr)
  230. {
  231. vfp_put_double(dd, vfp_double_packed_negate(vfp_get_double(dm)));
  232. return 0;
  233. }
  234. static u32 vfp_double_fsqrt(int dd, int unused, int dm, u32 fpscr)
  235. {
  236. struct vfp_double vdm, vdd;
  237. int ret, tm;
  238. vfp_double_unpack(&vdm, vfp_get_double(dm));
  239. tm = vfp_double_type(&vdm);
  240. if (tm & (VFP_NAN|VFP_INFINITY)) {
  241. struct vfp_double *vdp = &vdd;
  242. if (tm & VFP_NAN)
  243. ret = vfp_propagate_nan(vdp, &vdm, NULL, fpscr);
  244. else if (vdm.sign == 0) {
  245. sqrt_copy:
  246. vdp = &vdm;
  247. ret = 0;
  248. } else {
  249. sqrt_invalid:
  250. vdp = &vfp_double_default_qnan;
  251. ret = FPSCR_IOC;
  252. }
  253. vfp_put_double(dd, vfp_double_pack(vdp));
  254. return ret;
  255. }
  256. /*
  257. * sqrt(+/- 0) == +/- 0
  258. */
  259. if (tm & VFP_ZERO)
  260. goto sqrt_copy;
  261. /*
  262. * Normalise a denormalised number
  263. */
  264. if (tm & VFP_DENORMAL)
  265. vfp_double_normalise_denormal(&vdm);
  266. /*
  267. * sqrt(<0) = invalid
  268. */
  269. if (vdm.sign)
  270. goto sqrt_invalid;
  271. vfp_double_dump("sqrt", &vdm);
  272. /*
  273. * Estimate the square root.
  274. */
  275. vdd.sign = 0;
  276. vdd.exponent = ((vdm.exponent - 1023) >> 1) + 1023;
  277. vdd.significand = (u64)vfp_estimate_sqrt_significand(vdm.exponent, vdm.significand >> 32) << 31;
  278. vfp_double_dump("sqrt estimate1", &vdd);
  279. vdm.significand >>= 1 + (vdm.exponent & 1);
  280. vdd.significand += 2 + vfp_estimate_div128to64(vdm.significand, 0, vdd.significand);
  281. vfp_double_dump("sqrt estimate2", &vdd);
  282. /*
  283. * And now adjust.
  284. */
  285. if ((vdd.significand & VFP_DOUBLE_LOW_BITS_MASK) <= 5) {
  286. if (vdd.significand < 2) {
  287. vdd.significand = ~0ULL;
  288. } else {
  289. u64 termh, terml, remh, reml;
  290. vdm.significand <<= 2;
  291. mul64to128(&termh, &terml, vdd.significand, vdd.significand);
  292. sub128(&remh, &reml, vdm.significand, 0, termh, terml);
  293. while ((s64)remh < 0) {
  294. vdd.significand -= 1;
  295. shift64left(&termh, &terml, vdd.significand);
  296. terml |= 1;
  297. add128(&remh, &reml, remh, reml, termh, terml);
  298. }
  299. vdd.significand |= (remh | reml) != 0;
  300. }
  301. }
  302. vdd.significand = vfp_shiftright64jamming(vdd.significand, 1);
  303. return vfp_double_normaliseround(dd, &vdd, fpscr, 0, "fsqrt");
  304. }
  305. /*
  306. * Equal := ZC
  307. * Less than := N
  308. * Greater than := C
  309. * Unordered := CV
  310. */
  311. static u32 vfp_compare(int dd, int signal_on_qnan, int dm, u32 fpscr)
  312. {
  313. s64 d, m;
  314. u32 ret = 0;
  315. m = vfp_get_double(dm);
  316. if (vfp_double_packed_exponent(m) == 2047 && vfp_double_packed_mantissa(m)) {
  317. ret |= FPSCR_C | FPSCR_V;
  318. if (signal_on_qnan || !(vfp_double_packed_mantissa(m) & (1ULL << (VFP_DOUBLE_MANTISSA_BITS - 1))))
  319. /*
  320. * Signalling NaN, or signalling on quiet NaN
  321. */
  322. ret |= FPSCR_IOC;
  323. }
  324. d = vfp_get_double(dd);
  325. if (vfp_double_packed_exponent(d) == 2047 && vfp_double_packed_mantissa(d)) {
  326. ret |= FPSCR_C | FPSCR_V;
  327. if (signal_on_qnan || !(vfp_double_packed_mantissa(d) & (1ULL << (VFP_DOUBLE_MANTISSA_BITS - 1))))
  328. /*
  329. * Signalling NaN, or signalling on quiet NaN
  330. */
  331. ret |= FPSCR_IOC;
  332. }
  333. if (ret == 0) {
  334. if (d == m || vfp_double_packed_abs(d | m) == 0) {
  335. /*
  336. * equal
  337. */
  338. ret |= FPSCR_Z | FPSCR_C;
  339. } else if (vfp_double_packed_sign(d ^ m)) {
  340. /*
  341. * different signs
  342. */
  343. if (vfp_double_packed_sign(d))
  344. /*
  345. * d is negative, so d < m
  346. */
  347. ret |= FPSCR_N;
  348. else
  349. /*
  350. * d is positive, so d > m
  351. */
  352. ret |= FPSCR_C;
  353. } else if ((vfp_double_packed_sign(d) != 0) ^ (d < m)) {
  354. /*
  355. * d < m
  356. */
  357. ret |= FPSCR_N;
  358. } else if ((vfp_double_packed_sign(d) != 0) ^ (d > m)) {
  359. /*
  360. * d > m
  361. */
  362. ret |= FPSCR_C;
  363. }
  364. }
  365. return ret;
  366. }
  367. static u32 vfp_double_fcmp(int dd, int unused, int dm, u32 fpscr)
  368. {
  369. return vfp_compare(dd, 0, dm, fpscr);
  370. }
  371. static u32 vfp_double_fcmpe(int dd, int unused, int dm, u32 fpscr)
  372. {
  373. return vfp_compare(dd, 1, dm, fpscr);
  374. }
  375. static u32 vfp_double_fcmpz(int dd, int unused, int dm, u32 fpscr)
  376. {
  377. return vfp_compare(dd, 0, VFP_REG_ZERO, fpscr);
  378. }
  379. static u32 vfp_double_fcmpez(int dd, int unused, int dm, u32 fpscr)
  380. {
  381. return vfp_compare(dd, 1, VFP_REG_ZERO, fpscr);
  382. }
  383. static u32 vfp_double_fcvts(int sd, int unused, int dm, u32 fpscr)
  384. {
  385. struct vfp_double vdm;
  386. struct vfp_single vsd;
  387. int tm;
  388. u32 exceptions = 0;
  389. vfp_double_unpack(&vdm, vfp_get_double(dm));
  390. tm = vfp_double_type(&vdm);
  391. /*
  392. * If we have a signalling NaN, signal invalid operation.
  393. */
  394. if (tm == VFP_SNAN)
  395. exceptions = FPSCR_IOC;
  396. if (tm & VFP_DENORMAL)
  397. vfp_double_normalise_denormal(&vdm);
  398. vsd.sign = vdm.sign;
  399. vsd.significand = vfp_hi64to32jamming(vdm.significand);
  400. /*
  401. * If we have an infinity or a NaN, the exponent must be 255
  402. */
  403. if (tm & (VFP_INFINITY|VFP_NAN)) {
  404. vsd.exponent = 255;
  405. if (tm & VFP_NAN)
  406. vsd.significand |= VFP_SINGLE_SIGNIFICAND_QNAN;
  407. goto pack_nan;
  408. } else if (tm & VFP_ZERO)
  409. vsd.exponent = 0;
  410. else
  411. vsd.exponent = vdm.exponent - (1023 - 127);
  412. return vfp_single_normaliseround(sd, &vsd, fpscr, exceptions, "fcvts");
  413. pack_nan:
  414. vfp_put_float(sd, vfp_single_pack(&vsd));
  415. return exceptions;
  416. }
  417. static u32 vfp_double_fuito(int dd, int unused, int dm, u32 fpscr)
  418. {
  419. struct vfp_double vdm;
  420. u32 m = vfp_get_float(dm);
  421. vdm.sign = 0;
  422. vdm.exponent = 1023 + 63 - 1;
  423. vdm.significand = (u64)m;
  424. return vfp_double_normaliseround(dd, &vdm, fpscr, 0, "fuito");
  425. }
  426. static u32 vfp_double_fsito(int dd, int unused, int dm, u32 fpscr)
  427. {
  428. struct vfp_double vdm;
  429. u32 m = vfp_get_float(dm);
  430. vdm.sign = (m & 0x80000000) >> 16;
  431. vdm.exponent = 1023 + 63 - 1;
  432. vdm.significand = vdm.sign ? -m : m;
  433. return vfp_double_normaliseround(dd, &vdm, fpscr, 0, "fsito");
  434. }
  435. static u32 vfp_double_ftoui(int sd, int unused, int dm, u32 fpscr)
  436. {
  437. struct vfp_double vdm;
  438. u32 d, exceptions = 0;
  439. int rmode = fpscr & FPSCR_RMODE_MASK;
  440. int tm;
  441. vfp_double_unpack(&vdm, vfp_get_double(dm));
  442. /*
  443. * Do we have a denormalised number?
  444. */
  445. tm = vfp_double_type(&vdm);
  446. if (tm & VFP_DENORMAL)
  447. exceptions |= FPSCR_IDC;
  448. if (tm & VFP_NAN)
  449. vdm.sign = 0;
  450. if (vdm.exponent >= 1023 + 32) {
  451. d = vdm.sign ? 0 : 0xffffffff;
  452. exceptions = FPSCR_IOC;
  453. } else if (vdm.exponent >= 1023 - 1) {
  454. int shift = 1023 + 63 - vdm.exponent;
  455. u64 rem, incr = 0;
  456. /*
  457. * 2^0 <= m < 2^32-2^8
  458. */
  459. d = (vdm.significand << 1) >> shift;
  460. rem = vdm.significand << (65 - shift);
  461. if (rmode == FPSCR_ROUND_NEAREST) {
  462. incr = 0x8000000000000000ULL;
  463. if ((d & 1) == 0)
  464. incr -= 1;
  465. } else if (rmode == FPSCR_ROUND_TOZERO) {
  466. incr = 0;
  467. } else if ((rmode == FPSCR_ROUND_PLUSINF) ^ (vdm.sign != 0)) {
  468. incr = ~0ULL;
  469. }
  470. if ((rem + incr) < rem) {
  471. if (d < 0xffffffff)
  472. d += 1;
  473. else
  474. exceptions |= FPSCR_IOC;
  475. }
  476. if (d && vdm.sign) {
  477. d = 0;
  478. exceptions |= FPSCR_IOC;
  479. } else if (rem)
  480. exceptions |= FPSCR_IXC;
  481. } else {
  482. d = 0;
  483. if (vdm.exponent | vdm.significand) {
  484. exceptions |= FPSCR_IXC;
  485. if (rmode == FPSCR_ROUND_PLUSINF && vdm.sign == 0)
  486. d = 1;
  487. else if (rmode == FPSCR_ROUND_MINUSINF && vdm.sign) {
  488. d = 0;
  489. exceptions |= FPSCR_IOC;
  490. }
  491. }
  492. }
  493. pr_debug("VFP: ftoui: d(s%d)=%08x exceptions=%08x\n", sd, d, exceptions);
  494. vfp_put_float(sd, d);
  495. return exceptions;
  496. }
  497. static u32 vfp_double_ftouiz(int sd, int unused, int dm, u32 fpscr)
  498. {
  499. return vfp_double_ftoui(sd, unused, dm, FPSCR_ROUND_TOZERO);
  500. }
  501. static u32 vfp_double_ftosi(int sd, int unused, int dm, u32 fpscr)
  502. {
  503. struct vfp_double vdm;
  504. u32 d, exceptions = 0;
  505. int rmode = fpscr & FPSCR_RMODE_MASK;
  506. vfp_double_unpack(&vdm, vfp_get_double(dm));
  507. vfp_double_dump("VDM", &vdm);
  508. /*
  509. * Do we have denormalised number?
  510. */
  511. if (vfp_double_type(&vdm) & VFP_DENORMAL)
  512. exceptions |= FPSCR_IDC;
  513. if (vdm.exponent >= 1023 + 32) {
  514. d = 0x7fffffff;
  515. if (vdm.sign)
  516. d = ~d;
  517. exceptions |= FPSCR_IOC;
  518. } else if (vdm.exponent >= 1023 - 1) {
  519. int shift = 1023 + 63 - vdm.exponent; /* 58 */
  520. u64 rem, incr = 0;
  521. d = (vdm.significand << 1) >> shift;
  522. rem = vdm.significand << (65 - shift);
  523. if (rmode == FPSCR_ROUND_NEAREST) {
  524. incr = 0x8000000000000000ULL;
  525. if ((d & 1) == 0)
  526. incr -= 1;
  527. } else if (rmode == FPSCR_ROUND_TOZERO) {
  528. incr = 0;
  529. } else if ((rmode == FPSCR_ROUND_PLUSINF) ^ (vdm.sign != 0)) {
  530. incr = ~0ULL;
  531. }
  532. if ((rem + incr) < rem && d < 0xffffffff)
  533. d += 1;
  534. if (d > 0x7fffffff + (vdm.sign != 0)) {
  535. d = 0x7fffffff + (vdm.sign != 0);
  536. exceptions |= FPSCR_IOC;
  537. } else if (rem)
  538. exceptions |= FPSCR_IXC;
  539. if (vdm.sign)
  540. d = -d;
  541. } else {
  542. d = 0;
  543. if (vdm.exponent | vdm.significand) {
  544. exceptions |= FPSCR_IXC;
  545. if (rmode == FPSCR_ROUND_PLUSINF && vdm.sign == 0)
  546. d = 1;
  547. else if (rmode == FPSCR_ROUND_MINUSINF && vdm.sign)
  548. d = -1;
  549. }
  550. }
  551. pr_debug("VFP: ftosi: d(s%d)=%08x exceptions=%08x\n", sd, d, exceptions);
  552. vfp_put_float(sd, (s32)d);
  553. return exceptions;
  554. }
  555. static u32 vfp_double_ftosiz(int dd, int unused, int dm, u32 fpscr)
  556. {
  557. return vfp_double_ftosi(dd, unused, dm, FPSCR_ROUND_TOZERO);
  558. }
  559. static u32 (* const fop_extfns[32])(int dd, int unused, int dm, u32 fpscr) = {
  560. [FEXT_TO_IDX(FEXT_FCPY)] = vfp_double_fcpy,
  561. [FEXT_TO_IDX(FEXT_FABS)] = vfp_double_fabs,
  562. [FEXT_TO_IDX(FEXT_FNEG)] = vfp_double_fneg,
  563. [FEXT_TO_IDX(FEXT_FSQRT)] = vfp_double_fsqrt,
  564. [FEXT_TO_IDX(FEXT_FCMP)] = vfp_double_fcmp,
  565. [FEXT_TO_IDX(FEXT_FCMPE)] = vfp_double_fcmpe,
  566. [FEXT_TO_IDX(FEXT_FCMPZ)] = vfp_double_fcmpz,
  567. [FEXT_TO_IDX(FEXT_FCMPEZ)] = vfp_double_fcmpez,
  568. [FEXT_TO_IDX(FEXT_FCVT)] = vfp_double_fcvts,
  569. [FEXT_TO_IDX(FEXT_FUITO)] = vfp_double_fuito,
  570. [FEXT_TO_IDX(FEXT_FSITO)] = vfp_double_fsito,
  571. [FEXT_TO_IDX(FEXT_FTOUI)] = vfp_double_ftoui,
  572. [FEXT_TO_IDX(FEXT_FTOUIZ)] = vfp_double_ftouiz,
  573. [FEXT_TO_IDX(FEXT_FTOSI)] = vfp_double_ftosi,
  574. [FEXT_TO_IDX(FEXT_FTOSIZ)] = vfp_double_ftosiz,
  575. };
  576. static u32
  577. vfp_double_fadd_nonnumber(struct vfp_double *vdd, struct vfp_double *vdn,
  578. struct vfp_double *vdm, u32 fpscr)
  579. {
  580. struct vfp_double *vdp;
  581. u32 exceptions = 0;
  582. int tn, tm;
  583. tn = vfp_double_type(vdn);
  584. tm = vfp_double_type(vdm);
  585. if (tn & tm & VFP_INFINITY) {
  586. /*
  587. * Two infinities. Are they different signs?
  588. */
  589. if (vdn->sign ^ vdm->sign) {
  590. /*
  591. * different signs -> invalid
  592. */
  593. exceptions = FPSCR_IOC;
  594. vdp = &vfp_double_default_qnan;
  595. } else {
  596. /*
  597. * same signs -> valid
  598. */
  599. vdp = vdn;
  600. }
  601. } else if (tn & VFP_INFINITY && tm & VFP_NUMBER) {
  602. /*
  603. * One infinity and one number -> infinity
  604. */
  605. vdp = vdn;
  606. } else {
  607. /*
  608. * 'n' is a NaN of some type
  609. */
  610. return vfp_propagate_nan(vdd, vdn, vdm, fpscr);
  611. }
  612. *vdd = *vdp;
  613. return exceptions;
  614. }
  615. static u32
  616. vfp_double_add(struct vfp_double *vdd, struct vfp_double *vdn,
  617. struct vfp_double *vdm, u32 fpscr)
  618. {
  619. u32 exp_diff;
  620. u64 m_sig;
  621. if (vdn->significand & (1ULL << 63) ||
  622. vdm->significand & (1ULL << 63)) {
  623. pr_info("VFP: bad FP values in %s\n", __func__);
  624. vfp_double_dump("VDN", vdn);
  625. vfp_double_dump("VDM", vdm);
  626. }
  627. /*
  628. * Ensure that 'n' is the largest magnitude number. Note that
  629. * if 'n' and 'm' have equal exponents, we do not swap them.
  630. * This ensures that NaN propagation works correctly.
  631. */
  632. if (vdn->exponent < vdm->exponent) {
  633. struct vfp_double *t = vdn;
  634. vdn = vdm;
  635. vdm = t;
  636. }
  637. /*
  638. * Is 'n' an infinity or a NaN? Note that 'm' may be a number,
  639. * infinity or a NaN here.
  640. */
  641. if (vdn->exponent == 2047)
  642. return vfp_double_fadd_nonnumber(vdd, vdn, vdm, fpscr);
  643. /*
  644. * We have two proper numbers, where 'vdn' is the larger magnitude.
  645. *
  646. * Copy 'n' to 'd' before doing the arithmetic.
  647. */
  648. *vdd = *vdn;
  649. /*
  650. * Align 'm' with the result.
  651. */
  652. exp_diff = vdn->exponent - vdm->exponent;
  653. m_sig = vfp_shiftright64jamming(vdm->significand, exp_diff);
  654. /*
  655. * If the signs are different, we are really subtracting.
  656. */
  657. if (vdn->sign ^ vdm->sign) {
  658. m_sig = vdn->significand - m_sig;
  659. if ((s64)m_sig < 0) {
  660. vdd->sign = vfp_sign_negate(vdd->sign);
  661. m_sig = -m_sig;
  662. }
  663. } else {
  664. m_sig += vdn->significand;
  665. }
  666. vdd->significand = m_sig;
  667. return 0;
  668. }
  669. static u32
  670. vfp_double_multiply(struct vfp_double *vdd, struct vfp_double *vdn,
  671. struct vfp_double *vdm, u32 fpscr)
  672. {
  673. vfp_double_dump("VDN", vdn);
  674. vfp_double_dump("VDM", vdm);
  675. /*
  676. * Ensure that 'n' is the largest magnitude number. Note that
  677. * if 'n' and 'm' have equal exponents, we do not swap them.
  678. * This ensures that NaN propagation works correctly.
  679. */
  680. if (vdn->exponent < vdm->exponent) {
  681. struct vfp_double *t = vdn;
  682. vdn = vdm;
  683. vdm = t;
  684. pr_debug("VFP: swapping M <-> N\n");
  685. }
  686. vdd->sign = vdn->sign ^ vdm->sign;
  687. /*
  688. * If 'n' is an infinity or NaN, handle it. 'm' may be anything.
  689. */
  690. if (vdn->exponent == 2047) {
  691. if (vdn->significand || (vdm->exponent == 2047 && vdm->significand))
  692. return vfp_propagate_nan(vdd, vdn, vdm, fpscr);
  693. if ((vdm->exponent | vdm->significand) == 0) {
  694. *vdd = vfp_double_default_qnan;
  695. return FPSCR_IOC;
  696. }
  697. vdd->exponent = vdn->exponent;
  698. vdd->significand = 0;
  699. return 0;
  700. }
  701. /*
  702. * If 'm' is zero, the result is always zero. In this case,
  703. * 'n' may be zero or a number, but it doesn't matter which.
  704. */
  705. if ((vdm->exponent | vdm->significand) == 0) {
  706. vdd->exponent = 0;
  707. vdd->significand = 0;
  708. return 0;
  709. }
  710. /*
  711. * We add 2 to the destination exponent for the same reason
  712. * as the addition case - though this time we have +1 from
  713. * each input operand.
  714. */
  715. vdd->exponent = vdn->exponent + vdm->exponent - 1023 + 2;
  716. vdd->significand = vfp_hi64multiply64(vdn->significand, vdm->significand);
  717. vfp_double_dump("VDD", vdd);
  718. return 0;
  719. }
  720. #define NEG_MULTIPLY (1 << 0)
  721. #define NEG_SUBTRACT (1 << 1)
  722. static u32
  723. vfp_double_multiply_accumulate(int dd, int dn, int dm, u32 fpscr, u32 negate, char *func)
  724. {
  725. struct vfp_double vdd, vdp, vdn, vdm;
  726. u32 exceptions;
  727. vfp_double_unpack(&vdn, vfp_get_double(dn));
  728. if (vdn.exponent == 0 && vdn.significand)
  729. vfp_double_normalise_denormal(&vdn);
  730. vfp_double_unpack(&vdm, vfp_get_double(dm));
  731. if (vdm.exponent == 0 && vdm.significand)
  732. vfp_double_normalise_denormal(&vdm);
  733. exceptions = vfp_double_multiply(&vdp, &vdn, &vdm, fpscr);
  734. if (negate & NEG_MULTIPLY)
  735. vdp.sign = vfp_sign_negate(vdp.sign);
  736. vfp_double_unpack(&vdn, vfp_get_double(dd));
  737. if (negate & NEG_SUBTRACT)
  738. vdn.sign = vfp_sign_negate(vdn.sign);
  739. exceptions |= vfp_double_add(&vdd, &vdn, &vdp, fpscr);
  740. return vfp_double_normaliseround(dd, &vdd, fpscr, exceptions, func);
  741. }
  742. /*
  743. * Standard operations
  744. */
  745. /*
  746. * sd = sd + (sn * sm)
  747. */
  748. static u32 vfp_double_fmac(int dd, int dn, int dm, u32 fpscr)
  749. {
  750. return vfp_double_multiply_accumulate(dd, dn, dm, fpscr, 0, "fmac");
  751. }
  752. /*
  753. * sd = sd - (sn * sm)
  754. */
  755. static u32 vfp_double_fnmac(int dd, int dn, int dm, u32 fpscr)
  756. {
  757. return vfp_double_multiply_accumulate(dd, dn, dm, fpscr, NEG_MULTIPLY, "fnmac");
  758. }
  759. /*
  760. * sd = -sd + (sn * sm)
  761. */
  762. static u32 vfp_double_fmsc(int dd, int dn, int dm, u32 fpscr)
  763. {
  764. return vfp_double_multiply_accumulate(dd, dn, dm, fpscr, NEG_SUBTRACT, "fmsc");
  765. }
  766. /*
  767. * sd = -sd - (sn * sm)
  768. */
  769. static u32 vfp_double_fnmsc(int dd, int dn, int dm, u32 fpscr)
  770. {
  771. return vfp_double_multiply_accumulate(dd, dn, dm, fpscr, NEG_SUBTRACT | NEG_MULTIPLY, "fnmsc");
  772. }
  773. /*
  774. * sd = sn * sm
  775. */
  776. static u32 vfp_double_fmul(int dd, int dn, int dm, u32 fpscr)
  777. {
  778. struct vfp_double vdd, vdn, vdm;
  779. u32 exceptions;
  780. vfp_double_unpack(&vdn, vfp_get_double(dn));
  781. if (vdn.exponent == 0 && vdn.significand)
  782. vfp_double_normalise_denormal(&vdn);
  783. vfp_double_unpack(&vdm, vfp_get_double(dm));
  784. if (vdm.exponent == 0 && vdm.significand)
  785. vfp_double_normalise_denormal(&vdm);
  786. exceptions = vfp_double_multiply(&vdd, &vdn, &vdm, fpscr);
  787. return vfp_double_normaliseround(dd, &vdd, fpscr, exceptions, "fmul");
  788. }
  789. /*
  790. * sd = -(sn * sm)
  791. */
  792. static u32 vfp_double_fnmul(int dd, int dn, int dm, u32 fpscr)
  793. {
  794. struct vfp_double vdd, vdn, vdm;
  795. u32 exceptions;
  796. vfp_double_unpack(&vdn, vfp_get_double(dn));
  797. if (vdn.exponent == 0 && vdn.significand)
  798. vfp_double_normalise_denormal(&vdn);
  799. vfp_double_unpack(&vdm, vfp_get_double(dm));
  800. if (vdm.exponent == 0 && vdm.significand)
  801. vfp_double_normalise_denormal(&vdm);
  802. exceptions = vfp_double_multiply(&vdd, &vdn, &vdm, fpscr);
  803. vdd.sign = vfp_sign_negate(vdd.sign);
  804. return vfp_double_normaliseround(dd, &vdd, fpscr, exceptions, "fnmul");
  805. }
  806. /*
  807. * sd = sn + sm
  808. */
  809. static u32 vfp_double_fadd(int dd, int dn, int dm, u32 fpscr)
  810. {
  811. struct vfp_double vdd, vdn, vdm;
  812. u32 exceptions;
  813. vfp_double_unpack(&vdn, vfp_get_double(dn));
  814. if (vdn.exponent == 0 && vdn.significand)
  815. vfp_double_normalise_denormal(&vdn);
  816. vfp_double_unpack(&vdm, vfp_get_double(dm));
  817. if (vdm.exponent == 0 && vdm.significand)
  818. vfp_double_normalise_denormal(&vdm);
  819. exceptions = vfp_double_add(&vdd, &vdn, &vdm, fpscr);
  820. return vfp_double_normaliseround(dd, &vdd, fpscr, exceptions, "fadd");
  821. }
  822. /*
  823. * sd = sn - sm
  824. */
  825. static u32 vfp_double_fsub(int dd, int dn, int dm, u32 fpscr)
  826. {
  827. struct vfp_double vdd, vdn, vdm;
  828. u32 exceptions;
  829. vfp_double_unpack(&vdn, vfp_get_double(dn));
  830. if (vdn.exponent == 0 && vdn.significand)
  831. vfp_double_normalise_denormal(&vdn);
  832. vfp_double_unpack(&vdm, vfp_get_double(dm));
  833. if (vdm.exponent == 0 && vdm.significand)
  834. vfp_double_normalise_denormal(&vdm);
  835. /*
  836. * Subtraction is like addition, but with a negated operand.
  837. */
  838. vdm.sign = vfp_sign_negate(vdm.sign);
  839. exceptions = vfp_double_add(&vdd, &vdn, &vdm, fpscr);
  840. return vfp_double_normaliseround(dd, &vdd, fpscr, exceptions, "fsub");
  841. }
  842. /*
  843. * sd = sn / sm
  844. */
  845. static u32 vfp_double_fdiv(int dd, int dn, int dm, u32 fpscr)
  846. {
  847. struct vfp_double vdd, vdn, vdm;
  848. u32 exceptions = 0;
  849. int tm, tn;
  850. vfp_double_unpack(&vdn, vfp_get_double(dn));
  851. vfp_double_unpack(&vdm, vfp_get_double(dm));
  852. vdd.sign = vdn.sign ^ vdm.sign;
  853. tn = vfp_double_type(&vdn);
  854. tm = vfp_double_type(&vdm);
  855. /*
  856. * Is n a NAN?
  857. */
  858. if (tn & VFP_NAN)
  859. goto vdn_nan;
  860. /*
  861. * Is m a NAN?
  862. */
  863. if (tm & VFP_NAN)
  864. goto vdm_nan;
  865. /*
  866. * If n and m are infinity, the result is invalid
  867. * If n and m are zero, the result is invalid
  868. */
  869. if (tm & tn & (VFP_INFINITY|VFP_ZERO))
  870. goto invalid;
  871. /*
  872. * If n is infinity, the result is infinity
  873. */
  874. if (tn & VFP_INFINITY)
  875. goto infinity;
  876. /*
  877. * If m is zero, raise div0 exceptions
  878. */
  879. if (tm & VFP_ZERO)
  880. goto divzero;
  881. /*
  882. * If m is infinity, or n is zero, the result is zero
  883. */
  884. if (tm & VFP_INFINITY || tn & VFP_ZERO)
  885. goto zero;
  886. if (tn & VFP_DENORMAL)
  887. vfp_double_normalise_denormal(&vdn);
  888. if (tm & VFP_DENORMAL)
  889. vfp_double_normalise_denormal(&vdm);
  890. /*
  891. * Ok, we have two numbers, we can perform division.
  892. */
  893. vdd.exponent = vdn.exponent - vdm.exponent + 1023 - 1;
  894. vdm.significand <<= 1;
  895. if (vdm.significand <= (2 * vdn.significand)) {
  896. vdn.significand >>= 1;
  897. vdd.exponent++;
  898. }
  899. vdd.significand = vfp_estimate_div128to64(vdn.significand, 0, vdm.significand);
  900. if ((vdd.significand & 0x1ff) <= 2) {
  901. u64 termh, terml, remh, reml;
  902. mul64to128(&termh, &terml, vdm.significand, vdd.significand);
  903. sub128(&remh, &reml, vdn.significand, 0, termh, terml);
  904. while ((s64)remh < 0) {
  905. vdd.significand -= 1;
  906. add128(&remh, &reml, remh, reml, 0, vdm.significand);
  907. }
  908. vdd.significand |= (reml != 0);
  909. }
  910. return vfp_double_normaliseround(dd, &vdd, fpscr, 0, "fdiv");
  911. vdn_nan:
  912. exceptions = vfp_propagate_nan(&vdd, &vdn, &vdm, fpscr);
  913. pack:
  914. vfp_put_double(dd, vfp_double_pack(&vdd));
  915. return exceptions;
  916. vdm_nan:
  917. exceptions = vfp_propagate_nan(&vdd, &vdm, &vdn, fpscr);
  918. goto pack;
  919. zero:
  920. vdd.exponent = 0;
  921. vdd.significand = 0;
  922. goto pack;
  923. divzero:
  924. exceptions = FPSCR_DZC;
  925. infinity:
  926. vdd.exponent = 2047;
  927. vdd.significand = 0;
  928. goto pack;
  929. invalid:
  930. vfp_put_double(dd, vfp_double_pack(&vfp_double_default_qnan));
  931. return FPSCR_IOC;
  932. }
  933. static u32 (* const fop_fns[16])(int dd, int dn, int dm, u32 fpscr) = {
  934. [FOP_TO_IDX(FOP_FMAC)] = vfp_double_fmac,
  935. [FOP_TO_IDX(FOP_FNMAC)] = vfp_double_fnmac,
  936. [FOP_TO_IDX(FOP_FMSC)] = vfp_double_fmsc,
  937. [FOP_TO_IDX(FOP_FNMSC)] = vfp_double_fnmsc,
  938. [FOP_TO_IDX(FOP_FMUL)] = vfp_double_fmul,
  939. [FOP_TO_IDX(FOP_FNMUL)] = vfp_double_fnmul,
  940. [FOP_TO_IDX(FOP_FADD)] = vfp_double_fadd,
  941. [FOP_TO_IDX(FOP_FSUB)] = vfp_double_fsub,
  942. [FOP_TO_IDX(FOP_FDIV)] = vfp_double_fdiv,
  943. };
  944. #define FREG_BANK(x) ((x) & 0x0c)
  945. #define FREG_IDX(x) ((x) & 3)
  946. u32 vfp_double_cpdo(u32 inst, u32 fpscr)
  947. {
  948. u32 op = inst & FOP_MASK;
  949. u32 exceptions = 0;
  950. unsigned int dd = vfp_get_sd(inst);
  951. unsigned int dn = vfp_get_sn(inst);
  952. unsigned int dm = vfp_get_sm(inst);
  953. unsigned int vecitr, veclen, vecstride;
  954. u32 (*fop)(int, int, s32, u32);
  955. veclen = fpscr & FPSCR_LENGTH_MASK;
  956. vecstride = (1 + ((fpscr & FPSCR_STRIDE_MASK) == FPSCR_STRIDE_MASK)) * 2;
  957. /*
  958. * If destination bank is zero, vector length is always '1'.
  959. * ARM DDI0100F C5.1.3, C5.3.2.
  960. */
  961. if (FREG_BANK(dd) == 0)
  962. veclen = 0;
  963. pr_debug("VFP: vecstride=%u veclen=%u\n", vecstride,
  964. (veclen >> FPSCR_LENGTH_BIT) + 1);
  965. fop = (op == FOP_EXT) ? fop_extfns[dn] : fop_fns[FOP_TO_IDX(op)];
  966. if (!fop)
  967. goto invalid;
  968. for (vecitr = 0; vecitr <= veclen; vecitr += 1 << FPSCR_LENGTH_BIT) {
  969. u32 except;
  970. if (op == FOP_EXT)
  971. pr_debug("VFP: itr%d (d%u.%u) = op[%u] (d%u.%u)\n",
  972. vecitr >> FPSCR_LENGTH_BIT,
  973. dd >> 1, dd & 1, dn,
  974. dm >> 1, dm & 1);
  975. else
  976. pr_debug("VFP: itr%d (d%u.%u) = (d%u.%u) op[%u] (d%u.%u)\n",
  977. vecitr >> FPSCR_LENGTH_BIT,
  978. dd >> 1, dd & 1,
  979. dn >> 1, dn & 1,
  980. FOP_TO_IDX(op),
  981. dm >> 1, dm & 1);
  982. except = fop(dd, dn, dm, fpscr);
  983. pr_debug("VFP: itr%d: exceptions=%08x\n",
  984. vecitr >> FPSCR_LENGTH_BIT, except);
  985. exceptions |= except;
  986. /*
  987. * This ensures that comparisons only operate on scalars;
  988. * comparisons always return with one FPSCR status bit set.
  989. */
  990. if (except & (FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V))
  991. break;
  992. /*
  993. * CHECK: It appears to be undefined whether we stop when
  994. * we encounter an exception. We continue.
  995. */
  996. dd = FREG_BANK(dd) + ((FREG_IDX(dd) + vecstride) & 6);
  997. dn = FREG_BANK(dn) + ((FREG_IDX(dn) + vecstride) & 6);
  998. if (FREG_BANK(dm) != 0)
  999. dm = FREG_BANK(dm) + ((FREG_IDX(dm) + vecstride) & 6);
  1000. }
  1001. return exceptions;
  1002. invalid:
  1003. return ~0;
  1004. }