5 ms·
But that seems obvious. You can't load an integer from an unaligned address. It's not only C-level is it. There's no (guarantee across architectures for) machi
by tovej 5mo ago
But that seems obvious. You can't load an integer from an unaligned address.
It's not only C-level is it. There's no (guarantee across architectures for) machine code for that either.
- mbel 5mo agoUnless your code targets some exotic architecture, like idk x86.
- cataphract 5mo agoNot really. Wait until the compiler starts vectorizing your code and using instructions requiring alignment (like the ones with A or NT in the mnemonic).
- saagarjha 5mo agoUsually the compiler will probably not generate those
- bigfishrunning 5mo ago> Usually...probably... you're betting against the compiler ever improving.
- saagarjha 5mo agoThis would be a regression
- bigfishrunning 5mo agoWhy? Automatic vectorization is pretty bad and has been for years, but wouldn't it be nice if the compiler could unroll-loops and use SIMD instructions to make your code faster while also being correct?
- saagarjha 5mo agoIf you are using SIMD instructions on x86 you probably want the unaligned ones
- pjc50 5mo agoYou missed the point: the pointer existing as a value of that type at all is UB, even if you never try to access anything through it and no corresponding machine code is ever emitted.
- tovej 5mo agoYes? I agree with that. I don't really see the issue there. The computer will allocate data in aligned addresses, so you would have to be doing something weird to begin with to access unaligned pointers. And aligned access is always better anyway. I guess packed structs are a thing if you're really byte golfing. Maybe compressed network data would also make sense. But then I would assume you are aware of unaligned pointers, and have a sane way to parse that data, rather than read individual parts of it from a raw pointer. I am curious, what would be a legitimate reason for an unaligned pointer to int?
- simonask 5mo agoString search algorithms would be one example, where a 64-bit register can be used as a “vector” containing 8x1 bytes.
- jstimpfle 5mo agoWhere is the part about unaligned pointers?
- simonask 5mo agoStrings typically consist of UTF-8 bytes, and any old `char*` pair has no alignment guarantees.
- jstimpfle 5mo agoThat's true, and that's why your typical string vector code has a prelude and a postlude to do the incomplete chunks at the ends. Between the ends, it's processing larger self-aligned chunks.
- codeflo 5mo ago> You can't load an integer from an unaligned address. You can, and the results are machine specific, clearly defined and well-documented. Ancient ARM raises an exception, modern ARM and x86 can do it with a performance penalty. It's only the C or C++ layer that is allowed to translate the code into arbitrary garbage, not the CPU.
- matheusmoreira 5mo agoSure you can. In many architectures it works just fine. Works perfectly in x86_64, for example. It's just a little slower.
- tovej 5mo agoIn many architectures does not mean you can. The standard is supposed to cover all architectures.
- matheusmoreira 5mo agoIf some architecture traps on unaligned access, then the compiler can and should simply generate the correct code so that it loads the integer piece by piece instead. Load multiple integers and shift and mask away the irrelevant bits, done. This is exactly what modern architectures already do in hardware. Works, it's just a little slower. This is exactly what the compilers do if you use a packed structure to access unaligned data. Works everywhere, as expected. Compilers have always known what to do, they just weren't doing it. C standard says no. The fact is the standard is garbage and the first thing every C programmer should learn is that they can and should ignore it. There is never any reason to wonder what the standard is supposed to do. The only thing that matters is what compilers actually do.
- bluGill 5mo agoThe pointer might be something you forced. The compiler needs to do the right thing but if you set the pointer to an unaligned address because you have information on the hardware you can get this undefined situation with nothing the compiler can do about it.
- matheusmoreira 5mo agoAny reason the hardware pointer can't be accessed via the packed structure? https://news.ycombinator.com/item?id=48205371 https://news.ycombinator.com/item?id=48205371