vfpsingle.c 28 KB

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