[Bug libc/14455] roundup macro bug
johan.bolmsjo at gmail dot com
sourceware-bugzilla@sourceware.org
Fri Jun 16 12:37:00 GMT 2017
https://sourceware.org/bugzilla/show_bug.cgi?id=14455
Johan Bolmsjö <johan.bolmsjo at gmail dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |johan.bolmsjo at gmail dot com
--- Comment #2 from Johan Bolmsjö <johan.bolmsjo at gmail dot com> ---
// The roundup macro from /usr/include/sys/param.h (misc/sys/param.h in
// git repository) produce different results in the GCC and non-GCC
// versions for some input. This is why I would consider this a bug.
// Also only the fast path of the macro contains the "bug" so it
// will produce different results based on if y is a constant or not.
// Compile this program using:
// gcc -std=c99 -O2 -Wall -g -o roundup_glibc_bug roundup_glibc_bug.c
/*
Code block in sys/param.h looks like this:
#ifdef __GNUC__
# define roundup(x, y) (__builtin_constant_p (y) && powerof2 (y) \
? (((x) + (y) - 1) & ~((y) - 1)) \
: ((((x) + ((y) - 1)) / (y)) * (y)))
#else
# define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
#endif
*/
// Both macros has been made available with _gcc and _non_gcc postfix to
// be able to illustrate the issue.
# define roundup_gcc(x, y) (__builtin_constant_p (y) && powerof2 (y) \
? (((x) + (y) - 1) & ~((y) - 1)) \
: ((((x) + ((y) - 1)) / (y)) * (y)))
# define roundup_non_gcc(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
#define powerof2(x) ((((x) - 1) & (x)) == 0)
#include <inttypes.h>
#include <stdio.h>
int main()
{
uint64_t value = 0x100000003;
const unsigned multiple = 8;
printf("gcc roundup(0x%"PRIx64", %u) => %"PRIx64"\n",
value, multiple, roundup_gcc(value, multiple));
printf("non-gcc roundup(0x%"PRIx64", %u) => %"PRIx64"\n",
value, multiple, roundup_non_gcc(value, multiple));
uint32_t value2 = 0x000000003;
const uint64_t multiple2 = 0x100000000;
printf("gcc roundup(%u, 0x%"PRIx64") => %"PRIx64"\n",
value2, multiple2, roundup_gcc(value2, multiple2));
printf("non-gcc roundup(%u, 0x%"PRIx64") => %"PRIx64"\n",
value2, multiple2, roundup_non_gcc(value2, multiple2));
// Output:
// gcc roundup(0x100000003, 8) => 8
// non-gcc roundup(0x100000003, 8) => 100000008
// gcc roundup(3, 0x100000000) => 100000000
// non-gcc roundup(3, 0x100000000) => 100000000
return 0;
}
--
You are receiving this mail because:
You are on the CC list for the bug.
More information about the Glibc-bugs
mailing list