public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/94206] New: Wrong optimization: memset of n-bit integer types (from bit-fields) is truncated to n bits (instead of sizeof)
@ 2020-03-17 21:15 ch3root at openwall dot com
  2020-03-17 21:22 ` [Bug middle-end/94206] " pinskia at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: ch3root at openwall dot com @ 2020-03-17 21:15 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94206

            Bug ID: 94206
           Summary: Wrong optimization: memset of n-bit integer types
                    (from bit-fields) is truncated to n bits (instead of
                    sizeof)
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ch3root at openwall dot com
  Target Milestone: ---

Bit-fields of different widths have their own types in gcc. And `memset` seems
to be optimized strangely for them. Even at `-O0`!

This is not standard C due to the use of `typeof` but this topic is starting to
be more interesting in light of N2472 "Adding Fundamental Type for N-bit
Integers".

----------------------------------------------------------------------
#include <string.h>
#include <stdio.h>

struct {
    unsigned long x:33;
} s;
typedef __typeof__(s.x + 0) uint33;

int main()
{
    uint33 x;
    printf("sizeof = %zu\n", sizeof x);

    memset(&x, -1, sizeof x);

    unsigned long u;
    memcpy(&u, &x, sizeof u);
    printf("%lx\n", u);
}
----------------------------------------------------------------------
$ gcc -std=c11 -pedantic -Wall -Wextra test.c && ./a.out
sizeof = 8
1ffffffff
$ gcc -std=c11 -pedantic -Wall -Wextra -O3 test.c && ./a.out
sizeof = 8
1ffffffff
----------------------------------------------------------------------
gcc x86-64 version: gcc (GCC) 10.0.1 20200317 (experimental)
----------------------------------------------------------------------

The right result is `ffffffffffffffff`.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2020-04-15  9:59 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-17 21:15 [Bug middle-end/94206] New: Wrong optimization: memset of n-bit integer types (from bit-fields) is truncated to n bits (instead of sizeof) ch3root at openwall dot com
2020-03-17 21:22 ` [Bug middle-end/94206] " pinskia at gcc dot gnu.org
2020-03-18  8:12 ` rguenth at gcc dot gnu.org
2020-03-18 12:12 ` cvs-commit at gcc dot gnu.org
2020-03-18 12:13 ` rguenth at gcc dot gnu.org
2020-04-02 14:49 ` cvs-commit at gcc dot gnu.org
2020-04-15  9:59 ` rguenth at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).