printf_fphex.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 = fpnum.ieee.mant_high;
  202. num1 = fpnum.ieee.mant_low;
  203. zero_mantissa = (num0|num1) == 0;
  204. if (sizeof (unsigned long int) > 6)
  205. {
  206. numstr = _itoa_word (num1, numbuf + sizeof numbuf, 16,
  207. info->spec == 'A');
  208. wnumstr = _itowa_word (num1,
  209. wnumbuf + sizeof (wnumbuf) / sizeof (wchar_t),
  210. 16, info->spec == 'A');
  211. }
  212. else
  213. {
  214. numstr = _itoa (num1, numbuf + sizeof numbuf, 16,
  215. info->spec == 'A');
  216. wnumstr = _itowa (num1,
  217. wnumbuf + sizeof (wnumbuf) / sizeof (wchar_t),
  218. 16, info->spec == 'A');
  219. }
  220. while (numstr > numbuf + (sizeof numbuf - 64 / 4))
  221. {
  222. *--numstr = '0';
  223. *--wnumstr = L_('0');
  224. }
  225. if (sizeof (unsigned long int) > 6)
  226. {
  227. numstr = _itoa_word (num0, numstr, 16, info->spec == 'A');
  228. wnumstr = _itowa_word (num0, wnumstr, 16, info->spec == 'A');
  229. }
  230. else
  231. {
  232. numstr = _itoa (num0, numstr, 16, info->spec == 'A');
  233. wnumstr = _itowa (num0, wnumstr, 16, info->spec == 'A');
  234. }
  235. /* Fill with zeroes. */
  236. while (numstr > numbuf + (sizeof numbuf - 112 / 4))
  237. {
  238. *--wnumstr = L_('0');
  239. *--numstr = '0';
  240. }
  241. leading = fpnum.ieee.exponent == 0 ? '0' : '1';
  242. exponent = fpnum.ieee.exponent;
  243. if (exponent == 0)
  244. {
  245. if (zero_mantissa)
  246. expnegative = 0;
  247. else
  248. {
  249. /* This is a denormalized number. */
  250. expnegative = 1;
  251. exponent = IEEE854_FLOAT128_BIAS - 1;
  252. }
  253. }
  254. else if (exponent >= IEEE854_FLOAT128_BIAS)
  255. {
  256. expnegative = 0;
  257. exponent -= IEEE854_FLOAT128_BIAS;
  258. }
  259. else
  260. {
  261. expnegative = 1;
  262. exponent = -(exponent - IEEE854_FLOAT128_BIAS);
  263. }
  264. }
  265. /* Look for trailing zeroes. */
  266. if (! zero_mantissa)
  267. {
  268. wnumend = &wnumbuf[sizeof wnumbuf / sizeof wnumbuf[0]];
  269. numend = &numbuf[sizeof numbuf / sizeof numbuf[0]];
  270. while (wnumend[-1] == L_('0'))
  271. {
  272. --wnumend;
  273. --numend;
  274. }
  275. do_round_away = false;
  276. if (precision != -1 && precision < numend - numstr)
  277. {
  278. char last_digit = precision > 0 ? numstr[precision - 1] : leading;
  279. char next_digit = numstr[precision];
  280. int last_digit_value = (last_digit >= 'A' && last_digit <= 'F'
  281. ? last_digit - 'A' + 10
  282. : (last_digit >= 'a' && last_digit <= 'f'
  283. ? last_digit - 'a' + 10
  284. : last_digit - '0'));
  285. int next_digit_value = (next_digit >= 'A' && next_digit <= 'F'
  286. ? next_digit - 'A' + 10
  287. : (next_digit >= 'a' && next_digit <= 'f'
  288. ? next_digit - 'a' + 10
  289. : next_digit - '0'));
  290. bool more_bits = ((next_digit_value & 7) != 0
  291. || precision + 1 < numend - numstr);
  292. #ifdef HAVE_FENV_H
  293. int rounding_mode = get_rounding_mode ();
  294. do_round_away = round_away (negative, last_digit_value & 1,
  295. next_digit_value >= 8, more_bits,
  296. rounding_mode);
  297. #endif
  298. }
  299. if (precision == -1)
  300. precision = numend - numstr;
  301. else if (do_round_away)
  302. {
  303. /* Round up. */
  304. int cnt = precision;
  305. while (--cnt >= 0)
  306. {
  307. char ch = numstr[cnt];
  308. /* We assume that the digits and the letters are ordered
  309. like in ASCII. This is true for the rest of GNU, too. */
  310. if (ch == '9')
  311. {
  312. wnumstr[cnt] = (wchar_t) info->spec;
  313. numstr[cnt] = info->spec; /* This is tricky,
  314. think about it! */
  315. break;
  316. }
  317. else if (tolower (ch) < 'f')
  318. {
  319. ++numstr[cnt];
  320. ++wnumstr[cnt];
  321. break;
  322. }
  323. else
  324. {
  325. numstr[cnt] = '0';
  326. wnumstr[cnt] = L_('0');
  327. }
  328. }
  329. if (cnt < 0)
  330. {
  331. /* The mantissa so far was fff...f Now increment the
  332. leading digit. Here it is again possible that we
  333. get an overflow. */
  334. if (leading == '9')
  335. leading = info->spec;
  336. else if (tolower (leading) < 'f')
  337. ++leading;
  338. else
  339. {
  340. leading = '1';
  341. if (expnegative)
  342. {
  343. exponent -= 4;
  344. if (exponent <= 0)
  345. {
  346. exponent = -exponent;
  347. expnegative = 0;
  348. }
  349. }
  350. else
  351. exponent += 4;
  352. }
  353. }
  354. }
  355. }
  356. else
  357. {
  358. if (precision == -1)
  359. precision = 0;
  360. numend = numstr;
  361. wnumend = wnumstr;
  362. }
  363. /* Now we can compute the exponent string. */
  364. expstr = _itoa_word (exponent, expbuf + sizeof expbuf, 10, 0);
  365. wexpstr = _itowa_word (exponent,
  366. wexpbuf + sizeof wexpbuf / sizeof (wchar_t), 10, 0);
  367. /* Now we have all information to compute the size. */
  368. width -= ((negative || info->showsign || info->space)
  369. /* Sign. */
  370. + 2 + 1 + 0 + precision + 1 + 1
  371. /* 0x h . hhh P ExpoSign. */
  372. + ((expbuf + sizeof expbuf) - expstr));
  373. /* Exponent. */
  374. /* Count the decimal point.
  375. A special case when the mantissa or the precision is zero and the `#'
  376. is not given. In this case we must not print the decimal point. */
  377. if (precision > 0 || info->alt)
  378. width -= wide ? 1 : strlen (decimal);
  379. if (!info->left && info->pad != '0' && width > 0)
  380. PADN (' ', width);
  381. if (negative)
  382. outchar ('-');
  383. else if (info->showsign)
  384. outchar ('+');
  385. else if (info->space)
  386. outchar (' ');
  387. outchar ('0');
  388. if ('X' - 'A' == 'x' - 'a')
  389. outchar (info->spec + ('x' - 'a'));
  390. else
  391. outchar (info->spec == 'A' ? 'X' : 'x');
  392. if (!info->left && info->pad == '0' && width > 0)
  393. PADN ('0', width);
  394. outchar (leading);
  395. if (precision > 0 || info->alt)
  396. {
  397. const wchar_t *wtmp = &decimalwc;
  398. PRINT (decimal, wtmp, wide ? 1 : strlen (decimal));
  399. }
  400. if (precision > 0)
  401. {
  402. ssize_t tofill = precision - (numend - numstr);
  403. PRINT (numstr, wnumstr, MIN (numend - numstr, precision));
  404. if (tofill > 0)
  405. PADN ('0', tofill);
  406. }
  407. if ('P' - 'A' == 'p' - 'a')
  408. outchar (info->spec + ('p' - 'a'));
  409. else
  410. outchar (info->spec == 'A' ? 'P' : 'p');
  411. outchar (expnegative ? '-' : '+');
  412. PRINT (expstr, wexpstr, (expbuf + sizeof expbuf) - expstr);
  413. if (info->left && info->pad != '0' && width > 0)
  414. PADN (info->pad, width);
  415. return done;
  416. }