vfpsingle.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  1. /*
  2. vfp/vfpsingle.c - ARM VFPv3 emulation unit - SoftFloat single instruction
  3. Copyright (C) 2003 Skyeye Develop Group
  4. for help please send mail to <skyeye-developer@lists.gro.clinux.org>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. /*
  18. * This code is derived in part from :
  19. * - Android kernel
  20. * - John R. Housers softfloat library, which
  21. * carries the following notice:
  22. *
  23. * ===========================================================================
  24. * This C source file is part of the SoftFloat IEC/IEEE Floating-point
  25. * Arithmetic Package, Release 2.
  26. *
  27. * Written by John R. Hauser. This work was made possible in part by the
  28. * International Computer Science Institute, located at Suite 600, 1947 Center
  29. * Street, Berkeley, California 94704. Funding was partially provided by the
  30. * National Science Foundation under grant MIP-9311980. The original version
  31. * of this code was written as part of a project to build a fixed-point vector
  32. * processor in collaboration with the University of California at Berkeley,
  33. * overseen by Profs. Nelson Morgan and John Wawrzynek. More information
  34. * is available through the web page `http://HTTP.CS.Berkeley.EDU/~jhauser/
  35. * arithmetic/softfloat.html'.
  36. *
  37. * THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort
  38. * has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
  39. * TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO
  40. * PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
  41. * AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
  42. *
  43. * Derivative works are acceptable, even for commercial purposes, so long as
  44. * (1) they include prominent notice that the work is derivative, and (2) they
  45. * include prominent notice akin to these three paragraphs for those parts of
  46. * this code that are retained.
  47. * ===========================================================================
  48. */
  49. #include "core/arm/skyeye_common/vfp/vfp_helper.h"
  50. #include "core/arm/skyeye_common/vfp/asm_vfp.h"
  51. #include "core/arm/skyeye_common/vfp/vfp.h"
  52. static struct vfp_single vfp_single_default_qnan = {
  53. 255,
  54. 0,
  55. VFP_SINGLE_SIGNIFICAND_QNAN,
  56. };
  57. static void vfp_single_dump(const char *str, struct vfp_single *s)
  58. {
  59. pr_debug("VFP: %s: sign=%d exponent=%d significand=%08x\n",
  60. str, s->sign != 0, s->exponent, s->significand);
  61. }
  62. static void vfp_single_normalise_denormal(struct vfp_single *vs)
  63. {
  64. int bits = 31 - fls(vs->significand);
  65. vfp_single_dump("normalise_denormal: in", vs);
  66. if (bits) {
  67. vs->exponent -= bits - 1;
  68. vs->significand <<= bits;
  69. }
  70. vfp_single_dump("normalise_denormal: out", vs);
  71. }
  72. u32 vfp_single_normaliseround(ARMul_State* state, int sd, struct vfp_single *vs, u32 fpscr, u32 exceptions, const char *func)
  73. {
  74. u32 significand, incr, rmode;
  75. int exponent, shift, underflow;
  76. vfp_single_dump("pack: in", vs);
  77. /*
  78. * Infinities and NaNs are a special case.
  79. */
  80. if (vs->exponent == 255 && (vs->significand == 0 || exceptions))
  81. goto pack;
  82. /*
  83. * Special-case zero.
  84. */
  85. if (vs->significand == 0) {
  86. vs->exponent = 0;
  87. goto pack;
  88. }
  89. exponent = vs->exponent;
  90. significand = vs->significand;
  91. /*
  92. * Normalise first. Note that we shift the significand up to
  93. * bit 31, so we have VFP_SINGLE_LOW_BITS + 1 below the least
  94. * significant bit.
  95. */
  96. shift = 32 - fls(significand);
  97. if (shift < 32 && shift) {
  98. exponent -= shift;
  99. significand <<= shift;
  100. }
  101. #if 1
  102. vs->exponent = exponent;
  103. vs->significand = significand;
  104. vfp_single_dump("pack: normalised", vs);
  105. #endif
  106. /*
  107. * Tiny number?
  108. */
  109. underflow = exponent < 0;
  110. if (underflow) {
  111. significand = vfp_shiftright32jamming(significand, -exponent);
  112. exponent = 0;
  113. #if 1
  114. vs->exponent = exponent;
  115. vs->significand = significand;
  116. vfp_single_dump("pack: tiny number", vs);
  117. #endif
  118. if (!(significand & ((1 << (VFP_SINGLE_LOW_BITS + 1)) - 1)))
  119. underflow = 0;
  120. }
  121. /*
  122. * Select rounding increment.
  123. */
  124. incr = 0;
  125. rmode = fpscr & FPSCR_RMODE_MASK;
  126. if (rmode == FPSCR_ROUND_NEAREST) {
  127. incr = 1 << VFP_SINGLE_LOW_BITS;
  128. if ((significand & (1 << (VFP_SINGLE_LOW_BITS + 1))) == 0)
  129. incr -= 1;
  130. } else if (rmode == FPSCR_ROUND_TOZERO) {
  131. incr = 0;
  132. } else if ((rmode == FPSCR_ROUND_PLUSINF) ^ (vs->sign != 0))
  133. incr = (1 << (VFP_SINGLE_LOW_BITS + 1)) - 1;
  134. pr_debug("VFP: rounding increment = 0x%08x\n", incr);
  135. /*
  136. * Is our rounding going to overflow?
  137. */
  138. if ((significand + incr) < significand) {
  139. exponent += 1;
  140. significand = (significand >> 1) | (significand & 1);
  141. incr >>= 1;
  142. #if 1
  143. vs->exponent = exponent;
  144. vs->significand = significand;
  145. vfp_single_dump("pack: overflow", vs);
  146. #endif
  147. }
  148. /*
  149. * If any of the low bits (which will be shifted out of the
  150. * number) are non-zero, the result is inexact.
  151. */
  152. if (significand & ((1 << (VFP_SINGLE_LOW_BITS + 1)) - 1))
  153. exceptions |= FPSCR_IXC;
  154. /*
  155. * Do our rounding.
  156. */
  157. significand += incr;
  158. /*
  159. * Infinity?
  160. */
  161. if (exponent >= 254) {
  162. exceptions |= FPSCR_OFC | FPSCR_IXC;
  163. if (incr == 0) {
  164. vs->exponent = 253;
  165. vs->significand = 0x7fffffff;
  166. } else {
  167. vs->exponent = 255; /* infinity */
  168. vs->significand = 0;
  169. }
  170. } else {
  171. if (significand >> (VFP_SINGLE_LOW_BITS + 1) == 0)
  172. exponent = 0;
  173. if (exponent || significand > 0x80000000)
  174. underflow = 0;
  175. if (underflow)
  176. exceptions |= FPSCR_UFC;
  177. vs->exponent = exponent;
  178. vs->significand = significand >> 1;
  179. }
  180. pack:
  181. vfp_single_dump("pack: final", vs);
  182. {
  183. s32 d = vfp_single_pack(vs);
  184. #if 1
  185. pr_debug("VFP: %s: d(s%d)=%08x exceptions=%08x\n", func,
  186. sd, d, exceptions);
  187. #endif
  188. vfp_put_float(state, d, sd);
  189. }
  190. return exceptions;
  191. }
  192. /*
  193. * Propagate the NaN, setting exceptions if it is signalling.
  194. * 'n' is always a NaN. 'm' may be a number, NaN or infinity.
  195. */
  196. static u32
  197. vfp_propagate_nan(struct vfp_single *vsd, struct vfp_single *vsn,
  198. struct vfp_single *vsm, u32 fpscr)
  199. {
  200. struct vfp_single *nan;
  201. int tn, tm = 0;
  202. tn = vfp_single_type(vsn);
  203. if (vsm)
  204. tm = vfp_single_type(vsm);
  205. if (fpscr & FPSCR_DEFAULT_NAN)
  206. /*
  207. * Default NaN mode - always returns a quiet NaN
  208. */
  209. nan = &vfp_single_default_qnan;
  210. else {
  211. /*
  212. * Contemporary mode - select the first signalling
  213. * NAN, or if neither are signalling, the first
  214. * quiet NAN.
  215. */
  216. if (tn == VFP_SNAN || (tm != VFP_SNAN && tn == VFP_QNAN))
  217. nan = vsn;
  218. else
  219. nan = vsm;
  220. /*
  221. * Make the NaN quiet.
  222. */
  223. nan->significand |= VFP_SINGLE_SIGNIFICAND_QNAN;
  224. }
  225. *vsd = *nan;
  226. /*
  227. * If one was a signalling NAN, raise invalid operation.
  228. */
  229. return tn == VFP_SNAN || tm == VFP_SNAN ? FPSCR_IOC : VFP_NAN_FLAG;
  230. }
  231. /*
  232. * Extended operations
  233. */
  234. static u32 vfp_single_fabs(ARMul_State* state, int sd, int unused, s32 m, u32 fpscr)
  235. {
  236. vfp_put_float(state, vfp_single_packed_abs(m), sd);
  237. return 0;
  238. }
  239. static u32 vfp_single_fcpy(ARMul_State* state, int sd, int unused, s32 m, u32 fpscr)
  240. {
  241. vfp_put_float(state, m, sd);
  242. return 0;
  243. }
  244. static u32 vfp_single_fneg(ARMul_State* state, int sd, int unused, s32 m, u32 fpscr)
  245. {
  246. vfp_put_float(state, vfp_single_packed_negate(m), sd);
  247. return 0;
  248. }
  249. static const u16 sqrt_oddadjust[] = {
  250. 0x0004, 0x0022, 0x005d, 0x00b1, 0x011d, 0x019f, 0x0236, 0x02e0,
  251. 0x039c, 0x0468, 0x0545, 0x0631, 0x072b, 0x0832, 0x0946, 0x0a67
  252. };
  253. static const u16 sqrt_evenadjust[] = {
  254. 0x0a2d, 0x08af, 0x075a, 0x0629, 0x051a, 0x0429, 0x0356, 0x029e,
  255. 0x0200, 0x0179, 0x0109, 0x00af, 0x0068, 0x0034, 0x0012, 0x0002
  256. };
  257. u32 vfp_estimate_sqrt_significand(u32 exponent, u32 significand)
  258. {
  259. int index;
  260. u32 z, a;
  261. if ((significand & 0xc0000000) != 0x40000000) {
  262. pr_debug("VFP: estimate_sqrt: invalid significand\n");
  263. }
  264. a = significand << 1;
  265. index = (a >> 27) & 15;
  266. if (exponent & 1) {
  267. z = 0x4000 + (a >> 17) - sqrt_oddadjust[index];
  268. z = ((a / z) << 14) + (z << 15);
  269. a >>= 1;
  270. } else {
  271. z = 0x8000 + (a >> 17) - sqrt_evenadjust[index];
  272. z = a / z + z;
  273. z = (z >= 0x20000) ? 0xffff8000 : (z << 15);
  274. if (z <= a)
  275. return (s32)a >> 1;
  276. }
  277. {
  278. u64 v = (u64)a << 31;
  279. do_div(v, z);
  280. return (u32)(v + (z >> 1));
  281. }
  282. }
  283. static u32 vfp_single_fsqrt(ARMul_State* state, int sd, int unused, s32 m, u32 fpscr)
  284. {
  285. struct vfp_single vsm, vsd, *vsp;
  286. int ret, tm;
  287. vfp_single_unpack(&vsm, m);
  288. tm = vfp_single_type(&vsm);
  289. if (tm & (VFP_NAN|VFP_INFINITY)) {
  290. vsp = &vsd;
  291. if (tm & VFP_NAN)
  292. ret = vfp_propagate_nan(vsp, &vsm, NULL, fpscr);
  293. else if (vsm.sign == 0) {
  294. sqrt_copy:
  295. vsp = &vsm;
  296. ret = 0;
  297. } else {
  298. sqrt_invalid:
  299. vsp = &vfp_single_default_qnan;
  300. ret = FPSCR_IOC;
  301. }
  302. vfp_put_float(state, vfp_single_pack(vsp), sd);
  303. return ret;
  304. }
  305. /*
  306. * sqrt(+/- 0) == +/- 0
  307. */
  308. if (tm & VFP_ZERO)
  309. goto sqrt_copy;
  310. /*
  311. * Normalise a denormalised number
  312. */
  313. if (tm & VFP_DENORMAL)
  314. vfp_single_normalise_denormal(&vsm);
  315. /*
  316. * sqrt(<0) = invalid
  317. */
  318. if (vsm.sign)
  319. goto sqrt_invalid;
  320. vfp_single_dump("sqrt", &vsm);
  321. /*
  322. * Estimate the square root.
  323. */
  324. vsd.sign = 0;
  325. vsd.exponent = ((vsm.exponent - 127) >> 1) + 127;
  326. vsd.significand = vfp_estimate_sqrt_significand(vsm.exponent, vsm.significand) + 2;
  327. vfp_single_dump("sqrt estimate", &vsd);
  328. /*
  329. * And now adjust.
  330. */
  331. if ((vsd.significand & VFP_SINGLE_LOW_BITS_MASK) <= 5) {
  332. if (vsd.significand < 2) {
  333. vsd.significand = 0xffffffff;
  334. } else {
  335. u64 term;
  336. s64 rem;
  337. vsm.significand <<= !(vsm.exponent & 1);
  338. term = (u64)vsd.significand * vsd.significand;
  339. rem = ((u64)vsm.significand << 32) - term;
  340. pr_debug("VFP: term=%016llx rem=%016llx\n", term, rem);
  341. while (rem < 0) {
  342. vsd.significand -= 1;
  343. rem += ((u64)vsd.significand << 1) | 1;
  344. }
  345. vsd.significand |= rem != 0;
  346. }
  347. }
  348. vsd.significand = vfp_shiftright32jamming(vsd.significand, 1);
  349. return vfp_single_normaliseround(state, sd, &vsd, fpscr, 0, "fsqrt");
  350. }
  351. /*
  352. * Equal := ZC
  353. * Less than := N
  354. * Greater than := C
  355. * Unordered := CV
  356. */
  357. static u32 vfp_compare(ARMul_State* state, int sd, int signal_on_qnan, s32 m, u32 fpscr)
  358. {
  359. s32 d;
  360. u32 ret = 0;
  361. d = vfp_get_float(state, sd);
  362. if (vfp_single_packed_exponent(m) == 255 && vfp_single_packed_mantissa(m)) {
  363. ret |= FPSCR_C | FPSCR_V;
  364. if (signal_on_qnan || !(vfp_single_packed_mantissa(m) & (1 << (VFP_SINGLE_MANTISSA_BITS - 1))))
  365. /*
  366. * Signalling NaN, or signalling on quiet NaN
  367. */
  368. ret |= FPSCR_IOC;
  369. }
  370. if (vfp_single_packed_exponent(d) == 255 && vfp_single_packed_mantissa(d)) {
  371. ret |= FPSCR_C | FPSCR_V;
  372. if (signal_on_qnan || !(vfp_single_packed_mantissa(d) & (1 << (VFP_SINGLE_MANTISSA_BITS - 1))))
  373. /*
  374. * Signalling NaN, or signalling on quiet NaN
  375. */
  376. ret |= FPSCR_IOC;
  377. }
  378. if (ret == 0) {
  379. if (d == m || vfp_single_packed_abs(d | m) == 0) {
  380. /*
  381. * equal
  382. */
  383. ret |= FPSCR_Z | FPSCR_C;
  384. } else if (vfp_single_packed_sign(d ^ m)) {
  385. /*
  386. * different signs
  387. */
  388. if (vfp_single_packed_sign(d))
  389. /*
  390. * d is negative, so d < m
  391. */
  392. ret |= FPSCR_N;
  393. else
  394. /*
  395. * d is positive, so d > m
  396. */
  397. ret |= FPSCR_C;
  398. } else if ((vfp_single_packed_sign(d) != 0) ^ (d < m)) {
  399. /*
  400. * d < m
  401. */
  402. ret |= FPSCR_N;
  403. } else if ((vfp_single_packed_sign(d) != 0) ^ (d > m)) {
  404. /*
  405. * d > m
  406. */
  407. ret |= FPSCR_C;
  408. }
  409. }
  410. return ret;
  411. }
  412. static u32 vfp_single_fcmp(ARMul_State* state, int sd, int unused, s32 m, u32 fpscr)
  413. {
  414. return vfp_compare(state, sd, 0, m, fpscr);
  415. }
  416. static u32 vfp_single_fcmpe(ARMul_State* state, int sd, int unused, s32 m, u32 fpscr)
  417. {
  418. return vfp_compare(state, sd, 1, m, fpscr);
  419. }
  420. static u32 vfp_single_fcmpz(ARMul_State* state, int sd, int unused, s32 m, u32 fpscr)
  421. {
  422. return vfp_compare(state, sd, 0, 0, fpscr);
  423. }
  424. static u32 vfp_single_fcmpez(ARMul_State* state, int sd, int unused, s32 m, u32 fpscr)
  425. {
  426. return vfp_compare(state, sd, 1, 0, fpscr);
  427. }
  428. static s64 vfp_single_to_doubleintern(ARMul_State* state, s32 m, u32 fpscr) //ichfly for internal use only
  429. {
  430. struct vfp_single vsm;
  431. struct vfp_double vdd;
  432. int tm;
  433. u32 exceptions = 0;
  434. vfp_single_unpack(&vsm, m);
  435. tm = vfp_single_type(&vsm);
  436. /*
  437. * If we have a signalling NaN, signal invalid operation.
  438. */
  439. if (tm == VFP_SNAN)
  440. exceptions = FPSCR_IOC;
  441. if (tm & VFP_DENORMAL)
  442. vfp_single_normalise_denormal(&vsm);
  443. vdd.sign = vsm.sign;
  444. vdd.significand = (u64)vsm.significand << 32;
  445. /*
  446. * If we have an infinity or NaN, the exponent must be 2047.
  447. */
  448. if (tm & (VFP_INFINITY | VFP_NAN)) {
  449. vdd.exponent = 2047;
  450. if (tm == VFP_QNAN)
  451. vdd.significand |= VFP_DOUBLE_SIGNIFICAND_QNAN;
  452. goto pack_nan;
  453. } else if (tm & VFP_ZERO)
  454. vdd.exponent = 0;
  455. else
  456. vdd.exponent = vsm.exponent + (1023 - 127);
  457. pack_nan:
  458. vfp_double_normaliseroundintern(state, &vdd, fpscr, exceptions, "fcvtd");
  459. return vfp_double_pack(&vdd);
  460. }
  461. static u32 vfp_single_fcvtd(ARMul_State* state, int dd, int unused, s32 m, u32 fpscr)
  462. {
  463. struct vfp_single vsm;
  464. struct vfp_double vdd;
  465. int tm;
  466. u32 exceptions = 0;
  467. vfp_single_unpack(&vsm, m);
  468. tm = vfp_single_type(&vsm);
  469. /*
  470. * If we have a signalling NaN, signal invalid operation.
  471. */
  472. if (tm == VFP_SNAN)
  473. exceptions = FPSCR_IOC;
  474. if (tm & VFP_DENORMAL)
  475. vfp_single_normalise_denormal(&vsm);
  476. vdd.sign = vsm.sign;
  477. vdd.significand = (u64)vsm.significand << 32;
  478. /*
  479. * If we have an infinity or NaN, the exponent must be 2047.
  480. */
  481. if (tm & (VFP_INFINITY|VFP_NAN)) {
  482. vdd.exponent = 2047;
  483. if (tm == VFP_QNAN)
  484. vdd.significand |= VFP_DOUBLE_SIGNIFICAND_QNAN;
  485. goto pack_nan;
  486. } else if (tm & VFP_ZERO)
  487. vdd.exponent = 0;
  488. else
  489. vdd.exponent = vsm.exponent + (1023 - 127);
  490. return vfp_double_normaliseround(state, dd, &vdd, fpscr, exceptions, "fcvtd");
  491. pack_nan:
  492. vfp_put_double(state, vfp_double_pack(&vdd), dd);
  493. return exceptions;
  494. }
  495. static u32 vfp_single_fuito(ARMul_State* state, int sd, int unused, s32 m, u32 fpscr)
  496. {
  497. struct vfp_single vs;
  498. vs.sign = 0;
  499. vs.exponent = 127 + 31 - 1;
  500. vs.significand = (u32)m;
  501. return vfp_single_normaliseround(state, sd, &vs, fpscr, 0, "fuito");
  502. }
  503. static u32 vfp_single_fsito(ARMul_State* state, int sd, int unused, s32 m, u32 fpscr)
  504. {
  505. struct vfp_single vs;
  506. vs.sign = (m & 0x80000000) >> 16;
  507. vs.exponent = 127 + 31 - 1;
  508. vs.significand = vs.sign ? -m : m;
  509. return vfp_single_normaliseround(state, sd, &vs, fpscr, 0, "fsito");
  510. }
  511. static u32 vfp_single_ftoui(ARMul_State* state, int sd, int unused, s32 m, u32 fpscr)
  512. {
  513. struct vfp_single vsm;
  514. u32 d, exceptions = 0;
  515. int rmode = fpscr & FPSCR_RMODE_MASK;
  516. int tm;
  517. vfp_single_unpack(&vsm, m);
  518. vfp_single_dump("VSM", &vsm);
  519. /*
  520. * Do we have a denormalised number?
  521. */
  522. tm = vfp_single_type(&vsm);
  523. if (tm & VFP_DENORMAL)
  524. exceptions |= FPSCR_IDC;
  525. if (tm & VFP_NAN)
  526. vsm.sign = 1;
  527. if (vsm.exponent >= 127 + 32) {
  528. d = vsm.sign ? 0 : 0xffffffff;
  529. exceptions = FPSCR_IOC;
  530. } else if (vsm.exponent >= 127) {
  531. int shift = 127 + 31 - vsm.exponent;
  532. u32 rem, incr = 0;
  533. /*
  534. * 2^0 <= m < 2^32-2^8
  535. */
  536. d = (vsm.significand << 1) >> shift;
  537. rem = vsm.significand << (33 - shift);
  538. if (rmode == FPSCR_ROUND_NEAREST) {
  539. incr = 0x80000000;
  540. if ((d & 1) == 0)
  541. incr -= 1;
  542. } else if (rmode == FPSCR_ROUND_TOZERO) {
  543. incr = 0;
  544. } else if ((rmode == FPSCR_ROUND_PLUSINF) ^ (vsm.sign != 0)) {
  545. incr = ~0;
  546. }
  547. if ((rem + incr) < rem) {
  548. if (d < 0xffffffff)
  549. d += 1;
  550. else
  551. exceptions |= FPSCR_IOC;
  552. }
  553. if (d && vsm.sign) {
  554. d = 0;
  555. exceptions |= FPSCR_IOC;
  556. } else if (rem)
  557. exceptions |= FPSCR_IXC;
  558. } else {
  559. d = 0;
  560. if (vsm.exponent | vsm.significand) {
  561. exceptions |= FPSCR_IXC;
  562. if (rmode == FPSCR_ROUND_PLUSINF && vsm.sign == 0)
  563. d = 1;
  564. else if (rmode == FPSCR_ROUND_MINUSINF && vsm.sign) {
  565. d = 0;
  566. exceptions |= FPSCR_IOC;
  567. }
  568. }
  569. }
  570. pr_debug("VFP: ftoui: d(s%d)=%08x exceptions=%08x\n", sd, d, exceptions);
  571. vfp_put_float(state, d, sd);
  572. return exceptions;
  573. }
  574. static u32 vfp_single_ftouiz(ARMul_State* state, int sd, int unused, s32 m, u32 fpscr)
  575. {
  576. return vfp_single_ftoui(state, sd, unused, m, FPSCR_ROUND_TOZERO);
  577. }
  578. static u32 vfp_single_ftosi(ARMul_State* state, int sd, int unused, s32 m, u32 fpscr)
  579. {
  580. struct vfp_single vsm;
  581. u32 d, exceptions = 0;
  582. int rmode = fpscr & FPSCR_RMODE_MASK;
  583. int tm;
  584. vfp_single_unpack(&vsm, m);
  585. vfp_single_dump("VSM", &vsm);
  586. /*
  587. * Do we have a denormalised number?
  588. */
  589. tm = vfp_single_type(&vsm);
  590. if (vfp_single_type(&vsm) & VFP_DENORMAL)
  591. exceptions |= FPSCR_IDC;
  592. if (tm & VFP_NAN) {
  593. d = 0;
  594. exceptions |= FPSCR_IOC;
  595. } else if (vsm.exponent >= 127 + 32) {
  596. /*
  597. * m >= 2^31-2^7: invalid
  598. */
  599. d = 0x7fffffff;
  600. if (vsm.sign)
  601. d = ~d;
  602. exceptions |= FPSCR_IOC;
  603. } else if (vsm.exponent >= 127) {
  604. int shift = 127 + 31 - vsm.exponent;
  605. u32 rem, incr = 0;
  606. /* 2^0 <= m <= 2^31-2^7 */
  607. d = (vsm.significand << 1) >> shift;
  608. rem = vsm.significand << (33 - shift);
  609. if (rmode == FPSCR_ROUND_NEAREST) {
  610. incr = 0x80000000;
  611. if ((d & 1) == 0)
  612. incr -= 1;
  613. } else if (rmode == FPSCR_ROUND_TOZERO) {
  614. incr = 0;
  615. } else if ((rmode == FPSCR_ROUND_PLUSINF) ^ (vsm.sign != 0)) {
  616. incr = ~0;
  617. }
  618. if ((rem + incr) < rem && d < 0xffffffff)
  619. d += 1;
  620. if (d > (0x7fffffffu + (vsm.sign != 0))) {
  621. d = (0x7fffffffu + (vsm.sign != 0));
  622. exceptions |= FPSCR_IOC;
  623. } else if (rem)
  624. exceptions |= FPSCR_IXC;
  625. if (vsm.sign)
  626. d = 0-d;
  627. } else {
  628. d = 0;
  629. if (vsm.exponent | vsm.significand) {
  630. exceptions |= FPSCR_IXC;
  631. if (rmode == FPSCR_ROUND_PLUSINF && vsm.sign == 0)
  632. d = 1;
  633. else if (rmode == FPSCR_ROUND_MINUSINF && vsm.sign)
  634. d = -1;
  635. }
  636. }
  637. pr_debug("VFP: ftosi: d(s%d)=%08x exceptions=%08x\n", sd, d, exceptions);
  638. vfp_put_float(state, (s32)d, sd);
  639. return exceptions;
  640. }
  641. static u32 vfp_single_ftosiz(ARMul_State* state, int sd, int unused, s32 m, u32 fpscr)
  642. {
  643. return vfp_single_ftosi(state, sd, unused, m, FPSCR_ROUND_TOZERO);
  644. }
  645. static struct op fops_ext[] = {
  646. { vfp_single_fcpy, 0 }, //0x00000000 - FEXT_FCPY
  647. { vfp_single_fabs, 0 }, //0x00000001 - FEXT_FABS
  648. { vfp_single_fneg, 0 }, //0x00000002 - FEXT_FNEG
  649. { vfp_single_fsqrt, 0 }, //0x00000003 - FEXT_FSQRT
  650. { NULL, 0 },
  651. { NULL, 0 },
  652. { NULL, 0 },
  653. { NULL, 0 },
  654. { vfp_single_fcmp, OP_SCALAR }, //0x00000008 - FEXT_FCMP
  655. { vfp_single_fcmpe, OP_SCALAR }, //0x00000009 - FEXT_FCMPE
  656. { vfp_single_fcmpz, OP_SCALAR }, //0x0000000A - FEXT_FCMPZ
  657. { vfp_single_fcmpez, OP_SCALAR }, //0x0000000B - FEXT_FCMPEZ
  658. { NULL, 0 },
  659. { NULL, 0 },
  660. { NULL, 0 },
  661. { vfp_single_fcvtd, OP_SCALAR|OP_DD }, //0x0000000F - FEXT_FCVT
  662. { vfp_single_fuito, OP_SCALAR }, //0x00000010 - FEXT_FUITO
  663. { vfp_single_fsito, OP_SCALAR }, //0x00000011 - FEXT_FSITO
  664. { NULL, 0 },
  665. { NULL, 0 },
  666. { NULL, 0 },
  667. { NULL, 0 },
  668. { NULL, 0 },
  669. { NULL, 0 },
  670. { vfp_single_ftoui, OP_SCALAR }, //0x00000018 - FEXT_FTOUI
  671. { vfp_single_ftouiz, OP_SCALAR }, //0x00000019 - FEXT_FTOUIZ
  672. { vfp_single_ftosi, OP_SCALAR }, //0x0000001A - FEXT_FTOSI
  673. { vfp_single_ftosiz, OP_SCALAR }, //0x0000001B - FEXT_FTOSIZ
  674. };
  675. static u32
  676. vfp_single_fadd_nonnumber(struct vfp_single *vsd, struct vfp_single *vsn,
  677. struct vfp_single *vsm, u32 fpscr)
  678. {
  679. struct vfp_single *vsp;
  680. u32 exceptions = 0;
  681. int tn, tm;
  682. tn = vfp_single_type(vsn);
  683. tm = vfp_single_type(vsm);
  684. if (tn & tm & VFP_INFINITY) {
  685. /*
  686. * Two infinities. Are they different signs?
  687. */
  688. if (vsn->sign ^ vsm->sign) {
  689. /*
  690. * different signs -> invalid
  691. */
  692. exceptions = FPSCR_IOC;
  693. vsp = &vfp_single_default_qnan;
  694. } else {
  695. /*
  696. * same signs -> valid
  697. */
  698. vsp = vsn;
  699. }
  700. } else if (tn & VFP_INFINITY && tm & VFP_NUMBER) {
  701. /*
  702. * One infinity and one number -> infinity
  703. */
  704. vsp = vsn;
  705. } else {
  706. /*
  707. * 'n' is a NaN of some type
  708. */
  709. return vfp_propagate_nan(vsd, vsn, vsm, fpscr);
  710. }
  711. *vsd = *vsp;
  712. return exceptions;
  713. }
  714. static u32
  715. vfp_single_add(struct vfp_single *vsd, struct vfp_single *vsn,
  716. struct vfp_single *vsm, u32 fpscr)
  717. {
  718. u32 exp_diff, m_sig;
  719. if (vsn->significand & 0x80000000 ||
  720. vsm->significand & 0x80000000) {
  721. pr_info("VFP: bad FP values in %s\n", __func__);
  722. vfp_single_dump("VSN", vsn);
  723. vfp_single_dump("VSM", vsm);
  724. }
  725. /*
  726. * Ensure that 'n' is the largest magnitude number. Note that
  727. * if 'n' and 'm' have equal exponents, we do not swap them.
  728. * This ensures that NaN propagation works correctly.
  729. */
  730. if (vsn->exponent < vsm->exponent) {
  731. struct vfp_single *t = vsn;
  732. vsn = vsm;
  733. vsm = t;
  734. }
  735. /*
  736. * Is 'n' an infinity or a NaN? Note that 'm' may be a number,
  737. * infinity or a NaN here.
  738. */
  739. if (vsn->exponent == 255)
  740. return vfp_single_fadd_nonnumber(vsd, vsn, vsm, fpscr);
  741. /*
  742. * We have two proper numbers, where 'vsn' is the larger magnitude.
  743. *
  744. * Copy 'n' to 'd' before doing the arithmetic.
  745. */
  746. *vsd = *vsn;
  747. /*
  748. * Align both numbers.
  749. */
  750. exp_diff = vsn->exponent - vsm->exponent;
  751. m_sig = vfp_shiftright32jamming(vsm->significand, exp_diff);
  752. /*
  753. * If the signs are different, we are really subtracting.
  754. */
  755. if (vsn->sign ^ vsm->sign) {
  756. m_sig = vsn->significand - m_sig;
  757. if ((s32)m_sig < 0) {
  758. vsd->sign = vfp_sign_negate(vsd->sign);
  759. m_sig = 0-m_sig;
  760. } else if (m_sig == 0) {
  761. vsd->sign = (fpscr & FPSCR_RMODE_MASK) ==
  762. FPSCR_ROUND_MINUSINF ? 0x8000 : 0;
  763. }
  764. } else {
  765. m_sig = vsn->significand + m_sig;
  766. }
  767. vsd->significand = m_sig;
  768. return 0;
  769. }
  770. static u32
  771. vfp_single_multiply(struct vfp_single *vsd, struct vfp_single *vsn, struct vfp_single *vsm, u32 fpscr)
  772. {
  773. vfp_single_dump("VSN", vsn);
  774. vfp_single_dump("VSM", vsm);
  775. /*
  776. * Ensure that 'n' is the largest magnitude number. Note that
  777. * if 'n' and 'm' have equal exponents, we do not swap them.
  778. * This ensures that NaN propagation works correctly.
  779. */
  780. if (vsn->exponent < vsm->exponent) {
  781. struct vfp_single *t = vsn;
  782. vsn = vsm;
  783. vsm = t;
  784. pr_debug("VFP: swapping M <-> N\n");
  785. }
  786. vsd->sign = vsn->sign ^ vsm->sign;
  787. /*
  788. * If 'n' is an infinity or NaN, handle it. 'm' may be anything.
  789. */
  790. if (vsn->exponent == 255) {
  791. if (vsn->significand || (vsm->exponent == 255 && vsm->significand))
  792. return vfp_propagate_nan(vsd, vsn, vsm, fpscr);
  793. if ((vsm->exponent | vsm->significand) == 0) {
  794. *vsd = vfp_single_default_qnan;
  795. return FPSCR_IOC;
  796. }
  797. vsd->exponent = vsn->exponent;
  798. vsd->significand = 0;
  799. return 0;
  800. }
  801. /*
  802. * If 'm' is zero, the result is always zero. In this case,
  803. * 'n' may be zero or a number, but it doesn't matter which.
  804. */
  805. if ((vsm->exponent | vsm->significand) == 0) {
  806. vsd->exponent = 0;
  807. vsd->significand = 0;
  808. return 0;
  809. }
  810. /*
  811. * We add 2 to the destination exponent for the same reason as
  812. * the addition case - though this time we have +1 from each
  813. * input operand.
  814. */
  815. vsd->exponent = vsn->exponent + vsm->exponent - 127 + 2;
  816. vsd->significand = vfp_hi64to32jamming((u64)vsn->significand * vsm->significand);
  817. vfp_single_dump("VSD", vsd);
  818. return 0;
  819. }
  820. #define NEG_MULTIPLY (1 << 0)
  821. #define NEG_SUBTRACT (1 << 1)
  822. static u32
  823. vfp_single_multiply_accumulate(ARMul_State* state, int sd, int sn, s32 m, u32 fpscr, u32 negate, const char *func)
  824. {
  825. vfp_single vsd, vsp, vsn, vsm;
  826. u32 exceptions;
  827. s32 v;
  828. v = vfp_get_float(state, sn);
  829. pr_debug("VFP: s%u = %08x\n", sn, v);
  830. vfp_single_unpack(&vsn, v);
  831. if (vsn.exponent == 0 && vsn.significand)
  832. vfp_single_normalise_denormal(&vsn);
  833. vfp_single_unpack(&vsm, m);
  834. if (vsm.exponent == 0 && vsm.significand)
  835. vfp_single_normalise_denormal(&vsm);
  836. exceptions = vfp_single_multiply(&vsp, &vsn, &vsm, fpscr);
  837. if (negate & NEG_MULTIPLY)
  838. vsp.sign = vfp_sign_negate(vsp.sign);
  839. v = vfp_get_float(state, sd);
  840. pr_debug("VFP: s%u = %08x\n", sd, v);
  841. vfp_single_unpack(&vsn, v);
  842. if (negate & NEG_SUBTRACT)
  843. vsn.sign = vfp_sign_negate(vsn.sign);
  844. exceptions |= vfp_single_add(&vsd, &vsn, &vsp, fpscr);
  845. return vfp_single_normaliseround(state, sd, &vsd, fpscr, exceptions, func);
  846. }
  847. /*
  848. * Standard operations
  849. */
  850. /*
  851. * sd = sd + (sn * sm)
  852. */
  853. static u32 vfp_single_fmac(ARMul_State* state, int sd, int sn, s32 m, u32 fpscr)
  854. {
  855. pr_debug("In %sVFP: s%u = %08x\n", __FUNCTION__, sn, sd);
  856. return vfp_single_multiply_accumulate(state, sd, sn, m, fpscr, 0, "fmac");
  857. }
  858. /*
  859. * sd = sd - (sn * sm)
  860. */
  861. static u32 vfp_single_fnmac(ARMul_State* state, int sd, int sn, s32 m, u32 fpscr)
  862. {
  863. pr_debug("In %sVFP: s%u = %08x\n", __FUNCTION__, sd, sn);
  864. return vfp_single_multiply_accumulate(state, sd, sn, m, fpscr, NEG_MULTIPLY, "fnmac");
  865. }
  866. /*
  867. * sd = -sd + (sn * sm)
  868. */
  869. static u32 vfp_single_fmsc(ARMul_State* state, int sd, int sn, s32 m, u32 fpscr)
  870. {
  871. pr_debug("In %sVFP: s%u = %08x\n", __FUNCTION__, sn, sd);
  872. return vfp_single_multiply_accumulate(state, sd, sn, m, fpscr, NEG_SUBTRACT, "fmsc");
  873. }
  874. /*
  875. * sd = -sd - (sn * sm)
  876. */
  877. static u32 vfp_single_fnmsc(ARMul_State* state, int sd, int sn, s32 m, u32 fpscr)
  878. {
  879. pr_debug("In %sVFP: s%u = %08x\n", __FUNCTION__, sn, sd);
  880. return vfp_single_multiply_accumulate(state, sd, sn, m, fpscr, NEG_SUBTRACT | NEG_MULTIPLY, "fnmsc");
  881. }
  882. /*
  883. * sd = sn * sm
  884. */
  885. static u32 vfp_single_fmul(ARMul_State* state, int sd, int sn, s32 m, u32 fpscr)
  886. {
  887. struct vfp_single vsd, vsn, vsm;
  888. u32 exceptions;
  889. s32 n = vfp_get_float(state, sn);
  890. pr_debug("In %sVFP: s%u = %08x\n", __FUNCTION__, sn, n);
  891. vfp_single_unpack(&vsn, n);
  892. if (vsn.exponent == 0 && vsn.significand)
  893. vfp_single_normalise_denormal(&vsn);
  894. vfp_single_unpack(&vsm, m);
  895. if (vsm.exponent == 0 && vsm.significand)
  896. vfp_single_normalise_denormal(&vsm);
  897. exceptions = vfp_single_multiply(&vsd, &vsn, &vsm, fpscr);
  898. return vfp_single_normaliseround(state, sd, &vsd, fpscr, exceptions, "fmul");
  899. }
  900. /*
  901. * sd = -(sn * sm)
  902. */
  903. static u32 vfp_single_fnmul(ARMul_State* state, int sd, int sn, s32 m, u32 fpscr)
  904. {
  905. struct vfp_single vsd, vsn, vsm;
  906. u32 exceptions;
  907. s32 n = vfp_get_float(state, sn);
  908. pr_debug("VFP: s%u = %08x\n", sn, n);
  909. vfp_single_unpack(&vsn, n);
  910. if (vsn.exponent == 0 && vsn.significand)
  911. vfp_single_normalise_denormal(&vsn);
  912. vfp_single_unpack(&vsm, m);
  913. if (vsm.exponent == 0 && vsm.significand)
  914. vfp_single_normalise_denormal(&vsm);
  915. exceptions = vfp_single_multiply(&vsd, &vsn, &vsm, fpscr);
  916. vsd.sign = vfp_sign_negate(vsd.sign);
  917. return vfp_single_normaliseround(state, sd, &vsd, fpscr, exceptions, "fnmul");
  918. }
  919. /*
  920. * sd = sn + sm
  921. */
  922. static u32 vfp_single_fadd(ARMul_State* state, int sd, int sn, s32 m, u32 fpscr)
  923. {
  924. struct vfp_single vsd, vsn, vsm;
  925. u32 exceptions;
  926. s32 n = vfp_get_float(state, sn);
  927. pr_debug("VFP: s%u = %08x\n", sn, n);
  928. /*
  929. * Unpack and normalise denormals.
  930. */
  931. vfp_single_unpack(&vsn, n);
  932. if (vsn.exponent == 0 && vsn.significand)
  933. vfp_single_normalise_denormal(&vsn);
  934. vfp_single_unpack(&vsm, m);
  935. if (vsm.exponent == 0 && vsm.significand)
  936. vfp_single_normalise_denormal(&vsm);
  937. exceptions = vfp_single_add(&vsd, &vsn, &vsm, fpscr);
  938. return vfp_single_normaliseround(state, sd, &vsd, fpscr, exceptions, "fadd");
  939. }
  940. /*
  941. * sd = sn - sm
  942. */
  943. static u32 vfp_single_fsub(ARMul_State* state, int sd, int sn, s32 m, u32 fpscr)
  944. {
  945. pr_debug("In %sVFP: s%u = %08x\n", __FUNCTION__, sn, sd);
  946. /*
  947. * Subtraction is addition with one sign inverted.
  948. */
  949. if (m != 0x7FC00000) // Only negate if m isn't NaN.
  950. m = vfp_single_packed_negate(m);
  951. return vfp_single_fadd(state, sd, sn, m, fpscr);
  952. }
  953. /*
  954. * sd = sn / sm
  955. */
  956. static u32 vfp_single_fdiv(ARMul_State* state, int sd, int sn, s32 m, u32 fpscr)
  957. {
  958. struct vfp_single vsd, vsn, vsm;
  959. u32 exceptions = 0;
  960. s32 n = vfp_get_float(state, sn);
  961. int tm, tn;
  962. pr_debug("VFP: s%u = %08x\n", sn, n);
  963. vfp_single_unpack(&vsn, n);
  964. vfp_single_unpack(&vsm, m);
  965. vsd.sign = vsn.sign ^ vsm.sign;
  966. tn = vfp_single_type(&vsn);
  967. tm = vfp_single_type(&vsm);
  968. /*
  969. * Is n a NAN?
  970. */
  971. if (tn & VFP_NAN)
  972. goto vsn_nan;
  973. /*
  974. * Is m a NAN?
  975. */
  976. if (tm & VFP_NAN)
  977. goto vsm_nan;
  978. /*
  979. * If n and m are infinity, the result is invalid
  980. * If n and m are zero, the result is invalid
  981. */
  982. if (tm & tn & (VFP_INFINITY|VFP_ZERO))
  983. goto invalid;
  984. /*
  985. * If n is infinity, the result is infinity
  986. */
  987. if (tn & VFP_INFINITY)
  988. goto infinity;
  989. /*
  990. * If m is zero, raise div0 exception
  991. */
  992. if (tm & VFP_ZERO)
  993. goto divzero;
  994. /*
  995. * If m is infinity, or n is zero, the result is zero
  996. */
  997. if (tm & VFP_INFINITY || tn & VFP_ZERO)
  998. goto zero;
  999. if (tn & VFP_DENORMAL)
  1000. vfp_single_normalise_denormal(&vsn);
  1001. if (tm & VFP_DENORMAL)
  1002. vfp_single_normalise_denormal(&vsm);
  1003. /*
  1004. * Ok, we have two numbers, we can perform division.
  1005. */
  1006. vsd.exponent = vsn.exponent - vsm.exponent + 127 - 1;
  1007. vsm.significand <<= 1;
  1008. if (vsm.significand <= (2 * vsn.significand)) {
  1009. vsn.significand >>= 1;
  1010. vsd.exponent++;
  1011. }
  1012. {
  1013. u64 significand = (u64)vsn.significand << 32;
  1014. do_div(significand, vsm.significand);
  1015. vsd.significand = (u32)significand;
  1016. }
  1017. if ((vsd.significand & 0x3f) == 0)
  1018. vsd.significand |= ((u64)vsm.significand * vsd.significand != (u64)vsn.significand << 32);
  1019. return vfp_single_normaliseround(state, sd, &vsd, fpscr, 0, "fdiv");
  1020. vsn_nan:
  1021. exceptions = vfp_propagate_nan(&vsd, &vsn, &vsm, fpscr);
  1022. pack:
  1023. vfp_put_float(state, vfp_single_pack(&vsd), sd);
  1024. return exceptions;
  1025. vsm_nan:
  1026. exceptions = vfp_propagate_nan(&vsd, &vsm, &vsn, fpscr);
  1027. goto pack;
  1028. zero:
  1029. vsd.exponent = 0;
  1030. vsd.significand = 0;
  1031. goto pack;
  1032. divzero:
  1033. exceptions = FPSCR_DZC;
  1034. infinity:
  1035. vsd.exponent = 255;
  1036. vsd.significand = 0;
  1037. goto pack;
  1038. invalid:
  1039. vfp_put_float(state, vfp_single_pack(&vfp_single_default_qnan), sd);
  1040. return FPSCR_IOC;
  1041. }
  1042. static struct op fops[] = {
  1043. { vfp_single_fmac, 0 },
  1044. { vfp_single_fmsc, 0 },
  1045. { vfp_single_fmul, 0 },
  1046. { vfp_single_fadd, 0 },
  1047. { vfp_single_fnmac, 0 },
  1048. { vfp_single_fnmsc, 0 },
  1049. { vfp_single_fnmul, 0 },
  1050. { vfp_single_fsub, 0 },
  1051. { vfp_single_fdiv, 0 },
  1052. };
  1053. #define FREG_BANK(x) ((x) & 0x18)
  1054. #define FREG_IDX(x) ((x) & 7)
  1055. u32 vfp_single_cpdo(ARMul_State* state, u32 inst, u32 fpscr)
  1056. {
  1057. u32 op = inst & FOP_MASK;
  1058. u32 exceptions = 0;
  1059. unsigned int dest;
  1060. unsigned int sn = vfp_get_sn(inst);
  1061. unsigned int sm = vfp_get_sm(inst);
  1062. unsigned int vecitr, veclen, vecstride;
  1063. struct op *fop;
  1064. pr_debug("In %s\n", __FUNCTION__);
  1065. vecstride = 1 + ((fpscr & FPSCR_STRIDE_MASK) == FPSCR_STRIDE_MASK);
  1066. fop = (op == FOP_EXT) ? &fops_ext[FEXT_TO_IDX(inst)] : &fops[FOP_TO_IDX(op)];
  1067. /*
  1068. * fcvtsd takes a dN register number as destination, not sN.
  1069. * Technically, if bit 0 of dd is set, this is an invalid
  1070. * instruction. However, we ignore this for efficiency.
  1071. * It also only operates on scalars.
  1072. */
  1073. if (fop->flags & OP_DD)
  1074. dest = vfp_get_dd(inst);
  1075. else
  1076. dest = vfp_get_sd(inst);
  1077. /*
  1078. * If destination bank is zero, vector length is always '1'.
  1079. * ARM DDI0100F C5.1.3, C5.3.2.
  1080. */
  1081. if ((fop->flags & OP_SCALAR) || FREG_BANK(dest) == 0)
  1082. veclen = 0;
  1083. else
  1084. veclen = fpscr & FPSCR_LENGTH_MASK;
  1085. pr_debug("VFP: vecstride=%u veclen=%u\n", vecstride,
  1086. (veclen >> FPSCR_LENGTH_BIT) + 1);
  1087. if (!fop->fn) {
  1088. printf("VFP: could not find single op %d, inst=0x%x@0x%x\n", FEXT_TO_IDX(inst), inst, state->Reg[15]);
  1089. exit(-1);
  1090. goto invalid;
  1091. }
  1092. for (vecitr = 0; vecitr <= veclen; vecitr += 1 << FPSCR_LENGTH_BIT) {
  1093. s32 m = vfp_get_float(state, sm);
  1094. u32 except;
  1095. char type;
  1096. type = fop->flags & OP_DD ? 'd' : 's';
  1097. if (op == FOP_EXT)
  1098. pr_debug("VFP: itr%d (%c%u) = op[%u] (s%u=%08x)\n",
  1099. vecitr >> FPSCR_LENGTH_BIT, type, dest, sn,
  1100. sm, m);
  1101. else
  1102. pr_debug("VFP: itr%d (%c%u) = (s%u) op[%u] (s%u=%08x)\n",
  1103. vecitr >> FPSCR_LENGTH_BIT, type, dest, sn,
  1104. FOP_TO_IDX(op), sm, m);
  1105. except = fop->fn(state, dest, sn, m, fpscr);
  1106. pr_debug("VFP: itr%d: exceptions=%08x\n",
  1107. vecitr >> FPSCR_LENGTH_BIT, except);
  1108. exceptions |= except;
  1109. /*
  1110. * CHECK: It appears to be undefined whether we stop when
  1111. * we encounter an exception. We continue.
  1112. */
  1113. dest = FREG_BANK(dest) + ((FREG_IDX(dest) + vecstride) & 7);
  1114. sn = FREG_BANK(sn) + ((FREG_IDX(sn) + vecstride) & 7);
  1115. if (FREG_BANK(sm) != 0)
  1116. sm = FREG_BANK(sm) + ((FREG_IDX(sm) + vecstride) & 7);
  1117. }
  1118. return exceptions;
  1119. invalid:
  1120. return (u32)-1;
  1121. }