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 26, 2021

Motivation

Memory alignment cannot be dynamically determined in a standard manner, since the standard does not define how the casting from a pointer to integer shall be implemented.

§ 6.3.2.3 Pointers - ISO/IEC 9899:2018

  1. Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.

cast operator - cppreference.com

  • Any pointer type can be cast to any integer type. The result is implementation-defined, even for null pointer values (they do not necessarily result in the value zero). If the result cannot be represented in the target type, the behavior is undefined (unsigned integers do not implement modulo arithmetic on a cast from pointer).

How to allocate aligned memory only using the standard library? - Stack Overflow

Unfortunately, in C99 it seems pretty tough to guarantee alignment of any sort in a way which would be portable across any C implementation conforming to C99. Why? Because a pointer is not guaranteed to be the "byte address" one might imagine with a flat memory model. Neither is the representation of uintptr_t so guaranteed, which itself is an optional type anyway.

Can I determine whether a pointer is aligned in a fully standard manner? - Reddit

But yes, you are right, there is no standard compliant way to check if a pointer is aligned. If you need to align an object that might be misaligned, you can memcpy it into a malloc-ed buffer.

@dlOuOlb
Copy link
Author

dlOuOlb commented Jan 27, 2021

Modify the functions' name, scope, signature, specification, implementation, or anything as you want. #

uMyth_

int uMyth_( void );

Testing whether an integer, casted from a pointer, shows an alignment and a size as expected:

  • Returns
    • int - 1 for true, 0 for false.
  • Remarks
    • Note that the returned value could vary for each compiler, or even for each call.
    • The implementation has same meaning as below, where the type of SArray is size_t[ 2 ]:
     ( /*--------*/ 0 == /*---*/ alignof(size_t) & ( alignof(size_t) - 1 ) ) &&
     ( /*--------*/ 0 == /*----*/ sizeof(size_t) & ( alignof(size_t) - 1 ) ) &&
     ( /*--------*/ 0 == (size_t) ( SArray + 0 ) & ( alignof(size_t) - 1 ) ) &&
     ( sizeof(size_t) == (size_t) ( SArray + 1 ) - (size_t) ( SArray + 0 ) );

@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