xdiff: import new 32-bit version of count_masked_bytes()

Import the latest 32-bit implementation of count_masked_bytes() from Linux (arch/x86/include/asm/word-at-a-time.h). It's shorter and avoids overflows and negative numbers. This fixes test failures on 32-bit, where negative partial results had been shifted right using the "wrong" method (logical shift right instead of arithmetic short right). The compiler is free to chose the method, so it was only wrong in the sense that it didn't work as intended by us. Reported-by: Øyvind A. Holm <sunny@sunbase.org> Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed May 22, 2012 at 22:36 UTC 8072766cc6756e9218ea7c9433b097b696329407
1 file changed +5 -13
xdiff/xutils.c
+5 -13
@@ -309,19 +309,11 @@ static inline long count_masked_bytes(unsigned long mask)
309 long a = (REPEAT_BYTE(0x01) / 0xff + 1);
310 return mask * a >> (sizeof(long) * 7);
311 } else {
312 - /*
313 - * Modified Carl Chatfield G+ version for 32-bit *
314 - *
315 - * (a) gives us
316 - * -1 (0, ff), 0 (ffff) or 1 (ffffff)
317 - * (b) gives us
318 - * 0 for 0, 1 for (ff ffff ffffff)
319 - * (a+b+1) gives us
320 - * correct 0-3 bytemask count result
321 - */
322 - long a = (mask - 256) >> 23;
323 - long b = mask & 1;
324 - return a + b + 1;
312 + /* Carl Chatfield / Jan Achrenius G+ version for 32-bit */
313 + /* (000000 0000ff 00ffff ffffff) -> ( 1 1 2 3 ) */
314 + long a = (0x0ff0001 + mask) >> 23;
315 + /* Fix the 1 for 00 case */
316 + return a & mask;
317 }
318 }
319