vfpsingle.cpp 37 KB

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