Swift review splits on how to spell relaxed floating-point math
SE-0567 would let programmers trade strict floating-point rules for speed, but reviewers disagree on flags versus new operators.
Swift has opened review of a proposal for relaxed floating-point operators, and the argument is less about whether the language needs them than about how a programmer should ask for them.
The operators would let addition and multiplication drop some of the usual guarantees of IEEE floating-point arithmetic, licensing optimizations such as reassociation and contraction into a fused multiply-add. That can be substantially faster, and sometimes more accurate, but the results are not the same as strict evaluation and can differ by platform. The spelling under discussion reuses the ampersand operators Swift already uses for wrapping integer arithmetic. Review manager John McCall opened the review on November 17.
One suggestion was a compile-time switch in the style of the C flag for fast math, applied from the project down to a single file. Jordan Rose noted that Swift does not support per-file compilation flags, and preferred a function attribute or a macro block.
scanon was strongly opposed to flags or file scope. Copying a function into a file with a different default can turn a correct compensated sum into a result whose error term is always zero, or can make a norm more than an order of magnitude slower while still producing numbers that look fine in ordinary tests. "We should not use file- or module-level switches to control the behavior (there have been innumerable bugs introduced to C and C++ programs in exactly this manner)." Function- or block-scope modifiers are more palatable, because people tend to copy code at function granularity, but distinct operators are safer: the different semantics are visible at the use site.
Josh Osborne called the proposed spelling a trap. Programmers who learn that the ampersand form is somehow faster on floating-point types may start using it on integers, where it means wrapping arithmetic rather than trapping overflow, and "unlocked some unsafe C-style attacks against anything they write." He preferred a wrapper such as a relaxed initializer, or punctuation that does not collide with the integer operators. Florian Pircher defended the reuse: the same symbol can be read, for both integers and floats, as a less strict, more performant alternative, and ordinary addition already means different things on the two kinds of type. Osborne answered that Swift deliberately made integer addition trap so the unwary do not keep a value they will regret, while the ampersand forms are an attractive nuisance. One set should read as a security risk, the other as faster math with platform differences, and both look the same.
On vectors, reviewers asked whether floating-point SIMD types should get the same operators. scanon said yes for opportunistic fused multiply-add, though reassociation matters less once the code is already vectorized. A question about allowing that contraction while still trapping on overflow does not map onto floating-point as Swift implements it: overflow trapping is not expressed per operation, and existing instruction sets expose it, if at all, only through thread-local state.