3 ms·
Apropos of nothing, that is a horrible use of goto. The code would be clearer as a do { } while(n); instead--it's just a bottom-tested loop, which isn't that u
by jcranmer 3y ago
Apropos of nothing, that is a horrible use of goto.
The code would be clearer as a do { } while(n); instead--it's just a bottom-tested loop, which isn't that uncommon. (I guess the author may just be unfamiliar with do/while loops?)
- deleted 3y ago[deleted]
- eesmith 3y agoI came to say something similar, though I would use: while (1) { ... if (!n) { ... return 1 - ab * rsqrt_a2 * rsqrt_b2; } } Also, the "So let’s use the common Pythonic zip idiom:" version is missing the sqrt calls so doesn't give the same answer as the first version.
- secondcoming 3y agoDo you really think it’s likely that someone who uses SIMD intrinsic is unfamiliar with do…while loops? It’s more likely that compilers generate better autovectorised code with a goto, even though they’re semantically the same.
- LoganDark 3y ago> Do you really think it’s likely that someone who uses SIMD intrinsic is unfamiliar with do…while loops? You don't have to think them unfamiliar to recommend it. Sometimes very skilled people can forget to apply a particular style of control flow, even if they're otherwise experts - everybody makes mistakes.
- ashvardanian 3y agoI've just wasted a day of benchmarks, because in one of my major ongoing refactoring efforts a compilation flag wasn't propagated to the needed target - so I definitely make a lot of mistakes :) As for `do`/`while` keywords, its just a preference, but feel free to recommend more patches - I'm always happy to see new contributors - it's a luxury in low-level projects :)
- jcranmer 3y ago> It’s more likely that compilers generate better autovectorised code with a goto, even though they’re semantically the same. No, it's not. Most compiler IRs will work on a basic block representation, where the compiler will reduce all control flow to unconditional and conditional jumps. If they're not working on that level, then they likely have some form of structure control flow, where autovectorization will ignore gotos entirely because they're the definition of unstructured control flow, so they won't notice when the goto forms a loop that could be autovectorized.
- secondcoming 3y agoMaybe, but in my experience gcc is, and clang to a lesser extent, is notoriously useless at autovectorisation unless you hold its hand all the way. Even things like changing an ‘int’ to a ‘bool’ can throw it off completely.
- ashvardanian 3y agoThat matches my experience, and goes beyond GCC and Clang. Between 2018 and 2020 I was giving a lot of lectures on this topic and we did a bunch of case studies with Intel on their older ICC and what later became the OneAPI. Short story, unless you are doing trivial data-parallel operations, like in SimSIMD, compilers are practically useless. As a proof, I wrote what is now the StringZilla library (https://github.com/ashvardanian/stringzilla https://github.com/ashvardanian/stringzilla) and we've spent weeks with an Intel team, tuning the compiler, no result. So if you are processing a lot of strings, or variable-length coded data, like compression/decompression, hand-written SIMD kernels are pretty much unbeatable.
- ashvardanian 3y agoThere are several `do {} while ()` loops in the SimSIMD codebase, mostly in SVE [1]. One of the reasons I don't like putting them in many places - mostly in cases with complex conditions, but also to avoid adding one more level of nesting for that scope. [1]: https://github.com/ashvardanian/SimSIMD/blob/f8ff727dcddcd142e7e8dece33c43801af96c210/include/simsimd/binary.h#L118 https://github.com/ashvardanian/SimSIMD/blob/f8ff727dcddcd14...