vfpdouble.c 28 KB

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