~ubuntu-branches/ubuntu/utopic/coreutils/utopic-proposed

« back to all changes in this revision

Viewing changes to lib/vasnprintf.c

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2012-11-28 03:03:42 UTC
  • mfrom: (8.3.4 sid)
  • Revision ID: package-import@ubuntu.com-20121128030342-21zanj8354gas5gr
Tags: 8.20-3ubuntu1
* Resynchronise with Debian.  Remaining changes:
  - Make 'uname -i -p' return the real processor/hardware, instead of
    unknown.
  - Build-depend on gettext:any instead of on gettext, so that apt-get can
    properly resolve build-dependencies on the tool when cross-building.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- buffer-read-only: t -*- vi: set ro: */
2
 
/* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3
1
/* vsprintf with automatic memory allocation.
4
 
   Copyright (C) 1999, 2002-2011 Free Software Foundation, Inc.
 
2
   Copyright (C) 1999, 2002-2012 Free Software Foundation, Inc.
5
3
 
6
4
   This program is free software; you can redistribute it and/or modify
7
5
   it under the terms of the GNU General Public License as published by
14
12
   GNU General Public License for more details.
15
13
 
16
14
   You should have received a copy of the GNU General Public License along
17
 
   with this program; if not, write to the Free Software Foundation,
18
 
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
15
   with this program; if not, see <http://www.gnu.org/licenses/>.  */
19
16
 
20
17
/* This file can be parametrized with the following macros:
21
18
     VASNPRINTF         The name of the function being defined.
278
275
{
279
276
  const char *point;
280
277
  /* Determine it in a multithread-safe way.  We know nl_langinfo is
281
 
     multithread-safe on glibc systems and MacOS X systems, but is not required
 
278
     multithread-safe on glibc systems and Mac OS X systems, but is not required
282
279
     to be multithread-safe by POSIX.  sprintf(), however, is multithread-safe.
283
280
     localeconv() is rarely multithread-safe.  */
284
281
#  if HAVE_NL_LANGINFO && (__GLIBC__ || defined __UCLIBC__ || (defined __APPLE__ && defined __MACH__))
555
552
      size_t s;
556
553
      {
557
554
        mp_limb_t msd = b_ptr[b_len - 1]; /* = b[n-1], > 0 */
558
 
        s = 31;
559
 
        if (msd >= 0x10000)
560
 
          {
561
 
            msd = msd >> 16;
562
 
            s -= 16;
563
 
          }
564
 
        if (msd >= 0x100)
565
 
          {
566
 
            msd = msd >> 8;
567
 
            s -= 8;
568
 
          }
569
 
        if (msd >= 0x10)
570
 
          {
571
 
            msd = msd >> 4;
572
 
            s -= 4;
573
 
          }
574
 
        if (msd >= 0x4)
575
 
          {
576
 
            msd = msd >> 2;
577
 
            s -= 2;
578
 
          }
579
 
        if (msd >= 0x2)
580
 
          {
581
 
            msd = msd >> 1;
582
 
            s -= 1;
583
 
          }
 
555
        /* Determine s = GMP_LIMB_BITS - integer_length (msd).
 
556
           Code copied from gnulib's integer_length.c.  */
 
557
# if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
 
558
        s = __builtin_clz (msd);
 
559
# else
 
560
#  if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
 
561
        if (GMP_LIMB_BITS <= DBL_MANT_BIT)
 
562
          {
 
563
            /* Use 'double' operations.
 
564
               Assumes an IEEE 754 'double' implementation.  */
 
565
#   define DBL_EXP_MASK ((DBL_MAX_EXP - DBL_MIN_EXP) | 7)
 
566
#   define DBL_EXP_BIAS (DBL_EXP_MASK / 2 - 1)
 
567
#   define NWORDS \
 
568
     ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
 
569
            union { double value; unsigned int word[NWORDS]; } m;
 
570
 
 
571
            /* Use a single integer to floating-point conversion.  */
 
572
            m.value = msd;
 
573
 
 
574
            s = GMP_LIMB_BITS
 
575
                - (((m.word[DBL_EXPBIT0_WORD] >> DBL_EXPBIT0_BIT) & DBL_EXP_MASK)
 
576
                   - DBL_EXP_BIAS);
 
577
          }
 
578
        else
 
579
#   undef NWORDS
 
580
#  endif
 
581
          {
 
582
            s = 31;
 
583
            if (msd >= 0x10000)
 
584
              {
 
585
                msd = msd >> 16;
 
586
                s -= 16;
 
587
              }
 
588
            if (msd >= 0x100)
 
589
              {
 
590
                msd = msd >> 8;
 
591
                s -= 8;
 
592
              }
 
593
            if (msd >= 0x10)
 
594
              {
 
595
                msd = msd >> 4;
 
596
                s -= 4;
 
597
              }
 
598
            if (msd >= 0x4)
 
599
              {
 
600
                msd = msd >> 2;
 
601
                s -= 2;
 
602
              }
 
603
            if (msd >= 0x2)
 
604
              {
 
605
                msd = msd >> 1;
 
606
                s -= 1;
 
607
              }
 
608
          }
 
609
# endif
584
610
      }
585
611
      /* 0 <= s < GMP_LIMB_BITS.
586
612
         Copy b, shifting it left by s bits.  */
887
913
  y = frexpl (x, &exp);
888
914
  if (!(y >= 0.0L && y < 1.0L))
889
915
    abort ();
890
 
  /* x = 2^exp * y = 2^(exp - LDBL_MANT_BIT) * (y * LDBL_MANT_BIT), and the
 
916
  /* x = 2^exp * y = 2^(exp - LDBL_MANT_BIT) * (y * 2^LDBL_MANT_BIT), and the
891
917
     latter is an integer.  */
892
 
  /* Convert the mantissa (y * LDBL_MANT_BIT) to a sequence of limbs.
 
918
  /* Convert the mantissa (y * 2^LDBL_MANT_BIT) to a sequence of limbs.
893
919
     I'm not sure whether it's safe to cast a 'long double' value between
894
920
     2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
895
921
     'long double' values between 0 and 2^16 (to 'unsigned int' or 'int',
975
1001
  y = frexp (x, &exp);
976
1002
  if (!(y >= 0.0 && y < 1.0))
977
1003
    abort ();
978
 
  /* x = 2^exp * y = 2^(exp - DBL_MANT_BIT) * (y * DBL_MANT_BIT), and the
 
1004
  /* x = 2^exp * y = 2^(exp - DBL_MANT_BIT) * (y * 2^DBL_MANT_BIT), and the
979
1005
     latter is an integer.  */
980
 
  /* Convert the mantissa (y * DBL_MANT_BIT) to a sequence of limbs.
 
1006
  /* Convert the mantissa (y * 2^DBL_MANT_BIT) to a sequence of limbs.
981
1007
     I'm not sure whether it's safe to cast a 'double' value between
982
1008
     2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
983
1009
     'double' values between 0 and 2^16 (to 'unsigned int' or 'int',
2787
2813
                  if (has_width)
2788
2814
                    {
2789
2815
#  if ENABLE_UNISTDIO
2790
 
                      /* Outside POSIX, it's preferrable to compare the width
 
2816
                      /* Outside POSIX, it's preferable to compare the width
2791
2817
                         against the number of _characters_ of the converted
2792
2818
                         value.  */
2793
2819
                      w = DCHAR_MBSNLEN (result + length, characters);
4858
4884
                   in format strings in writable memory may crash the program
4859
4885
                   (if compiled with _FORTIFY_SOURCE=2), so we should avoid it
4860
4886
                   in this situation.  */
4861
 
                /* On native Win32 systems (such as mingw), we can avoid using
 
4887
                /* On native Windows systems (such as mingw), we can avoid using
4862
4888
                   %n because:
4863
4889
                     - Although the gl_SNPRINTF_TRUNCATION_C99 test fails,
4864
4890
                       snprintf does not write more than the specified number
4867
4893
                     - Although the gl_SNPRINTF_RETVAL_C99 test fails, snprintf
4868
4894
                       allows us to recognize the case of an insufficient
4869
4895
                       buffer size: it returns -1 in this case.
4870
 
                   On native Win32 systems (such as mingw) where the OS is
 
4896
                   On native Windows systems (such as mingw) where the OS is
4871
4897
                   Windows Vista, the use of %n in format strings by default
4872
4898
                   crashes the program. See
4873
4899
                     <http://gcc.gnu.org/ml/gcc/2007-06/msg00122.html> and
5390
5416
                      {
5391
5417
                        size_t w;
5392
5418
# if ENABLE_UNISTDIO
5393
 
                        /* Outside POSIX, it's preferrable to compare the width
 
5419
                        /* Outside POSIX, it's preferable to compare the width
5394
5420
                           against the number of _characters_ of the converted
5395
5421
                           value.  */
5396
5422
                        w = DCHAR_MBSNLEN (result + length, count);