I tried to write constant-time if (x < 0) return -1; if (x > 0) return 1; return 0; in x86 asm. Compiler did better.https://twitter.com/RichFelker/status/709496169236398080 …
@stevecheckoway How do you write constant-time anything in C? The compiler is free to make transformations that might not be constant-time.
-
-
@RichFelker I wrote it just like that and the compiler optimized to, iirc, cmp, setc, movzx, mov, and cmov. Of course it could have branched -
@stevecheckoway@RichFelker You can do better than that: http://goo.gl/tUNyMT -
@sevenps@stevecheckoway@RichFelker oops, forgot to initialize r: http://goo.gl/rjshrI -
@sevenps@RichFelker Definitely shorter than clang's, but that has a partial register update. Will that lead to a stall before the or? -
@stevecheckoway@RichFelker I don't think so, not in recent chips anyway. In any case, the dependency chain is much longer in clang. -
@sevenps@RichFelker I'll take your word for it. I have to use IACA to figure this stuff out. -
@stevecheckoway@RichFelker Running each snippet in a self-feeding loop gives 3 cycle latency for f, 4 for g (Haswell). Expected 5 for g.
End of conversation
New conversation -
-
-
@stevecheckoway And even if the asm looks constant-time the cpu/cache/HT/etc. may do things that make it non-constant-time.Thanks. Twitter will use this to make your timeline better. UndoUndo
-
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.