printf_fphex.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /* Print floating point number in hexadecimal notation according to ISO C99.
  2. Copyright (C) 1997-2012 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library 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 GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <config.h>
  17. #include <math.h>
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdbool.h>
  22. #define NDEBUG
  23. #include <assert.h>
  24. #include "quadmath-rounding-mode.h"
  25. #include "quadmath-printf.h"
  26. #include "_itoa.h"
  27. #include "_itowa.h"
  28. /* Macros for doing the actual output. */
  29. #define outchar(ch) \
  30. do \
  31. { \
  32. register const int outc = (ch); \
  33. if (PUTC (outc, fp) == EOF) \
  34. return -1; \
  35. ++done; \
  36. } while (0)
  37. #define PRINT(ptr, wptr, len) \
  38. do \
  39. { \
  40. register size_t outlen = (len); \
  41. if (wide) \
  42. while (outlen-- > 0) \
  43. outchar (*wptr++); \
  44. else \
  45. while (outlen-- > 0) \
  46. outchar (*ptr++); \
  47. } while (0)
  48. #define PADN(ch, len) \
  49. do \
  50. { \
  51. if (PAD (fp, ch, len) != len) \
  52. return -1; \
  53. done += len; \
  54. } \
  55. while (0)
  56. int
  57. __quadmath_printf_fphex (struct __quadmath_printf_file *fp,
  58. const struct printf_info *info,
  59. const void *const *args)
  60. {
  61. /* The floating-point value to output. */
  62. ieee854_float128 fpnum;
  63. /* Locale-dependent representation of decimal point. */
  64. const char *decimal;
  65. wchar_t decimalwc;
  66. /* "NaN" or "Inf" for the special cases. */
  67. const char *special = NULL;
  68. const wchar_t *wspecial = NULL;
  69. /* Buffer for the generated number string for the mantissa. The
  70. maximal size for the mantissa is 128 bits. */
  71. char numbuf[32];
  72. char *numstr;
  73. char *numend;
  74. wchar_t wnumbuf[32];
  75. wchar_t *wnumstr;
  76. wchar_t *wnumend;
  77. int negative;
  78. /* The maximal exponent of two in decimal notation has 5 digits. */
  79. char expbuf[5];
  80. char *expstr;
  81. wchar_t wexpbuf[5];
  82. wchar_t *wexpstr;
  83. int expnegative;
  84. int exponent;
  85. /* Non-zero is mantissa is zero. */
  86. int zero_mantissa;
  87. /* The leading digit before the decimal point. */
  88. char leading;
  89. /* Precision. */
  90. int precision = info->prec;
  91. /* Width. */
  92. int width = info->width;
  93. /* Number of characters written. */
  94. int done = 0;
  95. /* Nonzero if this is output on a wide character stream. */
  96. int wide = info->wide;
  97. bool do_round_away;
  98. /* Figure out the decimal point character. */
  99. #ifdef USE_NL_LANGINFO
  100. if (info->extra == 0)
  101. decimal = nl_langinfo (DECIMAL_POINT);
  102. else
  103. {
  104. decimal = nl_langinfo (MON_DECIMAL_POINT);
  105. if (*decimal == '\0')
  106. decimal = nl_langinfo (DECIMAL_POINT);
  107. }
  108. /* The decimal point character must never be zero. */
  109. assert (*decimal != '\0');
  110. #elif defined USE_LOCALECONV
  111. const struct lconv *lc = localeconv ();
  112. if (info->extra == 0)
  113. decimal = lc->decimal_point;
  114. else
  115. {
  116. decimal = lc->mon_decimal_point;
  117. if (decimal == NULL || *decimal == '\0')
  118. decimal = lc->decimal_point;
  119. }
  120. if (decimal == NULL || *decimal == '\0')
  121. decimal = ".";
  122. #else
  123. decimal = ".";
  124. #endif
  125. #ifdef USE_NL_LANGINFO_WC
  126. if (info->extra == 0)
  127. decimalwc = nl_langinfo_wc (_NL_NUMERIC_DECIMAL_POINT_WC);
  128. else
  129. {
  130. decimalwc = nl_langinfo_wc (_NL_MONETARY_DECIMAL_POINT_WC);
  131. if (decimalwc == L_('\0'))
  132. decimalwc = nl_langinfo_wc (_NL_NUMERIC_DECIMAL_POINT_WC);
  133. }
  134. /* The decimal point character must never be zero. */
  135. assert (decimalwc != L_('\0'));
  136. #else
  137. decimalwc = L_('.');
  138. #endif
  139. /* Fetch the argument value. */
  140. {
  141. fpnum.value = **(const __float128 **) args[0];
  142. /* Check for special values: not a number or infinity. */
  143. if (isnanq (fpnum.value))
  144. {
  145. negative = fpnum.ieee.negative != 0;
  146. if (isupper (info->spec))
  147. {
  148. special = "NAN";
  149. wspecial = L_("NAN");
  150. }
  151. else
  152. {
  153. special = "nan";
  154. wspecial = L_("nan");
  155. }
  156. }
  157. else
  158. {
  159. if (isinfq (fpnum.value))
  160. {
  161. if (isupper (info->spec))
  162. {
  163. special = "INF";
  164. wspecial = L_("INF");
  165. }
  166. else
  167. {
  168. special = "inf";
  169. wspecial = L_("inf");
  170. }
  171. }
  172. negative = signbitq (fpnum.value);
  173. }
  174. }
  175. if (special)
  176. {
  177. int width = info->width;
  178. if (negative || info->showsign || info->space)
  179. --width;
  180. width -= 3;
  181. if (!info->left && width > 0)
  182. PADN (' ', width);
  183. if (negative)
  184. outchar ('-');
  185. else if (info->showsign)
  186. outchar ('+');
  187. else if (info->space)
  188. outchar (' ');
  189. PRINT (special, wspecial, 3);
  190. if (info->left && width > 0)
  191. PADN (' ', width);
  192. return done;
  193. }
  194. {
  195. /* We have 112 bits of mantissa plus one implicit digit. Since
  196. 112 bits are representable without rest using hexadecimal
  197. digits we use only the implicit digits for the number before
  198. the decimal point. */
  199. uint64_t num0, num1;
  200. assert (sizeof (long double) == 16);
  201. num0 = (((unsigned long long int) fpnum.ieee.mantissa0) << 32
  202. | fpnum.ieee.mantissa1);
  203. num1 = (((unsigned long long int) fpnum.ieee.mantissa2) << 32
  204. | fpnum.ieee.mantissa3);
  205. zero_mantissa = (num0|num1) == 0;
  206. if (sizeof (unsigned long int) > 6)
  207. {
  208. numstr = _itoa_word (num1, numbuf + sizeof numbuf, 16,
  209. info->spec == 'A');
  210. wnumstr = _itowa_word (num1,
  211. wnumbuf + sizeof (wnumbuf) / sizeof (wchar_t),
  212. 16, info->spec == 'A');
  213. }
  214. else
  215. {
  216. numstr = _itoa (num1, numbuf + sizeof numbuf, 16,
  217. info->spec == 'A');
  218. wnumstr = _itowa (num1,
  219. wnumbuf + sizeof (wnumbuf) / sizeof (wchar_t),
  220. 16, info->spec == 'A');
  221. }
  222. while (numstr > numbuf + (sizeof numbuf - 64 / 4))
  223. {
  224. *--numstr = '0';
  225. *--wnumstr = L_('0');
  226. }
  227. if (sizeof (unsigned long int) > 6)
  228. {
  229. numstr = _itoa_word (num0, numstr, 16, info->spec == 'A');
  230. wnumstr = _itowa_word (num0, wnumstr, 16, info->spec == 'A');
  231. }
  232. else
  233. {
  234. numstr = _itoa (num0, numstr, 16, info->spec == 'A');
  235. wnumstr = _itowa (num0, wnumstr, 16, info->spec == 'A');
  236. }
  237. /* Fill with zeroes. */
  238. while (numstr > numbuf + (sizeof numbuf - 112 / 4))
  239. {
  240. *--wnumstr = L_('0');
  241. *--numstr = '0';
  242. }
  243. leading = fpnum.ieee.exponent == 0 ? '0' : '1';
  244. exponent = fpnum.ieee.exponent;
  245. if (exponent == 0)
  246. {
  247. if (zero_mantissa)
  248. expnegative = 0;
  249. else
  250. {
  251. /* This is a denormalized number. */
  252. expnegative = 1;
  253. exponent = IEEE854_FLOAT128_BIAS - 1;
  254. }
  255. }
  256. else if (exponent >= IEEE854_FLOAT128_BIAS)
  257. {
  258. expnegative = 0;
  259. exponent -= IEEE854_FLOAT128_BIAS;
  260. }
  261. else
  262. {
  263. expnegative = 1;
  264. exponent = -(exponent - IEEE854_FLOAT128_BIAS);
  265. }
  266. }
  267. /* Look for trailing zeroes. */
  268. if (! zero_mantissa)
  269. {
  270. wnumend = &wnumbuf[sizeof wnumbuf / sizeof wnumbuf[0]];
  271. numend = &numbuf[sizeof numbuf / sizeof numbuf[0]];
  272. while (wnumend[-1] == L_('0'))
  273. {
  274. --wnumend;
  275. --numend;
  276. }
  277. do_round_away = false;
  278. if (precision != -1 && precision < numend - numstr)
  279. {
  280. char last_digit = precision > 0 ? numstr[precision - 1] : leading;
  281. char next_digit = numstr[precision];
  282. int last_digit_value = (last_digit >= 'A' && last_digit <= 'F'
  283. ? last_digit - 'A' + 10
  284. : (last_digit >= 'a' && last_digit <= 'f'
  285. ? last_digit - 'a' + 10
  286. : last_digit - '0'));
  287. int next_digit_value = (next_digit >= 'A' && next_digit <= 'F'
  288. ? next_digit - 'A' + 10
  289. : (next_digit >= 'a' && next_digit <= 'f'
  290. ? next_digit - 'a' + 10
  291. : next_digit - '0'));
  292. bool more_bits = ((next_digit_value & 7) != 0
  293. || precision + 1 < numend - numstr);
  294. #ifdef HAVE_FENV_H
  295. int rounding_mode = get_rounding_mode ();
  296. do_round_away = round_away (negative, last_digit_value & 1,
  297. next_digit_value >= 8, more_bits,
  298. rounding_mode);
  299. #endif
  300. }
  301. if (precision == -1)
  302. precision = numend - numstr;
  303. else if (do_round_away)
  304. {
  305. /* Round up. */
  306. int cnt = precision;
  307. while (--cnt >= 0)
  308. {
  309. char ch = numstr[cnt];
  310. /* We assume that the digits and the letters are ordered
  311. like in ASCII. This is true for the rest of GNU, too. */
  312. if (ch == '9')
  313. {
  314. wnumstr[cnt] = (wchar_t) info->spec;
  315. numstr[cnt] = info->spec; /* This is tricky,
  316. think about it! */
  317. break;
  318. }
  319. else if (tolower (ch) < 'f')
  320. {
  321. ++numstr[cnt];
  322. ++wnumstr[cnt];
  323. break;
  324. }
  325. else
  326. {
  327. numstr[cnt] = '0';
  328. wnumstr[cnt] = L_('0');
  329. }
  330. }
  331. if (cnt < 0)
  332. {
  333. /* The mantissa so far was fff...f Now increment the
  334. leading digit. Here it is again possible that we
  335. get an overflow. */
  336. if (leading == '9')
  337. leading = info->spec;
  338. else if (tolower (leading) < 'f')
  339. ++leading;
  340. else
  341. {
  342. leading = '1';
  343. if (expnegative)
  344. {
  345. exponent -= 4;
  346. if (exponent <= 0)
  347. {
  348. exponent = -exponent;
  349. expnegative = 0;
  350. }
  351. }
  352. else
  353. exponent += 4;
  354. }
  355. }
  356. }
  357. }
  358. else
  359. {
  360. if (precision == -1)
  361. precision = 0;
  362. numend = numstr;
  363. wnumend = wnumstr;
  364. }
  365. /* Now we can compute the exponent string. */
  366. expstr = _itoa_word (exponent, expbuf + sizeof expbuf, 10, 0);
  367. wexpstr = _itowa_word (exponent,
  368. wexpbuf + sizeof wexpbuf / sizeof (wchar_t), 10, 0);
  369. /* Now we have all information to compute the size. */
  370. width -= ((negative || info->showsign || info->space)
  371. /* Sign. */
  372. + 2 + 1 + 0 + precision + 1 + 1
  373. /* 0x h . hhh P ExpoSign. */
  374. + ((expbuf + sizeof expbuf) - expstr));
  375. /* Exponent. */
  376. /* Count the decimal point.
  377. A special case when the mantissa or the precision is zero and the `#'
  378. is not given. In this case we must not print the decimal point. */
  379. if (precision > 0 || info->alt)
  380. width -= wide ? 1 : strlen (decimal);
  381. if (!info->left && info->pad != '0' && width > 0)
  382. PADN (' ', width);
  383. if (negative)
  384. outchar ('-');
  385. else if (info->showsign)
  386. outchar ('+');
  387. else if (info->space)
  388. outchar (' ');
  389. outchar ('0');
  390. if ('X' - 'A' == 'x' - 'a')
  391. outchar (info->spec + ('x' - 'a'));
  392. else
  393. outchar (info->spec == 'A' ? 'X' : 'x');
  394. if (!info->left && info->pad == '0' && width > 0)
  395. PADN ('0', width);
  396. outchar (leading);
  397. if (precision > 0 || info->alt)
  398. {
  399. const wchar_t *wtmp = &decimalwc;
  400. PRINT (decimal, wtmp, wide ? 1 : strlen (decimal));
  401. }
  402. if (precision > 0)
  403. {
  404. ssize_t tofill = precision - (numend - numstr);
  405. PRINT (numstr, wnumstr, MIN (numend - numstr, precision));
  406. if (tofill > 0)
  407. PADN ('0', tofill);
  408. }
  409. if ('P' - 'A' == 'p' - 'a')
  410. outchar (info->spec + ('p' - 'a'));
  411. else
  412. outchar (info->spec == 'A' ? 'P' : 'p');
  413. outchar (expnegative ? '-' : '+');
  414. PRINT (expstr, wexpstr, (expbuf + sizeof expbuf) - expstr);
  415. if (info->left && info->pad != '0' && width > 0)
  416. PADN (info->pad, width);
  417. return done;
  418. }