Skip to content

Instantly share code, notes, and snippets.

@dlOuOlb
Last active January 27, 2021 06:12
Show Gist options
  • Save dlOuOlb/e949cf6fd70b96d21a494e807474637f to your computer and use it in GitHub Desktop.
Save dlOuOlb/e949cf6fd70b96d21a494e807474637f to your computer and use it in GitHub Desktop.
Testing a myth that an integer, casted from a pointer, can be used to determine its alignment.
#if __STDC__
#else
# error A standard C compiler is required!
#endif
#include "conf.h"
#include <stddef.h> /* size_t; offsetof. */
extern int uMyth_( void )
{
typedef const struct { const char Trap; const size_t Gold; } BOX;
auto const size_t Rule[ 2 ] = { offsetof( BOX, Gold ), sizeof(size_t) };
register const size_t
ConA = ( Rule[ 0 ] - 1 ) & ( Rule[ 0 ] | Rule[ 1 ] | (size_t) ( Rule + 0 ) ),
ConS = ( Rule[ 1 ] - 0 ) ^ ( (size_t) ( Rule + 1 ) - (size_t) ( Rule + 0 ) );
return !( ConA | ConS );
}
#include "conf.h"
#include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS. */
extern int main( void )
{
extern int uMyth_( void );
return uMyth_( )? EXIT_SUCCESS: EXIT_FAILURE;
}
@dlOuOlb
Copy link
Author

dlOuOlb commented Jan 27, 2021

Compiler Tests

Special thanks to Compiler Explorer.


GCC

-Wall -Wextra -pedantic-errors -O2 -std=c89

x86-64 gcc 10.2
uMyth_:
        mov     eax, 1
        ret
ARM64 gcc 8.2
uMyth_:
        mov     w0, 1
        ret
RISC-V rv64gc gcc 8.2.0
uMyth_:
        li      a0,1
        ret
PowerPC64 gcc 6.3.0
uMyth_:
        li 3,1
        blr
        .long 0
        .byte 0,0,0,0,0,0,0,0
MIPS64 gcc 5.4
uMyth_:
        j       $31
        li      $2,1                        # 0x1
MSP430 gcc 6.2.1
uMyth_:
        MOV.B   #1, R12
        RET

Clang

-Wall -Wextra -pedantic-errors -O2 -std=c89

x86-64 clang 11.0.1
uMyth_:                                 # @uMyth_
        mov     eax, 1
        ret
armv8-a clang 11.0.1
uMyth_:                                 // @uMyth_
        mov     w0, #1
        ret
RISC-V rv64gc clang 11.0.1
uMyth_:                                 # @uMyth_
        addi    a0, zero, 1
        ret

MSVC

-WX -Wall -wd4820 -O2 -TC -Za

x64 msvc v19.14 (WINE)
Rule$ = 0
uMyth_  PROC                                                ; COMDAT
$LN4:
        sub     rsp, 24
        lea     rax, QWORD PTR Rule$[rsp]
        test    al, 7
        mov     eax, 0
        sete    al
        add     rsp, 24
        ret     0
uMyth_  ENDP
ARM64 msvc v19.14 (WINE)
        ;Flags[SingleProEpi] functionLength[24] RegF[0] RegI[0] H[0] frameChainReturn[UnChained] frameSize[16]

|uMyth_| PROC
|$LN4|
        sub         sp,sp,#0x10
        mov         x8,sp
        tst         x8,#7
        cseteq      w0
        add         sp,sp,#0x10
        ret

        ENDP  ; |uMyth_|

ICC

-Wall -Wextra -pedantic-errors -O2 -std=c89

x86-64 icc 19.0.1
uMyth_:
        mov       eax, 1                                        #20.19
        ret                                                     #20.19

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment