printf function output differs by platform or ?

101 views
Skip to first unread message

qlyne...@gmail.com

unread,
Jul 9, 2020, 1:58:26 PM7/9/20
to Compiler Explorer Discussion
Hello fellow explorers 

I have come to notice what you may have long long ago. 

Comparing simple lines of code 

alignas(16) __uint128_t u128 = 72623859790382856; 
printf("%x\n", (int)u128 ); 
printf("%llx \n", u128 ); 
printf("%llx \n", &u128 ); 
printf("%llx address ? \n", (uint64_t*)&u128 ); 
printf("%llx \n", (uint64_t)u128 ); 

From locally, 
x86-64 gcc (8.3 | 10.1) -march=core2 -std=gnu++1z -dM -xc++ -O3 -mwindows -mconsole (windows 7 | 10), outputs 

5060708 
22fd80 
22fd90 
22fd90 address ? 
102030405060708 

, which are obviously output three memory pointer addresses, and two values, and are 
to the standard? on windows? 

From Compiler Explorer, ( -mwindows -mconsole not recognized) 
x86-64 gcc 10.1 -march=core2 -std=gnu++1z -dM -xc++ -O3 , outputs 

5060708 
102030405060708 
7ffeaa2fd530 
7ffeaa2fd530 address ? 
102030405060708 
( clang 10, icc 19 produced similar output as gcc on CE ) 

Second output line is one observed possible difference. 


I am assuming Compiler Explorer is Linux. 


Thanks. 

Matt Godbolt

unread,
Jul 9, 2020, 3:45:21 PM7/9/20
to compiler-explo...@googlegroups.com
Hi there!

Compiler Explorer is indeed linux under the hood. This group is mainly to discuss compiler explorer itself, not the output of the compilers, but we can of course try and help!

I'm not sure what your example is demonstrating here - you're printing the value of a (non-standard) __uint128_t in different ways, first the integer value, then as a u128 value (which I don't think is supported by printf?), then its address.

A u128 is too big to fit in a register so I suspect the `...` of the printf() passes the address value. Again, it's not a standard, so you might get almost anything. Sharing a CE link is always useful to make this easier but I made this: https://godbolt.org/z/69cTPb -- here you see that  the second call is passing the 128-bit value in two adjacent registers (rdx and rsi). I don't think printf() is expecting that.

So: I think what you're seeing is undefined behaviour in action! If you turn warnings on you'll see that GCC will tell you what you're doing is problematic: https://godbolt.org/z/fEYGYc

Hope that helps! :)

Cheers, Matt

--
You received this message because you are subscribed to the Google Groups "Compiler Explorer Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to compiler-explorer-di...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/compiler-explorer-discussion/d212c9b4-446b-4ac9-b06b-9d58b1c03a04o%40googlegroups.com.


--
Matt
Reply all
Reply to author
Forward
0 new messages