Last active
January 27, 2021 06:12
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if __STDC__ | |
#else | |
# error A standard C compiler is required! | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "conf.h" | |
#include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS. */ | |
extern int main( void ) | |
{ | |
extern int uMyth_( void ); | |
return uMyth_( )? EXIT_SUCCESS: EXIT_FAILURE; | |
} |
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
issize_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 ) );
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
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
cast operator - cppreference.com
How to allocate aligned memory only using the standard library? - Stack Overflow
Can I determine whether a pointer is aligned in a fully standard manner? - Reddit