yann@1365: Original patch from: gentoo/src/patchsets/glibc/2.9/1020_all_glibc-2.9-strlen-hack.patch yann@1365: yann@1365: -= BEGIN original header =- yann@1365: http://sourceware.org/bugzilla/show_bug.cgi?id=5807 yann@1365: http://www.cl.cam.ac.uk/~am21/progtricks.html yann@1365: yann@1365: -= END original header =- yann@1365: yann@1365: diff -durN glibc-2_9.orig/string/strlen.c glibc-2_9/string/strlen.c yann@1365: --- glibc-2_9.orig/string/strlen.c 2005-12-14 12:09:07.000000000 +0100 yann@1365: +++ glibc-2_9/string/strlen.c 2009-02-02 22:00:51.000000000 +0100 yann@1365: @@ -32,7 +32,7 @@ yann@1365: { yann@1365: const char *char_ptr; yann@1365: const unsigned long int *longword_ptr; yann@1365: - unsigned long int longword, magic_bits, himagic, lomagic; yann@1365: + unsigned long int longword, himagic, lomagic; yann@1365: yann@1365: /* Handle the first few characters by reading one character at a time. yann@1365: Do this until CHAR_PTR is aligned on a longword boundary. */ yann@1365: @@ -42,28 +42,14 @@ yann@1365: if (*char_ptr == '\0') yann@1365: return char_ptr - str; yann@1365: yann@1365: - /* All these elucidatory comments refer to 4-byte longwords, yann@1365: - but the theory applies equally well to 8-byte longwords. */ yann@1365: - yann@1365: longword_ptr = (unsigned long int *) char_ptr; yann@1365: yann@1365: - /* Bits 31, 24, 16, and 8 of this number are zero. Call these bits yann@1365: - the "holes." Note that there is a hole just to the left of yann@1365: - each byte, with an extra at the end: yann@1365: - yann@1365: - bits: 01111110 11111110 11111110 11111111 yann@1365: - bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD yann@1365: - yann@1365: - The 1-bits make sure that carries propagate to the next 0-bit. yann@1365: - The 0-bits provide holes for carries to fall into. */ yann@1365: - magic_bits = 0x7efefeffL; yann@1365: himagic = 0x80808080L; yann@1365: lomagic = 0x01010101L; yann@1365: if (sizeof (longword) > 4) yann@1365: { yann@1365: /* 64-bit version of the magic. */ yann@1365: /* Do the shift in two steps to avoid a warning if long has 32 bits. */ yann@1365: - magic_bits = ((0x7efefefeL << 16) << 16) | 0xfefefeffL; yann@1365: himagic = ((himagic << 16) << 16) | himagic; yann@1365: lomagic = ((lomagic << 16) << 16) | lomagic; yann@1365: } yann@1365: @@ -75,56 +61,12 @@ yann@1365: if *any of the four* bytes in the longword in question are zero. */ yann@1365: for (;;) yann@1365: { yann@1365: - /* We tentatively exit the loop if adding MAGIC_BITS to yann@1365: - LONGWORD fails to change any of the hole bits of LONGWORD. yann@1365: - yann@1365: - 1) Is this safe? Will it catch all the zero bytes? yann@1365: - Suppose there is a byte with all zeros. Any carry bits yann@1365: - propagating from its left will fall into the hole at its yann@1365: - least significant bit and stop. Since there will be no yann@1365: - carry from its most significant bit, the LSB of the yann@1365: - byte to the left will be unchanged, and the zero will be yann@1365: - detected. yann@1365: - yann@1365: - 2) Is this worthwhile? Will it ignore everything except yann@1365: - zero bytes? Suppose every byte of LONGWORD has a bit set yann@1365: - somewhere. There will be a carry into bit 8. If bit 8 yann@1365: - is set, this will carry into bit 16. If bit 8 is clear, yann@1365: - one of bits 9-15 must be set, so there will be a carry yann@1365: - into bit 16. Similarly, there will be a carry into bit yann@1365: - 24. If one of bits 24-30 is set, there will be a carry yann@1365: - into bit 31, so all of the hole bits will be changed. yann@1365: - yann@1365: - The one misfire occurs when bits 24-30 are clear and bit yann@1365: - 31 is set; in this case, the hole at bit 31 is not yann@1365: - changed. If we had access to the processor carry flag, yann@1365: - we could close this loophole by putting the fourth hole yann@1365: - at bit 32! yann@1365: - yann@1365: - So it ignores everything except 128's, when they're aligned yann@1365: - properly. */ yann@1365: - yann@1365: longword = *longword_ptr++; yann@1365: yann@1365: - if ( yann@1365: -#if 0 yann@1365: - /* Add MAGIC_BITS to LONGWORD. */ yann@1365: - (((longword + magic_bits) yann@1365: - yann@1365: - /* Set those bits that were unchanged by the addition. */ yann@1365: - ^ ~longword) yann@1365: - yann@1365: - /* Look at only the hole bits. If any of the hole bits yann@1365: - are unchanged, most likely one of the bytes was a yann@1365: - zero. */ yann@1365: - & ~magic_bits) yann@1365: -#else yann@1365: - ((longword - lomagic) & himagic) yann@1365: -#endif yann@1365: - != 0) yann@1365: + /* This hack taken from Alan Mycroft's HAKMEMC postings. yann@1365: + See: http://www.cl.cam.ac.uk/~am21/progtricks.html */ yann@1365: + if (((longword - lomagic) & ~longword & himagic) != 0) yann@1365: { yann@1365: - /* Which of the bytes was the zero? If none of them were, it was yann@1365: - a misfire; continue the search. */ yann@1365: yann@1365: const char *cp = (const char *) (longword_ptr - 1); yann@1365: