vfpsingle.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  1. /*
  2. * linux/arch/arm/vfp/vfpsingle.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/div64.h>
  36. #include <asm/ptrace.h>
  37. #include <asm/vfp.h>
  38. #include "vfpinstr.h"
  39. #include "vfp.h"
  40. static struct vfp_single vfp_single_default_qnan = {
  41. .exponent = 255,
  42. .sign = 0,
  43. .significand = VFP_SINGLE_SIGNIFICAND_QNAN,
  44. };
  45. static void vfp_single_dump(const char *str, struct vfp_single *s)
  46. {
  47. pr_debug("VFP: %s: sign=%d exponent=%d significand=%08x\n",
  48. str, s->sign != 0, s->exponent, s->significand);
  49. }
  50. static void vfp_single_normalise_denormal(struct vfp_single *vs)
  51. {
  52. int bits = 31 - fls(vs->significand);
  53. vfp_single_dump("normalise_denormal: in", vs);
  54. if (bits) {
  55. vs->exponent -= bits - 1;
  56. vs->significand <<= bits;
  57. }
  58. vfp_single_dump("normalise_denormal: out", vs);
  59. }
  60. #ifndef DEBUG
  61. #define vfp_single_normaliseround(sd,vsd,fpscr,except,func) __vfp_single_normaliseround(sd,vsd,fpscr,except)
  62. u32 __vfp_single_normaliseround(int sd, struct vfp_single *vs, u32 fpscr, u32 exceptions)
  63. #else
  64. u32 vfp_single_normaliseround(int sd, struct vfp_single *vs, u32 fpscr, u32 exceptions, const char *func)
  65. #endif
  66. {
  67. u32 significand, incr, rmode;
  68. int exponent, shift, underflow;
  69. vfp_single_dump("pack: in", vs);
  70. /*
  71. * Infinities and NaNs are a special case.
  72. */
  73. if (vs->exponent == 255 && (vs->significand == 0 || exceptions))
  74. goto pack;
  75. /*
  76. * Special-case zero.
  77. */
  78. if (vs->significand == 0) {
  79. vs->exponent = 0;
  80. goto pack;
  81. }
  82. exponent = vs->exponent;
  83. significand = vs->significand;
  84. /*
  85. * Normalise first. Note that we shift the significand up to
  86. * bit 31, so we have VFP_SINGLE_LOW_BITS + 1 below the least
  87. * significant bit.
  88. */
  89. shift = 32 - fls(significand);
  90. if (shift < 32 && shift) {
  91. exponent -= shift;
  92. significand <<= shift;
  93. }
  94. #ifdef DEBUG
  95. vs->exponent = exponent;
  96. vs->significand = significand;
  97. vfp_single_dump("pack: normalised", vs);
  98. #endif
  99. /*
  100. * Tiny number?
  101. */
  102. underflow = exponent < 0;
  103. if (underflow) {
  104. significand = vfp_shiftright32jamming(significand, -exponent);
  105. exponent = 0;
  106. #ifdef DEBUG
  107. vs->exponent = exponent;
  108. vs->significand = significand;
  109. vfp_single_dump("pack: tiny number", vs);
  110. #endif
  111. if (!(significand & ((1 << (VFP_SINGLE_LOW_BITS + 1)) - 1)))
  112. underflow = 0;
  113. }
  114. /*
  115. * Select rounding increment.
  116. */
  117. incr = 0;
  118. rmode = fpscr & FPSCR_RMODE_MASK;
  119. if (rmode == FPSCR_ROUND_NEAREST) {
  120. incr = 1 << VFP_SINGLE_LOW_BITS;
  121. if ((significand & (1 << (VFP_SINGLE_LOW_BITS + 1))) == 0)
  122. incr -= 1;
  123. } else if (rmode == FPSCR_ROUND_TOZERO) {
  124. incr = 0;
  125. } else if ((rmode == FPSCR_ROUND_PLUSINF) ^ (vs->sign != 0))
  126. incr = (1 << (VFP_SINGLE_LOW_BITS + 1)) - 1;
  127. pr_debug("VFP: rounding increment = 0x%08x\n", incr);
  128. /*
  129. * Is our rounding going to overflow?
  130. */
  131. if ((significand + incr) < significand) {
  132. exponent += 1;
  133. significand = (significand >> 1) | (significand & 1);
  134. incr >>= 1;
  135. #ifdef DEBUG
  136. vs->exponent = exponent;
  137. vs->significand = significand;
  138. vfp_single_dump("pack: overflow", vs);
  139. #endif
  140. }
  141. /*
  142. * If any of the low bits (which will be shifted out of the
  143. * number) are non-zero, the result is inexact.
  144. */
  145. if (significand & ((1 << (VFP_SINGLE_LOW_BITS + 1)) - 1))
  146. exceptions |= FPSCR_IXC;
  147. /*
  148. * Do our rounding.
  149. */
  150. significand += incr;
  151. /*
  152. * Infinity?
  153. */
  154. if (exponent >= 254) {
  155. exceptions |= FPSCR_OFC | FPSCR_IXC;
  156. if (incr == 0) {
  157. vs->exponent = 253;
  158. vs->significand = 0x7fffffff;
  159. } else {
  160. vs->exponent = 255; /* infinity */
  161. vs->significand = 0;
  162. }
  163. } else {
  164. if (significand >> (VFP_SINGLE_LOW_BITS + 1) == 0)
  165. exponent = 0;
  166. if (exponent || significand > 0x80000000)
  167. underflow = 0;
  168. if (underflow)
  169. exceptions |= FPSCR_UFC;
  170. vs->exponent = exponent;
  171. vs->significand = significand >> 1;
  172. }
  173. pack:
  174. vfp_single_dump("pack: final", vs);
  175. {
  176. s32 d = vfp_single_pack(vs);
  177. pr_debug("VFP: %s: d(s%d)=%08x exceptions=%08x\n", func,
  178. sd, d, exceptions);
  179. vfp_put_float(sd, d);
  180. }
  181. return exceptions & ~VFP_NAN_FLAG;
  182. }
  183. /*
  184. * Propagate the NaN, setting exceptions if it is signalling.
  185. * 'n' is always a NaN. 'm' may be a number, NaN or infinity.
  186. */
  187. static u32
  188. vfp_propagate_nan(struct vfp_single *vsd, struct vfp_single *vsn,
  189. struct vfp_single *vsm, u32 fpscr)
  190. {
  191. struct vfp_single *nan;
  192. int tn, tm = 0;
  193. tn = vfp_single_type(vsn);
  194. if (vsm)
  195. tm = vfp_single_type(vsm);
  196. if (fpscr & FPSCR_DEFAULT_NAN)
  197. /*
  198. * Default NaN mode - always returns a quiet NaN
  199. */
  200. nan = &vfp_single_default_qnan;
  201. else {
  202. /*
  203. * Contemporary mode - select the first signalling
  204. * NAN, or if neither are signalling, the first
  205. * quiet NAN.
  206. */
  207. if (tn == VFP_SNAN || (tm != VFP_SNAN && tn == VFP_QNAN))
  208. nan = vsn;
  209. else
  210. nan = vsm;
  211. /*
  212. * Make the NaN quiet.
  213. */
  214. nan->significand |= VFP_SINGLE_SIGNIFICAND_QNAN;
  215. }
  216. *vsd = *nan;
  217. /*
  218. * If one was a signalling NAN, raise invalid operation.
  219. */
  220. return tn == VFP_SNAN || tm == VFP_SNAN ? FPSCR_IOC : VFP_NAN_FLAG;
  221. }
  222. /*
  223. * Extended operations
  224. */
  225. static u32 vfp_single_fabs(int sd, int unused, s32 m, u32 fpscr)
  226. {
  227. vfp_put_float(sd, vfp_single_packed_abs(m));
  228. return 0;
  229. }
  230. static u32 vfp_single_fcpy(int sd, int unused, s32 m, u32 fpscr)
  231. {
  232. vfp_put_float(sd, m);
  233. return 0;
  234. }
  235. static u32 vfp_single_fneg(int sd, int unused, s32 m, u32 fpscr)
  236. {
  237. vfp_put_float(sd, vfp_single_packed_negate(m));
  238. return 0;
  239. }
  240. static const u16 sqrt_oddadjust[] = {
  241. 0x0004, 0x0022, 0x005d, 0x00b1, 0x011d, 0x019f, 0x0236, 0x02e0,
  242. 0x039c, 0x0468, 0x0545, 0x0631, 0x072b, 0x0832, 0x0946, 0x0a67
  243. };
  244. static const u16 sqrt_evenadjust[] = {
  245. 0x0a2d, 0x08af, 0x075a, 0x0629, 0x051a, 0x0429, 0x0356, 0x029e,
  246. 0x0200, 0x0179, 0x0109, 0x00af, 0x0068, 0x0034, 0x0012, 0x0002
  247. };
  248. u32 vfp_estimate_sqrt_significand(u32 exponent, u32 significand)
  249. {
  250. int index;
  251. u32 z, a;
  252. if ((significand & 0xc0000000) != 0x40000000) {
  253. printk(KERN_WARNING "VFP: estimate_sqrt: invalid significand\n");
  254. }
  255. a = significand << 1;
  256. index = (a >> 27) & 15;
  257. if (exponent & 1) {
  258. z = 0x4000 + (a >> 17) - sqrt_oddadjust[index];
  259. z = ((a / z) << 14) + (z << 15);
  260. a >>= 1;
  261. } else {
  262. z = 0x8000 + (a >> 17) - sqrt_evenadjust[index];
  263. z = a / z + z;
  264. z = (z >= 0x20000) ? 0xffff8000 : (z << 15);
  265. if (z <= a)
  266. return (s32)a >> 1;
  267. }
  268. {
  269. u64 v = (u64)a << 31;
  270. do_div(v, z);
  271. return v + (z >> 1);
  272. }
  273. }
  274. static u32 vfp_single_fsqrt(int sd, int unused, s32 m, u32 fpscr)
  275. {
  276. struct vfp_single vsm, vsd;
  277. int ret, tm;
  278. vfp_single_unpack(&vsm, m);
  279. tm = vfp_single_type(&vsm);
  280. if (tm & (VFP_NAN|VFP_INFINITY)) {
  281. struct vfp_single *vsp = &vsd;
  282. if (tm & VFP_NAN)
  283. ret = vfp_propagate_nan(vsp, &vsm, NULL, fpscr);
  284. else if (vsm.sign == 0) {
  285. sqrt_copy:
  286. vsp = &vsm;
  287. ret = 0;
  288. } else {
  289. sqrt_invalid:
  290. vsp = &vfp_single_default_qnan;
  291. ret = FPSCR_IOC;
  292. }
  293. vfp_put_float(sd, vfp_single_pack(vsp));
  294. return ret;
  295. }
  296. /*
  297. * sqrt(+/- 0) == +/- 0
  298. */
  299. if (tm & VFP_ZERO)
  300. goto sqrt_copy;
  301. /*
  302. * Normalise a denormalised number
  303. */
  304. if (tm & VFP_DENORMAL)
  305. vfp_single_normalise_denormal(&vsm);
  306. /*
  307. * sqrt(<0) = invalid
  308. */
  309. if (vsm.sign)
  310. goto sqrt_invalid;
  311. vfp_single_dump("sqrt", &vsm);
  312. /*
  313. * Estimate the square root.
  314. */
  315. vsd.sign = 0;
  316. vsd.exponent = ((vsm.exponent - 127) >> 1) + 127;
  317. vsd.significand = vfp_estimate_sqrt_significand(vsm.exponent, vsm.significand) + 2;
  318. vfp_single_dump("sqrt estimate", &vsd);
  319. /*
  320. * And now adjust.
  321. */
  322. if ((vsd.significand & VFP_SINGLE_LOW_BITS_MASK) <= 5) {
  323. if (vsd.significand < 2) {
  324. vsd.significand = 0xffffffff;
  325. } else {
  326. u64 term;
  327. s64 rem;
  328. vsm.significand <<= !(vsm.exponent & 1);
  329. term = (u64)vsd.significand * vsd.significand;
  330. rem = ((u64)vsm.significand << 32) - term;
  331. pr_debug("VFP: term=%016llx rem=%016llx\n", term, rem);
  332. while (rem < 0) {
  333. vsd.significand -= 1;
  334. rem += ((u64)vsd.significand << 1) | 1;
  335. }
  336. vsd.significand |= rem != 0;
  337. }
  338. }
  339. vsd.significand = vfp_shiftright32jamming(vsd.significand, 1);
  340. return vfp_single_normaliseround(sd, &vsd, fpscr, 0, "fsqrt");
  341. }
  342. /*
  343. * Equal := ZC
  344. * Less than := N
  345. * Greater than := C
  346. * Unordered := CV
  347. */
  348. static u32 vfp_compare(int sd, int signal_on_qnan, s32 m, u32 fpscr)
  349. {
  350. s32 d;
  351. u32 ret = 0;
  352. d = vfp_get_float(sd);
  353. if (vfp_single_packed_exponent(m) == 255 && vfp_single_packed_mantissa(m)) {
  354. ret |= FPSCR_C | FPSCR_V;
  355. if (signal_on_qnan || !(vfp_single_packed_mantissa(m) & (1 << (VFP_SINGLE_MANTISSA_BITS - 1))))
  356. /*
  357. * Signalling NaN, or signalling on quiet NaN
  358. */
  359. ret |= FPSCR_IOC;
  360. }
  361. if (vfp_single_packed_exponent(d) == 255 && vfp_single_packed_mantissa(d)) {
  362. ret |= FPSCR_C | FPSCR_V;
  363. if (signal_on_qnan || !(vfp_single_packed_mantissa(d) & (1 << (VFP_SINGLE_MANTISSA_BITS - 1))))
  364. /*
  365. * Signalling NaN, or signalling on quiet NaN
  366. */
  367. ret |= FPSCR_IOC;
  368. }
  369. if (ret == 0) {
  370. if (d == m || vfp_single_packed_abs(d | m) == 0) {
  371. /*
  372. * equal
  373. */
  374. ret |= FPSCR_Z | FPSCR_C;
  375. } else if (vfp_single_packed_sign(d ^ m)) {
  376. /*
  377. * different signs
  378. */
  379. if (vfp_single_packed_sign(d))
  380. /*
  381. * d is negative, so d < m
  382. */
  383. ret |= FPSCR_N;
  384. else
  385. /*
  386. * d is positive, so d > m
  387. */
  388. ret |= FPSCR_C;
  389. } else if ((vfp_single_packed_sign(d) != 0) ^ (d < m)) {
  390. /*
  391. * d < m
  392. */
  393. ret |= FPSCR_N;
  394. } else if ((vfp_single_packed_sign(d) != 0) ^ (d > m)) {
  395. /*
  396. * d > m
  397. */
  398. ret |= FPSCR_C;
  399. }
  400. }
  401. return ret;
  402. }
  403. static u32 vfp_single_fcmp(int sd, int unused, s32 m, u32 fpscr)
  404. {
  405. return vfp_compare(sd, 0, m, fpscr);
  406. }
  407. static u32 vfp_single_fcmpe(int sd, int unused, s32 m, u32 fpscr)
  408. {
  409. return vfp_compare(sd, 1, m, fpscr);
  410. }
  411. static u32 vfp_single_fcmpz(int sd, int unused, s32 m, u32 fpscr)
  412. {
  413. return vfp_compare(sd, 0, 0, fpscr);
  414. }
  415. static u32 vfp_single_fcmpez(int sd, int unused, s32 m, u32 fpscr)
  416. {
  417. return vfp_compare(sd, 1, 0, fpscr);
  418. }
  419. static u32 vfp_single_fcvtd(int dd, int unused, s32 m, u32 fpscr)
  420. {
  421. struct vfp_single vsm;
  422. struct vfp_double vdd;
  423. int tm;
  424. u32 exceptions = 0;
  425. vfp_single_unpack(&vsm, m);
  426. tm = vfp_single_type(&vsm);
  427. /*
  428. * If we have a signalling NaN, signal invalid operation.
  429. */
  430. if (tm == VFP_SNAN)
  431. exceptions = FPSCR_IOC;
  432. if (tm & VFP_DENORMAL)
  433. vfp_single_normalise_denormal(&vsm);
  434. vdd.sign = vsm.sign;
  435. vdd.significand = (u64)vsm.significand << 32;
  436. /*
  437. * If we have an infinity or NaN, the exponent must be 2047.
  438. */
  439. if (tm & (VFP_INFINITY|VFP_NAN)) {
  440. vdd.exponent = 2047;
  441. if (tm & VFP_NAN)
  442. vdd.significand |= VFP_DOUBLE_SIGNIFICAND_QNAN;
  443. goto pack_nan;
  444. } else if (tm & VFP_ZERO)
  445. vdd.exponent = 0;
  446. else
  447. vdd.exponent = vsm.exponent + (1023 - 127);
  448. /*
  449. * Technically, if bit 0 of dd is set, this is an invalid
  450. * instruction. However, we ignore this for efficiency.
  451. */
  452. return vfp_double_normaliseround(dd, &vdd, fpscr, exceptions, "fcvtd");
  453. pack_nan:
  454. vfp_put_double(dd, vfp_double_pack(&vdd));
  455. return exceptions;
  456. }
  457. static u32 vfp_single_fuito(int sd, int unused, s32 m, u32 fpscr)
  458. {
  459. struct vfp_single vs;
  460. vs.sign = 0;
  461. vs.exponent = 127 + 31 - 1;
  462. vs.significand = (u32)m;
  463. return vfp_single_normaliseround(sd, &vs, fpscr, 0, "fuito");
  464. }
  465. static u32 vfp_single_fsito(int sd, int unused, s32 m, u32 fpscr)
  466. {
  467. struct vfp_single vs;
  468. vs.sign = (m & 0x80000000) >> 16;
  469. vs.exponent = 127 + 31 - 1;
  470. vs.significand = vs.sign ? -m : m;
  471. return vfp_single_normaliseround(sd, &vs, fpscr, 0, "fsito");
  472. }
  473. static u32 vfp_single_ftoui(int sd, int unused, s32 m, u32 fpscr)
  474. {
  475. struct vfp_single vsm;
  476. u32 d, exceptions = 0;
  477. int rmode = fpscr & FPSCR_RMODE_MASK;
  478. int tm;
  479. vfp_single_unpack(&vsm, m);
  480. vfp_single_dump("VSM", &vsm);
  481. /*
  482. * Do we have a denormalised number?
  483. */
  484. tm = vfp_single_type(&vsm);
  485. if (tm & VFP_DENORMAL)
  486. exceptions |= FPSCR_IDC;
  487. if (tm & VFP_NAN)
  488. vsm.sign = 0;
  489. if (vsm.exponent >= 127 + 32) {
  490. d = vsm.sign ? 0 : 0xffffffff;
  491. exceptions = FPSCR_IOC;
  492. } else if (vsm.exponent >= 127 - 1) {
  493. int shift = 127 + 31 - vsm.exponent;
  494. u32 rem, incr = 0;
  495. /*
  496. * 2^0 <= m < 2^32-2^8
  497. */
  498. d = (vsm.significand << 1) >> shift;
  499. rem = vsm.significand << (33 - shift);
  500. if (rmode == FPSCR_ROUND_NEAREST) {
  501. incr = 0x80000000;
  502. if ((d & 1) == 0)
  503. incr -= 1;
  504. } else if (rmode == FPSCR_ROUND_TOZERO) {
  505. incr = 0;
  506. } else if ((rmode == FPSCR_ROUND_PLUSINF) ^ (vsm.sign != 0)) {
  507. incr = ~0;
  508. }
  509. if ((rem + incr) < rem) {
  510. if (d < 0xffffffff)
  511. d += 1;
  512. else
  513. exceptions |= FPSCR_IOC;
  514. }
  515. if (d && vsm.sign) {
  516. d = 0;
  517. exceptions |= FPSCR_IOC;
  518. } else if (rem)
  519. exceptions |= FPSCR_IXC;
  520. } else {
  521. d = 0;
  522. if (vsm.exponent | vsm.significand) {
  523. exceptions |= FPSCR_IXC;
  524. if (rmode == FPSCR_ROUND_PLUSINF && vsm.sign == 0)
  525. d = 1;
  526. else if (rmode == FPSCR_ROUND_MINUSINF && vsm.sign) {
  527. d = 0;
  528. exceptions |= FPSCR_IOC;
  529. }
  530. }
  531. }
  532. pr_debug("VFP: ftoui: d(s%d)=%08x exceptions=%08x\n", sd, d, exceptions);
  533. vfp_put_float(sd, d);
  534. return exceptions;
  535. }
  536. static u32 vfp_single_ftouiz(int sd, int unused, s32 m, u32 fpscr)
  537. {
  538. return vfp_single_ftoui(sd, unused, m, FPSCR_ROUND_TOZERO);
  539. }
  540. static u32 vfp_single_ftosi(int sd, int unused, s32 m, u32 fpscr)
  541. {
  542. struct vfp_single vsm;
  543. u32 d, exceptions = 0;
  544. int rmode = fpscr & FPSCR_RMODE_MASK;
  545. vfp_single_unpack(&vsm, m);
  546. vfp_single_dump("VSM", &vsm);
  547. /*
  548. * Do we have a denormalised number?
  549. */
  550. if (vfp_single_type(&vsm) & VFP_DENORMAL)
  551. exceptions |= FPSCR_IDC;
  552. if (vsm.exponent >= 127 + 32) {
  553. /*
  554. * m >= 2^31-2^7: invalid
  555. */
  556. d = 0x7fffffff;
  557. if (vsm.sign)
  558. d = ~d;
  559. exceptions |= FPSCR_IOC;
  560. } else if (vsm.exponent >= 127 - 1) {
  561. int shift = 127 + 31 - vsm.exponent;
  562. u32 rem, incr = 0;
  563. /* 2^0 <= m <= 2^31-2^7 */
  564. d = (vsm.significand << 1) >> shift;
  565. rem = vsm.significand << (33 - shift);
  566. if (rmode == FPSCR_ROUND_NEAREST) {
  567. incr = 0x80000000;
  568. if ((d & 1) == 0)
  569. incr -= 1;
  570. } else if (rmode == FPSCR_ROUND_TOZERO) {
  571. incr = 0;
  572. } else if ((rmode == FPSCR_ROUND_PLUSINF) ^ (vsm.sign != 0)) {
  573. incr = ~0;
  574. }
  575. if ((rem + incr) < rem && d < 0xffffffff)
  576. d += 1;
  577. if (d > 0x7fffffff + (vsm.sign != 0)) {
  578. d = 0x7fffffff + (vsm.sign != 0);
  579. exceptions |= FPSCR_IOC;
  580. } else if (rem)
  581. exceptions |= FPSCR_IXC;
  582. if (vsm.sign)
  583. d = -d;
  584. } else {
  585. d = 0;
  586. if (vsm.exponent | vsm.significand) {
  587. exceptions |= FPSCR_IXC;
  588. if (rmode == FPSCR_ROUND_PLUSINF && vsm.sign == 0)
  589. d = 1;
  590. else if (rmode == FPSCR_ROUND_MINUSINF && vsm.sign)
  591. d = -1;
  592. }
  593. }
  594. pr_debug("VFP: ftosi: d(s%d)=%08x exceptions=%08x\n", sd, d, exceptions);
  595. vfp_put_float(sd, (s32)d);
  596. return exceptions;
  597. }
  598. static u32 vfp_single_ftosiz(int sd, int unused, s32 m, u32 fpscr)
  599. {
  600. return vfp_single_ftosi(sd, unused, m, FPSCR_ROUND_TOZERO);
  601. }
  602. static u32 (* const fop_extfns[32])(int sd, int unused, s32 m, u32 fpscr) = {
  603. [FEXT_TO_IDX(FEXT_FCPY)] = vfp_single_fcpy,
  604. [FEXT_TO_IDX(FEXT_FABS)] = vfp_single_fabs,
  605. [FEXT_TO_IDX(FEXT_FNEG)] = vfp_single_fneg,
  606. [FEXT_TO_IDX(FEXT_FSQRT)] = vfp_single_fsqrt,
  607. [FEXT_TO_IDX(FEXT_FCMP)] = vfp_single_fcmp,
  608. [FEXT_TO_IDX(FEXT_FCMPE)] = vfp_single_fcmpe,
  609. [FEXT_TO_IDX(FEXT_FCMPZ)] = vfp_single_fcmpz,
  610. [FEXT_TO_IDX(FEXT_FCMPEZ)] = vfp_single_fcmpez,
  611. [FEXT_TO_IDX(FEXT_FCVT)] = vfp_single_fcvtd,
  612. [FEXT_TO_IDX(FEXT_FUITO)] = vfp_single_fuito,
  613. [FEXT_TO_IDX(FEXT_FSITO)] = vfp_single_fsito,
  614. [FEXT_TO_IDX(FEXT_FTOUI)] = vfp_single_ftoui,
  615. [FEXT_TO_IDX(FEXT_FTOUIZ)] = vfp_single_ftouiz,
  616. [FEXT_TO_IDX(FEXT_FTOSI)] = vfp_single_ftosi,
  617. [FEXT_TO_IDX(FEXT_FTOSIZ)] = vfp_single_ftosiz,
  618. };
  619. static u32
  620. vfp_single_fadd_nonnumber(struct vfp_single *vsd, struct vfp_single *vsn,
  621. struct vfp_single *vsm, u32 fpscr)
  622. {
  623. struct vfp_single *vsp;
  624. u32 exceptions = 0;
  625. int tn, tm;
  626. tn = vfp_single_type(vsn);
  627. tm = vfp_single_type(vsm);
  628. if (tn & tm & VFP_INFINITY) {
  629. /*
  630. * Two infinities. Are they different signs?
  631. */
  632. if (vsn->sign ^ vsm->sign) {
  633. /*
  634. * different signs -> invalid
  635. */
  636. exceptions = FPSCR_IOC;
  637. vsp = &vfp_single_default_qnan;
  638. } else {
  639. /*
  640. * same signs -> valid
  641. */
  642. vsp = vsn;
  643. }
  644. } else if (tn & VFP_INFINITY && tm & VFP_NUMBER) {
  645. /*
  646. * One infinity and one number -> infinity
  647. */
  648. vsp = vsn;
  649. } else {
  650. /*
  651. * 'n' is a NaN of some type
  652. */
  653. return vfp_propagate_nan(vsd, vsn, vsm, fpscr);
  654. }
  655. *vsd = *vsp;
  656. return exceptions;
  657. }
  658. static u32
  659. vfp_single_add(struct vfp_single *vsd, struct vfp_single *vsn,
  660. struct vfp_single *vsm, u32 fpscr)
  661. {
  662. u32 exp_diff, m_sig;
  663. if (vsn->significand & 0x80000000 ||
  664. vsm->significand & 0x80000000) {
  665. pr_info("VFP: bad FP values in %s\n", __func__);
  666. vfp_single_dump("VSN", vsn);
  667. vfp_single_dump("VSM", vsm);
  668. }
  669. /*
  670. * Ensure that 'n' is the largest magnitude number. Note that
  671. * if 'n' and 'm' have equal exponents, we do not swap them.
  672. * This ensures that NaN propagation works correctly.
  673. */
  674. if (vsn->exponent < vsm->exponent) {
  675. struct vfp_single *t = vsn;
  676. vsn = vsm;
  677. vsm = t;
  678. }
  679. /*
  680. * Is 'n' an infinity or a NaN? Note that 'm' may be a number,
  681. * infinity or a NaN here.
  682. */
  683. if (vsn->exponent == 255)
  684. return vfp_single_fadd_nonnumber(vsd, vsn, vsm, fpscr);
  685. /*
  686. * We have two proper numbers, where 'vsn' is the larger magnitude.
  687. *
  688. * Copy 'n' to 'd' before doing the arithmetic.
  689. */
  690. *vsd = *vsn;
  691. /*
  692. * Align both numbers.
  693. */
  694. exp_diff = vsn->exponent - vsm->exponent;
  695. m_sig = vfp_shiftright32jamming(vsm->significand, exp_diff);
  696. /*
  697. * If the signs are different, we are really subtracting.
  698. */
  699. if (vsn->sign ^ vsm->sign) {
  700. m_sig = vsn->significand - m_sig;
  701. if ((s32)m_sig < 0) {
  702. vsd->sign = vfp_sign_negate(vsd->sign);
  703. m_sig = -m_sig;
  704. } else if (m_sig == 0) {
  705. vsd->sign = (fpscr & FPSCR_RMODE_MASK) ==
  706. FPSCR_ROUND_MINUSINF ? 0x8000 : 0;
  707. }
  708. } else {
  709. m_sig = vsn->significand + m_sig;
  710. }
  711. vsd->significand = m_sig;
  712. return 0;
  713. }
  714. static u32
  715. vfp_single_multiply(struct vfp_single *vsd, struct vfp_single *vsn, struct vfp_single *vsm, u32 fpscr)
  716. {
  717. vfp_single_dump("VSN", vsn);
  718. vfp_single_dump("VSM", vsm);
  719. /*
  720. * Ensure that 'n' is the largest magnitude number. Note that
  721. * if 'n' and 'm' have equal exponents, we do not swap them.
  722. * This ensures that NaN propagation works correctly.
  723. */
  724. if (vsn->exponent < vsm->exponent) {
  725. struct vfp_single *t = vsn;
  726. vsn = vsm;
  727. vsm = t;
  728. pr_debug("VFP: swapping M <-> N\n");
  729. }
  730. vsd->sign = vsn->sign ^ vsm->sign;
  731. /*
  732. * If 'n' is an infinity or NaN, handle it. 'm' may be anything.
  733. */
  734. if (vsn->exponent == 255) {
  735. if (vsn->significand || (vsm->exponent == 255 && vsm->significand))
  736. return vfp_propagate_nan(vsd, vsn, vsm, fpscr);
  737. if ((vsm->exponent | vsm->significand) == 0) {
  738. *vsd = vfp_single_default_qnan;
  739. return FPSCR_IOC;
  740. }
  741. vsd->exponent = vsn->exponent;
  742. vsd->significand = 0;
  743. return 0;
  744. }
  745. /*
  746. * If 'm' is zero, the result is always zero. In this case,
  747. * 'n' may be zero or a number, but it doesn't matter which.
  748. */
  749. if ((vsm->exponent | vsm->significand) == 0) {
  750. vsd->exponent = 0;
  751. vsd->significand = 0;
  752. return 0;
  753. }
  754. /*
  755. * We add 2 to the destination exponent for the same reason as
  756. * the addition case - though this time we have +1 from each
  757. * input operand.
  758. */
  759. vsd->exponent = vsn->exponent + vsm->exponent - 127 + 2;
  760. vsd->significand = vfp_hi64to32jamming((u64)vsn->significand * vsm->significand);
  761. vfp_single_dump("VSD", vsd);
  762. return 0;
  763. }
  764. #define NEG_MULTIPLY (1 << 0)
  765. #define NEG_SUBTRACT (1 << 1)
  766. static u32
  767. vfp_single_multiply_accumulate(int sd, int sn, s32 m, u32 fpscr, u32 negate, char *func)
  768. {
  769. struct vfp_single vsd, vsp, vsn, vsm;
  770. u32 exceptions;
  771. s32 v;
  772. v = vfp_get_float(sn);
  773. pr_debug("VFP: s%u = %08x\n", sn, v);
  774. vfp_single_unpack(&vsn, v);
  775. if (vsn.exponent == 0 && vsn.significand)
  776. vfp_single_normalise_denormal(&vsn);
  777. vfp_single_unpack(&vsm, m);
  778. if (vsm.exponent == 0 && vsm.significand)
  779. vfp_single_normalise_denormal(&vsm);
  780. exceptions = vfp_single_multiply(&vsp, &vsn, &vsm, fpscr);
  781. if (negate & NEG_MULTIPLY)
  782. vsp.sign = vfp_sign_negate(vsp.sign);
  783. v = vfp_get_float(sd);
  784. pr_debug("VFP: s%u = %08x\n", sd, v);
  785. vfp_single_unpack(&vsn, v);
  786. if (negate & NEG_SUBTRACT)
  787. vsn.sign = vfp_sign_negate(vsn.sign);
  788. exceptions |= vfp_single_add(&vsd, &vsn, &vsp, fpscr);
  789. return vfp_single_normaliseround(sd, &vsd, fpscr, exceptions, func);
  790. }
  791. /*
  792. * Standard operations
  793. */
  794. /*
  795. * sd = sd + (sn * sm)
  796. */
  797. static u32 vfp_single_fmac(int sd, int sn, s32 m, u32 fpscr)
  798. {
  799. return vfp_single_multiply_accumulate(sd, sn, m, fpscr, 0, "fmac");
  800. }
  801. /*
  802. * sd = sd - (sn * sm)
  803. */
  804. static u32 vfp_single_fnmac(int sd, int sn, s32 m, u32 fpscr)
  805. {
  806. return vfp_single_multiply_accumulate(sd, sn, m, fpscr, NEG_MULTIPLY, "fnmac");
  807. }
  808. /*
  809. * sd = -sd + (sn * sm)
  810. */
  811. static u32 vfp_single_fmsc(int sd, int sn, s32 m, u32 fpscr)
  812. {
  813. return vfp_single_multiply_accumulate(sd, sn, m, fpscr, NEG_SUBTRACT, "fmsc");
  814. }
  815. /*
  816. * sd = -sd - (sn * sm)
  817. */
  818. static u32 vfp_single_fnmsc(int sd, int sn, s32 m, u32 fpscr)
  819. {
  820. return vfp_single_multiply_accumulate(sd, sn, m, fpscr, NEG_SUBTRACT | NEG_MULTIPLY, "fnmsc");
  821. }
  822. /*
  823. * sd = sn * sm
  824. */
  825. static u32 vfp_single_fmul(int sd, int sn, s32 m, u32 fpscr)
  826. {
  827. struct vfp_single vsd, vsn, vsm;
  828. u32 exceptions;
  829. s32 n = vfp_get_float(sn);
  830. pr_debug("VFP: s%u = %08x\n", sn, n);
  831. vfp_single_unpack(&vsn, n);
  832. if (vsn.exponent == 0 && vsn.significand)
  833. vfp_single_normalise_denormal(&vsn);
  834. vfp_single_unpack(&vsm, m);
  835. if (vsm.exponent == 0 && vsm.significand)
  836. vfp_single_normalise_denormal(&vsm);
  837. exceptions = vfp_single_multiply(&vsd, &vsn, &vsm, fpscr);
  838. return vfp_single_normaliseround(sd, &vsd, fpscr, exceptions, "fmul");
  839. }
  840. /*
  841. * sd = -(sn * sm)
  842. */
  843. static u32 vfp_single_fnmul(int sd, int sn, s32 m, u32 fpscr)
  844. {
  845. struct vfp_single vsd, vsn, vsm;
  846. u32 exceptions;
  847. s32 n = vfp_get_float(sn);
  848. pr_debug("VFP: s%u = %08x\n", sn, n);
  849. vfp_single_unpack(&vsn, n);
  850. if (vsn.exponent == 0 && vsn.significand)
  851. vfp_single_normalise_denormal(&vsn);
  852. vfp_single_unpack(&vsm, m);
  853. if (vsm.exponent == 0 && vsm.significand)
  854. vfp_single_normalise_denormal(&vsm);
  855. exceptions = vfp_single_multiply(&vsd, &vsn, &vsm, fpscr);
  856. vsd.sign = vfp_sign_negate(vsd.sign);
  857. return vfp_single_normaliseround(sd, &vsd, fpscr, exceptions, "fnmul");
  858. }
  859. /*
  860. * sd = sn + sm
  861. */
  862. static u32 vfp_single_fadd(int sd, int sn, s32 m, u32 fpscr)
  863. {
  864. struct vfp_single vsd, vsn, vsm;
  865. u32 exceptions;
  866. s32 n = vfp_get_float(sn);
  867. pr_debug("VFP: s%u = %08x\n", sn, n);
  868. /*
  869. * Unpack and normalise denormals.
  870. */
  871. vfp_single_unpack(&vsn, n);
  872. if (vsn.exponent == 0 && vsn.significand)
  873. vfp_single_normalise_denormal(&vsn);
  874. vfp_single_unpack(&vsm, m);
  875. if (vsm.exponent == 0 && vsm.significand)
  876. vfp_single_normalise_denormal(&vsm);
  877. exceptions = vfp_single_add(&vsd, &vsn, &vsm, fpscr);
  878. return vfp_single_normaliseround(sd, &vsd, fpscr, exceptions, "fadd");
  879. }
  880. /*
  881. * sd = sn - sm
  882. */
  883. static u32 vfp_single_fsub(int sd, int sn, s32 m, u32 fpscr)
  884. {
  885. /*
  886. * Subtraction is addition with one sign inverted.
  887. */
  888. return vfp_single_fadd(sd, sn, vfp_single_packed_negate(m), fpscr);
  889. }
  890. /*
  891. * sd = sn / sm
  892. */
  893. static u32 vfp_single_fdiv(int sd, int sn, s32 m, u32 fpscr)
  894. {
  895. struct vfp_single vsd, vsn, vsm;
  896. u32 exceptions = 0;
  897. s32 n = vfp_get_float(sn);
  898. int tm, tn;
  899. pr_debug("VFP: s%u = %08x\n", sn, n);
  900. vfp_single_unpack(&vsn, n);
  901. vfp_single_unpack(&vsm, m);
  902. vsd.sign = vsn.sign ^ vsm.sign;
  903. tn = vfp_single_type(&vsn);
  904. tm = vfp_single_type(&vsm);
  905. /*
  906. * Is n a NAN?
  907. */
  908. if (tn & VFP_NAN)
  909. goto vsn_nan;
  910. /*
  911. * Is m a NAN?
  912. */
  913. if (tm & VFP_NAN)
  914. goto vsm_nan;
  915. /*
  916. * If n and m are infinity, the result is invalid
  917. * If n and m are zero, the result is invalid
  918. */
  919. if (tm & tn & (VFP_INFINITY|VFP_ZERO))
  920. goto invalid;
  921. /*
  922. * If n is infinity, the result is infinity
  923. */
  924. if (tn & VFP_INFINITY)
  925. goto infinity;
  926. /*
  927. * If m is zero, raise div0 exception
  928. */
  929. if (tm & VFP_ZERO)
  930. goto divzero;
  931. /*
  932. * If m is infinity, or n is zero, the result is zero
  933. */
  934. if (tm & VFP_INFINITY || tn & VFP_ZERO)
  935. goto zero;
  936. if (tn & VFP_DENORMAL)
  937. vfp_single_normalise_denormal(&vsn);
  938. if (tm & VFP_DENORMAL)
  939. vfp_single_normalise_denormal(&vsm);
  940. /*
  941. * Ok, we have two numbers, we can perform division.
  942. */
  943. vsd.exponent = vsn.exponent - vsm.exponent + 127 - 1;
  944. vsm.significand <<= 1;
  945. if (vsm.significand <= (2 * vsn.significand)) {
  946. vsn.significand >>= 1;
  947. vsd.exponent++;
  948. }
  949. {
  950. u64 significand = (u64)vsn.significand << 32;
  951. do_div(significand, vsm.significand);
  952. vsd.significand = significand;
  953. }
  954. if ((vsd.significand & 0x3f) == 0)
  955. vsd.significand |= ((u64)vsm.significand * vsd.significand != (u64)vsn.significand << 32);
  956. return vfp_single_normaliseround(sd, &vsd, fpscr, 0, "fdiv");
  957. vsn_nan:
  958. exceptions = vfp_propagate_nan(&vsd, &vsn, &vsm, fpscr);
  959. pack:
  960. vfp_put_float(sd, vfp_single_pack(&vsd));
  961. return exceptions;
  962. vsm_nan:
  963. exceptions = vfp_propagate_nan(&vsd, &vsm, &vsn, fpscr);
  964. goto pack;
  965. zero:
  966. vsd.exponent = 0;
  967. vsd.significand = 0;
  968. goto pack;
  969. divzero:
  970. exceptions = FPSCR_DZC;
  971. infinity:
  972. vsd.exponent = 255;
  973. vsd.significand = 0;
  974. goto pack;
  975. invalid:
  976. vfp_put_float(sd, vfp_single_pack(&vfp_single_default_qnan));
  977. return FPSCR_IOC;
  978. }
  979. static u32 (* const fop_fns[16])(int sd, int sn, s32 m, u32 fpscr) = {
  980. [FOP_TO_IDX(FOP_FMAC)] = vfp_single_fmac,
  981. [FOP_TO_IDX(FOP_FNMAC)] = vfp_single_fnmac,
  982. [FOP_TO_IDX(FOP_FMSC)] = vfp_single_fmsc,
  983. [FOP_TO_IDX(FOP_FNMSC)] = vfp_single_fnmsc,
  984. [FOP_TO_IDX(FOP_FMUL)] = vfp_single_fmul,
  985. [FOP_TO_IDX(FOP_FNMUL)] = vfp_single_fnmul,
  986. [FOP_TO_IDX(FOP_FADD)] = vfp_single_fadd,
  987. [FOP_TO_IDX(FOP_FSUB)] = vfp_single_fsub,
  988. [FOP_TO_IDX(FOP_FDIV)] = vfp_single_fdiv,
  989. };
  990. #define FREG_BANK(x) ((x) & 0x18)
  991. #define FREG_IDX(x) ((x) & 7)
  992. u32 vfp_single_cpdo(u32 inst, u32 fpscr)
  993. {
  994. u32 op = inst & FOP_MASK;
  995. u32 exceptions = 0;
  996. unsigned int sd = vfp_get_sd(inst);
  997. unsigned int sn = vfp_get_sn(inst);
  998. unsigned int sm = vfp_get_sm(inst);
  999. unsigned int vecitr, veclen, vecstride;
  1000. u32 (*fop)(int, int, s32, u32);
  1001. veclen = fpscr & FPSCR_LENGTH_MASK;
  1002. vecstride = 1 + ((fpscr & FPSCR_STRIDE_MASK) == FPSCR_STRIDE_MASK);
  1003. /*
  1004. * If destination bank is zero, vector length is always '1'.
  1005. * ARM DDI0100F C5.1.3, C5.3.2.
  1006. */
  1007. if (FREG_BANK(sd) == 0)
  1008. veclen = 0;
  1009. pr_debug("VFP: vecstride=%u veclen=%u\n", vecstride,
  1010. (veclen >> FPSCR_LENGTH_BIT) + 1);
  1011. fop = (op == FOP_EXT) ? fop_extfns[sn] : fop_fns[FOP_TO_IDX(op)];
  1012. if (!fop)
  1013. goto invalid;
  1014. for (vecitr = 0; vecitr <= veclen; vecitr += 1 << FPSCR_LENGTH_BIT) {
  1015. s32 m = vfp_get_float(sm);
  1016. u32 except;
  1017. if (op == FOP_EXT)
  1018. pr_debug("VFP: itr%d (s%u) = op[%u] (s%u=%08x)\n",
  1019. vecitr >> FPSCR_LENGTH_BIT, sd, sn, sm, m);
  1020. else
  1021. pr_debug("VFP: itr%d (s%u) = (s%u) op[%u] (s%u=%08x)\n",
  1022. vecitr >> FPSCR_LENGTH_BIT, sd, sn,
  1023. FOP_TO_IDX(op), sm, m);
  1024. except = fop(sd, sn, m, fpscr);
  1025. pr_debug("VFP: itr%d: exceptions=%08x\n",
  1026. vecitr >> FPSCR_LENGTH_BIT, except);
  1027. exceptions |= except;
  1028. /*
  1029. * This ensures that comparisons only operate on scalars;
  1030. * comparisons always return with one FPSCR status bit set.
  1031. */
  1032. if (except & (FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V))
  1033. break;
  1034. /*
  1035. * CHECK: It appears to be undefined whether we stop when
  1036. * we encounter an exception. We continue.
  1037. */
  1038. sd = FREG_BANK(sd) + ((FREG_IDX(sd) + vecstride) & 7);
  1039. sn = FREG_BANK(sn) + ((FREG_IDX(sn) + vecstride) & 7);
  1040. if (FREG_BANK(sm) != 0)
  1041. sm = FREG_BANK(sm) + ((FREG_IDX(sm) + vecstride) & 7);
  1042. }
  1043. return exceptions;
  1044. invalid:
  1045. return (u32)-1;
  1046. }