patches/glibc/2_9/210-2.9-strlen-hack.patch
branchnewlib
changeset 1365 c4d124ed9f8e
parent 1364 9227d2a2c080
child 1366 5e5d1e6f55d3
     1.1 --- a/patches/glibc/2_9/210-2.9-strlen-hack.patch	Sat Apr 11 19:03:02 2009 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,109 +0,0 @@
     1.4 -Original patch from: gentoo/src/patchsets/glibc/2.9/1020_all_glibc-2.9-strlen-hack.patch
     1.5 -
     1.6 --= BEGIN original header =-
     1.7 -http://sourceware.org/bugzilla/show_bug.cgi?id=5807
     1.8 -http://www.cl.cam.ac.uk/~am21/progtricks.html
     1.9 -
    1.10 --= END original header =-
    1.11 -
    1.12 -diff -durN glibc-2_9.orig/string/strlen.c glibc-2_9/string/strlen.c
    1.13 ---- glibc-2_9.orig/string/strlen.c	2005-12-14 12:09:07.000000000 +0100
    1.14 -+++ glibc-2_9/string/strlen.c	2009-02-02 22:00:51.000000000 +0100
    1.15 -@@ -32,7 +32,7 @@
    1.16 - {
    1.17 -   const char *char_ptr;
    1.18 -   const unsigned long int *longword_ptr;
    1.19 --  unsigned long int longword, magic_bits, himagic, lomagic;
    1.20 -+  unsigned long int longword, himagic, lomagic;
    1.21 - 
    1.22 -   /* Handle the first few characters by reading one character at a time.
    1.23 -      Do this until CHAR_PTR is aligned on a longword boundary.  */
    1.24 -@@ -42,28 +42,14 @@
    1.25 -     if (*char_ptr == '\0')
    1.26 -       return char_ptr - str;
    1.27 - 
    1.28 --  /* All these elucidatory comments refer to 4-byte longwords,
    1.29 --     but the theory applies equally well to 8-byte longwords.  */
    1.30 --
    1.31 -   longword_ptr = (unsigned long int *) char_ptr;
    1.32 - 
    1.33 --  /* Bits 31, 24, 16, and 8 of this number are zero.  Call these bits
    1.34 --     the "holes."  Note that there is a hole just to the left of
    1.35 --     each byte, with an extra at the end:
    1.36 --
    1.37 --     bits:  01111110 11111110 11111110 11111111
    1.38 --     bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
    1.39 --
    1.40 --     The 1-bits make sure that carries propagate to the next 0-bit.
    1.41 --     The 0-bits provide holes for carries to fall into.  */
    1.42 --  magic_bits = 0x7efefeffL;
    1.43 -   himagic = 0x80808080L;
    1.44 -   lomagic = 0x01010101L;
    1.45 -   if (sizeof (longword) > 4)
    1.46 -     {
    1.47 -       /* 64-bit version of the magic.  */
    1.48 -       /* Do the shift in two steps to avoid a warning if long has 32 bits.  */
    1.49 --      magic_bits = ((0x7efefefeL << 16) << 16) | 0xfefefeffL;
    1.50 -       himagic = ((himagic << 16) << 16) | himagic;
    1.51 -       lomagic = ((lomagic << 16) << 16) | lomagic;
    1.52 -     }
    1.53 -@@ -75,56 +61,12 @@
    1.54 -      if *any of the four* bytes in the longword in question are zero.  */
    1.55 -   for (;;)
    1.56 -     {
    1.57 --      /* We tentatively exit the loop if adding MAGIC_BITS to
    1.58 --	 LONGWORD fails to change any of the hole bits of LONGWORD.
    1.59 --
    1.60 --	 1) Is this safe?  Will it catch all the zero bytes?
    1.61 --	 Suppose there is a byte with all zeros.  Any carry bits
    1.62 --	 propagating from its left will fall into the hole at its
    1.63 --	 least significant bit and stop.  Since there will be no
    1.64 --	 carry from its most significant bit, the LSB of the
    1.65 --	 byte to the left will be unchanged, and the zero will be
    1.66 --	 detected.
    1.67 --
    1.68 --	 2) Is this worthwhile?  Will it ignore everything except
    1.69 --	 zero bytes?  Suppose every byte of LONGWORD has a bit set
    1.70 --	 somewhere.  There will be a carry into bit 8.  If bit 8
    1.71 --	 is set, this will carry into bit 16.  If bit 8 is clear,
    1.72 --	 one of bits 9-15 must be set, so there will be a carry
    1.73 --	 into bit 16.  Similarly, there will be a carry into bit
    1.74 --	 24.  If one of bits 24-30 is set, there will be a carry
    1.75 --	 into bit 31, so all of the hole bits will be changed.
    1.76 --
    1.77 --	 The one misfire occurs when bits 24-30 are clear and bit
    1.78 --	 31 is set; in this case, the hole at bit 31 is not
    1.79 --	 changed.  If we had access to the processor carry flag,
    1.80 --	 we could close this loophole by putting the fourth hole
    1.81 --	 at bit 32!
    1.82 --
    1.83 --	 So it ignores everything except 128's, when they're aligned
    1.84 --	 properly.  */
    1.85 --
    1.86 -       longword = *longword_ptr++;
    1.87 - 
    1.88 --      if (
    1.89 --#if 0
    1.90 --	  /* Add MAGIC_BITS to LONGWORD.  */
    1.91 --	  (((longword + magic_bits)
    1.92 --
    1.93 --	    /* Set those bits that were unchanged by the addition.  */
    1.94 --	    ^ ~longword)
    1.95 --
    1.96 --	   /* Look at only the hole bits.  If any of the hole bits
    1.97 --	      are unchanged, most likely one of the bytes was a
    1.98 --	      zero.  */
    1.99 --	   & ~magic_bits)
   1.100 --#else
   1.101 --	  ((longword - lomagic) & himagic)
   1.102 --#endif
   1.103 --	  != 0)
   1.104 -+      /* This hack taken from Alan Mycroft's HAKMEMC postings.
   1.105 -+         See: http://www.cl.cam.ac.uk/~am21/progtricks.html */
   1.106 -+      if (((longword - lomagic) & ~longword & himagic) != 0)
   1.107 - 	{
   1.108 --	  /* Which of the bytes was the zero?  If none of them were, it was
   1.109 --	     a misfire; continue the search.  */
   1.110 - 
   1.111 - 	  const char *cp = (const char *) (longword_ptr - 1);
   1.112 -