vfpsingle.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  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. int tm;
  546. vfp_single_unpack(&vsm, m);
  547. vfp_single_dump("VSM", &vsm);
  548. /*
  549. * Do we have a denormalised number?
  550. */
  551. tm = vfp_single_type(&vsm);
  552. if (vfp_single_type(&vsm) & VFP_DENORMAL)
  553. exceptions |= FPSCR_IDC;
  554. if (tm & VFP_NAN) {
  555. d = 0;
  556. exceptions |= FPSCR_IOC;
  557. } else if (vsm.exponent >= 127 + 32) {
  558. /*
  559. * m >= 2^31-2^7: invalid
  560. */
  561. d = 0x7fffffff;
  562. if (vsm.sign)
  563. d = ~d;
  564. exceptions |= FPSCR_IOC;
  565. } else if (vsm.exponent >= 127 - 1) {
  566. int shift = 127 + 31 - vsm.exponent;
  567. u32 rem, incr = 0;
  568. /* 2^0 <= m <= 2^31-2^7 */
  569. d = (vsm.significand << 1) >> shift;
  570. rem = vsm.significand << (33 - shift);
  571. if (rmode == FPSCR_ROUND_NEAREST) {
  572. incr = 0x80000000;
  573. if ((d & 1) == 0)
  574. incr -= 1;
  575. } else if (rmode == FPSCR_ROUND_TOZERO) {
  576. incr = 0;
  577. } else if ((rmode == FPSCR_ROUND_PLUSINF) ^ (vsm.sign != 0)) {
  578. incr = ~0;
  579. }
  580. if ((rem + incr) < rem && d < 0xffffffff)
  581. d += 1;
  582. if (d > 0x7fffffff + (vsm.sign != 0)) {
  583. d = 0x7fffffff + (vsm.sign != 0);
  584. exceptions |= FPSCR_IOC;
  585. } else if (rem)
  586. exceptions |= FPSCR_IXC;
  587. if (vsm.sign)
  588. d = -d;
  589. } else {
  590. d = 0;
  591. if (vsm.exponent | vsm.significand) {
  592. exceptions |= FPSCR_IXC;
  593. if (rmode == FPSCR_ROUND_PLUSINF && vsm.sign == 0)
  594. d = 1;
  595. else if (rmode == FPSCR_ROUND_MINUSINF && vsm.sign)
  596. d = -1;
  597. }
  598. }
  599. pr_debug("VFP: ftosi: d(s%d)=%08x exceptions=%08x\n", sd, d, exceptions);
  600. vfp_put_float(sd, (s32)d);
  601. return exceptions;
  602. }
  603. static u32 vfp_single_ftosiz(int sd, int unused, s32 m, u32 fpscr)
  604. {
  605. return vfp_single_ftosi(sd, unused, m, FPSCR_ROUND_TOZERO);
  606. }
  607. static u32 (* const fop_extfns[32])(int sd, int unused, s32 m, u32 fpscr) = {
  608. [FEXT_TO_IDX(FEXT_FCPY)] = vfp_single_fcpy,
  609. [FEXT_TO_IDX(FEXT_FABS)] = vfp_single_fabs,
  610. [FEXT_TO_IDX(FEXT_FNEG)] = vfp_single_fneg,
  611. [FEXT_TO_IDX(FEXT_FSQRT)] = vfp_single_fsqrt,
  612. [FEXT_TO_IDX(FEXT_FCMP)] = vfp_single_fcmp,
  613. [FEXT_TO_IDX(FEXT_FCMPE)] = vfp_single_fcmpe,
  614. [FEXT_TO_IDX(FEXT_FCMPZ)] = vfp_single_fcmpz,
  615. [FEXT_TO_IDX(FEXT_FCMPEZ)] = vfp_single_fcmpez,
  616. [FEXT_TO_IDX(FEXT_FCVT)] = vfp_single_fcvtd,
  617. [FEXT_TO_IDX(FEXT_FUITO)] = vfp_single_fuito,
  618. [FEXT_TO_IDX(FEXT_FSITO)] = vfp_single_fsito,
  619. [FEXT_TO_IDX(FEXT_FTOUI)] = vfp_single_ftoui,
  620. [FEXT_TO_IDX(FEXT_FTOUIZ)] = vfp_single_ftouiz,
  621. [FEXT_TO_IDX(FEXT_FTOSI)] = vfp_single_ftosi,
  622. [FEXT_TO_IDX(FEXT_FTOSIZ)] = vfp_single_ftosiz,
  623. };
  624. static u32
  625. vfp_single_fadd_nonnumber(struct vfp_single *vsd, struct vfp_single *vsn,
  626. struct vfp_single *vsm, u32 fpscr)
  627. {
  628. struct vfp_single *vsp;
  629. u32 exceptions = 0;
  630. int tn, tm;
  631. tn = vfp_single_type(vsn);
  632. tm = vfp_single_type(vsm);
  633. if (tn & tm & VFP_INFINITY) {
  634. /*
  635. * Two infinities. Are they different signs?
  636. */
  637. if (vsn->sign ^ vsm->sign) {
  638. /*
  639. * different signs -> invalid
  640. */
  641. exceptions = FPSCR_IOC;
  642. vsp = &vfp_single_default_qnan;
  643. } else {
  644. /*
  645. * same signs -> valid
  646. */
  647. vsp = vsn;
  648. }
  649. } else if (tn & VFP_INFINITY && tm & VFP_NUMBER) {
  650. /*
  651. * One infinity and one number -> infinity
  652. */
  653. vsp = vsn;
  654. } else {
  655. /*
  656. * 'n' is a NaN of some type
  657. */
  658. return vfp_propagate_nan(vsd, vsn, vsm, fpscr);
  659. }
  660. *vsd = *vsp;
  661. return exceptions;
  662. }
  663. static u32
  664. vfp_single_add(struct vfp_single *vsd, struct vfp_single *vsn,
  665. struct vfp_single *vsm, u32 fpscr)
  666. {
  667. u32 exp_diff, m_sig;
  668. if (vsn->significand & 0x80000000 ||
  669. vsm->significand & 0x80000000) {
  670. pr_info("VFP: bad FP values in %s\n", __func__);
  671. vfp_single_dump("VSN", vsn);
  672. vfp_single_dump("VSM", vsm);
  673. }
  674. /*
  675. * Ensure that 'n' is the largest magnitude number. Note that
  676. * if 'n' and 'm' have equal exponents, we do not swap them.
  677. * This ensures that NaN propagation works correctly.
  678. */
  679. if (vsn->exponent < vsm->exponent) {
  680. struct vfp_single *t = vsn;
  681. vsn = vsm;
  682. vsm = t;
  683. }
  684. /*
  685. * Is 'n' an infinity or a NaN? Note that 'm' may be a number,
  686. * infinity or a NaN here.
  687. */
  688. if (vsn->exponent == 255)
  689. return vfp_single_fadd_nonnumber(vsd, vsn, vsm, fpscr);
  690. /*
  691. * We have two proper numbers, where 'vsn' is the larger magnitude.
  692. *
  693. * Copy 'n' to 'd' before doing the arithmetic.
  694. */
  695. *vsd = *vsn;
  696. /*
  697. * Align both numbers.
  698. */
  699. exp_diff = vsn->exponent - vsm->exponent;
  700. m_sig = vfp_shiftright32jamming(vsm->significand, exp_diff);
  701. /*
  702. * If the signs are different, we are really subtracting.
  703. */
  704. if (vsn->sign ^ vsm->sign) {
  705. m_sig = vsn->significand - m_sig;
  706. if ((s32)m_sig < 0) {
  707. vsd->sign = vfp_sign_negate(vsd->sign);
  708. m_sig = -m_sig;
  709. } else if (m_sig == 0) {
  710. vsd->sign = (fpscr & FPSCR_RMODE_MASK) ==
  711. FPSCR_ROUND_MINUSINF ? 0x8000 : 0;
  712. }
  713. } else {
  714. m_sig = vsn->significand + m_sig;
  715. }
  716. vsd->significand = m_sig;
  717. return 0;
  718. }
  719. static u32
  720. vfp_single_multiply(struct vfp_single *vsd, struct vfp_single *vsn, struct vfp_single *vsm, u32 fpscr)
  721. {
  722. vfp_single_dump("VSN", vsn);
  723. vfp_single_dump("VSM", vsm);
  724. /*
  725. * Ensure that 'n' is the largest magnitude number. Note that
  726. * if 'n' and 'm' have equal exponents, we do not swap them.
  727. * This ensures that NaN propagation works correctly.
  728. */
  729. if (vsn->exponent < vsm->exponent) {
  730. struct vfp_single *t = vsn;
  731. vsn = vsm;
  732. vsm = t;
  733. pr_debug("VFP: swapping M <-> N\n");
  734. }
  735. vsd->sign = vsn->sign ^ vsm->sign;
  736. /*
  737. * If 'n' is an infinity or NaN, handle it. 'm' may be anything.
  738. */
  739. if (vsn->exponent == 255) {
  740. if (vsn->significand || (vsm->exponent == 255 && vsm->significand))
  741. return vfp_propagate_nan(vsd, vsn, vsm, fpscr);
  742. if ((vsm->exponent | vsm->significand) == 0) {
  743. *vsd = vfp_single_default_qnan;
  744. return FPSCR_IOC;
  745. }
  746. vsd->exponent = vsn->exponent;
  747. vsd->significand = 0;
  748. return 0;
  749. }
  750. /*
  751. * If 'm' is zero, the result is always zero. In this case,
  752. * 'n' may be zero or a number, but it doesn't matter which.
  753. */
  754. if ((vsm->exponent | vsm->significand) == 0) {
  755. vsd->exponent = 0;
  756. vsd->significand = 0;
  757. return 0;
  758. }
  759. /*
  760. * We add 2 to the destination exponent for the same reason as
  761. * the addition case - though this time we have +1 from each
  762. * input operand.
  763. */
  764. vsd->exponent = vsn->exponent + vsm->exponent - 127 + 2;
  765. vsd->significand = vfp_hi64to32jamming((u64)vsn->significand * vsm->significand);
  766. vfp_single_dump("VSD", vsd);
  767. return 0;
  768. }
  769. #define NEG_MULTIPLY (1 << 0)
  770. #define NEG_SUBTRACT (1 << 1)
  771. static u32
  772. vfp_single_multiply_accumulate(int sd, int sn, s32 m, u32 fpscr, u32 negate, char *func)
  773. {
  774. struct vfp_single vsd, vsp, vsn, vsm;
  775. u32 exceptions;
  776. s32 v;
  777. v = vfp_get_float(sn);
  778. pr_debug("VFP: s%u = %08x\n", sn, v);
  779. vfp_single_unpack(&vsn, v);
  780. if (vsn.exponent == 0 && vsn.significand)
  781. vfp_single_normalise_denormal(&vsn);
  782. vfp_single_unpack(&vsm, m);
  783. if (vsm.exponent == 0 && vsm.significand)
  784. vfp_single_normalise_denormal(&vsm);
  785. exceptions = vfp_single_multiply(&vsp, &vsn, &vsm, fpscr);
  786. if (negate & NEG_MULTIPLY)
  787. vsp.sign = vfp_sign_negate(vsp.sign);
  788. v = vfp_get_float(sd);
  789. pr_debug("VFP: s%u = %08x\n", sd, v);
  790. vfp_single_unpack(&vsn, v);
  791. if (negate & NEG_SUBTRACT)
  792. vsn.sign = vfp_sign_negate(vsn.sign);
  793. exceptions |= vfp_single_add(&vsd, &vsn, &vsp, fpscr);
  794. return vfp_single_normaliseround(sd, &vsd, fpscr, exceptions, func);
  795. }
  796. /*
  797. * Standard operations
  798. */
  799. /*
  800. * sd = sd + (sn * sm)
  801. */
  802. static u32 vfp_single_fmac(int sd, int sn, s32 m, u32 fpscr)
  803. {
  804. return vfp_single_multiply_accumulate(sd, sn, m, fpscr, 0, "fmac");
  805. }
  806. /*
  807. * sd = sd - (sn * sm)
  808. */
  809. static u32 vfp_single_fnmac(int sd, int sn, s32 m, u32 fpscr)
  810. {
  811. return vfp_single_multiply_accumulate(sd, sn, m, fpscr, NEG_MULTIPLY, "fnmac");
  812. }
  813. /*
  814. * sd = -sd + (sn * sm)
  815. */
  816. static u32 vfp_single_fmsc(int sd, int sn, s32 m, u32 fpscr)
  817. {
  818. return vfp_single_multiply_accumulate(sd, sn, m, fpscr, NEG_SUBTRACT, "fmsc");
  819. }
  820. /*
  821. * sd = -sd - (sn * sm)
  822. */
  823. static u32 vfp_single_fnmsc(int sd, int sn, s32 m, u32 fpscr)
  824. {
  825. return vfp_single_multiply_accumulate(sd, sn, m, fpscr, NEG_SUBTRACT | NEG_MULTIPLY, "fnmsc");
  826. }
  827. /*
  828. * sd = sn * sm
  829. */
  830. static u32 vfp_single_fmul(int sd, int sn, s32 m, u32 fpscr)
  831. {
  832. struct vfp_single vsd, vsn, vsm;
  833. u32 exceptions;
  834. s32 n = vfp_get_float(sn);
  835. pr_debug("VFP: s%u = %08x\n", sn, n);
  836. vfp_single_unpack(&vsn, n);
  837. if (vsn.exponent == 0 && vsn.significand)
  838. vfp_single_normalise_denormal(&vsn);
  839. vfp_single_unpack(&vsm, m);
  840. if (vsm.exponent == 0 && vsm.significand)
  841. vfp_single_normalise_denormal(&vsm);
  842. exceptions = vfp_single_multiply(&vsd, &vsn, &vsm, fpscr);
  843. return vfp_single_normaliseround(sd, &vsd, fpscr, exceptions, "fmul");
  844. }
  845. /*
  846. * sd = -(sn * sm)
  847. */
  848. static u32 vfp_single_fnmul(int sd, int sn, s32 m, u32 fpscr)
  849. {
  850. struct vfp_single vsd, vsn, vsm;
  851. u32 exceptions;
  852. s32 n = vfp_get_float(sn);
  853. pr_debug("VFP: s%u = %08x\n", sn, n);
  854. vfp_single_unpack(&vsn, n);
  855. if (vsn.exponent == 0 && vsn.significand)
  856. vfp_single_normalise_denormal(&vsn);
  857. vfp_single_unpack(&vsm, m);
  858. if (vsm.exponent == 0 && vsm.significand)
  859. vfp_single_normalise_denormal(&vsm);
  860. exceptions = vfp_single_multiply(&vsd, &vsn, &vsm, fpscr);
  861. vsd.sign = vfp_sign_negate(vsd.sign);
  862. return vfp_single_normaliseround(sd, &vsd, fpscr, exceptions, "fnmul");
  863. }
  864. /*
  865. * sd = sn + sm
  866. */
  867. static u32 vfp_single_fadd(int sd, int sn, s32 m, u32 fpscr)
  868. {
  869. struct vfp_single vsd, vsn, vsm;
  870. u32 exceptions;
  871. s32 n = vfp_get_float(sn);
  872. pr_debug("VFP: s%u = %08x\n", sn, n);
  873. /*
  874. * Unpack and normalise denormals.
  875. */
  876. vfp_single_unpack(&vsn, n);
  877. if (vsn.exponent == 0 && vsn.significand)
  878. vfp_single_normalise_denormal(&vsn);
  879. vfp_single_unpack(&vsm, m);
  880. if (vsm.exponent == 0 && vsm.significand)
  881. vfp_single_normalise_denormal(&vsm);
  882. exceptions = vfp_single_add(&vsd, &vsn, &vsm, fpscr);
  883. return vfp_single_normaliseround(sd, &vsd, fpscr, exceptions, "fadd");
  884. }
  885. /*
  886. * sd = sn - sm
  887. */
  888. static u32 vfp_single_fsub(int sd, int sn, s32 m, u32 fpscr)
  889. {
  890. /*
  891. * Subtraction is addition with one sign inverted.
  892. */
  893. return vfp_single_fadd(sd, sn, vfp_single_packed_negate(m), fpscr);
  894. }
  895. /*
  896. * sd = sn / sm
  897. */
  898. static u32 vfp_single_fdiv(int sd, int sn, s32 m, u32 fpscr)
  899. {
  900. struct vfp_single vsd, vsn, vsm;
  901. u32 exceptions = 0;
  902. s32 n = vfp_get_float(sn);
  903. int tm, tn;
  904. pr_debug("VFP: s%u = %08x\n", sn, n);
  905. vfp_single_unpack(&vsn, n);
  906. vfp_single_unpack(&vsm, m);
  907. vsd.sign = vsn.sign ^ vsm.sign;
  908. tn = vfp_single_type(&vsn);
  909. tm = vfp_single_type(&vsm);
  910. /*
  911. * Is n a NAN?
  912. */
  913. if (tn & VFP_NAN)
  914. goto vsn_nan;
  915. /*
  916. * Is m a NAN?
  917. */
  918. if (tm & VFP_NAN)
  919. goto vsm_nan;
  920. /*
  921. * If n and m are infinity, the result is invalid
  922. * If n and m are zero, the result is invalid
  923. */
  924. if (tm & tn & (VFP_INFINITY|VFP_ZERO))
  925. goto invalid;
  926. /*
  927. * If n is infinity, the result is infinity
  928. */
  929. if (tn & VFP_INFINITY)
  930. goto infinity;
  931. /*
  932. * If m is zero, raise div0 exception
  933. */
  934. if (tm & VFP_ZERO)
  935. goto divzero;
  936. /*
  937. * If m is infinity, or n is zero, the result is zero
  938. */
  939. if (tm & VFP_INFINITY || tn & VFP_ZERO)
  940. goto zero;
  941. if (tn & VFP_DENORMAL)
  942. vfp_single_normalise_denormal(&vsn);
  943. if (tm & VFP_DENORMAL)
  944. vfp_single_normalise_denormal(&vsm);
  945. /*
  946. * Ok, we have two numbers, we can perform division.
  947. */
  948. vsd.exponent = vsn.exponent - vsm.exponent + 127 - 1;
  949. vsm.significand <<= 1;
  950. if (vsm.significand <= (2 * vsn.significand)) {
  951. vsn.significand >>= 1;
  952. vsd.exponent++;
  953. }
  954. {
  955. u64 significand = (u64)vsn.significand << 32;
  956. do_div(significand, vsm.significand);
  957. vsd.significand = significand;
  958. }
  959. if ((vsd.significand & 0x3f) == 0)
  960. vsd.significand |= ((u64)vsm.significand * vsd.significand != (u64)vsn.significand << 32);
  961. return vfp_single_normaliseround(sd, &vsd, fpscr, 0, "fdiv");
  962. vsn_nan:
  963. exceptions = vfp_propagate_nan(&vsd, &vsn, &vsm, fpscr);
  964. pack:
  965. vfp_put_float(sd, vfp_single_pack(&vsd));
  966. return exceptions;
  967. vsm_nan:
  968. exceptions = vfp_propagate_nan(&vsd, &vsm, &vsn, fpscr);
  969. goto pack;
  970. zero:
  971. vsd.exponent = 0;
  972. vsd.significand = 0;
  973. goto pack;
  974. divzero:
  975. exceptions = FPSCR_DZC;
  976. infinity:
  977. vsd.exponent = 255;
  978. vsd.significand = 0;
  979. goto pack;
  980. invalid:
  981. vfp_put_float(sd, vfp_single_pack(&vfp_single_default_qnan));
  982. return FPSCR_IOC;
  983. }
  984. static u32 (* const fop_fns[16])(int sd, int sn, s32 m, u32 fpscr) = {
  985. [FOP_TO_IDX(FOP_FMAC)] = vfp_single_fmac,
  986. [FOP_TO_IDX(FOP_FNMAC)] = vfp_single_fnmac,
  987. [FOP_TO_IDX(FOP_FMSC)] = vfp_single_fmsc,
  988. [FOP_TO_IDX(FOP_FNMSC)] = vfp_single_fnmsc,
  989. [FOP_TO_IDX(FOP_FMUL)] = vfp_single_fmul,
  990. [FOP_TO_IDX(FOP_FNMUL)] = vfp_single_fnmul,
  991. [FOP_TO_IDX(FOP_FADD)] = vfp_single_fadd,
  992. [FOP_TO_IDX(FOP_FSUB)] = vfp_single_fsub,
  993. [FOP_TO_IDX(FOP_FDIV)] = vfp_single_fdiv,
  994. };
  995. #define FREG_BANK(x) ((x) & 0x18)
  996. #define FREG_IDX(x) ((x) & 7)
  997. u32 vfp_single_cpdo(u32 inst, u32 fpscr)
  998. {
  999. u32 op = inst & FOP_MASK;
  1000. u32 exceptions = 0;
  1001. unsigned int sd = vfp_get_sd(inst);
  1002. unsigned int sn = vfp_get_sn(inst);
  1003. unsigned int sm = vfp_get_sm(inst);
  1004. unsigned int vecitr, veclen, vecstride;
  1005. u32 (*fop)(int, int, s32, u32);
  1006. veclen = fpscr & FPSCR_LENGTH_MASK;
  1007. vecstride = 1 + ((fpscr & FPSCR_STRIDE_MASK) == FPSCR_STRIDE_MASK);
  1008. /*
  1009. * If destination bank is zero, vector length is always '1'.
  1010. * ARM DDI0100F C5.1.3, C5.3.2.
  1011. */
  1012. if (FREG_BANK(sd) == 0)
  1013. veclen = 0;
  1014. pr_debug("VFP: vecstride=%u veclen=%u\n", vecstride,
  1015. (veclen >> FPSCR_LENGTH_BIT) + 1);
  1016. fop = (op == FOP_EXT) ? fop_extfns[FEXT_TO_IDX(inst)] : fop_fns[FOP_TO_IDX(op)];
  1017. if (!fop)
  1018. goto invalid;
  1019. for (vecitr = 0; vecitr <= veclen; vecitr += 1 << FPSCR_LENGTH_BIT) {
  1020. s32 m = vfp_get_float(sm);
  1021. u32 except;
  1022. if (op == FOP_EXT)
  1023. pr_debug("VFP: itr%d (s%u) = op[%u] (s%u=%08x)\n",
  1024. vecitr >> FPSCR_LENGTH_BIT, sd, sn, sm, m);
  1025. else
  1026. pr_debug("VFP: itr%d (s%u) = (s%u) op[%u] (s%u=%08x)\n",
  1027. vecitr >> FPSCR_LENGTH_BIT, sd, sn,
  1028. FOP_TO_IDX(op), sm, m);
  1029. except = fop(sd, sn, m, fpscr);
  1030. pr_debug("VFP: itr%d: exceptions=%08x\n",
  1031. vecitr >> FPSCR_LENGTH_BIT, except);
  1032. exceptions |= except;
  1033. /*
  1034. * This ensures that comparisons only operate on scalars;
  1035. * comparisons always return with one FPSCR status bit set.
  1036. */
  1037. if (except & (FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V))
  1038. break;
  1039. /*
  1040. * CHECK: It appears to be undefined whether we stop when
  1041. * we encounter an exception. We continue.
  1042. */
  1043. sd = FREG_BANK(sd) + ((FREG_IDX(sd) + vecstride) & 7);
  1044. sn = FREG_BANK(sn) + ((FREG_IDX(sn) + vecstride) & 7);
  1045. if (FREG_BANK(sm) != 0)
  1046. sm = FREG_BANK(sm) + ((FREG_IDX(sm) + vecstride) & 7);
  1047. }
  1048. return exceptions;
  1049. invalid:
  1050. return (u32)-1;
  1051. }