cp1.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  1. /*> cp1.c <*/
  2. /* MIPS Simulator FPU (CoProcessor 1) support.
  3. Copyright (C) 2002-2015 Free Software Foundation, Inc.
  4. Originally created by Cygnus Solutions. Extensive modifications,
  5. including paired-single operation support and MIPS-3D support
  6. contributed by Ed Satterthwaite and Chris Demetriou, of Broadcom
  7. Corporation (SiByte).
  8. This file is part of GDB, the GNU debugger.
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 3 of the License, or
  12. (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  19. /* XXX: The following notice should be removed as soon as is practical: */
  20. /* Floating Point Support for gdb MIPS simulators
  21. This file is part of the MIPS sim
  22. THIS SOFTWARE IS NOT COPYRIGHTED
  23. (by Cygnus.)
  24. Cygnus offers the following for use in the public domain. Cygnus
  25. makes no warranty with regard to the software or it's performance
  26. and the user accepts the software "AS IS" with all faults.
  27. CYGNUS DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO
  28. THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  29. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  30. (Originally, this code was in interp.c)
  31. */
  32. #include "sim-main.h"
  33. /* Within cp1.c we refer to sim_cpu directly. */
  34. #define CPU cpu
  35. #define SD CPU_STATE(cpu)
  36. /*-- FPU support routines ---------------------------------------------------*/
  37. /* Numbers are held in normalized form. The SINGLE and DOUBLE binary
  38. formats conform to ANSI/IEEE Std 754-1985.
  39. SINGLE precision floating:
  40. seeeeeeeefffffffffffffffffffffff
  41. s = 1bit = sign
  42. e = 8bits = exponent
  43. f = 23bits = fraction
  44. SINGLE precision fixed:
  45. siiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
  46. s = 1bit = sign
  47. i = 31bits = integer
  48. DOUBLE precision floating:
  49. seeeeeeeeeeeffffffffffffffffffffffffffffffffffffffffffffffffffff
  50. s = 1bit = sign
  51. e = 11bits = exponent
  52. f = 52bits = fraction
  53. DOUBLE precision fixed:
  54. siiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
  55. s = 1bit = sign
  56. i = 63bits = integer
  57. PAIRED SINGLE precision floating:
  58. seeeeeeeefffffffffffffffffffffffseeeeeeeefffffffffffffffffffffff
  59. | upper || lower |
  60. s = 1bit = sign
  61. e = 8bits = exponent
  62. f = 23bits = fraction
  63. Note: upper = [63..32], lower = [31..0]
  64. */
  65. /* Extract packed single values: */
  66. #define FP_PS_upper(v) (((v) >> 32) & (unsigned)0xFFFFFFFF)
  67. #define FP_PS_lower(v) ((v) & (unsigned)0xFFFFFFFF)
  68. #define FP_PS_cat(u,l) (((unsigned64)((u) & (unsigned)0xFFFFFFFF) << 32) \
  69. | (unsigned64)((l) & 0xFFFFFFFF))
  70. /* Explicit QNaN values. */
  71. #define FPQNaN_SINGLE (0x7FBFFFFF)
  72. #define FPQNaN_WORD (0x7FFFFFFF)
  73. #define FPQNaN_DOUBLE (UNSIGNED64 (0x7FF7FFFFFFFFFFFF))
  74. #define FPQNaN_LONG (UNSIGNED64 (0x7FFFFFFFFFFFFFFF))
  75. #define FPQNaN_PS (FP_PS_cat (FPQNaN_SINGLE, FPQNaN_SINGLE))
  76. static const char *fpu_format_name (FP_formats fmt);
  77. #ifdef DEBUG
  78. static const char *fpu_rounding_mode_name (int rm);
  79. #endif
  80. uword64
  81. value_fpr (sim_cpu *cpu,
  82. address_word cia,
  83. int fpr,
  84. FP_formats fmt)
  85. {
  86. uword64 value = 0;
  87. int err = 0;
  88. /* Treat unused register values, as fixed-point 64bit values. */
  89. if (fmt == fmt_unknown)
  90. {
  91. #if 1
  92. /* If request to read data as "unknown", then use the current
  93. encoding: */
  94. fmt = FPR_STATE[fpr];
  95. #else
  96. fmt = fmt_long;
  97. #endif
  98. }
  99. /* For values not yet accessed, set to the desired format. */
  100. if (fmt < fmt_uninterpreted)
  101. {
  102. if (FPR_STATE[fpr] == fmt_uninterpreted)
  103. {
  104. FPR_STATE[fpr] = fmt;
  105. #ifdef DEBUG
  106. printf ("DBG: Register %d was fmt_uninterpreted. Now %s\n", fpr,
  107. fpu_format_name (fmt));
  108. #endif /* DEBUG */
  109. }
  110. else if (fmt != FPR_STATE[fpr])
  111. {
  112. sim_io_eprintf (SD, "FPR %d (format %s) being accessed with format %s - setting to unknown (PC = 0x%s)\n",
  113. fpr, fpu_format_name (FPR_STATE[fpr]),
  114. fpu_format_name (fmt), pr_addr (cia));
  115. FPR_STATE[fpr] = fmt_unknown;
  116. }
  117. }
  118. if (FPR_STATE[fpr] == fmt_unknown)
  119. {
  120. /* Set QNaN value: */
  121. switch (fmt)
  122. {
  123. case fmt_single: value = FPQNaN_SINGLE; break;
  124. case fmt_double: value = FPQNaN_DOUBLE; break;
  125. case fmt_word: value = FPQNaN_WORD; break;
  126. case fmt_long: value = FPQNaN_LONG; break;
  127. case fmt_ps: value = FPQNaN_PS; break;
  128. default: err = -1; break;
  129. }
  130. }
  131. else if (SizeFGR () == 64)
  132. {
  133. switch (fmt)
  134. {
  135. case fmt_uninterpreted_32:
  136. case fmt_single:
  137. case fmt_word:
  138. value = (FGR[fpr] & 0xFFFFFFFF);
  139. break;
  140. case fmt_uninterpreted_64:
  141. case fmt_uninterpreted:
  142. case fmt_double:
  143. case fmt_long:
  144. case fmt_ps:
  145. value = FGR[fpr];
  146. break;
  147. default:
  148. err = -1;
  149. break;
  150. }
  151. }
  152. else
  153. {
  154. switch (fmt)
  155. {
  156. case fmt_uninterpreted_32:
  157. case fmt_single:
  158. case fmt_word:
  159. value = (FGR[fpr] & 0xFFFFFFFF);
  160. break;
  161. case fmt_uninterpreted_64:
  162. case fmt_uninterpreted:
  163. case fmt_double:
  164. case fmt_long:
  165. if ((fpr & 1) == 0)
  166. {
  167. /* Even register numbers only. */
  168. #ifdef DEBUG
  169. printf ("DBG: ValueFPR: FGR[%d] = %s, FGR[%d] = %s\n",
  170. fpr + 1, pr_uword64 ((uword64) FGR[fpr+1]),
  171. fpr, pr_uword64 ((uword64) FGR[fpr]));
  172. #endif
  173. value = ((((uword64) FGR[fpr+1]) << 32)
  174. | (FGR[fpr] & 0xFFFFFFFF));
  175. }
  176. else
  177. {
  178. SignalException (ReservedInstruction, 0);
  179. }
  180. break;
  181. case fmt_ps:
  182. SignalException (ReservedInstruction, 0);
  183. break;
  184. default:
  185. err = -1;
  186. break;
  187. }
  188. }
  189. if (err)
  190. SignalExceptionSimulatorFault ("Unrecognised FP format in ValueFPR ()");
  191. #ifdef DEBUG
  192. printf ("DBG: ValueFPR: fpr = %d, fmt = %s, value = 0x%s : PC = 0x%s : SizeFGR () = %d\n",
  193. fpr, fpu_format_name (fmt), pr_uword64 (value), pr_addr (cia),
  194. SizeFGR ());
  195. #endif /* DEBUG */
  196. return (value);
  197. }
  198. void
  199. store_fpr (sim_cpu *cpu,
  200. address_word cia,
  201. int fpr,
  202. FP_formats fmt,
  203. uword64 value)
  204. {
  205. int err = 0;
  206. #ifdef DEBUG
  207. printf ("DBG: StoreFPR: fpr = %d, fmt = %s, value = 0x%s : PC = 0x%s : SizeFGR () = %d, \n",
  208. fpr, fpu_format_name (fmt), pr_uword64 (value), pr_addr (cia),
  209. SizeFGR ());
  210. #endif /* DEBUG */
  211. if (SizeFGR () == 64)
  212. {
  213. switch (fmt)
  214. {
  215. case fmt_uninterpreted_32:
  216. fmt = fmt_uninterpreted;
  217. case fmt_single:
  218. case fmt_word:
  219. if (STATE_VERBOSE_P (SD))
  220. sim_io_eprintf (SD,
  221. "Warning: PC 0x%s: interp.c store_fpr DEADCODE\n",
  222. pr_addr (cia));
  223. FGR[fpr] = (((uword64) 0xDEADC0DE << 32) | (value & 0xFFFFFFFF));
  224. FPR_STATE[fpr] = fmt;
  225. break;
  226. case fmt_uninterpreted_64:
  227. fmt = fmt_uninterpreted;
  228. case fmt_uninterpreted:
  229. case fmt_double:
  230. case fmt_long:
  231. case fmt_ps:
  232. FGR[fpr] = value;
  233. FPR_STATE[fpr] = fmt;
  234. break;
  235. default:
  236. FPR_STATE[fpr] = fmt_unknown;
  237. err = -1;
  238. break;
  239. }
  240. }
  241. else
  242. {
  243. switch (fmt)
  244. {
  245. case fmt_uninterpreted_32:
  246. fmt = fmt_uninterpreted;
  247. case fmt_single:
  248. case fmt_word:
  249. FGR[fpr] = (value & 0xFFFFFFFF);
  250. FPR_STATE[fpr] = fmt;
  251. break;
  252. case fmt_uninterpreted_64:
  253. fmt = fmt_uninterpreted;
  254. case fmt_uninterpreted:
  255. case fmt_double:
  256. case fmt_long:
  257. if ((fpr & 1) == 0)
  258. {
  259. /* Even register numbers only. */
  260. FGR[fpr+1] = (value >> 32);
  261. FGR[fpr] = (value & 0xFFFFFFFF);
  262. FPR_STATE[fpr + 1] = fmt;
  263. FPR_STATE[fpr] = fmt;
  264. }
  265. else
  266. {
  267. FPR_STATE[fpr] = fmt_unknown;
  268. FPR_STATE[fpr ^ 1] = fmt_unknown;
  269. SignalException (ReservedInstruction, 0);
  270. }
  271. break;
  272. case fmt_ps:
  273. FPR_STATE[fpr] = fmt_unknown;
  274. SignalException (ReservedInstruction, 0);
  275. break;
  276. default:
  277. FPR_STATE[fpr] = fmt_unknown;
  278. err = -1;
  279. break;
  280. }
  281. }
  282. if (err)
  283. SignalExceptionSimulatorFault ("Unrecognised FP format in StoreFPR ()");
  284. #ifdef DEBUG
  285. printf ("DBG: StoreFPR: fpr[%d] = 0x%s (format %s)\n",
  286. fpr, pr_uword64 (FGR[fpr]), fpu_format_name (fmt));
  287. #endif /* DEBUG */
  288. return;
  289. }
  290. /* CP1 control/status register access functions. */
  291. void
  292. test_fcsr (sim_cpu *cpu,
  293. address_word cia)
  294. {
  295. unsigned int cause;
  296. cause = (FCSR & fcsr_CAUSE_mask) >> fcsr_CAUSE_shift;
  297. if ((cause & ((FCSR & fcsr_ENABLES_mask) >> fcsr_ENABLES_shift)) != 0
  298. || (cause & (1 << UO)))
  299. {
  300. SignalExceptionFPE();
  301. }
  302. }
  303. unsigned_word
  304. value_fcr(sim_cpu *cpu,
  305. address_word cia,
  306. int fcr)
  307. {
  308. unsigned32 value = 0;
  309. switch (fcr)
  310. {
  311. case 0: /* FP Implementation and Revision Register. */
  312. value = FCR0;
  313. break;
  314. case 25: /* FP Condition Codes Register (derived from FCSR). */
  315. value = (FCR31 & fcsr_FCC_mask) >> fcsr_FCC_shift;
  316. value = (value & 0x1) | (value >> 1); /* Close FCC gap. */
  317. break;
  318. case 26: /* FP Exceptions Register (derived from FCSR). */
  319. value = FCR31 & (fcsr_CAUSE_mask | fcsr_FLAGS_mask);
  320. break;
  321. case 28: /* FP Enables Register (derived from FCSR). */
  322. value = FCR31 & (fcsr_ENABLES_mask | fcsr_RM_mask);
  323. if ((FCR31 & fcsr_FS) != 0)
  324. value |= fenr_FS;
  325. break;
  326. case 31: /* FP Control/Status Register (FCSR). */
  327. value = FCR31 & ~fcsr_ZERO_mask;
  328. break;
  329. }
  330. return (EXTEND32 (value));
  331. }
  332. void
  333. store_fcr(sim_cpu *cpu,
  334. address_word cia,
  335. int fcr,
  336. unsigned_word value)
  337. {
  338. unsigned32 v;
  339. v = VL4_8(value);
  340. switch (fcr)
  341. {
  342. case 25: /* FP Condition Codes Register (stored into FCSR). */
  343. v = (v << 1) | (v & 0x1); /* Adjust for FCC gap. */
  344. FCR31 &= ~fcsr_FCC_mask;
  345. FCR31 |= ((v << fcsr_FCC_shift) & fcsr_FCC_mask);
  346. break;
  347. case 26: /* FP Exceptions Register (stored into FCSR). */
  348. FCR31 &= ~(fcsr_CAUSE_mask | fcsr_FLAGS_mask);
  349. FCR31 |= (v & (fcsr_CAUSE_mask | fcsr_FLAGS_mask));
  350. test_fcsr(cpu, cia);
  351. break;
  352. case 28: /* FP Enables Register (stored into FCSR). */
  353. if ((v & fenr_FS) != 0)
  354. v |= fcsr_FS;
  355. else
  356. v &= ~fcsr_FS;
  357. FCR31 &= (fcsr_FCC_mask | fcsr_CAUSE_mask | fcsr_FLAGS_mask);
  358. FCR31 |= (v & (fcsr_FS | fcsr_ENABLES_mask | fcsr_RM_mask));
  359. test_fcsr(cpu, cia);
  360. break;
  361. case 31: /* FP Control/Status Register (FCSR). */
  362. FCR31 = v & ~fcsr_ZERO_mask;
  363. test_fcsr(cpu, cia);
  364. break;
  365. }
  366. }
  367. void
  368. update_fcsr (sim_cpu *cpu,
  369. address_word cia,
  370. sim_fpu_status status)
  371. {
  372. FCSR &= ~fcsr_CAUSE_mask;
  373. if (status != 0)
  374. {
  375. unsigned int cause = 0;
  376. /* map between sim_fpu codes and MIPS FCSR */
  377. if (status & (sim_fpu_status_invalid_snan
  378. | sim_fpu_status_invalid_isi
  379. | sim_fpu_status_invalid_idi
  380. | sim_fpu_status_invalid_zdz
  381. | sim_fpu_status_invalid_imz
  382. | sim_fpu_status_invalid_cmp
  383. | sim_fpu_status_invalid_sqrt
  384. | sim_fpu_status_invalid_cvi))
  385. cause |= (1 << IO);
  386. if (status & sim_fpu_status_invalid_div0)
  387. cause |= (1 << DZ);
  388. if (status & sim_fpu_status_overflow)
  389. cause |= (1 << OF);
  390. if (status & sim_fpu_status_underflow)
  391. cause |= (1 << UF);
  392. if (status & sim_fpu_status_inexact)
  393. cause |= (1 << IR);
  394. #if 0 /* Not yet. */
  395. /* Implicit clearing of other bits by unimplemented done by callers. */
  396. if (status & sim_fpu_status_unimplemented)
  397. cause |= (1 << UO);
  398. #endif
  399. FCSR |= (cause << fcsr_CAUSE_shift);
  400. test_fcsr (cpu, cia);
  401. FCSR |= ((cause & ~(1 << UO)) << fcsr_FLAGS_shift);
  402. }
  403. return;
  404. }
  405. static sim_fpu_round
  406. rounding_mode(int rm)
  407. {
  408. sim_fpu_round round;
  409. switch (rm)
  410. {
  411. case FP_RM_NEAREST:
  412. /* Round result to nearest representable value. When two
  413. representable values are equally near, round to the value
  414. that has a least significant bit of zero (i.e. is even). */
  415. round = sim_fpu_round_near;
  416. break;
  417. case FP_RM_TOZERO:
  418. /* Round result to the value closest to, and not greater in
  419. magnitude than, the result. */
  420. round = sim_fpu_round_zero;
  421. break;
  422. case FP_RM_TOPINF:
  423. /* Round result to the value closest to, and not less than,
  424. the result. */
  425. round = sim_fpu_round_up;
  426. break;
  427. case FP_RM_TOMINF:
  428. /* Round result to the value closest to, and not greater than,
  429. the result. */
  430. round = sim_fpu_round_down;
  431. break;
  432. default:
  433. round = 0;
  434. fprintf (stderr, "Bad switch\n");
  435. abort ();
  436. }
  437. return round;
  438. }
  439. /* When the FS bit is set, MIPS processors return zero for
  440. denormalized results and optionally replace denormalized inputs
  441. with zero. When FS is clear, some implementation trap on input
  442. and/or output, while other perform the operation in hardware. */
  443. static sim_fpu_denorm
  444. denorm_mode(sim_cpu *cpu)
  445. {
  446. sim_fpu_denorm denorm;
  447. /* XXX: FIXME: Eventually should be CPU model dependent. */
  448. if (GETFS())
  449. denorm = sim_fpu_denorm_zero;
  450. else
  451. denorm = 0;
  452. return denorm;
  453. }
  454. /* Comparison operations. */
  455. static sim_fpu_status
  456. fp_test(unsigned64 op1,
  457. unsigned64 op2,
  458. FP_formats fmt,
  459. int abs,
  460. int cond,
  461. int *condition)
  462. {
  463. sim_fpu wop1;
  464. sim_fpu wop2;
  465. sim_fpu_status status = 0;
  466. int less, equal, unordered;
  467. /* The format type has already been checked: */
  468. switch (fmt)
  469. {
  470. case fmt_single:
  471. {
  472. sim_fpu_32to (&wop1, op1);
  473. sim_fpu_32to (&wop2, op2);
  474. break;
  475. }
  476. case fmt_double:
  477. {
  478. sim_fpu_64to (&wop1, op1);
  479. sim_fpu_64to (&wop2, op2);
  480. break;
  481. }
  482. default:
  483. fprintf (stderr, "Bad switch\n");
  484. abort ();
  485. }
  486. if (sim_fpu_is_nan (&wop1) || sim_fpu_is_nan (&wop2))
  487. {
  488. if ((cond & (1 << 3)) ||
  489. sim_fpu_is_snan (&wop1) || sim_fpu_is_snan (&wop2))
  490. status = sim_fpu_status_invalid_snan;
  491. less = 0;
  492. equal = 0;
  493. unordered = 1;
  494. }
  495. else
  496. {
  497. if (abs)
  498. {
  499. status |= sim_fpu_abs (&wop1, &wop1);
  500. status |= sim_fpu_abs (&wop2, &wop2);
  501. }
  502. equal = sim_fpu_is_eq (&wop1, &wop2);
  503. less = !equal && sim_fpu_is_lt (&wop1, &wop2);
  504. unordered = 0;
  505. }
  506. *condition = (((cond & (1 << 2)) && less)
  507. || ((cond & (1 << 1)) && equal)
  508. || ((cond & (1 << 0)) && unordered));
  509. return status;
  510. }
  511. void
  512. fp_cmp(sim_cpu *cpu,
  513. address_word cia,
  514. unsigned64 op1,
  515. unsigned64 op2,
  516. FP_formats fmt,
  517. int abs,
  518. int cond,
  519. int cc)
  520. {
  521. sim_fpu_status status = 0;
  522. /* The format type should already have been checked. The FCSR is
  523. updated before the condition codes so that any exceptions will
  524. be signalled before the condition codes are changed. */
  525. switch (fmt)
  526. {
  527. case fmt_single:
  528. case fmt_double:
  529. {
  530. int result;
  531. status = fp_test(op1, op2, fmt, abs, cond, &result);
  532. update_fcsr (cpu, cia, status);
  533. SETFCC (cc, result);
  534. break;
  535. }
  536. case fmt_ps:
  537. {
  538. int result0, result1;
  539. status = fp_test(FP_PS_lower (op1), FP_PS_lower (op2), fmt_single,
  540. abs, cond, &result0);
  541. status |= fp_test(FP_PS_upper (op1), FP_PS_upper (op2), fmt_single,
  542. abs, cond, &result1);
  543. update_fcsr (cpu, cia, status);
  544. SETFCC (cc, result0);
  545. SETFCC (cc+1, result1);
  546. break;
  547. }
  548. default:
  549. sim_io_eprintf (SD, "Bad switch\n");
  550. abort ();
  551. }
  552. }
  553. /* Basic arithmetic operations. */
  554. static unsigned64
  555. fp_unary(sim_cpu *cpu,
  556. address_word cia,
  557. int (*sim_fpu_op)(sim_fpu *, const sim_fpu *),
  558. unsigned64 op,
  559. FP_formats fmt)
  560. {
  561. sim_fpu wop;
  562. sim_fpu ans;
  563. sim_fpu_round round = rounding_mode (GETRM());
  564. sim_fpu_denorm denorm = denorm_mode (cpu);
  565. sim_fpu_status status = 0;
  566. unsigned64 result = 0;
  567. /* The format type has already been checked: */
  568. switch (fmt)
  569. {
  570. case fmt_single:
  571. {
  572. unsigned32 res;
  573. sim_fpu_32to (&wop, op);
  574. status |= (*sim_fpu_op) (&ans, &wop);
  575. status |= sim_fpu_round_32 (&ans, round, denorm);
  576. sim_fpu_to32 (&res, &ans);
  577. result = res;
  578. break;
  579. }
  580. case fmt_double:
  581. {
  582. unsigned64 res;
  583. sim_fpu_64to (&wop, op);
  584. status |= (*sim_fpu_op) (&ans, &wop);
  585. status |= sim_fpu_round_64 (&ans, round, denorm);
  586. sim_fpu_to64 (&res, &ans);
  587. result = res;
  588. break;
  589. }
  590. case fmt_ps:
  591. {
  592. int status_u = 0, status_l = 0;
  593. unsigned32 res_u, res_l;
  594. sim_fpu_32to (&wop, FP_PS_upper(op));
  595. status_u |= (*sim_fpu_op) (&ans, &wop);
  596. sim_fpu_to32 (&res_u, &ans);
  597. sim_fpu_32to (&wop, FP_PS_lower(op));
  598. status_l |= (*sim_fpu_op) (&ans, &wop);
  599. sim_fpu_to32 (&res_l, &ans);
  600. result = FP_PS_cat(res_u, res_l);
  601. status = status_u | status_l;
  602. break;
  603. }
  604. default:
  605. sim_io_eprintf (SD, "Bad switch\n");
  606. abort ();
  607. }
  608. update_fcsr (cpu, cia, status);
  609. return result;
  610. }
  611. static unsigned64
  612. fp_binary(sim_cpu *cpu,
  613. address_word cia,
  614. int (*sim_fpu_op)(sim_fpu *, const sim_fpu *, const sim_fpu *),
  615. unsigned64 op1,
  616. unsigned64 op2,
  617. FP_formats fmt)
  618. {
  619. sim_fpu wop1;
  620. sim_fpu wop2;
  621. sim_fpu ans;
  622. sim_fpu_round round = rounding_mode (GETRM());
  623. sim_fpu_denorm denorm = denorm_mode (cpu);
  624. sim_fpu_status status = 0;
  625. unsigned64 result = 0;
  626. /* The format type has already been checked: */
  627. switch (fmt)
  628. {
  629. case fmt_single:
  630. {
  631. unsigned32 res;
  632. sim_fpu_32to (&wop1, op1);
  633. sim_fpu_32to (&wop2, op2);
  634. status |= (*sim_fpu_op) (&ans, &wop1, &wop2);
  635. status |= sim_fpu_round_32 (&ans, round, denorm);
  636. sim_fpu_to32 (&res, &ans);
  637. result = res;
  638. break;
  639. }
  640. case fmt_double:
  641. {
  642. unsigned64 res;
  643. sim_fpu_64to (&wop1, op1);
  644. sim_fpu_64to (&wop2, op2);
  645. status |= (*sim_fpu_op) (&ans, &wop1, &wop2);
  646. status |= sim_fpu_round_64 (&ans, round, denorm);
  647. sim_fpu_to64 (&res, &ans);
  648. result = res;
  649. break;
  650. }
  651. case fmt_ps:
  652. {
  653. int status_u = 0, status_l = 0;
  654. unsigned32 res_u, res_l;
  655. sim_fpu_32to (&wop1, FP_PS_upper(op1));
  656. sim_fpu_32to (&wop2, FP_PS_upper(op2));
  657. status_u |= (*sim_fpu_op) (&ans, &wop1, &wop2);
  658. sim_fpu_to32 (&res_u, &ans);
  659. sim_fpu_32to (&wop1, FP_PS_lower(op1));
  660. sim_fpu_32to (&wop2, FP_PS_lower(op2));
  661. status_l |= (*sim_fpu_op) (&ans, &wop1, &wop2);
  662. sim_fpu_to32 (&res_l, &ans);
  663. result = FP_PS_cat(res_u, res_l);
  664. status = status_u | status_l;
  665. break;
  666. }
  667. default:
  668. sim_io_eprintf (SD, "Bad switch\n");
  669. abort ();
  670. }
  671. update_fcsr (cpu, cia, status);
  672. return result;
  673. }
  674. /* Common MAC code for single operands (.s or .d), defers setting FCSR. */
  675. static sim_fpu_status
  676. inner_mac(int (*sim_fpu_op)(sim_fpu *, const sim_fpu *, const sim_fpu *),
  677. unsigned64 op1,
  678. unsigned64 op2,
  679. unsigned64 op3,
  680. int scale,
  681. int negate,
  682. FP_formats fmt,
  683. sim_fpu_round round,
  684. sim_fpu_denorm denorm,
  685. unsigned64 *result)
  686. {
  687. sim_fpu wop1;
  688. sim_fpu wop2;
  689. sim_fpu ans;
  690. sim_fpu_status status = 0;
  691. sim_fpu_status op_status;
  692. unsigned64 temp = 0;
  693. switch (fmt)
  694. {
  695. case fmt_single:
  696. {
  697. unsigned32 res;
  698. sim_fpu_32to (&wop1, op1);
  699. sim_fpu_32to (&wop2, op2);
  700. status |= sim_fpu_mul (&ans, &wop1, &wop2);
  701. if (scale != 0 && sim_fpu_is_number (&ans)) /* number or denorm */
  702. ans.normal_exp += scale;
  703. status |= sim_fpu_round_32 (&ans, round, denorm);
  704. wop1 = ans;
  705. op_status = 0;
  706. sim_fpu_32to (&wop2, op3);
  707. op_status |= (*sim_fpu_op) (&ans, &wop1, &wop2);
  708. op_status |= sim_fpu_round_32 (&ans, round, denorm);
  709. status |= op_status;
  710. if (negate)
  711. {
  712. wop1 = ans;
  713. op_status = sim_fpu_neg (&ans, &wop1);
  714. op_status |= sim_fpu_round_32 (&ans, round, denorm);
  715. status |= op_status;
  716. }
  717. sim_fpu_to32 (&res, &ans);
  718. temp = res;
  719. break;
  720. }
  721. case fmt_double:
  722. {
  723. unsigned64 res;
  724. sim_fpu_64to (&wop1, op1);
  725. sim_fpu_64to (&wop2, op2);
  726. status |= sim_fpu_mul (&ans, &wop1, &wop2);
  727. if (scale != 0 && sim_fpu_is_number (&ans)) /* number or denorm */
  728. ans.normal_exp += scale;
  729. status |= sim_fpu_round_64 (&ans, round, denorm);
  730. wop1 = ans;
  731. op_status = 0;
  732. sim_fpu_64to (&wop2, op3);
  733. op_status |= (*sim_fpu_op) (&ans, &wop1, &wop2);
  734. op_status |= sim_fpu_round_64 (&ans, round, denorm);
  735. status |= op_status;
  736. if (negate)
  737. {
  738. wop1 = ans;
  739. op_status = sim_fpu_neg (&ans, &wop1);
  740. op_status |= sim_fpu_round_64 (&ans, round, denorm);
  741. status |= op_status;
  742. }
  743. sim_fpu_to64 (&res, &ans);
  744. temp = res;
  745. break;
  746. }
  747. default:
  748. fprintf (stderr, "Bad switch\n");
  749. abort ();
  750. }
  751. *result = temp;
  752. return status;
  753. }
  754. /* Common implementation of madd, nmadd, msub, nmsub that does
  755. intermediate rounding per spec. Also used for recip2 and rsqrt2,
  756. which are transformed into equivalent nmsub operations. The scale
  757. argument is an adjustment to the exponent of the intermediate
  758. product op1*op2. It is currently non-zero for rsqrt2 (-1), which
  759. requires an effective division by 2. */
  760. static unsigned64
  761. fp_mac(sim_cpu *cpu,
  762. address_word cia,
  763. int (*sim_fpu_op)(sim_fpu *, const sim_fpu *, const sim_fpu *),
  764. unsigned64 op1,
  765. unsigned64 op2,
  766. unsigned64 op3,
  767. int scale,
  768. int negate,
  769. FP_formats fmt)
  770. {
  771. sim_fpu_round round = rounding_mode (GETRM());
  772. sim_fpu_denorm denorm = denorm_mode (cpu);
  773. sim_fpu_status status = 0;
  774. unsigned64 result = 0;
  775. /* The format type has already been checked: */
  776. switch (fmt)
  777. {
  778. case fmt_single:
  779. case fmt_double:
  780. status = inner_mac(sim_fpu_op, op1, op2, op3, scale,
  781. negate, fmt, round, denorm, &result);
  782. break;
  783. case fmt_ps:
  784. {
  785. int status_u, status_l;
  786. unsigned64 result_u, result_l;
  787. status_u = inner_mac(sim_fpu_op, FP_PS_upper(op1), FP_PS_upper(op2),
  788. FP_PS_upper(op3), scale, negate, fmt_single,
  789. round, denorm, &result_u);
  790. status_l = inner_mac(sim_fpu_op, FP_PS_lower(op1), FP_PS_lower(op2),
  791. FP_PS_lower(op3), scale, negate, fmt_single,
  792. round, denorm, &result_l);
  793. result = FP_PS_cat(result_u, result_l);
  794. status = status_u | status_l;
  795. break;
  796. }
  797. default:
  798. sim_io_eprintf (SD, "Bad switch\n");
  799. abort ();
  800. }
  801. update_fcsr (cpu, cia, status);
  802. return result;
  803. }
  804. /* Common rsqrt code for single operands (.s or .d), intermediate rounding. */
  805. static sim_fpu_status
  806. inner_rsqrt(unsigned64 op1,
  807. FP_formats fmt,
  808. sim_fpu_round round,
  809. sim_fpu_denorm denorm,
  810. unsigned64 *result)
  811. {
  812. sim_fpu wop1;
  813. sim_fpu ans;
  814. sim_fpu_status status = 0;
  815. sim_fpu_status op_status;
  816. unsigned64 temp = 0;
  817. switch (fmt)
  818. {
  819. case fmt_single:
  820. {
  821. unsigned32 res;
  822. sim_fpu_32to (&wop1, op1);
  823. status |= sim_fpu_sqrt (&ans, &wop1);
  824. status |= sim_fpu_round_32 (&ans, status, round);
  825. wop1 = ans;
  826. op_status = sim_fpu_inv (&ans, &wop1);
  827. op_status |= sim_fpu_round_32 (&ans, round, denorm);
  828. sim_fpu_to32 (&res, &ans);
  829. temp = res;
  830. status |= op_status;
  831. break;
  832. }
  833. case fmt_double:
  834. {
  835. unsigned64 res;
  836. sim_fpu_64to (&wop1, op1);
  837. status |= sim_fpu_sqrt (&ans, &wop1);
  838. status |= sim_fpu_round_64 (&ans, round, denorm);
  839. wop1 = ans;
  840. op_status = sim_fpu_inv (&ans, &wop1);
  841. op_status |= sim_fpu_round_64 (&ans, round, denorm);
  842. sim_fpu_to64 (&res, &ans);
  843. temp = res;
  844. status |= op_status;
  845. break;
  846. }
  847. default:
  848. fprintf (stderr, "Bad switch\n");
  849. abort ();
  850. }
  851. *result = temp;
  852. return status;
  853. }
  854. static unsigned64
  855. fp_inv_sqrt(sim_cpu *cpu,
  856. address_word cia,
  857. unsigned64 op1,
  858. FP_formats fmt)
  859. {
  860. sim_fpu_round round = rounding_mode (GETRM());
  861. sim_fpu_round denorm = denorm_mode (cpu);
  862. sim_fpu_status status = 0;
  863. unsigned64 result = 0;
  864. /* The format type has already been checked: */
  865. switch (fmt)
  866. {
  867. case fmt_single:
  868. case fmt_double:
  869. status = inner_rsqrt (op1, fmt, round, denorm, &result);
  870. break;
  871. case fmt_ps:
  872. {
  873. int status_u, status_l;
  874. unsigned64 result_u, result_l;
  875. status_u = inner_rsqrt (FP_PS_upper(op1), fmt_single, round, denorm,
  876. &result_u);
  877. status_l = inner_rsqrt (FP_PS_lower(op1), fmt_single, round, denorm,
  878. &result_l);
  879. result = FP_PS_cat(result_u, result_l);
  880. status = status_u | status_l;
  881. break;
  882. }
  883. default:
  884. sim_io_eprintf (SD, "Bad switch\n");
  885. abort ();
  886. }
  887. update_fcsr (cpu, cia, status);
  888. return result;
  889. }
  890. unsigned64
  891. fp_abs(sim_cpu *cpu,
  892. address_word cia,
  893. unsigned64 op,
  894. FP_formats fmt)
  895. {
  896. return fp_unary(cpu, cia, &sim_fpu_abs, op, fmt);
  897. }
  898. unsigned64
  899. fp_neg(sim_cpu *cpu,
  900. address_word cia,
  901. unsigned64 op,
  902. FP_formats fmt)
  903. {
  904. return fp_unary(cpu, cia, &sim_fpu_neg, op, fmt);
  905. }
  906. unsigned64
  907. fp_add(sim_cpu *cpu,
  908. address_word cia,
  909. unsigned64 op1,
  910. unsigned64 op2,
  911. FP_formats fmt)
  912. {
  913. return fp_binary(cpu, cia, &sim_fpu_add, op1, op2, fmt);
  914. }
  915. unsigned64
  916. fp_sub(sim_cpu *cpu,
  917. address_word cia,
  918. unsigned64 op1,
  919. unsigned64 op2,
  920. FP_formats fmt)
  921. {
  922. return fp_binary(cpu, cia, &sim_fpu_sub, op1, op2, fmt);
  923. }
  924. unsigned64
  925. fp_mul(sim_cpu *cpu,
  926. address_word cia,
  927. unsigned64 op1,
  928. unsigned64 op2,
  929. FP_formats fmt)
  930. {
  931. return fp_binary(cpu, cia, &sim_fpu_mul, op1, op2, fmt);
  932. }
  933. unsigned64
  934. fp_div(sim_cpu *cpu,
  935. address_word cia,
  936. unsigned64 op1,
  937. unsigned64 op2,
  938. FP_formats fmt)
  939. {
  940. return fp_binary(cpu, cia, &sim_fpu_div, op1, op2, fmt);
  941. }
  942. unsigned64
  943. fp_recip(sim_cpu *cpu,
  944. address_word cia,
  945. unsigned64 op,
  946. FP_formats fmt)
  947. {
  948. return fp_unary(cpu, cia, &sim_fpu_inv, op, fmt);
  949. }
  950. unsigned64
  951. fp_sqrt(sim_cpu *cpu,
  952. address_word cia,
  953. unsigned64 op,
  954. FP_formats fmt)
  955. {
  956. return fp_unary(cpu, cia, &sim_fpu_sqrt, op, fmt);
  957. }
  958. unsigned64
  959. fp_rsqrt(sim_cpu *cpu,
  960. address_word cia,
  961. unsigned64 op,
  962. FP_formats fmt)
  963. {
  964. return fp_inv_sqrt(cpu, cia, op, fmt);
  965. }
  966. unsigned64
  967. fp_madd(sim_cpu *cpu,
  968. address_word cia,
  969. unsigned64 op1,
  970. unsigned64 op2,
  971. unsigned64 op3,
  972. FP_formats fmt)
  973. {
  974. return fp_mac(cpu, cia, &sim_fpu_add, op1, op2, op3, 0, 0, fmt);
  975. }
  976. unsigned64
  977. fp_msub(sim_cpu *cpu,
  978. address_word cia,
  979. unsigned64 op1,
  980. unsigned64 op2,
  981. unsigned64 op3,
  982. FP_formats fmt)
  983. {
  984. return fp_mac(cpu, cia, &sim_fpu_sub, op1, op2, op3, 0, 0, fmt);
  985. }
  986. unsigned64
  987. fp_nmadd(sim_cpu *cpu,
  988. address_word cia,
  989. unsigned64 op1,
  990. unsigned64 op2,
  991. unsigned64 op3,
  992. FP_formats fmt)
  993. {
  994. return fp_mac(cpu, cia, &sim_fpu_add, op1, op2, op3, 0, 1, fmt);
  995. }
  996. unsigned64
  997. fp_nmsub(sim_cpu *cpu,
  998. address_word cia,
  999. unsigned64 op1,
  1000. unsigned64 op2,
  1001. unsigned64 op3,
  1002. FP_formats fmt)
  1003. {
  1004. return fp_mac(cpu, cia, &sim_fpu_sub, op1, op2, op3, 0, 1, fmt);
  1005. }
  1006. /* MIPS-3D ASE operations. */
  1007. /* Variant of fp_binary for *r.ps MIPS-3D operations. */
  1008. static unsigned64
  1009. fp_binary_r(sim_cpu *cpu,
  1010. address_word cia,
  1011. int (*sim_fpu_op)(sim_fpu *, const sim_fpu *, const sim_fpu *),
  1012. unsigned64 op1,
  1013. unsigned64 op2)
  1014. {
  1015. sim_fpu wop1;
  1016. sim_fpu wop2;
  1017. sim_fpu ans;
  1018. sim_fpu_round round = rounding_mode (GETRM ());
  1019. sim_fpu_denorm denorm = denorm_mode (cpu);
  1020. sim_fpu_status status_u, status_l;
  1021. unsigned64 result;
  1022. unsigned32 res_u, res_l;
  1023. /* The format must be fmt_ps. */
  1024. status_u = 0;
  1025. sim_fpu_32to (&wop1, FP_PS_upper (op1));
  1026. sim_fpu_32to (&wop2, FP_PS_lower (op1));
  1027. status_u |= (*sim_fpu_op) (&ans, &wop1, &wop2);
  1028. status_u |= sim_fpu_round_32 (&ans, round, denorm);
  1029. sim_fpu_to32 (&res_u, &ans);
  1030. status_l = 0;
  1031. sim_fpu_32to (&wop1, FP_PS_upper (op2));
  1032. sim_fpu_32to (&wop2, FP_PS_lower (op2));
  1033. status_l |= (*sim_fpu_op) (&ans, &wop1, &wop2);
  1034. status_l |= sim_fpu_round_32 (&ans, round, denorm);
  1035. sim_fpu_to32 (&res_l, &ans);
  1036. result = FP_PS_cat (res_u, res_l);
  1037. update_fcsr (cpu, cia, status_u | status_l);
  1038. return result;
  1039. }
  1040. unsigned64
  1041. fp_add_r(sim_cpu *cpu,
  1042. address_word cia,
  1043. unsigned64 op1,
  1044. unsigned64 op2,
  1045. FP_formats fmt)
  1046. {
  1047. return fp_binary_r (cpu, cia, &sim_fpu_add, op1, op2);
  1048. }
  1049. unsigned64
  1050. fp_mul_r(sim_cpu *cpu,
  1051. address_word cia,
  1052. unsigned64 op1,
  1053. unsigned64 op2,
  1054. FP_formats fmt)
  1055. {
  1056. return fp_binary_r (cpu, cia, &sim_fpu_mul, op1, op2);
  1057. }
  1058. #define NR_FRAC_GUARD (60)
  1059. #define IMPLICIT_1 LSBIT64 (NR_FRAC_GUARD)
  1060. static int
  1061. fpu_inv1(sim_fpu *f, const sim_fpu *l)
  1062. {
  1063. static const sim_fpu sim_fpu_one = {
  1064. sim_fpu_class_number, 0, IMPLICIT_1, 0
  1065. };
  1066. int status = 0;
  1067. sim_fpu t;
  1068. if (sim_fpu_is_zero (l))
  1069. {
  1070. *f = sim_fpu_maxfp;
  1071. f->sign = l->sign;
  1072. return sim_fpu_status_invalid_div0;
  1073. }
  1074. if (sim_fpu_is_infinity (l))
  1075. {
  1076. *f = sim_fpu_zero;
  1077. f->sign = l->sign;
  1078. return status;
  1079. }
  1080. status |= sim_fpu_div (f, &sim_fpu_one, l);
  1081. return status;
  1082. }
  1083. static int
  1084. fpu_inv1_32(sim_fpu *f, const sim_fpu *l)
  1085. {
  1086. if (sim_fpu_is_zero (l))
  1087. {
  1088. *f = sim_fpu_max32;
  1089. f->sign = l->sign;
  1090. return sim_fpu_status_invalid_div0;
  1091. }
  1092. return fpu_inv1 (f, l);
  1093. }
  1094. static int
  1095. fpu_inv1_64(sim_fpu *f, const sim_fpu *l)
  1096. {
  1097. if (sim_fpu_is_zero (l))
  1098. {
  1099. *f = sim_fpu_max64;
  1100. f->sign = l->sign;
  1101. return sim_fpu_status_invalid_div0;
  1102. }
  1103. return fpu_inv1 (f, l);
  1104. }
  1105. unsigned64
  1106. fp_recip1(sim_cpu *cpu,
  1107. address_word cia,
  1108. unsigned64 op,
  1109. FP_formats fmt)
  1110. {
  1111. switch (fmt)
  1112. {
  1113. case fmt_single:
  1114. case fmt_ps:
  1115. return fp_unary (cpu, cia, &fpu_inv1_32, op, fmt);
  1116. case fmt_double:
  1117. return fp_unary (cpu, cia, &fpu_inv1_64, op, fmt);
  1118. }
  1119. return 0;
  1120. }
  1121. unsigned64
  1122. fp_recip2(sim_cpu *cpu,
  1123. address_word cia,
  1124. unsigned64 op1,
  1125. unsigned64 op2,
  1126. FP_formats fmt)
  1127. {
  1128. static const unsigned64 one_single = UNSIGNED64 (0x3F800000);
  1129. static const unsigned64 one_double = UNSIGNED64 (0x3FF0000000000000);
  1130. static const unsigned64 one_ps = (UNSIGNED64 (0x3F800000) << 32 | UNSIGNED64 (0x3F800000));
  1131. unsigned64 one;
  1132. /* Implemented as nmsub fd, 1, fs, ft. */
  1133. switch (fmt)
  1134. {
  1135. case fmt_single: one = one_single; break;
  1136. case fmt_double: one = one_double; break;
  1137. case fmt_ps: one = one_ps; break;
  1138. default: one = 0; abort ();
  1139. }
  1140. return fp_mac (cpu, cia, &sim_fpu_sub, op1, op2, one, 0, 1, fmt);
  1141. }
  1142. static int
  1143. fpu_inv_sqrt1(sim_fpu *f, const sim_fpu *l)
  1144. {
  1145. static const sim_fpu sim_fpu_one = {
  1146. sim_fpu_class_number, 0, IMPLICIT_1, 0
  1147. };
  1148. int status = 0;
  1149. sim_fpu t;
  1150. if (sim_fpu_is_zero (l))
  1151. {
  1152. *f = sim_fpu_maxfp;
  1153. f->sign = l->sign;
  1154. return sim_fpu_status_invalid_div0;
  1155. }
  1156. if (sim_fpu_is_infinity (l))
  1157. {
  1158. if (!l->sign)
  1159. {
  1160. f->class = sim_fpu_class_zero;
  1161. f->sign = 0;
  1162. }
  1163. else
  1164. {
  1165. *f = sim_fpu_qnan;
  1166. status = sim_fpu_status_invalid_sqrt;
  1167. }
  1168. return status;
  1169. }
  1170. status |= sim_fpu_sqrt (&t, l);
  1171. status |= sim_fpu_div (f, &sim_fpu_one, &t);
  1172. return status;
  1173. }
  1174. static int
  1175. fpu_inv_sqrt1_32(sim_fpu *f, const sim_fpu *l)
  1176. {
  1177. if (sim_fpu_is_zero (l))
  1178. {
  1179. *f = sim_fpu_max32;
  1180. f->sign = l->sign;
  1181. return sim_fpu_status_invalid_div0;
  1182. }
  1183. return fpu_inv_sqrt1 (f, l);
  1184. }
  1185. static int
  1186. fpu_inv_sqrt1_64(sim_fpu *f, const sim_fpu *l)
  1187. {
  1188. if (sim_fpu_is_zero (l))
  1189. {
  1190. *f = sim_fpu_max64;
  1191. f->sign = l->sign;
  1192. return sim_fpu_status_invalid_div0;
  1193. }
  1194. return fpu_inv_sqrt1 (f, l);
  1195. }
  1196. unsigned64
  1197. fp_rsqrt1(sim_cpu *cpu,
  1198. address_word cia,
  1199. unsigned64 op,
  1200. FP_formats fmt)
  1201. {
  1202. switch (fmt)
  1203. {
  1204. case fmt_single:
  1205. case fmt_ps:
  1206. return fp_unary (cpu, cia, &fpu_inv_sqrt1_32, op, fmt);
  1207. case fmt_double:
  1208. return fp_unary (cpu, cia, &fpu_inv_sqrt1_64, op, fmt);
  1209. }
  1210. return 0;
  1211. }
  1212. unsigned64
  1213. fp_rsqrt2(sim_cpu *cpu,
  1214. address_word cia,
  1215. unsigned64 op1,
  1216. unsigned64 op2,
  1217. FP_formats fmt)
  1218. {
  1219. static const unsigned64 half_single = UNSIGNED64 (0x3F000000);
  1220. static const unsigned64 half_double = UNSIGNED64 (0x3FE0000000000000);
  1221. static const unsigned64 half_ps = (UNSIGNED64 (0x3F000000) << 32 | UNSIGNED64 (0x3F000000));
  1222. unsigned64 half;
  1223. /* Implemented as (nmsub fd, 0.5, fs, ft)/2, where the divide is
  1224. done by scaling the exponent during multiply. */
  1225. switch (fmt)
  1226. {
  1227. case fmt_single: half = half_single; break;
  1228. case fmt_double: half = half_double; break;
  1229. case fmt_ps: half = half_ps; break;
  1230. default: half = 0; abort ();
  1231. }
  1232. return fp_mac (cpu, cia, &sim_fpu_sub, op1, op2, half, -1, 1, fmt);
  1233. }
  1234. /* Conversion operations. */
  1235. uword64
  1236. convert (sim_cpu *cpu,
  1237. address_word cia,
  1238. int rm,
  1239. uword64 op,
  1240. FP_formats from,
  1241. FP_formats to)
  1242. {
  1243. sim_fpu wop;
  1244. sim_fpu_round round = rounding_mode (rm);
  1245. sim_fpu_denorm denorm = denorm_mode (cpu);
  1246. unsigned32 result32;
  1247. unsigned64 result64;
  1248. sim_fpu_status status = 0;
  1249. /* Convert the input to sim_fpu internal format */
  1250. switch (from)
  1251. {
  1252. case fmt_double:
  1253. sim_fpu_64to (&wop, op);
  1254. break;
  1255. case fmt_single:
  1256. sim_fpu_32to (&wop, op);
  1257. break;
  1258. case fmt_word:
  1259. status = sim_fpu_i32to (&wop, op, round);
  1260. break;
  1261. case fmt_long:
  1262. status = sim_fpu_i64to (&wop, op, round);
  1263. break;
  1264. default:
  1265. sim_io_eprintf (SD, "Bad switch\n");
  1266. abort ();
  1267. }
  1268. /* Convert sim_fpu format into the output */
  1269. /* The value WOP is converted to the destination format, rounding
  1270. using mode RM. When the destination is a fixed-point format, then
  1271. a source value of Infinity, NaN or one which would round to an
  1272. integer outside the fixed point range then an IEEE Invalid Operation
  1273. condition is raised. Not used if destination format is PS. */
  1274. switch (to)
  1275. {
  1276. case fmt_single:
  1277. status |= sim_fpu_round_32 (&wop, round, denorm);
  1278. /* For a NaN, normalize mantissa bits (cvt.s.d can't preserve them) */
  1279. if (sim_fpu_is_qnan (&wop))
  1280. wop = sim_fpu_qnan;
  1281. sim_fpu_to32 (&result32, &wop);
  1282. result64 = result32;
  1283. break;
  1284. case fmt_double:
  1285. status |= sim_fpu_round_64 (&wop, round, denorm);
  1286. /* For a NaN, normalize mantissa bits (make cvt.d.s consistent) */
  1287. if (sim_fpu_is_qnan (&wop))
  1288. wop = sim_fpu_qnan;
  1289. sim_fpu_to64 (&result64, &wop);
  1290. break;
  1291. case fmt_word:
  1292. status |= sim_fpu_to32i (&result32, &wop, round);
  1293. result64 = result32;
  1294. break;
  1295. case fmt_long:
  1296. status |= sim_fpu_to64i (&result64, &wop, round);
  1297. break;
  1298. default:
  1299. result64 = 0;
  1300. sim_io_eprintf (SD, "Bad switch\n");
  1301. abort ();
  1302. }
  1303. update_fcsr (cpu, cia, status);
  1304. return result64;
  1305. }
  1306. unsigned64
  1307. ps_lower(sim_cpu *cpu,
  1308. address_word cia,
  1309. unsigned64 op)
  1310. {
  1311. return FP_PS_lower (op);
  1312. }
  1313. unsigned64
  1314. ps_upper(sim_cpu *cpu,
  1315. address_word cia,
  1316. unsigned64 op)
  1317. {
  1318. return FP_PS_upper(op);
  1319. }
  1320. unsigned64
  1321. pack_ps(sim_cpu *cpu,
  1322. address_word cia,
  1323. unsigned64 op1,
  1324. unsigned64 op2,
  1325. FP_formats fmt)
  1326. {
  1327. unsigned64 result = 0;
  1328. /* The registers must specify FPRs valid for operands of type
  1329. "fmt". If they are not valid, the result is undefined. */
  1330. /* The format type should already have been checked: */
  1331. switch (fmt)
  1332. {
  1333. case fmt_single:
  1334. {
  1335. sim_fpu wop;
  1336. unsigned32 res_u, res_l;
  1337. sim_fpu_32to (&wop, op1);
  1338. sim_fpu_to32 (&res_u, &wop);
  1339. sim_fpu_32to (&wop, op2);
  1340. sim_fpu_to32 (&res_l, &wop);
  1341. result = FP_PS_cat(res_u, res_l);
  1342. break;
  1343. }
  1344. default:
  1345. sim_io_eprintf (SD, "Bad switch\n");
  1346. abort ();
  1347. }
  1348. return result;
  1349. }
  1350. unsigned64
  1351. convert_ps (sim_cpu *cpu,
  1352. address_word cia,
  1353. int rm,
  1354. unsigned64 op,
  1355. FP_formats from,
  1356. FP_formats to)
  1357. {
  1358. sim_fpu wop_u, wop_l;
  1359. sim_fpu_round round = rounding_mode (rm);
  1360. sim_fpu_denorm denorm = denorm_mode (cpu);
  1361. unsigned32 res_u, res_l;
  1362. unsigned64 result;
  1363. sim_fpu_status status_u = 0, status_l = 0;
  1364. /* As convert, but used only for paired values (formats PS, PW) */
  1365. /* Convert the input to sim_fpu internal format */
  1366. switch (from)
  1367. {
  1368. case fmt_word: /* fmt_pw */
  1369. sim_fpu_i32to (&wop_u, (op >> 32) & (unsigned)0xFFFFFFFF, round);
  1370. sim_fpu_i32to (&wop_l, op & (unsigned)0xFFFFFFFF, round);
  1371. break;
  1372. case fmt_ps:
  1373. sim_fpu_32to (&wop_u, FP_PS_upper(op));
  1374. sim_fpu_32to (&wop_l, FP_PS_lower(op));
  1375. break;
  1376. default:
  1377. sim_io_eprintf (SD, "Bad switch\n");
  1378. abort ();
  1379. }
  1380. /* Convert sim_fpu format into the output */
  1381. switch (to)
  1382. {
  1383. case fmt_word: /* fmt_pw */
  1384. status_u |= sim_fpu_to32i (&res_u, &wop_u, round);
  1385. status_l |= sim_fpu_to32i (&res_l, &wop_l, round);
  1386. result = (((unsigned64)res_u) << 32) | (unsigned64)res_l;
  1387. break;
  1388. case fmt_ps:
  1389. status_u |= sim_fpu_round_32 (&wop_u, 0, round);
  1390. status_l |= sim_fpu_round_32 (&wop_l, 0, round);
  1391. sim_fpu_to32 (&res_u, &wop_u);
  1392. sim_fpu_to32 (&res_l, &wop_l);
  1393. result = FP_PS_cat(res_u, res_l);
  1394. break;
  1395. default:
  1396. result = 0;
  1397. sim_io_eprintf (SD, "Bad switch\n");
  1398. abort ();
  1399. }
  1400. update_fcsr (cpu, cia, status_u | status_l);
  1401. return result;
  1402. }
  1403. static const char *
  1404. fpu_format_name (FP_formats fmt)
  1405. {
  1406. switch (fmt)
  1407. {
  1408. case fmt_single:
  1409. return "single";
  1410. case fmt_double:
  1411. return "double";
  1412. case fmt_word:
  1413. return "word";
  1414. case fmt_long:
  1415. return "long";
  1416. case fmt_ps:
  1417. return "ps";
  1418. case fmt_unknown:
  1419. return "<unknown>";
  1420. case fmt_uninterpreted:
  1421. return "<uninterpreted>";
  1422. case fmt_uninterpreted_32:
  1423. return "<uninterpreted_32>";
  1424. case fmt_uninterpreted_64:
  1425. return "<uninterpreted_64>";
  1426. default:
  1427. return "<format error>";
  1428. }
  1429. }
  1430. #ifdef DEBUG
  1431. static const char *
  1432. fpu_rounding_mode_name (int rm)
  1433. {
  1434. switch (rm)
  1435. {
  1436. case FP_RM_NEAREST:
  1437. return "Round";
  1438. case FP_RM_TOZERO:
  1439. return "Trunc";
  1440. case FP_RM_TOPINF:
  1441. return "Ceil";
  1442. case FP_RM_TOMINF:
  1443. return "Floor";
  1444. default:
  1445. return "<rounding mode error>";
  1446. }
  1447. }
  1448. #endif /* DEBUG */