public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "george.thopas at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/101925] New: reversed storage order when compiling with -O3 only
Date: Sun, 15 Aug 2021 23:43:34 +0000	[thread overview]
Message-ID: <bug-101925-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 101925
           Summary: reversed storage order when compiling with -O3 only
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: george.thopas at gmail dot com
  Target Milestone: ---

/*
     reversed storage order when compiling with -O3 only. 
     this time sets up an all big-endian struct 
     from an all little-endian one
     no warnings 

     Target: x86_64-pc-linux-gnu
     gcc versie 11.1.0 (Gentoo 11.1.0-r2 p3) 
     gcc-trunk too

     $ gcc -Wall -Wextra -O3 test.c 
     $ ./a.out 
     Abort 

 */


#define BIG_ENDIAN   __attribute__((scalar_storage_order("big-endian")))

/* host order version (little endian)*/
struct _ip6_addr {
    union {
        char addr8[16];
        int  addr32[4];
    } u;
};

typedef struct _ip6_addr t_ip6_addr;

struct _net_addr {
    char is_v4;
    union {
        int        addr;
        t_ip6_addr addr6;
    } u;
};

typedef struct _net_addr t_net_addr;

/* big endian version */
struct _be_ip6_addr {
    union {
        char addr8[16];
    } BIG_ENDIAN u;
} BIG_ENDIAN;

typedef struct _be_ip6_addr t_be_ip6_addr;

struct _be_net_addr {
    char is_v4;
    union {
        t_be_ip6_addr addr6;
        int           addr;
    } BIG_ENDIAN u;
} BIG_ENDIAN;

typedef struct _be_net_addr t_be_net_addr;

/* convert */
t_be_ip6_addr be_ip6_addr(const t_ip6_addr ip6)
{
    t_be_ip6_addr rc = {
        .u.addr8[0] = ip6.u.addr8[0],
        .u.addr8[1] = ip6.u.addr8[1],
        .u.addr8[2] = ip6.u.addr8[2],
        .u.addr8[3] = ip6.u.addr8[3],
        .u.addr8[4] = ip6.u.addr8[4],
        .u.addr8[5] = ip6.u.addr8[5],
        .u.addr8[6] = ip6.u.addr8[6],
        .u.addr8[7] = ip6.u.addr8[7],
        .u.addr8[8] = ip6.u.addr8[8],
        .u.addr8[9] = ip6.u.addr8[9],
        .u.addr8[10] = ip6.u.addr8[10],
        .u.addr8[11] = ip6.u.addr8[11],
        .u.addr8[12] = ip6.u.addr8[12],
        .u.addr8[13] = ip6.u.addr8[13],
        .u.addr8[14] = ip6.u.addr8[14],
        .u.addr8[15] = ip6.u.addr8[15],
    };
    return rc;
}

t_be_net_addr be_net_addr(const t_net_addr ip)
{
    t_be_net_addr rc = {.is_v4 = ip.is_v4 };
    if (ip.is_v4) {
        rc.u.addr = ip.u.addr;
    } else {
        rc.u.addr6 = be_ip6_addr(ip.u.addr6);
    }
    return rc;
}

int main(void)
{
    t_be_net_addr out = { };

    t_net_addr in = {
        .is_v4 = 0,
        .u.addr6.u.addr8 =
            { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }
    };

    out = be_net_addr(in);

    // actually first 4 bytes are swapped
    if (in.u.addr6.u.addr8[0] != out.u.addr6.u.addr8[0])
        __builtin_abort();

    return 0;
}

             reply	other threads:[~2021-08-15 23:43 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-15 23:43 george.thopas at gmail dot com [this message]
2021-08-15 23:50 ` [Bug tree-optimization/101925] " pinskia at gcc dot gnu.org
2021-08-16  8:08 ` [Bug tree-optimization/101925] [10/11/12 Regression] " marxin at gcc dot gnu.org
2021-08-16  8:22 ` [Bug tree-optimization/101925] [10/11/12 Regression] reversed storage order when compiling with -O3 only since r10-4742-g9b75f56d4b7951c6 rguenth at gcc dot gnu.org
2021-08-16  9:27 ` ebotcazou at gcc dot gnu.org
2021-08-16 10:45 ` rguenth at gcc dot gnu.org
2021-08-16 11:20 ` rguenth at gcc dot gnu.org
2021-08-16 12:20 ` ebotcazou at gcc dot gnu.org
2021-08-16 13:16 ` rguenther at suse dot de
2021-08-16 13:21 ` rguenth at gcc dot gnu.org
2021-08-17  7:25 ` cvs-commit at gcc dot gnu.org
2021-09-06  8:50 ` [Bug tree-optimization/101925] [10/11 " cvs-commit at gcc dot gnu.org
2021-10-13 11:09 ` [Bug tree-optimization/101925] [10 " cvs-commit at gcc dot gnu.org
2021-10-13 11:10 ` rguenth at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-101925-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).