3 ms·
C99 and later provide the restrict keyword which tells the compiler that the arrays do not overlap. It pretty much solves the whole issue.
by sharpneli 13y ago
C99 and later provide the restrict keyword which tells the compiler that the arrays do not overlap.
It pretty much solves the whole issue.
- pcwalton 13y agoThat's true, although Rust provides for sound enforcement (so if you try to alias the equivalent of two restrict pointers the compiler will catch it at compile time). (It's also not technically in C++, although in practice it can be achieved through extensions.)
- pbsd 13y agoC++'s standard library has valarray, which is supposed to avoid aliasing effects, and is allowed to use expression templates and other tricks. Something like: float dot(std::valarray<float> const& x) { return (x*x).sum(); // Alternative: inner_product(begin(x), end(x), begin(x), 0.0f); } should, in theory, achieve the same kind of performance as the C version (but none of this is guaranteed, sadly). A smart implementation would actually end up calling BLAS routines, which is what libraries like Eigen do. Which reminds me: does Rust have infrastructure that would allow something like expression templates to exist?
- pcwalton 13y agoI believe you can use Rust traits to the same effect, although I haven't tried so you might hit stuff.
- noselasd 13y agoAnd before that, the alias issue in C compilers was one of the reasons why number crunching in Fortran could outperform C.