Twitter | Search | |
Nick Lockwood 10 May 19
Swift has at least 3 different versions of `mod`, but AFAICT not a single one of them handles negative numbers the way I usually want when I use that function, i.e. so that -3 % 10 == 7 Is there a `wrapToRange()` function somewhere?
Reply Retweet Like
Joe Groff 10 May 19
% is more properly the remainder operator, since it gives the value that matches integer division (which rounds toward zero like C). I don't think there's a standard mod function in Swift or libc. cc
Reply Retweet Like
🆆il 🆂hipley, his arms wide 10 May 19
I wrote this and wish there were something like it in the standard library — I use it a million times more often than any other modulo function.
Reply Retweet Like
Joe Groff 10 May 19
It’s probably more efficient to do the add conditionally than two remainder operations (all the more reason for the stdlib to provide the correct and fast implementation)
Reply Retweet Like
🆆il 🆂hipley, his arms wide
I wish I knew more about optimizing on modern CPUs. It’s so hard to get decent info these days, compared to the Apple //e where they published exactly how many cycles every op took. Nowadays it seems like branches are always potentially super-slow...but you’re saying math slower?
Reply Retweet Like More
Joe Groff 10 May 19
In practice, most branches are predictable, and predicted branches are very fast
Reply Retweet Like
🆆il 🆂hipley, his arms wide 10 May 19
Thank you. Do you know if that’s true for GPUs as well?
Reply Retweet Like
Joe Groff 10 May 19
Int division is extra extra slow, to the point it’s worth branching to avoid it. A 64 bit division will even get optimized to branch and do a 32 bit division instead when the operands are small enough
Reply Retweet Like
🆆il 🆂hipley, his arms wide 10 May 19
Paradoxically one of the teams at Apple I’d most like to work on are the low-level assembly performance teams (simd and vImage and those).
Reply Retweet Like
Steve Canon 10 May 19
Intel publishes most of this data for their CPUs, and Agner Fog publishes what they don’t.
Reply Retweet Like
🆆il 🆂hipley, his arms wide 10 May 19
Danger Frog? Angry Fog? Fanged Grog?
Reply Retweet Like