The source code and dockerfile for the GSW2024 AI Lab.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

1053 lines
22 KiB

2 months ago
  1. /**
  2. @file
  3. @ingroup epd
  4. @brief Arithmetic functions with extended double precision.
  5. @author In-Ho Moon
  6. @copyright@parblock
  7. Copyright (c) 1995-2015, Regents of the University of Colorado
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions
  11. are met:
  12. Redistributions of source code must retain the above copyright
  13. notice, this list of conditions and the following disclaimer.
  14. Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in the
  16. documentation and/or other materials provided with the distribution.
  17. Neither the name of the University of Colorado nor the names of its
  18. contributors may be used to endorse or promote products derived from
  19. this software without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  30. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. POSSIBILITY OF SUCH DAMAGE.
  32. @endparblock
  33. */
  34. #include <math.h>
  35. #include "util.h"
  36. #include "epdInt.h"
  37. /**
  38. @brief Allocates an EpDouble struct.
  39. */
  40. EpDouble *
  41. EpdAlloc(void)
  42. {
  43. EpDouble *epd;
  44. epd = ALLOC(EpDouble, 1);
  45. return(epd);
  46. }
  47. /**
  48. @brief Compares two EpDouble struct.
  49. @return 0 if the two structures hold the same value; 1 otherwise.
  50. */
  51. int
  52. EpdCmp(const void *key1, const void *key2)
  53. {
  54. EpDouble const *epd1 = (EpDouble const *) key1;
  55. EpDouble const *epd2 = (EpDouble const *) key2;
  56. if (epd1->type.value != epd2->type.value ||
  57. epd1->exponent != epd2->exponent) {
  58. return(1);
  59. }
  60. return(0);
  61. }
  62. /**
  63. @brief Frees an EpDouble struct.
  64. */
  65. void
  66. EpdFree(EpDouble *epd)
  67. {
  68. FREE(epd);
  69. }
  70. /**
  71. @brief Converts an extended precision double value to a string.
  72. @sideeffect The string is written at the address passed in `str`.
  73. */
  74. void
  75. EpdGetString(EpDouble const *epd, char *str)
  76. {
  77. double value;
  78. int exponent;
  79. char *pos;
  80. if (!str) return;
  81. if (IsNanDouble(epd->type.value)) {
  82. sprintf(str, "NaN");
  83. return;
  84. } else if (IsInfDouble(epd->type.value)) {
  85. if (epd->type.bits.sign == 1)
  86. sprintf(str, "-inf");
  87. else
  88. sprintf(str, "inf");
  89. return;
  90. }
  91. assert(epd->type.bits.exponent == EPD_MAX_BIN ||
  92. epd->type.bits.exponent == 0);
  93. EpdGetValueAndDecimalExponent(epd, &value, &exponent);
  94. sprintf(str, "%e", value);
  95. pos = strstr(str, "e");
  96. if (exponent >= 0) {
  97. if (exponent < 10)
  98. sprintf(pos + 1, "+0%d", exponent);
  99. else
  100. sprintf(pos + 1, "+%d", exponent);
  101. } else {
  102. exponent *= -1;
  103. if (exponent < 10)
  104. sprintf(pos + 1, "-0%d", exponent);
  105. else
  106. sprintf(pos + 1, "-%d", exponent);
  107. }
  108. }
  109. /**
  110. @brief Converts double to EpDouble struct.
  111. */
  112. void
  113. EpdConvert(double value, EpDouble *epd)
  114. {
  115. epd->type.value = value;
  116. epd->exponent = 0;
  117. EpdNormalize(epd);
  118. }
  119. /**
  120. @brief Multiplies an extended precision double by a double.
  121. */
  122. void
  123. EpdMultiply(EpDouble *epd1, double value)
  124. {
  125. EpDouble epd2;
  126. double tmp;
  127. int exponent;
  128. if (EpdIsNan(epd1) || IsNanDouble(value)) {
  129. EpdMakeNan(epd1);
  130. return;
  131. } else if (EpdIsInf(epd1) || IsInfDouble(value)) {
  132. int sign;
  133. EpdConvert(value, &epd2);
  134. sign = epd1->type.bits.sign ^ epd2.type.bits.sign;
  135. EpdMakeInf(epd1, sign);
  136. return;
  137. }
  138. assert(epd1->type.bits.exponent == EPD_MAX_BIN);
  139. EpdConvert(value, &epd2);
  140. tmp = epd1->type.value * epd2.type.value;
  141. exponent = epd1->exponent + epd2.exponent;
  142. epd1->type.value = tmp;
  143. epd1->exponent = exponent;
  144. EpdNormalize(epd1);
  145. }
  146. /**
  147. @brief Multiplies an extended precision double by another.
  148. */
  149. void
  150. EpdMultiply2(EpDouble *epd1, EpDouble const *epd2)
  151. {
  152. double value;
  153. int exponent;
  154. if (EpdIsNan(epd1) || EpdIsNan(epd2)) {
  155. EpdMakeNan(epd1);
  156. return;
  157. } else if (EpdIsInf(epd1) || EpdIsInf(epd2)) {
  158. int sign;
  159. sign = epd1->type.bits.sign ^ epd2->type.bits.sign;
  160. EpdMakeInf(epd1, sign);
  161. return;
  162. }
  163. assert(epd1->type.bits.exponent == EPD_MAX_BIN);
  164. assert(epd2->type.bits.exponent == EPD_MAX_BIN);
  165. value = epd1->type.value * epd2->type.value;
  166. exponent = epd1->exponent + epd2->exponent;
  167. epd1->type.value = value;
  168. epd1->exponent = exponent;
  169. EpdNormalize(epd1);
  170. }
  171. /**
  172. @brief Multiplies two extended precision double values.
  173. */
  174. void
  175. EpdMultiply2Decimal(EpDouble *epd1, EpDouble const *epd2)
  176. {
  177. double value;
  178. int exponent;
  179. if (EpdIsNan(epd1) || EpdIsNan(epd2)) {
  180. EpdMakeNan(epd1);
  181. return;
  182. } else if (EpdIsInf(epd1) || EpdIsInf(epd2)) {
  183. int sign;
  184. sign = epd1->type.bits.sign ^ epd2->type.bits.sign;
  185. EpdMakeInf(epd1, sign);
  186. return;
  187. }
  188. value = epd1->type.value * epd2->type.value;
  189. exponent = epd1->exponent + epd2->exponent;
  190. epd1->type.value = value;
  191. epd1->exponent = exponent;
  192. EpdNormalizeDecimal(epd1);
  193. }
  194. /**
  195. @brief Multiplies two extended precision double values.
  196. @details The result goes in the third operand.
  197. */
  198. void
  199. EpdMultiply3(EpDouble const *epd1, EpDouble const *epd2, EpDouble *epd3)
  200. {
  201. if (EpdIsNan(epd1) || EpdIsNan(epd2)) {
  202. EpdMakeNan(epd3);
  203. return;
  204. } else if (EpdIsInf(epd1) || EpdIsInf(epd2)) {
  205. int sign;
  206. sign = epd1->type.bits.sign ^ epd2->type.bits.sign;
  207. EpdMakeInf(epd3, sign);
  208. return;
  209. }
  210. assert(epd1->type.bits.exponent == EPD_MAX_BIN);
  211. assert(epd2->type.bits.exponent == EPD_MAX_BIN);
  212. epd3->type.value = epd1->type.value * epd2->type.value;
  213. epd3->exponent = epd1->exponent + epd2->exponent;
  214. EpdNormalize(epd3);
  215. }
  216. /**
  217. @brief Multiplies two extended precision double values.
  218. */
  219. void
  220. EpdMultiply3Decimal(EpDouble const *epd1, EpDouble const *epd2, EpDouble *epd3)
  221. {
  222. if (EpdIsNan(epd1) || EpdIsNan(epd2)) {
  223. EpdMakeNan(epd3);
  224. return;
  225. } else if (EpdIsInf(epd1) || EpdIsInf(epd2)) {
  226. int sign;
  227. sign = epd1->type.bits.sign ^ epd2->type.bits.sign;
  228. EpdMakeInf(epd3, sign);
  229. return;
  230. }
  231. epd3->type.value = epd1->type.value * epd2->type.value;
  232. epd3->exponent = epd1->exponent + epd2->exponent;
  233. EpdNormalizeDecimal(epd3);
  234. }
  235. /**
  236. @brief Divides an extended precision double by a double.
  237. */
  238. void
  239. EpdDivide(EpDouble *epd1, double value)
  240. {
  241. EpDouble epd2;
  242. double tmp;
  243. int exponent;
  244. if (EpdIsNan(epd1) || IsNanDouble(value)) {
  245. EpdMakeNan(epd1);
  246. return;
  247. } else if (EpdIsInf(epd1) || IsInfDouble(value)) {
  248. int sign;
  249. EpdConvert(value, &epd2);
  250. if (EpdIsInf(epd1) && IsInfDouble(value)) {
  251. EpdMakeNan(epd1);
  252. } else if (EpdIsInf(epd1)) {
  253. sign = epd1->type.bits.sign ^ epd2.type.bits.sign;
  254. EpdMakeInf(epd1, sign);
  255. } else {
  256. sign = epd1->type.bits.sign ^ epd2.type.bits.sign;
  257. EpdMakeZero(epd1, sign);
  258. }
  259. return;
  260. }
  261. if (value == 0.0) {
  262. EpdMakeNan(epd1);
  263. return;
  264. }
  265. assert(epd1->type.bits.exponent == EPD_MAX_BIN);
  266. EpdConvert(value, &epd2);
  267. tmp = epd1->type.value / epd2.type.value;
  268. exponent = epd1->exponent - epd2.exponent;
  269. epd1->type.value = tmp;
  270. epd1->exponent = exponent;
  271. EpdNormalize(epd1);
  272. }
  273. /**
  274. @brief Divides an extended precision double by another.
  275. */
  276. void
  277. EpdDivide2(EpDouble *epd1, EpDouble const *epd2)
  278. {
  279. double value;
  280. int exponent;
  281. if (EpdIsNan(epd1) || EpdIsNan(epd2)) {
  282. EpdMakeNan(epd1);
  283. return;
  284. } else if (EpdIsInf(epd1) || EpdIsInf(epd2)) {
  285. int sign;
  286. if (EpdIsInf(epd1) && EpdIsInf(epd2)) {
  287. EpdMakeNan(epd1);
  288. } else if (EpdIsInf(epd1)) {
  289. sign = epd1->type.bits.sign ^ epd2->type.bits.sign;
  290. EpdMakeInf(epd1, sign);
  291. } else {
  292. sign = epd1->type.bits.sign ^ epd2->type.bits.sign;
  293. EpdMakeZero(epd1, sign);
  294. }
  295. return;
  296. }
  297. if (epd2->type.value == 0.0) {
  298. EpdMakeNan(epd1);
  299. return;
  300. }
  301. assert(epd1->type.bits.exponent == EPD_MAX_BIN);
  302. assert(epd2->type.bits.exponent == EPD_MAX_BIN);
  303. value = epd1->type.value / epd2->type.value;
  304. exponent = epd1->exponent - epd2->exponent;
  305. epd1->type.value = value;
  306. epd1->exponent = exponent;
  307. EpdNormalize(epd1);
  308. }
  309. /**
  310. @brief Divides two extended precision double values.
  311. */
  312. void
  313. EpdDivide3(EpDouble const *epd1, EpDouble const *epd2, EpDouble *epd3)
  314. {
  315. if (EpdIsNan(epd1) || EpdIsNan(epd2)) {
  316. EpdMakeNan(epd3);
  317. return;
  318. } else if (EpdIsInf(epd1) || EpdIsInf(epd2)) {
  319. int sign;
  320. if (EpdIsInf(epd1) && EpdIsInf(epd2)) {
  321. EpdMakeNan(epd3);
  322. } else if (EpdIsInf(epd1)) {
  323. sign = epd1->type.bits.sign ^ epd2->type.bits.sign;
  324. EpdMakeInf(epd3, sign);
  325. } else {
  326. sign = epd1->type.bits.sign ^ epd2->type.bits.sign;
  327. EpdMakeZero(epd3, sign);
  328. }
  329. return;
  330. }
  331. if (epd2->type.value == 0.0) {
  332. EpdMakeNan(epd3);
  333. return;
  334. }
  335. assert(epd1->type.bits.exponent == EPD_MAX_BIN);
  336. assert(epd2->type.bits.exponent == EPD_MAX_BIN);
  337. epd3->type.value = epd1->type.value / epd2->type.value;
  338. epd3->exponent = epd1->exponent - epd2->exponent;
  339. EpdNormalize(epd3);
  340. }
  341. /**
  342. @brief Adds a double to an extended precision double.
  343. */
  344. void
  345. EpdAdd(EpDouble *epd1, double value)
  346. {
  347. EpDouble epd2;
  348. double tmp;
  349. int exponent, diff;
  350. if (EpdIsNan(epd1) || IsNanDouble(value)) {
  351. EpdMakeNan(epd1);
  352. return;
  353. } else if (EpdIsInf(epd1) || IsInfDouble(value)) {
  354. int sign;
  355. EpdConvert(value, &epd2);
  356. if (EpdIsInf(epd1) && IsInfDouble(value)) {
  357. sign = epd1->type.bits.sign ^ epd2.type.bits.sign;
  358. if (sign == 1)
  359. EpdMakeNan(epd1);
  360. } else if (EpdIsInf(&epd2)) {
  361. EpdCopy(&epd2, epd1);
  362. }
  363. return;
  364. }
  365. assert(epd1->type.bits.exponent == EPD_MAX_BIN);
  366. EpdConvert(value, &epd2);
  367. if (epd1->exponent > epd2.exponent) {
  368. diff = epd1->exponent - epd2.exponent;
  369. if (diff <= EPD_MAX_BIN)
  370. tmp = epd1->type.value + epd2.type.value / pow((double)2.0, (double)diff);
  371. else
  372. tmp = epd1->type.value;
  373. exponent = epd1->exponent;
  374. } else if (epd1->exponent < epd2.exponent) {
  375. diff = epd2.exponent - epd1->exponent;
  376. if (diff <= EPD_MAX_BIN)
  377. tmp = epd1->type.value / pow((double)2.0, (double)diff) + epd2.type.value;
  378. else
  379. tmp = epd2.type.value;
  380. exponent = epd2.exponent;
  381. } else {
  382. tmp = epd1->type.value + epd2.type.value;
  383. exponent = epd1->exponent;
  384. }
  385. epd1->type.value = tmp;
  386. epd1->exponent = exponent;
  387. EpdNormalize(epd1);
  388. }
  389. /**
  390. @brief Adds an extended precision double to another.
  391. @details The sum goes in the first argument.
  392. */
  393. void
  394. EpdAdd2(EpDouble *epd1, EpDouble const *epd2)
  395. {
  396. double value;
  397. int exponent, diff;
  398. if (EpdIsNan(epd1) || EpdIsNan(epd2)) {
  399. EpdMakeNan(epd1);
  400. return;
  401. } else if (EpdIsInf(epd1) || EpdIsInf(epd2)) {
  402. int sign;
  403. if (EpdIsInf(epd1) && EpdIsInf(epd2)) {
  404. sign = epd1->type.bits.sign ^ epd2->type.bits.sign;
  405. if (sign == 1)
  406. EpdMakeNan(epd1);
  407. } else if (EpdIsInf(epd2)) {
  408. EpdCopy(epd2, epd1);
  409. }
  410. return;
  411. }
  412. assert(epd1->type.bits.exponent == EPD_MAX_BIN);
  413. assert(epd2->type.bits.exponent == EPD_MAX_BIN);
  414. if (epd1->exponent > epd2->exponent) {
  415. diff = epd1->exponent - epd2->exponent;
  416. if (diff <= EPD_MAX_BIN) {
  417. value = epd1->type.value +
  418. epd2->type.value / pow((double)2.0, (double)diff);
  419. } else
  420. value = epd1->type.value;
  421. exponent = epd1->exponent;
  422. } else if (epd1->exponent < epd2->exponent) {
  423. diff = epd2->exponent - epd1->exponent;
  424. if (diff <= EPD_MAX_BIN) {
  425. value = epd1->type.value / pow((double)2.0, (double)diff) +
  426. epd2->type.value;
  427. } else
  428. value = epd2->type.value;
  429. exponent = epd2->exponent;
  430. } else {
  431. value = epd1->type.value + epd2->type.value;
  432. exponent = epd1->exponent;
  433. }
  434. epd1->type.value = value;
  435. epd1->exponent = exponent;
  436. EpdNormalize(epd1);
  437. }
  438. /**
  439. @brief Adds two extended precision double values.
  440. */
  441. void
  442. EpdAdd3(EpDouble const *epd1, EpDouble const *epd2, EpDouble *epd3)
  443. {
  444. double value;
  445. int exponent, diff;
  446. if (EpdIsNan(epd1) || EpdIsNan(epd2)) {
  447. EpdMakeNan(epd3);
  448. return;
  449. } else if (EpdIsInf(epd1) || EpdIsInf(epd2)) {
  450. int sign;
  451. if (EpdIsInf(epd1) && EpdIsInf(epd2)) {
  452. sign = epd1->type.bits.sign ^ epd2->type.bits.sign;
  453. if (sign == 1)
  454. EpdMakeNan(epd3);
  455. else
  456. EpdCopy(epd1, epd3);
  457. } else if (EpdIsInf(epd1)) {
  458. EpdCopy(epd1, epd3);
  459. } else {
  460. EpdCopy(epd2, epd3);
  461. }
  462. return;
  463. }
  464. assert(epd1->type.bits.exponent == EPD_MAX_BIN);
  465. assert(epd2->type.bits.exponent == EPD_MAX_BIN);
  466. if (epd1->exponent > epd2->exponent) {
  467. diff = epd1->exponent - epd2->exponent;
  468. if (diff <= EPD_MAX_BIN) {
  469. value = epd1->type.value +
  470. epd2->type.value / pow((double)2.0, (double)diff);
  471. } else
  472. value = epd1->type.value;
  473. exponent = epd1->exponent;
  474. } else if (epd1->exponent < epd2->exponent) {
  475. diff = epd2->exponent - epd1->exponent;
  476. if (diff <= EPD_MAX_BIN) {
  477. value = epd1->type.value / pow((double)2.0, (double)diff) +
  478. epd2->type.value;
  479. } else
  480. value = epd2->type.value;
  481. exponent = epd2->exponent;
  482. } else {
  483. value = epd1->type.value + epd2->type.value;
  484. exponent = epd1->exponent;
  485. }
  486. epd3->type.value = value;
  487. epd3->exponent = exponent;
  488. EpdNormalize(epd3);
  489. }
  490. /**
  491. @brief Subtracts a double from an extended precision double.
  492. */
  493. void
  494. EpdSubtract(EpDouble *epd1, double value)
  495. {
  496. EpDouble epd2;
  497. double tmp;
  498. int exponent, diff;
  499. if (EpdIsNan(epd1) || IsNanDouble(value)) {
  500. EpdMakeNan(epd1);
  501. return;
  502. } else if (EpdIsInf(epd1) || IsInfDouble(value)) {
  503. int sign;
  504. EpdConvert(value, &epd2);
  505. if (EpdIsInf(epd1) && IsInfDouble(value)) {
  506. sign = epd1->type.bits.sign ^ epd2.type.bits.sign;
  507. if (sign == 0)
  508. EpdMakeNan(epd1);
  509. } else if (EpdIsInf(&epd2)) {
  510. EpdCopy(&epd2, epd1);
  511. }
  512. return;
  513. }
  514. assert(epd1->type.bits.exponent == EPD_MAX_BIN);
  515. EpdConvert(value, &epd2);
  516. if (epd1->exponent > epd2.exponent) {
  517. diff = epd1->exponent - epd2.exponent;
  518. if (diff <= EPD_MAX_BIN)
  519. tmp = epd1->type.value - epd2.type.value / pow((double)2.0, (double)diff);
  520. else
  521. tmp = epd1->type.value;
  522. exponent = epd1->exponent;
  523. } else if (epd1->exponent < epd2.exponent) {
  524. diff = epd2.exponent - epd1->exponent;
  525. if (diff <= EPD_MAX_BIN)
  526. tmp = epd1->type.value / pow((double)2.0, (double)diff) - epd2.type.value;
  527. else
  528. tmp = epd2.type.value * (double)(-1.0);
  529. exponent = epd2.exponent;
  530. } else {
  531. tmp = epd1->type.value - epd2.type.value;
  532. exponent = epd1->exponent;
  533. }
  534. epd1->type.value = tmp;
  535. epd1->exponent = exponent;
  536. EpdNormalize(epd1);
  537. }
  538. /**
  539. @brief Subtracts an extended precision double from another.
  540. */
  541. void
  542. EpdSubtract2(EpDouble *epd1, EpDouble const *epd2)
  543. {
  544. double value;
  545. int exponent, diff;
  546. if (EpdIsNan(epd1) || EpdIsNan(epd2)) {
  547. EpdMakeNan(epd1);
  548. return;
  549. } else if (EpdIsInf(epd1) || EpdIsInf(epd2)) {
  550. int sign;
  551. if (EpdIsInf(epd1) && EpdIsInf(epd2)) {
  552. sign = epd1->type.bits.sign ^ epd2->type.bits.sign;
  553. if (sign == 0)
  554. EpdMakeNan(epd1);
  555. } else if (EpdIsInf(epd2)) {
  556. EpdCopy(epd2, epd1);
  557. }
  558. return;
  559. }
  560. assert(epd1->type.bits.exponent == EPD_MAX_BIN);
  561. assert(epd2->type.bits.exponent == EPD_MAX_BIN);
  562. if (epd1->exponent > epd2->exponent) {
  563. diff = epd1->exponent - epd2->exponent;
  564. if (diff <= EPD_MAX_BIN) {
  565. value = epd1->type.value -
  566. epd2->type.value / pow((double)2.0, (double)diff);
  567. } else
  568. value = epd1->type.value;
  569. exponent = epd1->exponent;
  570. } else if (epd1->exponent < epd2->exponent) {
  571. diff = epd2->exponent - epd1->exponent;
  572. if (diff <= EPD_MAX_BIN) {
  573. value = epd1->type.value / pow((double)2.0, (double)diff) -
  574. epd2->type.value;
  575. } else
  576. value = epd2->type.value * (double)(-1.0);
  577. exponent = epd2->exponent;
  578. } else {
  579. value = epd1->type.value - epd2->type.value;
  580. exponent = epd1->exponent;
  581. }
  582. epd1->type.value = value;
  583. epd1->exponent = exponent;
  584. EpdNormalize(epd1);
  585. }
  586. /**
  587. @brief Subtracts two extended precision double values.
  588. */
  589. void
  590. EpdSubtract3(EpDouble const *epd1, EpDouble const *epd2, EpDouble *epd3)
  591. {
  592. double value;
  593. int exponent, diff;
  594. if (EpdIsNan(epd1) || EpdIsNan(epd2)) {
  595. EpdMakeNan(epd3);
  596. return;
  597. } else if (EpdIsInf(epd1) || EpdIsInf(epd2)) {
  598. int sign;
  599. if (EpdIsInf(epd1) && EpdIsInf(epd2)) {
  600. sign = epd1->type.bits.sign ^ epd2->type.bits.sign;
  601. if (sign == 0)
  602. EpdCopy(epd1, epd3);
  603. else
  604. EpdMakeNan(epd3);
  605. } else if (EpdIsInf(epd1)) {
  606. EpdCopy(epd1, epd3);
  607. } else {
  608. sign = epd2->type.bits.sign ^ 0x1;
  609. EpdMakeInf(epd3, sign);
  610. }
  611. return;
  612. }
  613. assert(epd1->type.bits.exponent == EPD_MAX_BIN);
  614. assert(epd2->type.bits.exponent == EPD_MAX_BIN);
  615. if (epd1->exponent > epd2->exponent) {
  616. diff = epd1->exponent - epd2->exponent;
  617. if (diff <= EPD_MAX_BIN) {
  618. value = epd1->type.value -
  619. epd2->type.value / pow((double)2.0, (double)diff);
  620. } else
  621. value = epd1->type.value;
  622. exponent = epd1->exponent;
  623. } else if (epd1->exponent < epd2->exponent) {
  624. diff = epd2->exponent - epd1->exponent;
  625. if (diff <= EPD_MAX_BIN) {
  626. value = epd1->type.value / pow((double)2.0, (double)diff) -
  627. epd2->type.value;
  628. } else
  629. value = epd2->type.value * (double)(-1.0);
  630. exponent = epd2->exponent;
  631. } else {
  632. value = epd1->type.value - epd2->type.value;
  633. exponent = epd1->exponent;
  634. }
  635. epd3->type.value = value;
  636. epd3->exponent = exponent;
  637. EpdNormalize(epd3);
  638. }
  639. /**
  640. @brief Computes extended precision pow of base 2.
  641. */
  642. void
  643. EpdPow2(int n, EpDouble *epd)
  644. {
  645. if (n <= EPD_MAX_BIN) {
  646. EpdConvert(pow((double)2.0, (double)n), epd);
  647. } else {
  648. EpDouble epd1, epd2;
  649. int n1, n2;
  650. n1 = n / 2;
  651. n2 = n - n1;
  652. EpdPow2(n1, &epd1);
  653. EpdPow2(n2, &epd2);
  654. EpdMultiply3(&epd1, &epd2, epd);
  655. }
  656. }
  657. /**
  658. @brief Computes extended precision pow of base 2.
  659. */
  660. void
  661. EpdPow2Decimal(int n, EpDouble *epd)
  662. {
  663. if (n <= EPD_MAX_BIN) {
  664. epd->type.value = pow((double)2.0, (double)n);
  665. epd->exponent = 0;
  666. EpdNormalizeDecimal(epd);
  667. } else {
  668. EpDouble epd1, epd2;
  669. int n1, n2;
  670. n1 = n / 2;
  671. n2 = n - n1;
  672. EpdPow2Decimal(n1, &epd1);
  673. EpdPow2Decimal(n2, &epd2);
  674. EpdMultiply3Decimal(&epd1, &epd2, epd);
  675. }
  676. }
  677. /**
  678. @brief Normalize an extended precision double value.
  679. */
  680. void
  681. EpdNormalize(EpDouble *epd)
  682. {
  683. int exponent;
  684. if (IsNanOrInfDouble(epd->type.value)) {
  685. epd->exponent = 0;
  686. return;
  687. }
  688. exponent = EpdGetExponent(epd->type.value);
  689. if (exponent == EPD_MAX_BIN)
  690. return;
  691. exponent -= EPD_MAX_BIN;
  692. epd->type.bits.exponent = EPD_MAX_BIN;
  693. epd->exponent += exponent;
  694. }
  695. /**
  696. @brief Normalize an extended precision double value.
  697. */
  698. void
  699. EpdNormalizeDecimal(EpDouble *epd)
  700. {
  701. int exponent;
  702. if (IsNanOrInfDouble(epd->type.value)) {
  703. epd->exponent = 0;
  704. return;
  705. }
  706. exponent = EpdGetExponentDecimal(epd->type.value);
  707. epd->type.value /= pow((double)10.0, (double)exponent);
  708. epd->exponent += exponent;
  709. }
  710. /**
  711. @brief Returns value and decimal exponent of EpDouble.
  712. */
  713. void
  714. EpdGetValueAndDecimalExponent(EpDouble const *epd, double *value, int *exponent)
  715. {
  716. EpDouble epd1, epd2;
  717. if (EpdIsNanOrInf(epd)) {
  718. *exponent = EPD_EXP_INF;
  719. *value = 0.0;
  720. return;
  721. }
  722. if (EpdIsZero(epd)) {
  723. *value = 0.0;
  724. *exponent = 0;
  725. return;
  726. }
  727. epd1.type.value = epd->type.value;
  728. epd1.exponent = 0;
  729. EpdPow2Decimal(epd->exponent, &epd2);
  730. EpdMultiply2Decimal(&epd1, &epd2);
  731. *value = epd1.type.value;
  732. *exponent = epd1.exponent;
  733. }
  734. /**
  735. @brief Returns the exponent value of a double.
  736. */
  737. int
  738. EpdGetExponent(double value)
  739. {
  740. int exponent;
  741. EpDouble epd;
  742. epd.type.value = value;
  743. exponent = epd.type.bits.exponent;
  744. return(exponent);
  745. }
  746. /**
  747. @brief Returns the decimal exponent value of a double.
  748. */
  749. int
  750. EpdGetExponentDecimal(double value)
  751. {
  752. char *pos, str[24];
  753. int exponent;
  754. sprintf(str, "%E", value);
  755. pos = strstr(str, "E");
  756. sscanf(pos, "E%d", &exponent);
  757. return(exponent);
  758. }
  759. /**
  760. @brief Makes EpDouble Inf.
  761. */
  762. void
  763. EpdMakeInf(EpDouble *epd, int sign)
  764. {
  765. epd->type.bits.mantissa1 = 0;
  766. epd->type.bits.mantissa0 = 0;
  767. epd->type.bits.exponent = EPD_EXP_INF;
  768. epd->type.bits.sign = sign;
  769. epd->exponent = 0;
  770. }
  771. /**
  772. @brief Makes EpDouble Zero.
  773. */
  774. void
  775. EpdMakeZero(EpDouble *epd, int sign)
  776. {
  777. epd->type.bits.mantissa1 = 0;
  778. epd->type.bits.mantissa0 = 0;
  779. epd->type.bits.exponent = 0;
  780. epd->type.bits.sign = sign;
  781. epd->exponent = 0;
  782. }
  783. /**
  784. @brief Makes EpDouble NaN.
  785. */
  786. void
  787. EpdMakeNan(EpDouble *epd)
  788. {
  789. epd->type.nan.mantissa1 = 0;
  790. epd->type.nan.mantissa0 = 0;
  791. epd->type.nan.quiet_bit = 1;
  792. epd->type.nan.exponent = EPD_EXP_INF;
  793. epd->type.nan.sign = 1;
  794. epd->exponent = 0;
  795. }
  796. /**
  797. @brief Copies an EpDouble struct.
  798. */
  799. void
  800. EpdCopy(EpDouble const *from, EpDouble *to)
  801. {
  802. to->type.value = from->type.value;
  803. to->exponent = from->exponent;
  804. }
  805. /**
  806. @brief Checks whether the value is Inf.
  807. */
  808. int
  809. EpdIsInf(EpDouble const *epd)
  810. {
  811. return(IsInfDouble(epd->type.value));
  812. }
  813. /**
  814. @brief Checks whether the value is Zero.
  815. */
  816. int
  817. EpdIsZero(EpDouble const *epd)
  818. {
  819. if (epd->type.value == 0.0)
  820. return(1);
  821. else
  822. return(0);
  823. }
  824. /**
  825. @brief Checks whether the value is NaN.
  826. */
  827. int
  828. EpdIsNan(EpDouble const *epd)
  829. {
  830. return(IsNanDouble(epd->type.value));
  831. }
  832. /**
  833. @brief Checks whether the value is NaN or Inf.
  834. */
  835. int
  836. EpdIsNanOrInf(EpDouble const *epd)
  837. {
  838. return(IsNanOrInfDouble(epd->type.value));
  839. }
  840. /**
  841. @brief Checks whether the value is Inf.
  842. */
  843. int
  844. IsInfDouble(double value)
  845. {
  846. EpType val;
  847. val.value = value;
  848. if (val.bits.exponent == EPD_EXP_INF &&
  849. val.bits.mantissa0 == 0 &&
  850. val.bits.mantissa1 == 0) {
  851. if (val.bits.sign == 0)
  852. return(1);
  853. else
  854. return(-1);
  855. }
  856. return(0);
  857. }
  858. /**
  859. @brief Checks whether the value is NaN.
  860. */
  861. int
  862. IsNanDouble(double value)
  863. {
  864. EpType val;
  865. val.value = value;
  866. if (val.nan.exponent == EPD_EXP_INF &&
  867. val.nan.sign == 1 &&
  868. val.nan.quiet_bit == 1 &&
  869. val.nan.mantissa0 == 0 &&
  870. val.nan.mantissa1 == 0) {
  871. return(1);
  872. }
  873. return(0);
  874. }
  875. /**
  876. @brief Checks whether the value is NaN or Inf.
  877. */
  878. int
  879. IsNanOrInfDouble(double value)
  880. {
  881. EpType val;
  882. val.value = value;
  883. if (val.nan.exponent == EPD_EXP_INF &&
  884. val.nan.mantissa0 == 0 &&
  885. val.nan.mantissa1 == 0 &&
  886. (val.nan.sign == 1 || val.nan.quiet_bit == 0)) {
  887. return(1);
  888. }
  889. return(0);
  890. }