This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
PATCH: Use long long in _itoa and _itowa if PREFER_LONG_LONG isdefined
- From: "H.J. Lu" <hongjiu dot lu at intel dot com>
- To: GNU C Library <libc-alpha at sourceware dot org>
- Date: Fri, 16 Mar 2012 12:22:14 -0700
- Subject: PATCH: Use long long in _itoa and _itowa if PREFER_LONG_LONG isdefined
- Reply-to: "H.J. Lu" <hjl dot tools at gmail dot com>
X32 prefers to use long long in _itoa and _itowa. This patch changes _itoa
and _itowa to use long long for computation when PREFER_LONG_LONG is defined.
OK to install?
Thanks.
2012-03-16 H.J. Lu <hongjiu.lu@intel.com>
* stdio-common/_itoa.c (_itoa_base_table): Don't define if
PREFER_LONG_LONG is defined.
(_itoa): Use long long if PREFER_LONG_LONG is defined.
* stdio-common/_itowa.c (_itowa): Likewise.
diff --git a/stdio-common/_itoa.c b/stdio-common/_itoa.c
index e52f666..ba98edb 100644
--- a/stdio-common/_itoa.c
+++ b/stdio-common/_itoa.c
@@ -79,7 +79,7 @@ struct base_table_t
/* We do not compile _itoa if we always can use _itoa_word. */
-#if LLONG_MAX != LONG_MAX
+#if LLONG_MAX != LONG_MAX && !defined PREFER_LONG_LONG
/* Local variables. */
const struct base_table_t _itoa_base_table[] attribute_hidden =
{
@@ -201,7 +201,6 @@ _itoa_word (unsigned long value, char *buflim,
}
return buflim;
}
-#undef SPECIAL
#if LLONG_MAX != LONG_MAX
@@ -215,11 +214,31 @@ _itoa (value, buflim, base, upper_case)
const char *digits = (upper_case
? INTUSE(_itoa_upper_digits)
: INTUSE(_itoa_lower_digits));
+
+# ifdef PREFER_LONG_LONG
+ switch (base)
+ {
+# define SPECIAL(Base) \
+ case Base: \
+ do \
+ *--buflim = digits[value % Base]; \
+ while ((value /= Base) != 0); \
+ break
+
+ SPECIAL (10);
+ SPECIAL (16);
+ SPECIAL (8);
+ default:
+ do
+ *--buflim = digits[value % base];
+ while ((value /= base) != 0);
+ }
+# else
const struct base_table_t *brec = &_itoa_base_table[base - 2];
switch (base)
{
-# define RUN_2N(BITS) \
+# define RUN_2N(BITS) \
do \
{ \
/* `unsigned long long int' always has 64 bits. */ \
@@ -274,7 +293,7 @@ _itoa (value, buflim, base, upper_case)
default:
{
char *bufend = buflim;
-# if BITS_PER_MP_LIMB == 64
+# if BITS_PER_MP_LIMB == 64
mp_limb_t base_multiplier = brec->base_multiplier;
if (brec->flag)
while (value != 0)
@@ -298,8 +317,8 @@ _itoa (value, buflim, base, upper_case)
*--buflim = digits[rem];
value = quo;
}
-# endif
-# if BITS_PER_MP_LIMB == 32
+# endif
+# if BITS_PER_MP_LIMB == 32
mp_limb_t t[3];
int n;
@@ -307,11 +326,11 @@ _itoa (value, buflim, base, upper_case)
Optimize for frequent cases of 32 bit numbers. */
if ((mp_limb_t) (value >> 32) >= 1)
{
-# if UDIV_TIME > 2 * UMUL_TIME || UDIV_NEEDS_NORMALIZATION
+# if UDIV_TIME > 2 * UMUL_TIME || UDIV_NEEDS_NORMALIZATION
int big_normalization_steps = brec->big.normalization_steps;
mp_limb_t big_base_norm
= brec->big.base << big_normalization_steps;
-# endif
+# endif
if ((mp_limb_t) (value >> 32) >= brec->big.base)
{
mp_limb_t x1hi, x1lo, r;
@@ -320,7 +339,7 @@ _itoa (value, buflim, base, upper_case)
always be very small. It might be faster just to
subtract in a tight loop. */
-# if UDIV_TIME > 2 * UMUL_TIME
+# if UDIV_TIME > 2 * UMUL_TIME
mp_limb_t x, xh, xl;
if (big_normalization_steps == 0)
@@ -345,7 +364,7 @@ _itoa (value, buflim, base, upper_case)
udiv_qrnnd_preinv (t[0], x, xh, xl, big_base_norm,
brec->big.base_ninv);
t[1] = x >> big_normalization_steps;
-# elif UDIV_NEEDS_NORMALIZATION
+# elif UDIV_NEEDS_NORMALIZATION
mp_limb_t x, xh, xl;
if (big_normalization_steps == 0)
@@ -367,17 +386,17 @@ _itoa (value, buflim, base, upper_case)
xl = x1lo << big_normalization_steps;
udiv_qrnnd (t[0], x, xh, xl, big_base_norm);
t[1] = x >> big_normalization_steps;
-# else
+# else
udiv_qrnnd (x1hi, r, 0, (mp_limb_t) (value >> 32),
brec->big.base);
udiv_qrnnd (x1lo, t[2], r, (mp_limb_t) value, brec->big.base);
udiv_qrnnd (t[0], t[1], x1hi, x1lo, brec->big.base);
-# endif
+# endif
n = 3;
}
else
{
-# if UDIV_TIME > 2 * UMUL_TIME
+# if UDIV_TIME > 2 * UMUL_TIME
mp_limb_t x;
value <<= brec->big.normalization_steps;
@@ -385,17 +404,17 @@ _itoa (value, buflim, base, upper_case)
(mp_limb_t) value, big_base_norm,
brec->big.base_ninv);
t[1] = x >> brec->big.normalization_steps;
-# elif UDIV_NEEDS_NORMALIZATION
+# elif UDIV_NEEDS_NORMALIZATION
mp_limb_t x;
value <<= big_normalization_steps;
udiv_qrnnd (t[0], x, (mp_limb_t) (value >> 32),
(mp_limb_t) value, big_base_norm);
t[1] = x >> big_normalization_steps;
-# else
+# else
udiv_qrnnd (t[0], t[1], (mp_limb_t) (value >> 32),
(mp_limb_t) value, brec->big.base);
-# endif
+# endif
n = 2;
}
}
@@ -411,7 +430,7 @@ _itoa (value, buflim, base, upper_case)
mp_limb_t ti = t[--n];
int ndig_for_this_limb = 0;
-# if UDIV_TIME > 2 * UMUL_TIME
+# if UDIV_TIME > 2 * UMUL_TIME
mp_limb_t base_multiplier = brec->base_multiplier;
if (brec->flag)
while (ti != 0)
@@ -437,7 +456,7 @@ _itoa (value, buflim, base, upper_case)
ti = quo;
++ndig_for_this_limb;
}
-# else
+# else
while (ti != 0)
{
mp_limb_t quo, rem;
@@ -448,7 +467,7 @@ _itoa (value, buflim, base, upper_case)
ti = quo;
++ndig_for_this_limb;
}
-# endif
+# endif
/* If this wasn't the most significant word, pad with zeros. */
if (n != 0)
while (ndig_for_this_limb < brec->big.ndigits)
@@ -458,17 +477,20 @@ _itoa (value, buflim, base, upper_case)
}
}
while (n != 0);
-# endif
+# endif
if (buflim == bufend)
*--buflim = '0';
}
break;
}
+# endif /* PREFER_LONG_LONG */
return buflim;
}
#endif
+#undef SPECIAL
+
char *
_fitoa_word (unsigned long value, char *buf, unsigned int base, int upper_case)
{
diff --git a/stdio-common/_itowa.c b/stdio-common/_itowa.c
index 8047c87..b0a28ac 100644
--- a/stdio-common/_itowa.c
+++ b/stdio-common/_itowa.c
@@ -96,11 +96,32 @@ _itowa (value, buflim, base, upper_case)
const wchar_t *digits = (upper_case
? _itowa_upper_digits : _itowa_lower_digits);
wchar_t *bp = buflim;
+
+# ifdef PREFER_LONG_LONG
+ switch (base)
+ {
+# define SPECIAL(Base) \
+ case Base: \
+ do \
+ *--bp = digits[value % Base]; \
+ while ((value /= Base) != 0); \
+ break
+
+ SPECIAL (10);
+ SPECIAL (16);
+ SPECIAL (8);
+ default:
+ do
+ *--bp = digits[value % base];
+ while ((value /= base) != 0);
+ }
+# undef SPECIAL
+# else
const struct base_table_t *brec = &_itoa_base_table[base - 2];
switch (base)
{
-# define RUN_2N(BITS) \
+# define RUN_2N(BITS) \
do \
{ \
/* `unsigned long long int' always has 64 bits. */ \
@@ -154,7 +175,7 @@ _itowa (value, buflim, base, upper_case)
default:
{
-# if BITS_PER_MP_LIMB == 64
+# if BITS_PER_MP_LIMB == 64
mp_limb_t base_multiplier = brec->base_multiplier;
if (brec->flag)
while (value != 0)
@@ -178,8 +199,8 @@ _itowa (value, buflim, base, upper_case)
*--bp = digits[rem];
value = quo;
}
-# endif
-# if BITS_PER_MP_LIMB == 32
+# endif
+# if BITS_PER_MP_LIMB == 32
mp_limb_t t[3];
int n;
@@ -187,11 +208,11 @@ _itowa (value, buflim, base, upper_case)
Optimize for frequent cases of 32 bit numbers. */
if ((mp_limb_t) (value >> 32) >= 1)
{
-# if UDIV_TIME > 2 * UMUL_TIME || UDIV_NEEDS_NORMALIZATION
+# if UDIV_TIME > 2 * UMUL_TIME || UDIV_NEEDS_NORMALIZATION
int big_normalization_steps = brec->big.normalization_steps;
mp_limb_t big_base_norm
= brec->big.base << big_normalization_steps;
-# endif
+# endif
if ((mp_limb_t) (value >> 32) >= brec->big.base)
{
mp_limb_t x1hi, x1lo, r;
@@ -200,7 +221,7 @@ _itowa (value, buflim, base, upper_case)
always be very small. It might be faster just to
subtract in a tight loop. */
-# if UDIV_TIME > 2 * UMUL_TIME
+# if UDIV_TIME > 2 * UMUL_TIME
mp_limb_t x, xh, xl;
if (big_normalization_steps == 0)
@@ -225,7 +246,7 @@ _itowa (value, buflim, base, upper_case)
udiv_qrnnd_preinv (t[0], x, xh, xl, big_base_norm,
brec->big.base_ninv);
t[1] = x >> big_normalization_steps;
-# elif UDIV_NEEDS_NORMALIZATION
+# elif UDIV_NEEDS_NORMALIZATION
mp_limb_t x, xh, xl;
if (big_normalization_steps == 0)
@@ -247,17 +268,17 @@ _itowa (value, buflim, base, upper_case)
xl = x1lo << big_normalization_steps;
udiv_qrnnd (t[0], x, xh, xl, big_base_norm);
t[1] = x >> big_normalization_steps;
-# else
+# else
udiv_qrnnd (x1hi, r, 0, (mp_limb_t) (value >> 32),
brec->big.base);
udiv_qrnnd (x1lo, t[2], r, (mp_limb_t) value, brec->big.base);
udiv_qrnnd (t[0], t[1], x1hi, x1lo, brec->big.base);
-# endif
+# endif
n = 3;
}
else
{
-# if UDIV_TIME > 2 * UMUL_TIME
+# if UDIV_TIME > 2 * UMUL_TIME
mp_limb_t x;
value <<= brec->big.normalization_steps;
@@ -265,17 +286,17 @@ _itowa (value, buflim, base, upper_case)
(mp_limb_t) value, big_base_norm,
brec->big.base_ninv);
t[1] = x >> brec->big.normalization_steps;
-# elif UDIV_NEEDS_NORMALIZATION
+# elif UDIV_NEEDS_NORMALIZATION
mp_limb_t x;
value <<= big_normalization_steps;
udiv_qrnnd (t[0], x, (mp_limb_t) (value >> 32),
(mp_limb_t) value, big_base_norm);
t[1] = x >> big_normalization_steps;
-# else
+# else
udiv_qrnnd (t[0], t[1], (mp_limb_t) (value >> 32),
(mp_limb_t) value, brec->big.base);
-# endif
+# endif
n = 2;
}
}
@@ -291,7 +312,7 @@ _itowa (value, buflim, base, upper_case)
mp_limb_t ti = t[--n];
int ndig_for_this_limb = 0;
-# if UDIV_TIME > 2 * UMUL_TIME
+# if UDIV_TIME > 2 * UMUL_TIME
mp_limb_t base_multiplier = brec->base_multiplier;
if (brec->flag)
while (ti != 0)
@@ -317,7 +338,7 @@ _itowa (value, buflim, base, upper_case)
ti = quo;
++ndig_for_this_limb;
}
-# else
+# else
while (ti != 0)
{
mp_limb_t quo, rem;
@@ -328,7 +349,7 @@ _itowa (value, buflim, base, upper_case)
ti = quo;
++ndig_for_this_limb;
}
-# endif
+# endif
/* If this wasn't the most significant word, pad with zeros. */
if (n != 0)
while (ndig_for_this_limb < brec->big.ndigits)
@@ -338,10 +359,11 @@ _itowa (value, buflim, base, upper_case)
}
}
while (n != 0);
-# endif
+# endif
}
break;
}
+# endif /* PREFER_LONG_LONG */
return bp;
}
--
1.7.6.5