printf_fp.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  1. /* Floating point output for `printf'.
  2. Copyright (C) 1995-2012 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
  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 <float.h>
  18. #include <limits.h>
  19. #include <math.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <stdlib.h>
  23. #include <stdbool.h>
  24. #define NDEBUG
  25. #include <assert.h>
  26. #ifdef HAVE_ERRNO_H
  27. #include <errno.h>
  28. #endif
  29. #include <stdio.h>
  30. #include <stdarg.h>
  31. #ifdef HAVE_FENV_H
  32. #include "quadmath-rounding-mode.h"
  33. #endif
  34. #include "quadmath-printf.h"
  35. #include "fpioconst.h"
  36. #ifdef USE_I18N_NUMBER_H
  37. #include "_i18n_number.h"
  38. #endif
  39. /* Macros for doing the actual output. */
  40. #define outchar(ch) \
  41. do \
  42. { \
  43. register const int outc = (ch); \
  44. if (PUTC (outc, fp) == EOF) \
  45. { \
  46. if (buffer_malloced) \
  47. free (wbuffer); \
  48. return -1; \
  49. } \
  50. ++done; \
  51. } while (0)
  52. #define PRINT(ptr, wptr, len) \
  53. do \
  54. { \
  55. register size_t outlen = (len); \
  56. if (len > 20) \
  57. { \
  58. if (PUT (fp, wide ? (const char *) wptr : ptr, outlen) != outlen) \
  59. { \
  60. if (buffer_malloced) \
  61. free (wbuffer); \
  62. return -1; \
  63. } \
  64. ptr += outlen; \
  65. done += outlen; \
  66. } \
  67. else \
  68. { \
  69. if (wide) \
  70. while (outlen-- > 0) \
  71. outchar (*wptr++); \
  72. else \
  73. while (outlen-- > 0) \
  74. outchar (*ptr++); \
  75. } \
  76. } while (0)
  77. #define PADN(ch, len) \
  78. do \
  79. { \
  80. if (PAD (fp, ch, len) != len) \
  81. { \
  82. if (buffer_malloced) \
  83. free (wbuffer); \
  84. return -1; \
  85. } \
  86. done += len; \
  87. } \
  88. while (0)
  89. /* We use the GNU MP library to handle large numbers.
  90. An MP variable occupies a varying number of entries in its array. We keep
  91. track of this number for efficiency reasons. Otherwise we would always
  92. have to process the whole array. */
  93. #define MPN_VAR(name) mp_limb_t *name; mp_size_t name##size
  94. #define MPN_ASSIGN(dst,src) \
  95. memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t))
  96. #define MPN_GE(u,v) \
  97. (u##size > v##size || (u##size == v##size && mpn_cmp (u, v, u##size) >= 0))
  98. extern mp_size_t mpn_extract_flt128 (mp_ptr res_ptr, mp_size_t size,
  99. int *expt, int *is_neg,
  100. __float128 value) attribute_hidden;
  101. static unsigned int guess_grouping (unsigned int intdig_max,
  102. const char *grouping);
  103. static wchar_t *group_number (wchar_t *buf, wchar_t *bufend,
  104. unsigned int intdig_no, const char *grouping,
  105. wchar_t thousands_sep, int ngroups);
  106. int
  107. __quadmath_printf_fp (struct __quadmath_printf_file *fp,
  108. const struct printf_info *info,
  109. const void *const *args)
  110. {
  111. /* The floating-point value to output. */
  112. __float128 fpnum;
  113. /* Locale-dependent representation of decimal point. */
  114. const char *decimal;
  115. wchar_t decimalwc;
  116. /* Locale-dependent thousands separator and grouping specification. */
  117. const char *thousands_sep = NULL;
  118. wchar_t thousands_sepwc = L_('\0');
  119. const char *grouping;
  120. /* "NaN" or "Inf" for the special cases. */
  121. const char *special = NULL;
  122. const wchar_t *wspecial = NULL;
  123. /* We need just a few limbs for the input before shifting to the right
  124. position. */
  125. mp_limb_t fp_input[(FLT128_MANT_DIG + BITS_PER_MP_LIMB - 1) / BITS_PER_MP_LIMB];
  126. /* We need to shift the contents of fp_input by this amount of bits. */
  127. int to_shift = 0;
  128. /* The fraction of the floting-point value in question */
  129. MPN_VAR(frac);
  130. /* and the exponent. */
  131. int exponent;
  132. /* Sign of the exponent. */
  133. int expsign = 0;
  134. /* Sign of float number. */
  135. int is_neg = 0;
  136. /* Scaling factor. */
  137. MPN_VAR(scale);
  138. /* Temporary bignum value. */
  139. MPN_VAR(tmp);
  140. /* Digit which is result of last hack_digit() call. */
  141. wchar_t last_digit, next_digit;
  142. bool more_bits;
  143. /* The type of output format that will be used: 'e'/'E' or 'f'. */
  144. int type;
  145. /* Counter for number of written characters. */
  146. int done = 0;
  147. /* General helper (carry limb). */
  148. mp_limb_t cy;
  149. /* Nonzero if this is output on a wide character stream. */
  150. int wide = info->wide;
  151. /* Buffer in which we produce the output. */
  152. wchar_t *wbuffer = NULL;
  153. /* Flag whether wbuffer is malloc'ed or not. */
  154. int buffer_malloced = 0;
  155. auto wchar_t hack_digit (void);
  156. wchar_t hack_digit (void)
  157. {
  158. mp_limb_t hi;
  159. if (expsign != 0 && type == 'f' && exponent-- > 0)
  160. hi = 0;
  161. else if (scalesize == 0)
  162. {
  163. hi = frac[fracsize - 1];
  164. frac[fracsize - 1] = mpn_mul_1 (frac, frac, fracsize - 1, 10);
  165. }
  166. else
  167. {
  168. if (fracsize < scalesize)
  169. hi = 0;
  170. else
  171. {
  172. hi = mpn_divmod (tmp, frac, fracsize, scale, scalesize);
  173. tmp[fracsize - scalesize] = hi;
  174. hi = tmp[0];
  175. fracsize = scalesize;
  176. while (fracsize != 0 && frac[fracsize - 1] == 0)
  177. --fracsize;
  178. if (fracsize == 0)
  179. {
  180. /* We're not prepared for an mpn variable with zero
  181. limbs. */
  182. fracsize = 1;
  183. return L_('0') + hi;
  184. }
  185. }
  186. mp_limb_t _cy = mpn_mul_1 (frac, frac, fracsize, 10);
  187. if (_cy != 0)
  188. frac[fracsize++] = _cy;
  189. }
  190. return L_('0') + hi;
  191. }
  192. /* Figure out the decimal point character. */
  193. #ifdef USE_NL_LANGINFO
  194. if (info->extra == 0)
  195. decimal = nl_langinfo (DECIMAL_POINT);
  196. else
  197. {
  198. decimal = nl_langinfo (MON_DECIMAL_POINT);
  199. if (*decimal == '\0')
  200. decimal = nl_langinfo (DECIMAL_POINT);
  201. }
  202. /* The decimal point character must never be zero. */
  203. assert (*decimal != '\0');
  204. #elif defined USE_LOCALECONV
  205. const struct lconv *lc = localeconv ();
  206. if (info->extra == 0)
  207. decimal = lc->decimal_point;
  208. else
  209. {
  210. decimal = lc->mon_decimal_point;
  211. if (decimal == NULL || *decimal == '\0')
  212. decimal = lc->decimal_point;
  213. }
  214. if (decimal == NULL || *decimal == '\0')
  215. decimal = ".";
  216. #else
  217. decimal = ".";
  218. #endif
  219. #ifdef USE_NL_LANGINFO_WC
  220. if (info->extra == 0)
  221. decimalwc = nl_langinfo_wc (_NL_NUMERIC_DECIMAL_POINT_WC);
  222. else
  223. {
  224. decimalwc = nl_langinfo_wc (_NL_MONETARY_DECIMAL_POINT_WC);
  225. if (decimalwc == L_('\0'))
  226. decimalwc = nl_langinfo_wc (_NL_NUMERIC_DECIMAL_POINT_WC);
  227. }
  228. /* The decimal point character must never be zero. */
  229. assert (decimalwc != L_('\0'));
  230. #else
  231. decimalwc = L_('.');
  232. #endif
  233. #if defined USE_NL_LANGINFO && defined USE_NL_LANGINFO_WC
  234. if (info->group)
  235. {
  236. if (info->extra == 0)
  237. grouping = nl_langinfo (GROUPING);
  238. else
  239. grouping = nl_langinfo (MON_GROUPING);
  240. if (*grouping <= 0 || *grouping == CHAR_MAX)
  241. grouping = NULL;
  242. else
  243. {
  244. /* Figure out the thousands separator character. */
  245. if (wide)
  246. {
  247. if (info->extra == 0)
  248. thousands_sepwc = nl_langinfo_wc (_NL_NUMERIC_THOUSANDS_SEP_WC);
  249. else
  250. thousands_sepwc = nl_langinfo_wc (_NL_MONETARY_THOUSANDS_SEP_WC);
  251. if (thousands_sepwc == L_('\0'))
  252. grouping = NULL;
  253. }
  254. else
  255. {
  256. if (info->extra == 0)
  257. thousands_sep = nl_langinfo (THOUSANDS_SEP);
  258. else
  259. thousands_sep = nl_langinfo (MON_THOUSANDS_SEP);
  260. if (*thousands_sep == '\0')
  261. grouping = NULL;
  262. }
  263. }
  264. }
  265. else
  266. #elif defined USE_NL_LANGINFO
  267. if (info->group && !wide)
  268. {
  269. if (info->extra == 0)
  270. grouping = nl_langinfo (GROUPING);
  271. else
  272. grouping = nl_langinfo (MON_GROUPING);
  273. if (*grouping <= 0 || *grouping == CHAR_MAX)
  274. grouping = NULL;
  275. else
  276. {
  277. /* Figure out the thousands separator character. */
  278. if (info->extra == 0)
  279. thousands_sep = nl_langinfo (THOUSANDS_SEP);
  280. else
  281. thousands_sep = nl_langinfo (MON_THOUSANDS_SEP);
  282. if (*thousands_sep == '\0')
  283. grouping = NULL;
  284. }
  285. }
  286. else
  287. #elif defined USE_LOCALECONV
  288. if (info->group && !wide)
  289. {
  290. if (info->extra == 0)
  291. grouping = lc->grouping;
  292. else
  293. grouping = lc->mon_grouping;
  294. if (grouping == NULL || *grouping <= 0 || *grouping == CHAR_MAX)
  295. grouping = NULL;
  296. else
  297. {
  298. /* Figure out the thousands separator character. */
  299. if (info->extra == 0)
  300. thousands_sep = lc->thousands_sep;
  301. else
  302. thousands_sep = lc->mon_thousands_sep;
  303. if (thousands_sep == NULL || *thousands_sep == '\0')
  304. grouping = NULL;
  305. }
  306. }
  307. else
  308. #endif
  309. grouping = NULL;
  310. if (grouping != NULL && !wide)
  311. /* If we are printing multibyte characters and there is a
  312. multibyte representation for the thousands separator,
  313. we must ensure the wide character thousands separator
  314. is available, even if it is fake. */
  315. thousands_sepwc = (wchar_t) 0xfffffffe;
  316. /* Fetch the argument value. */
  317. {
  318. fpnum = **(const __float128 **) args[0];
  319. /* Check for special values: not a number or infinity. */
  320. if (isnanq (fpnum))
  321. {
  322. ieee854_float128 u = { .value = fpnum };
  323. is_neg = u.ieee.negative != 0;
  324. if (isupper (info->spec))
  325. {
  326. special = "NAN";
  327. wspecial = L_("NAN");
  328. }
  329. else
  330. {
  331. special = "nan";
  332. wspecial = L_("nan");
  333. }
  334. }
  335. else if (isinfq (fpnum))
  336. {
  337. is_neg = fpnum < 0;
  338. if (isupper (info->spec))
  339. {
  340. special = "INF";
  341. wspecial = L_("INF");
  342. }
  343. else
  344. {
  345. special = "inf";
  346. wspecial = L_("inf");
  347. }
  348. }
  349. else
  350. {
  351. fracsize = mpn_extract_flt128 (fp_input,
  352. (sizeof (fp_input) /
  353. sizeof (fp_input[0])),
  354. &exponent, &is_neg, fpnum);
  355. to_shift = 1 + fracsize * BITS_PER_MP_LIMB - FLT128_MANT_DIG;
  356. }
  357. }
  358. if (special)
  359. {
  360. int width = info->width;
  361. if (is_neg || info->showsign || info->space)
  362. --width;
  363. width -= 3;
  364. if (!info->left && width > 0)
  365. PADN (' ', width);
  366. if (is_neg)
  367. outchar ('-');
  368. else if (info->showsign)
  369. outchar ('+');
  370. else if (info->space)
  371. outchar (' ');
  372. PRINT (special, wspecial, 3);
  373. if (info->left && width > 0)
  374. PADN (' ', width);
  375. return done;
  376. }
  377. /* We need three multiprecision variables. Now that we have the exponent
  378. of the number we can allocate the needed memory. It would be more
  379. efficient to use variables of the fixed maximum size but because this
  380. would be really big it could lead to memory problems. */
  381. {
  382. mp_size_t bignum_size = ((ABS (exponent) + BITS_PER_MP_LIMB - 1)
  383. / BITS_PER_MP_LIMB
  384. + (FLT128_MANT_DIG / BITS_PER_MP_LIMB > 2 ? 8 : 4))
  385. * sizeof (mp_limb_t);
  386. frac = (mp_limb_t *) alloca (bignum_size);
  387. tmp = (mp_limb_t *) alloca (bignum_size);
  388. scale = (mp_limb_t *) alloca (bignum_size);
  389. }
  390. /* We now have to distinguish between numbers with positive and negative
  391. exponents because the method used for the one is not applicable/efficient
  392. for the other. */
  393. scalesize = 0;
  394. if (exponent > 2)
  395. {
  396. /* |FP| >= 8.0. */
  397. int scaleexpo = 0;
  398. int explog = FLT128_MAX_10_EXP_LOG;
  399. int exp10 = 0;
  400. const struct mp_power *powers = &_fpioconst_pow10[explog + 1];
  401. int cnt_h, cnt_l, i;
  402. if ((exponent + to_shift) % BITS_PER_MP_LIMB == 0)
  403. {
  404. MPN_COPY_DECR (frac + (exponent + to_shift) / BITS_PER_MP_LIMB,
  405. fp_input, fracsize);
  406. fracsize += (exponent + to_shift) / BITS_PER_MP_LIMB;
  407. }
  408. else
  409. {
  410. cy = mpn_lshift (frac + (exponent + to_shift) / BITS_PER_MP_LIMB,
  411. fp_input, fracsize,
  412. (exponent + to_shift) % BITS_PER_MP_LIMB);
  413. fracsize += (exponent + to_shift) / BITS_PER_MP_LIMB;
  414. if (cy)
  415. frac[fracsize++] = cy;
  416. }
  417. MPN_ZERO (frac, (exponent + to_shift) / BITS_PER_MP_LIMB);
  418. assert (powers > &_fpioconst_pow10[0]);
  419. do
  420. {
  421. --powers;
  422. /* The number of the product of two binary numbers with n and m
  423. bits respectively has m+n or m+n-1 bits. */
  424. if (exponent >= scaleexpo + powers->p_expo - 1)
  425. {
  426. if (scalesize == 0)
  427. {
  428. if (FLT128_MANT_DIG > _FPIO_CONST_OFFSET * BITS_PER_MP_LIMB)
  429. {
  430. #define _FPIO_CONST_SHIFT \
  431. (((FLT128_MANT_DIG + BITS_PER_MP_LIMB - 1) / BITS_PER_MP_LIMB) \
  432. - _FPIO_CONST_OFFSET)
  433. /* 64bit const offset is not enough for
  434. IEEE quad long double. */
  435. tmpsize = powers->arraysize + _FPIO_CONST_SHIFT;
  436. memcpy (tmp + _FPIO_CONST_SHIFT,
  437. &__tens[powers->arrayoff],
  438. tmpsize * sizeof (mp_limb_t));
  439. MPN_ZERO (tmp, _FPIO_CONST_SHIFT);
  440. /* Adjust exponent, as scaleexpo will be this much
  441. bigger too. */
  442. exponent += _FPIO_CONST_SHIFT * BITS_PER_MP_LIMB;
  443. }
  444. else
  445. {
  446. tmpsize = powers->arraysize;
  447. memcpy (tmp, &__tens[powers->arrayoff],
  448. tmpsize * sizeof (mp_limb_t));
  449. }
  450. }
  451. else
  452. {
  453. cy = mpn_mul (tmp, scale, scalesize,
  454. &__tens[powers->arrayoff
  455. + _FPIO_CONST_OFFSET],
  456. powers->arraysize - _FPIO_CONST_OFFSET);
  457. tmpsize = scalesize + powers->arraysize - _FPIO_CONST_OFFSET;
  458. if (cy == 0)
  459. --tmpsize;
  460. }
  461. if (MPN_GE (frac, tmp))
  462. {
  463. int cnt;
  464. MPN_ASSIGN (scale, tmp);
  465. count_leading_zeros (cnt, scale[scalesize - 1]);
  466. scaleexpo = (scalesize - 2) * BITS_PER_MP_LIMB - cnt - 1;
  467. exp10 |= 1 << explog;
  468. }
  469. }
  470. --explog;
  471. }
  472. while (powers > &_fpioconst_pow10[0]);
  473. exponent = exp10;
  474. /* Optimize number representations. We want to represent the numbers
  475. with the lowest number of bytes possible without losing any
  476. bytes. Also the highest bit in the scaling factor has to be set
  477. (this is a requirement of the MPN division routines). */
  478. if (scalesize > 0)
  479. {
  480. /* Determine minimum number of zero bits at the end of
  481. both numbers. */
  482. for (i = 0; scale[i] == 0 && frac[i] == 0; i++)
  483. ;
  484. /* Determine number of bits the scaling factor is misplaced. */
  485. count_leading_zeros (cnt_h, scale[scalesize - 1]);
  486. if (cnt_h == 0)
  487. {
  488. /* The highest bit of the scaling factor is already set. So
  489. we only have to remove the trailing empty limbs. */
  490. if (i > 0)
  491. {
  492. MPN_COPY_INCR (scale, scale + i, scalesize - i);
  493. scalesize -= i;
  494. MPN_COPY_INCR (frac, frac + i, fracsize - i);
  495. fracsize -= i;
  496. }
  497. }
  498. else
  499. {
  500. if (scale[i] != 0)
  501. {
  502. count_trailing_zeros (cnt_l, scale[i]);
  503. if (frac[i] != 0)
  504. {
  505. int cnt_l2;
  506. count_trailing_zeros (cnt_l2, frac[i]);
  507. if (cnt_l2 < cnt_l)
  508. cnt_l = cnt_l2;
  509. }
  510. }
  511. else
  512. count_trailing_zeros (cnt_l, frac[i]);
  513. /* Now shift the numbers to their optimal position. */
  514. if (i == 0 && BITS_PER_MP_LIMB - cnt_h > cnt_l)
  515. {
  516. /* We cannot save any memory. So just roll both numbers
  517. so that the scaling factor has its highest bit set. */
  518. (void) mpn_lshift (scale, scale, scalesize, cnt_h);
  519. cy = mpn_lshift (frac, frac, fracsize, cnt_h);
  520. if (cy != 0)
  521. frac[fracsize++] = cy;
  522. }
  523. else if (BITS_PER_MP_LIMB - cnt_h <= cnt_l)
  524. {
  525. /* We can save memory by removing the trailing zero limbs
  526. and by packing the non-zero limbs which gain another
  527. free one. */
  528. (void) mpn_rshift (scale, scale + i, scalesize - i,
  529. BITS_PER_MP_LIMB - cnt_h);
  530. scalesize -= i + 1;
  531. (void) mpn_rshift (frac, frac + i, fracsize - i,
  532. BITS_PER_MP_LIMB - cnt_h);
  533. fracsize -= frac[fracsize - i - 1] == 0 ? i + 1 : i;
  534. }
  535. else
  536. {
  537. /* We can only save the memory of the limbs which are zero.
  538. The non-zero parts occupy the same number of limbs. */
  539. (void) mpn_rshift (scale, scale + (i - 1),
  540. scalesize - (i - 1),
  541. BITS_PER_MP_LIMB - cnt_h);
  542. scalesize -= i;
  543. (void) mpn_rshift (frac, frac + (i - 1),
  544. fracsize - (i - 1),
  545. BITS_PER_MP_LIMB - cnt_h);
  546. fracsize -= frac[fracsize - (i - 1) - 1] == 0 ? i : i - 1;
  547. }
  548. }
  549. }
  550. }
  551. else if (exponent < 0)
  552. {
  553. /* |FP| < 1.0. */
  554. int exp10 = 0;
  555. int explog = FLT128_MAX_10_EXP_LOG;
  556. const struct mp_power *powers = &_fpioconst_pow10[explog + 1];
  557. /* Now shift the input value to its right place. */
  558. cy = mpn_lshift (frac, fp_input, fracsize, to_shift);
  559. frac[fracsize++] = cy;
  560. assert (cy == 1 || (frac[fracsize - 2] == 0 && frac[0] == 0));
  561. expsign = 1;
  562. exponent = -exponent;
  563. assert (powers != &_fpioconst_pow10[0]);
  564. do
  565. {
  566. --powers;
  567. if (exponent >= powers->m_expo)
  568. {
  569. int i, incr, cnt_h, cnt_l;
  570. mp_limb_t topval[2];
  571. /* The mpn_mul function expects the first argument to be
  572. bigger than the second. */
  573. if (fracsize < powers->arraysize - _FPIO_CONST_OFFSET)
  574. cy = mpn_mul (tmp, &__tens[powers->arrayoff
  575. + _FPIO_CONST_OFFSET],
  576. powers->arraysize - _FPIO_CONST_OFFSET,
  577. frac, fracsize);
  578. else
  579. cy = mpn_mul (tmp, frac, fracsize,
  580. &__tens[powers->arrayoff + _FPIO_CONST_OFFSET],
  581. powers->arraysize - _FPIO_CONST_OFFSET);
  582. tmpsize = fracsize + powers->arraysize - _FPIO_CONST_OFFSET;
  583. if (cy == 0)
  584. --tmpsize;
  585. count_leading_zeros (cnt_h, tmp[tmpsize - 1]);
  586. incr = (tmpsize - fracsize) * BITS_PER_MP_LIMB
  587. + BITS_PER_MP_LIMB - 1 - cnt_h;
  588. assert (incr <= powers->p_expo);
  589. /* If we increased the exponent by exactly 3 we have to test
  590. for overflow. This is done by comparing with 10 shifted
  591. to the right position. */
  592. if (incr == exponent + 3)
  593. {
  594. if (cnt_h <= BITS_PER_MP_LIMB - 4)
  595. {
  596. topval[0] = 0;
  597. topval[1]
  598. = ((mp_limb_t) 10) << (BITS_PER_MP_LIMB - 4 - cnt_h);
  599. }
  600. else
  601. {
  602. topval[0] = ((mp_limb_t) 10) << (BITS_PER_MP_LIMB - 4);
  603. topval[1] = 0;
  604. (void) mpn_lshift (topval, topval, 2,
  605. BITS_PER_MP_LIMB - cnt_h);
  606. }
  607. }
  608. /* We have to be careful when multiplying the last factor.
  609. If the result is greater than 1.0 be have to test it
  610. against 10.0. If it is greater or equal to 10.0 the
  611. multiplication was not valid. This is because we cannot
  612. determine the number of bits in the result in advance. */
  613. if (incr < exponent + 3
  614. || (incr == exponent + 3 &&
  615. (tmp[tmpsize - 1] < topval[1]
  616. || (tmp[tmpsize - 1] == topval[1]
  617. && tmp[tmpsize - 2] < topval[0]))))
  618. {
  619. /* The factor is right. Adapt binary and decimal
  620. exponents. */
  621. exponent -= incr;
  622. exp10 |= 1 << explog;
  623. /* If this factor yields a number greater or equal to
  624. 1.0, we must not shift the non-fractional digits down. */
  625. if (exponent < 0)
  626. cnt_h += -exponent;
  627. /* Now we optimize the number representation. */
  628. for (i = 0; tmp[i] == 0; ++i);
  629. if (cnt_h == BITS_PER_MP_LIMB - 1)
  630. {
  631. MPN_COPY (frac, tmp + i, tmpsize - i);
  632. fracsize = tmpsize - i;
  633. }
  634. else
  635. {
  636. count_trailing_zeros (cnt_l, tmp[i]);
  637. /* Now shift the numbers to their optimal position. */
  638. if (i == 0 && BITS_PER_MP_LIMB - 1 - cnt_h > cnt_l)
  639. {
  640. /* We cannot save any memory. Just roll the
  641. number so that the leading digit is in a
  642. separate limb. */
  643. cy = mpn_lshift (frac, tmp, tmpsize, cnt_h + 1);
  644. fracsize = tmpsize + 1;
  645. frac[fracsize - 1] = cy;
  646. }
  647. else if (BITS_PER_MP_LIMB - 1 - cnt_h <= cnt_l)
  648. {
  649. (void) mpn_rshift (frac, tmp + i, tmpsize - i,
  650. BITS_PER_MP_LIMB - 1 - cnt_h);
  651. fracsize = tmpsize - i;
  652. }
  653. else
  654. {
  655. /* We can only save the memory of the limbs which
  656. are zero. The non-zero parts occupy the same
  657. number of limbs. */
  658. (void) mpn_rshift (frac, tmp + (i - 1),
  659. tmpsize - (i - 1),
  660. BITS_PER_MP_LIMB - 1 - cnt_h);
  661. fracsize = tmpsize - (i - 1);
  662. }
  663. }
  664. }
  665. }
  666. --explog;
  667. }
  668. while (powers != &_fpioconst_pow10[1] && exponent > 0);
  669. /* All factors but 10^-1 are tested now. */
  670. if (exponent > 0)
  671. {
  672. int cnt_l;
  673. cy = mpn_mul_1 (tmp, frac, fracsize, 10);
  674. tmpsize = fracsize;
  675. assert (cy == 0 || tmp[tmpsize - 1] < 20);
  676. count_trailing_zeros (cnt_l, tmp[0]);
  677. if (cnt_l < MIN (4, exponent))
  678. {
  679. cy = mpn_lshift (frac, tmp, tmpsize,
  680. BITS_PER_MP_LIMB - MIN (4, exponent));
  681. if (cy != 0)
  682. frac[tmpsize++] = cy;
  683. }
  684. else
  685. (void) mpn_rshift (frac, tmp, tmpsize, MIN (4, exponent));
  686. fracsize = tmpsize;
  687. exp10 |= 1;
  688. assert (frac[fracsize - 1] < 10);
  689. }
  690. exponent = exp10;
  691. }
  692. else
  693. {
  694. /* This is a special case. We don't need a factor because the
  695. numbers are in the range of 1.0 <= |fp| < 8.0. We simply
  696. shift it to the right place and divide it by 1.0 to get the
  697. leading digit. (Of course this division is not really made.) */
  698. assert (0 <= exponent && exponent < 3 &&
  699. exponent + to_shift < BITS_PER_MP_LIMB);
  700. /* Now shift the input value to its right place. */
  701. cy = mpn_lshift (frac, fp_input, fracsize, (exponent + to_shift));
  702. frac[fracsize++] = cy;
  703. exponent = 0;
  704. }
  705. {
  706. int width = info->width;
  707. wchar_t *wstartp, *wcp;
  708. size_t chars_needed;
  709. int expscale;
  710. int intdig_max, intdig_no = 0;
  711. int fracdig_min;
  712. int fracdig_max;
  713. int dig_max;
  714. int significant;
  715. int ngroups = 0;
  716. char spec = tolower (info->spec);
  717. if (spec == 'e')
  718. {
  719. type = info->spec;
  720. intdig_max = 1;
  721. fracdig_min = fracdig_max = info->prec < 0 ? 6 : info->prec;
  722. chars_needed = 1 + 1 + (size_t) fracdig_max + 1 + 1 + 4;
  723. /* d . ddd e +- ddd */
  724. dig_max = __INT_MAX__; /* Unlimited. */
  725. significant = 1; /* Does not matter here. */
  726. }
  727. else if (spec == 'f')
  728. {
  729. type = 'f';
  730. fracdig_min = fracdig_max = info->prec < 0 ? 6 : info->prec;
  731. dig_max = __INT_MAX__; /* Unlimited. */
  732. significant = 1; /* Does not matter here. */
  733. if (expsign == 0)
  734. {
  735. intdig_max = exponent + 1;
  736. /* This can be really big! */ /* XXX Maybe malloc if too big? */
  737. chars_needed = (size_t) exponent + 1 + 1 + (size_t) fracdig_max;
  738. }
  739. else
  740. {
  741. intdig_max = 1;
  742. chars_needed = 1 + 1 + (size_t) fracdig_max;
  743. }
  744. }
  745. else
  746. {
  747. dig_max = info->prec < 0 ? 6 : (info->prec == 0 ? 1 : info->prec);
  748. if ((expsign == 0 && exponent >= dig_max)
  749. || (expsign != 0 && exponent > 4))
  750. {
  751. if ('g' - 'G' == 'e' - 'E')
  752. type = 'E' + (info->spec - 'G');
  753. else
  754. type = isupper (info->spec) ? 'E' : 'e';
  755. fracdig_max = dig_max - 1;
  756. intdig_max = 1;
  757. chars_needed = 1 + 1 + (size_t) fracdig_max + 1 + 1 + 4;
  758. }
  759. else
  760. {
  761. type = 'f';
  762. intdig_max = expsign == 0 ? exponent + 1 : 0;
  763. fracdig_max = dig_max - intdig_max;
  764. /* We need space for the significant digits and perhaps
  765. for leading zeros when < 1.0. The number of leading
  766. zeros can be as many as would be required for
  767. exponential notation with a negative two-digit
  768. exponent, which is 4. */
  769. chars_needed = (size_t) dig_max + 1 + 4;
  770. }
  771. fracdig_min = info->alt ? fracdig_max : 0;
  772. significant = 0; /* We count significant digits. */
  773. }
  774. if (grouping)
  775. {
  776. /* Guess the number of groups we will make, and thus how
  777. many spaces we need for separator characters. */
  778. ngroups = guess_grouping (intdig_max, grouping);
  779. /* Allocate one more character in case rounding increases the
  780. number of groups. */
  781. chars_needed += ngroups + 1;
  782. }
  783. /* Allocate buffer for output. We need two more because while rounding
  784. it is possible that we need two more characters in front of all the
  785. other output. If the amount of memory we have to allocate is too
  786. large use `malloc' instead of `alloca'. */
  787. if (__builtin_expect (chars_needed >= (size_t) -1 / sizeof (wchar_t) - 2
  788. || chars_needed < fracdig_max, 0))
  789. {
  790. /* Some overflow occurred. */
  791. #if defined HAVE_ERRNO_H && defined ERANGE
  792. errno = ERANGE;
  793. #endif
  794. return -1;
  795. }
  796. size_t wbuffer_to_alloc = (2 + chars_needed) * sizeof (wchar_t);
  797. buffer_malloced = wbuffer_to_alloc >= 4096;
  798. if (__builtin_expect (buffer_malloced, 0))
  799. {
  800. wbuffer = (wchar_t *) malloc (wbuffer_to_alloc);
  801. if (wbuffer == NULL)
  802. /* Signal an error to the caller. */
  803. return -1;
  804. }
  805. else
  806. wbuffer = (wchar_t *) alloca (wbuffer_to_alloc);
  807. wcp = wstartp = wbuffer + 2; /* Let room for rounding. */
  808. /* Do the real work: put digits in allocated buffer. */
  809. if (expsign == 0 || type != 'f')
  810. {
  811. assert (expsign == 0 || intdig_max == 1);
  812. while (intdig_no < intdig_max)
  813. {
  814. ++intdig_no;
  815. *wcp++ = hack_digit ();
  816. }
  817. significant = 1;
  818. if (info->alt
  819. || fracdig_min > 0
  820. || (fracdig_max > 0 && (fracsize > 1 || frac[0] != 0)))
  821. *wcp++ = decimalwc;
  822. }
  823. else
  824. {
  825. /* |fp| < 1.0 and the selected type is 'f', so put "0."
  826. in the buffer. */
  827. *wcp++ = L_('0');
  828. --exponent;
  829. *wcp++ = decimalwc;
  830. }
  831. /* Generate the needed number of fractional digits. */
  832. int fracdig_no = 0;
  833. int added_zeros = 0;
  834. while (fracdig_no < fracdig_min + added_zeros
  835. || (fracdig_no < fracdig_max && (fracsize > 1 || frac[0] != 0)))
  836. {
  837. ++fracdig_no;
  838. *wcp = hack_digit ();
  839. if (*wcp++ != L_('0'))
  840. significant = 1;
  841. else if (significant == 0)
  842. {
  843. ++fracdig_max;
  844. if (fracdig_min > 0)
  845. ++added_zeros;
  846. }
  847. }
  848. /* Do rounding. */
  849. last_digit = wcp[-1] != decimalwc ? wcp[-1] : wcp[-2];
  850. next_digit =hack_digit ();
  851. if (next_digit != L_('0') && next_digit != L_('5'))
  852. more_bits = true;
  853. else if (fracsize == 1 && frac[0] == 0)
  854. /* Rest of the number is zero. */
  855. more_bits = false;
  856. else if (scalesize == 0)
  857. {
  858. /* Here we have to see whether all limbs are zero since no
  859. normalization happened. */
  860. size_t lcnt = fracsize;
  861. while (lcnt >= 1 && frac[lcnt - 1] == 0)
  862. --lcnt;
  863. more_bits = lcnt > 0;
  864. }
  865. else
  866. more_bits = true;
  867. #ifdef HAVE_FENV_H
  868. int rounding_mode = get_rounding_mode ();
  869. if (round_away (is_neg, (last_digit - L_('0')) & 1, next_digit >= L_('5'),
  870. more_bits, rounding_mode))
  871. {
  872. wchar_t *wtp = wcp;
  873. if (fracdig_no > 0)
  874. {
  875. /* Process fractional digits. Terminate if not rounded or
  876. radix character is reached. */
  877. int removed = 0;
  878. while (*--wtp != decimalwc && *wtp == L_('9'))
  879. {
  880. *wtp = L_('0');
  881. ++removed;
  882. }
  883. if (removed == fracdig_min && added_zeros > 0)
  884. --added_zeros;
  885. if (*wtp != decimalwc)
  886. /* Round up. */
  887. (*wtp)++;
  888. else if (__builtin_expect (spec == 'g' && type == 'f' && info->alt
  889. && wtp == wstartp + 1
  890. && wstartp[0] == L_('0'),
  891. 0))
  892. /* This is a special case: the rounded number is 1.0,
  893. the format is 'g' or 'G', and the alternative format
  894. is selected. This means the result must be "1.". */
  895. --added_zeros;
  896. }
  897. if (fracdig_no == 0 || *wtp == decimalwc)
  898. {
  899. /* Round the integer digits. */
  900. if (*(wtp - 1) == decimalwc)
  901. --wtp;
  902. while (--wtp >= wstartp && *wtp == L_('9'))
  903. *wtp = L_('0');
  904. if (wtp >= wstartp)
  905. /* Round up. */
  906. (*wtp)++;
  907. else
  908. /* It is more critical. All digits were 9's. */
  909. {
  910. if (type != 'f')
  911. {
  912. *wstartp = '1';
  913. exponent += expsign == 0 ? 1 : -1;
  914. /* The above exponent adjustment could lead to 1.0e-00,
  915. e.g. for 0.999999999. Make sure exponent 0 always
  916. uses + sign. */
  917. if (exponent == 0)
  918. expsign = 0;
  919. }
  920. else if (intdig_no == dig_max)
  921. {
  922. /* This is the case where for type %g the number fits
  923. really in the range for %f output but after rounding
  924. the number of digits is too big. */
  925. *--wstartp = decimalwc;
  926. *--wstartp = L_('1');
  927. if (info->alt || fracdig_no > 0)
  928. {
  929. /* Overwrite the old radix character. */
  930. wstartp[intdig_no + 2] = L_('0');
  931. ++fracdig_no;
  932. }
  933. fracdig_no += intdig_no;
  934. intdig_no = 1;
  935. fracdig_max = intdig_max - intdig_no;
  936. ++exponent;
  937. /* Now we must print the exponent. */
  938. type = isupper (info->spec) ? 'E' : 'e';
  939. }
  940. else
  941. {
  942. /* We can simply add another another digit before the
  943. radix. */
  944. *--wstartp = L_('1');
  945. ++intdig_no;
  946. }
  947. /* While rounding the number of digits can change.
  948. If the number now exceeds the limits remove some
  949. fractional digits. */
  950. if (intdig_no + fracdig_no > dig_max)
  951. {
  952. wcp -= intdig_no + fracdig_no - dig_max;
  953. fracdig_no -= intdig_no + fracdig_no - dig_max;
  954. }
  955. }
  956. }
  957. }
  958. #endif
  959. /* Now remove unnecessary '0' at the end of the string. */
  960. while (fracdig_no > fracdig_min + added_zeros && *(wcp - 1) == L_('0'))
  961. {
  962. --wcp;
  963. --fracdig_no;
  964. }
  965. /* If we eliminate all fractional digits we perhaps also can remove
  966. the radix character. */
  967. if (fracdig_no == 0 && !info->alt && *(wcp - 1) == decimalwc)
  968. --wcp;
  969. if (grouping)
  970. {
  971. /* Rounding might have changed the number of groups. We allocated
  972. enough memory but we need here the correct number of groups. */
  973. if (intdig_no != intdig_max)
  974. ngroups = guess_grouping (intdig_no, grouping);
  975. /* Add in separator characters, overwriting the same buffer. */
  976. wcp = group_number (wstartp, wcp, intdig_no, grouping, thousands_sepwc,
  977. ngroups);
  978. }
  979. /* Write the exponent if it is needed. */
  980. if (type != 'f')
  981. {
  982. if (__builtin_expect (expsign != 0 && exponent == 4 && spec == 'g', 0))
  983. {
  984. /* This is another special case. The exponent of the number is
  985. really smaller than -4, which requires the 'e'/'E' format.
  986. But after rounding the number has an exponent of -4. */
  987. assert (wcp >= wstartp + 1);
  988. assert (wstartp[0] == L_('1'));
  989. memcpy (wstartp, L_("0.0001"), 6 * sizeof (wchar_t));
  990. wstartp[1] = decimalwc;
  991. if (wcp >= wstartp + 2)
  992. {
  993. size_t cnt;
  994. for (cnt = 0; cnt < wcp - (wstartp + 2); cnt++)
  995. wstartp[6 + cnt] = L_('0');
  996. wcp += 4;
  997. }
  998. else
  999. wcp += 5;
  1000. }
  1001. else
  1002. {
  1003. *wcp++ = (wchar_t) type;
  1004. *wcp++ = expsign ? L_('-') : L_('+');
  1005. /* Find the magnitude of the exponent. */
  1006. expscale = 10;
  1007. while (expscale <= exponent)
  1008. expscale *= 10;
  1009. if (exponent < 10)
  1010. /* Exponent always has at least two digits. */
  1011. *wcp++ = L_('0');
  1012. else
  1013. do
  1014. {
  1015. expscale /= 10;
  1016. *wcp++ = L_('0') + (exponent / expscale);
  1017. exponent %= expscale;
  1018. }
  1019. while (expscale > 10);
  1020. *wcp++ = L_('0') + exponent;
  1021. }
  1022. }
  1023. /* Compute number of characters which must be filled with the padding
  1024. character. */
  1025. if (is_neg || info->showsign || info->space)
  1026. --width;
  1027. width -= wcp - wstartp;
  1028. if (!info->left && info->pad != '0' && width > 0)
  1029. PADN (info->pad, width);
  1030. if (is_neg)
  1031. outchar ('-');
  1032. else if (info->showsign)
  1033. outchar ('+');
  1034. else if (info->space)
  1035. outchar (' ');
  1036. if (!info->left && info->pad == '0' && width > 0)
  1037. PADN ('0', width);
  1038. {
  1039. char *buffer = NULL;
  1040. char *buffer_end __attribute__((__unused__)) = NULL;
  1041. char *cp = NULL;
  1042. char *tmpptr;
  1043. if (! wide)
  1044. {
  1045. /* Create the single byte string. */
  1046. size_t decimal_len;
  1047. size_t thousands_sep_len;
  1048. wchar_t *copywc;
  1049. #ifdef USE_I18N_NUMBER_H
  1050. size_t factor = (info->i18n
  1051. ? nl_langinfo_wc (_NL_CTYPE_MB_CUR_MAX)
  1052. : 1);
  1053. #else
  1054. size_t factor = 1;
  1055. #endif
  1056. decimal_len = strlen (decimal);
  1057. if (thousands_sep == NULL)
  1058. thousands_sep_len = 0;
  1059. else
  1060. thousands_sep_len = strlen (thousands_sep);
  1061. size_t nbuffer = (2 + chars_needed * factor + decimal_len
  1062. + ngroups * thousands_sep_len);
  1063. if (__builtin_expect (buffer_malloced, 0))
  1064. {
  1065. buffer = (char *) malloc (nbuffer);
  1066. if (buffer == NULL)
  1067. {
  1068. /* Signal an error to the caller. */
  1069. free (wbuffer);
  1070. return -1;
  1071. }
  1072. }
  1073. else
  1074. buffer = (char *) alloca (nbuffer);
  1075. buffer_end = buffer + nbuffer;
  1076. /* Now copy the wide character string. Since the character
  1077. (except for the decimal point and thousands separator) must
  1078. be coming from the ASCII range we can esily convert the
  1079. string without mapping tables. */
  1080. for (cp = buffer, copywc = wstartp; copywc < wcp; ++copywc)
  1081. if (*copywc == decimalwc)
  1082. memcpy (cp, decimal, decimal_len), cp += decimal_len;
  1083. else if (*copywc == thousands_sepwc)
  1084. memcpy (cp, thousands_sep, thousands_sep_len), cp += thousands_sep_len;
  1085. else
  1086. *cp++ = (char) *copywc;
  1087. }
  1088. tmpptr = buffer;
  1089. #if USE_I18N_NUMBER_H
  1090. if (__builtin_expect (info->i18n, 0))
  1091. {
  1092. tmpptr = _i18n_number_rewrite (tmpptr, cp, buffer_end);
  1093. cp = buffer_end;
  1094. assert ((uintptr_t) buffer <= (uintptr_t) tmpptr);
  1095. assert ((uintptr_t) tmpptr < (uintptr_t) buffer_end);
  1096. }
  1097. #endif
  1098. PRINT (tmpptr, wstartp, wide ? wcp - wstartp : cp - tmpptr);
  1099. /* Free the memory if necessary. */
  1100. if (__builtin_expect (buffer_malloced, 0))
  1101. {
  1102. free (buffer);
  1103. free (wbuffer);
  1104. }
  1105. }
  1106. if (info->left && width > 0)
  1107. PADN (info->pad, width);
  1108. }
  1109. return done;
  1110. }
  1111. /* Return the number of extra grouping characters that will be inserted
  1112. into a number with INTDIG_MAX integer digits. */
  1113. static unsigned int
  1114. guess_grouping (unsigned int intdig_max, const char *grouping)
  1115. {
  1116. unsigned int groups;
  1117. /* We treat all negative values like CHAR_MAX. */
  1118. if (*grouping == CHAR_MAX || *grouping <= 0)
  1119. /* No grouping should be done. */
  1120. return 0;
  1121. groups = 0;
  1122. while (intdig_max > (unsigned int) *grouping)
  1123. {
  1124. ++groups;
  1125. intdig_max -= *grouping++;
  1126. if (*grouping == CHAR_MAX
  1127. #if CHAR_MIN < 0
  1128. || *grouping < 0
  1129. #endif
  1130. )
  1131. /* No more grouping should be done. */
  1132. break;
  1133. else if (*grouping == 0)
  1134. {
  1135. /* Same grouping repeats. */
  1136. groups += (intdig_max - 1) / grouping[-1];
  1137. break;
  1138. }
  1139. }
  1140. return groups;
  1141. }
  1142. /* Group the INTDIG_NO integer digits of the number in [BUF,BUFEND).
  1143. There is guaranteed enough space past BUFEND to extend it.
  1144. Return the new end of buffer. */
  1145. static wchar_t *
  1146. group_number (wchar_t *buf, wchar_t *bufend, unsigned int intdig_no,
  1147. const char *grouping, wchar_t thousands_sep, int ngroups)
  1148. {
  1149. wchar_t *p;
  1150. if (ngroups == 0)
  1151. return bufend;
  1152. /* Move the fractional part down. */
  1153. memmove (buf + intdig_no + ngroups, buf + intdig_no,
  1154. (bufend - (buf + intdig_no)) * sizeof (wchar_t));
  1155. p = buf + intdig_no + ngroups - 1;
  1156. do
  1157. {
  1158. unsigned int len = *grouping++;
  1159. do
  1160. *p-- = buf[--intdig_no];
  1161. while (--len > 0);
  1162. *p-- = thousands_sep;
  1163. if (*grouping == CHAR_MAX
  1164. #if CHAR_MIN < 0
  1165. || *grouping < 0
  1166. #endif
  1167. )
  1168. /* No more grouping should be done. */
  1169. break;
  1170. else if (*grouping == 0)
  1171. /* Same grouping repeats. */
  1172. --grouping;
  1173. } while (intdig_no > (unsigned int) *grouping);
  1174. /* Copy the remaining ungrouped digits. */
  1175. do
  1176. *p-- = buf[--intdig_no];
  1177. while (p > buf);
  1178. return bufend + ngroups;
  1179. }