Back to index

glibc  2.9
_itowa.c
Go to the documentation of this file.
00001 /* Internal function for converting integers to ASCII.
00002    Copyright (C) 1994-1996,1999,2000,2002,2007 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Torbjorn Granlund <tege@matematik.su.se>
00005    and Ulrich Drepper <drepper@gnu.org>.
00006 
00007    The GNU C Library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Lesser General Public
00009    License as published by the Free Software Foundation; either
00010    version 2.1 of the License, or (at your option) any later version.
00011 
00012    The GNU C Library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Lesser General Public License for more details.
00016 
00017    You should have received a copy of the GNU Lesser General Public
00018    License along with the GNU C Library; if not, write to the Free
00019    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00020    02111-1307 USA.  */
00021 
00022 #include <gmp-mparam.h>
00023 #include <gmp.h>
00024 #include <limits.h>
00025 #include <stdlib/gmp-impl.h>
00026 #include <stdlib/longlong.h>
00027 
00028 #include "_itowa.h"
00029 
00030 
00031 /* Canonize environment.  For some architectures not all values might
00032    be defined in the GMP header files.  */
00033 #ifndef UMUL_TIME
00034 # define UMUL_TIME 1
00035 #endif
00036 #ifndef UDIV_TIME
00037 # define UDIV_TIME 3
00038 #endif
00039 
00040 /* Control memory layout.  */
00041 #ifdef PACK
00042 # undef PACK
00043 # define PACK __attribute__ ((packed))
00044 #else
00045 # define PACK
00046 #endif
00047 
00048 
00049 /* Declare local types.  */
00050 struct base_table_t
00051 {
00052 #if (UDIV_TIME > 2 * UMUL_TIME)
00053   mp_limb_t base_multiplier;
00054 #endif
00055   char flag;
00056   char post_shift;
00057 #if BITS_PER_MP_LIMB == 32
00058   struct
00059     {
00060       char normalization_steps;
00061       char ndigits;
00062       mp_limb_t base PACK;
00063 #if UDIV_TIME > 2 * UMUL_TIME
00064       mp_limb_t base_ninv PACK;
00065 #endif
00066     } big;
00067 #endif
00068 };
00069 
00070 /* To reduce the memory needed we include some fields of the tables
00071    only conditionally.  */
00072 #if UDIV_TIME > 2 * UMUL_TIME
00073 # define SEL1(X) X,
00074 # define SEL2(X) ,X
00075 #else
00076 # define SEL1(X)
00077 # define SEL2(X)
00078 #endif
00079 
00080 /* Factor table for the different bases.  */
00081 extern const struct base_table_t _itoa_base_table[] attribute_hidden;
00082 
00083 /* Lower-case digits.  */
00084 extern const wchar_t _itowa_lower_digits[] attribute_hidden;
00085 /* Upper-case digits.  */
00086 extern const wchar_t _itowa_upper_digits[] attribute_hidden;
00087 
00088 
00089 #if LLONG_MAX != LONG_MAX
00090 wchar_t *
00091 _itowa (value, buflim, base, upper_case)
00092      unsigned long long int value;
00093      wchar_t *buflim;
00094      unsigned int base;
00095      int upper_case;
00096 {
00097   const wchar_t *digits = (upper_case
00098                         ? _itowa_upper_digits : _itowa_lower_digits);
00099   wchar_t *bp = buflim;
00100   const struct base_table_t *brec = &_itoa_base_table[base - 2];
00101 
00102   switch (base)
00103     {
00104 # define RUN_2N(BITS) \
00105       do                                                             \
00106         {                                                            \
00107          /* `unsigned long long int' always has 64 bits.  */                \
00108          mp_limb_t work_hi = value >> (64 - BITS_PER_MP_LIMB);              \
00109                                                                      \
00110          if (BITS_PER_MP_LIMB == 32)                                        \
00111            {                                                         \
00112              if (work_hi != 0)                                              \
00113               {                                                      \
00114                 mp_limb_t work_lo;                                   \
00115                 int cnt;                                             \
00116                                                                      \
00117                 work_lo = value & 0xfffffffful;                      \
00118                 for (cnt = BITS_PER_MP_LIMB / BITS; cnt > 0; --cnt)         \
00119                   {                                                  \
00120                     *--bp = digits[work_lo & ((1ul << BITS) - 1)];          \
00121                     work_lo >>= BITS;                                       \
00122                   }                                                  \
00123                 if (BITS_PER_MP_LIMB % BITS != 0)                           \
00124                   {                                                  \
00125                     work_lo                                          \
00126                      |= ((work_hi                                    \
00127                           & ((1 << (BITS - BITS_PER_MP_LIMB%BITS))          \
00128                             - 1))                                    \
00129                          << BITS_PER_MP_LIMB % BITS);                \
00130                     work_hi >>= BITS - BITS_PER_MP_LIMB % BITS;             \
00131                     if (work_hi == 0)                                       \
00132                      work_hi = work_lo;                              \
00133                     else                                             \
00134                      *--bp = digits[work_lo];                        \
00135                   }                                                  \
00136               }                                                      \
00137              else                                                    \
00138               work_hi = value & 0xfffffffful;                               \
00139            }                                                         \
00140          do                                                          \
00141            {                                                         \
00142              *--bp = digits[work_hi & ((1 << BITS) - 1)];                   \
00143              work_hi >>= BITS;                                              \
00144            }                                                         \
00145          while (work_hi != 0);                                              \
00146        }                                                             \
00147       while (0)
00148     case 8:
00149       RUN_2N (3);
00150       break;
00151 
00152     case 16:
00153       RUN_2N (4);
00154       break;
00155 
00156     default:
00157       {
00158 # if BITS_PER_MP_LIMB == 64
00159        mp_limb_t base_multiplier = brec->base_multiplier;
00160        if (brec->flag)
00161          while (value != 0)
00162            {
00163              mp_limb_t quo, rem, x, dummy;
00164 
00165              umul_ppmm (x, dummy, value, base_multiplier);
00166              quo = (x + ((value - x) >> 1)) >> (brec->post_shift - 1);
00167              rem = value - quo * base;
00168              *--bp = digits[rem];
00169              value = quo;
00170            }
00171        else
00172          while (value != 0)
00173            {
00174              mp_limb_t quo, rem, x, dummy;
00175 
00176              umul_ppmm (x, dummy, value, base_multiplier);
00177              quo = x >> brec->post_shift;
00178              rem = value - quo * base;
00179              *--bp = digits[rem];
00180              value = quo;
00181            }
00182 # endif
00183 # if BITS_PER_MP_LIMB == 32
00184        mp_limb_t t[3];
00185        int n;
00186 
00187        /* First convert x0 to 1-3 words in base s->big.base.
00188           Optimize for frequent cases of 32 bit numbers.  */
00189        if ((mp_limb_t) (value >> 32) >= 1)
00190          {
00191 # if UDIV_TIME > 2 * UMUL_TIME || UDIV_NEEDS_NORMALIZATION
00192            int big_normalization_steps = brec->big.normalization_steps;
00193            mp_limb_t big_base_norm
00194              = brec->big.base << big_normalization_steps;
00195 # endif
00196            if ((mp_limb_t) (value >> 32) >= brec->big.base)
00197              {
00198               mp_limb_t x1hi, x1lo, r;
00199               /* If you want to optimize this, take advantage of
00200                  that the quotient in the first udiv_qrnnd will
00201                  always be very small.  It might be faster just to
00202                  subtract in a tight loop.  */
00203 
00204 # if UDIV_TIME > 2 * UMUL_TIME
00205               mp_limb_t x, xh, xl;
00206 
00207               if (big_normalization_steps == 0)
00208                 xh = 0;
00209               else
00210                 xh = (mp_limb_t) (value >> (64 - big_normalization_steps));
00211               xl = (mp_limb_t) (value >> (32 - big_normalization_steps));
00212               udiv_qrnnd_preinv (x1hi, r, xh, xl, big_base_norm,
00213                                brec->big.base_ninv);
00214 
00215               xl = ((mp_limb_t) value) << big_normalization_steps;
00216               udiv_qrnnd_preinv (x1lo, x, r, xl, big_base_norm,
00217                                brec->big.base_ninv);
00218               t[2] = x >> big_normalization_steps;
00219 
00220               if (big_normalization_steps == 0)
00221                 xh = x1hi;
00222               else
00223                 xh = ((x1hi << big_normalization_steps)
00224                      | (x1lo >> (32 - big_normalization_steps)));
00225               xl = x1lo << big_normalization_steps;
00226               udiv_qrnnd_preinv (t[0], x, xh, xl, big_base_norm,
00227                                brec->big.base_ninv);
00228               t[1] = x >> big_normalization_steps;
00229 # elif UDIV_NEEDS_NORMALIZATION
00230               mp_limb_t x, xh, xl;
00231 
00232               if (big_normalization_steps == 0)
00233                 xh = 0;
00234               else
00235                 xh = (mp_limb_t) (value >> 64 - big_normalization_steps);
00236               xl = (mp_limb_t) (value >> 32 - big_normalization_steps);
00237               udiv_qrnnd (x1hi, r, xh, xl, big_base_norm);
00238 
00239               xl = ((mp_limb_t) value) << big_normalization_steps;
00240               udiv_qrnnd (x1lo, x, r, xl, big_base_norm);
00241               t[2] = x >> big_normalization_steps;
00242 
00243               if (big_normalization_steps == 0)
00244                 xh = x1hi;
00245               else
00246                 xh = ((x1hi << big_normalization_steps)
00247                      | (x1lo >> 32 - big_normalization_steps));
00248               xl = x1lo << big_normalization_steps;
00249               udiv_qrnnd (t[0], x, xh, xl, big_base_norm);
00250               t[1] = x >> big_normalization_steps;
00251 # else
00252               udiv_qrnnd (x1hi, r, 0, (mp_limb_t) (value >> 32),
00253                          brec->big.base);
00254               udiv_qrnnd (x1lo, t[2], r, (mp_limb_t) value, brec->big.base);
00255               udiv_qrnnd (t[0], t[1], x1hi, x1lo, brec->big.base);
00256 # endif
00257               n = 3;
00258              }
00259            else
00260              {
00261 # if UDIV_TIME > 2 * UMUL_TIME
00262               mp_limb_t x;
00263 
00264               value <<= brec->big.normalization_steps;
00265               udiv_qrnnd_preinv (t[0], x, (mp_limb_t) (value >> 32),
00266                                (mp_limb_t) value, big_base_norm,
00267                                brec->big.base_ninv);
00268               t[1] = x >> brec->big.normalization_steps;
00269 # elif UDIV_NEEDS_NORMALIZATION
00270               mp_limb_t x;
00271 
00272               value <<= big_normalization_steps;
00273               udiv_qrnnd (t[0], x, (mp_limb_t) (value >> 32),
00274                          (mp_limb_t) value, big_base_norm);
00275               t[1] = x >> big_normalization_steps;
00276 # else
00277               udiv_qrnnd (t[0], t[1], (mp_limb_t) (value >> 32),
00278                          (mp_limb_t) value, brec->big.base);
00279 # endif
00280               n = 2;
00281              }
00282          }
00283        else
00284          {
00285            t[0] = value;
00286            n = 1;
00287          }
00288 
00289        /* Convert the 1-3 words in t[], word by word, to ASCII.  */
00290        do
00291          {
00292            mp_limb_t ti = t[--n];
00293            int ndig_for_this_limb = 0;
00294 
00295 # if UDIV_TIME > 2 * UMUL_TIME
00296            mp_limb_t base_multiplier = brec->base_multiplier;
00297            if (brec->flag)
00298              while (ti != 0)
00299               {
00300                 mp_limb_t quo, rem, x, dummy;
00301 
00302                 umul_ppmm (x, dummy, ti, base_multiplier);
00303                 quo = (x + ((ti - x) >> 1)) >> (brec->post_shift - 1);
00304                 rem = ti - quo * base;
00305                 *--bp = digits[rem];
00306                 ti = quo;
00307                 ++ndig_for_this_limb;
00308               }
00309            else
00310              while (ti != 0)
00311               {
00312                 mp_limb_t quo, rem, x, dummy;
00313 
00314                 umul_ppmm (x, dummy, ti, base_multiplier);
00315                 quo = x >> brec->post_shift;
00316                 rem = ti - quo * base;
00317                 *--bp = digits[rem];
00318                 ti = quo;
00319                 ++ndig_for_this_limb;
00320               }
00321 # else
00322            while (ti != 0)
00323              {
00324               mp_limb_t quo, rem;
00325 
00326               quo = ti / base;
00327               rem = ti % base;
00328               *--bp = digits[rem];
00329               ti = quo;
00330               ++ndig_for_this_limb;
00331              }
00332 # endif
00333            /* If this wasn't the most significant word, pad with zeros.  */
00334            if (n != 0)
00335              while (ndig_for_this_limb < brec->big.ndigits)
00336               {
00337                 *--bp = '0';
00338                 ++ndig_for_this_limb;
00339               }
00340          }
00341        while (n != 0);
00342 # endif
00343       }
00344       break;
00345     }
00346 
00347   return bp;
00348 }
00349 #endif