Can I thrust in GCC doing the right thing when casting to a wider type in C?

Alexander Monakov amonakov@ispras.ru
Sat Aug 3 07:53:00 GMT 2019


On Sat, 3 Aug 2019, Aaron wrote:

> I am afraid LTO or other upcoming methods of code-improvementwill bring grief
> on us.
> Why should someone want do write such code? Answer: speed
> 
> Unfortunately, it is not valid C due to the effective type rules. (6.5
> Expression 7).

You are correct that such code is invalid and can be observably broken with LTO.

GCC supports the 'may_alias' type attribute to aid in writing such code. You can
refer to documentation at
https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html
and see how musl libc uses it in implementations of string functions:
(e.g. strlen)
http://git.musl-libc.org/cgit/musl/tree/src/string/strlen.c?id=4d0a82170a25464c39522d7190b9fe302045ddb2
(e.g. memmove)
http://git.musl-libc.org/cgit/musl/tree/src/string/memmove.c?id=4d0a82170a25464c39522d7190b9fe302045ddb2

(same for C++)

HTH.
Alexander



More information about the Gcc-help mailing list