__builtin_assume_aligned semantics

Evan Nemerson evan@coeus-group.com
Tue Feb 14 05:47:00 GMT 2017


I'm trying to put together a macro to abstract away some compiler
differences, but I'm having trouble with __builtin_assume_aligned, and
I'm not sure how to generate a reliable test case.

For ICC there is __assume_aligned.  This is what I'm looking to
emulate; you just pass it a pointer and the desired alignment:

    __assume_aligned(arg, 16)
    /* Compiler knows arg is 16-byte aligned */

For MSVC there is __assume, which is a bit of a pain to use, but
basically you pass an expression (which evaluates to true if the
variable is aligned as expected).  Something like

    __assume((((char*) arg) - ((char*) 0)) % (16) == 0)
    /* Compiler knows arg is 16-byte aligned */

GCC 4.7+, OTOH, does things a bit different.  It returns a value, and
you're supposed to use the returned value:

    void* x = __builtin_assume_aligned(arg, 16);
    /* Compiler knows x is 16-byte aligned */

My question is, with __builtin_assume_aligned, does GCC know that *arg*
(not x) is 16-byte aligned?  Basically, can I use it like
__assume_aligned?

Also, for 4.5+ (when __builtin_unreachable was added), if I do
something like

    #define assume_aligned(arg, align) \
      ((((char*) ptr) - ((char*) 0)) % (align) == 0) ? \
        (0) : __builtin_unreachable()

Would GCC know that arg is aligned?


-Evan



More information about the Gcc-help mailing list