4 ms·
What ahout FP registers? Is there special instructions to Zero them?
by billfruit 5y ago
What ahout FP registers? Is there special instructions to Zero them?
- woodruffw 5y agoFor x87, you can use FLDZ to load `+0.0` onto the register stack. My guess is that neither Intel nor AMD has tried particularly hard to optimize that, given x87's legacy status and weird register semantics (see: them being a pseudo-stack). For SIMD-based FP, you can use VZEROUPPER and VZEROALL if you want to clear all {X,Y,Z}MM state. For a single register, I believe PXOR is still the common idiom, mirroring `XOR EAX, EAX`.
- titzer 5y ago> For x87 Should read: for x87, unless you are doing this for fun, shoot yourself in the face and instead use SSE because no one in their right mind should be writing x87 code in 2021! Please help this architectural misfeature die, which should have happened decades ago.
- woodruffw 5y ago> Should read: for x87, unless you are doing this for fun, shoot yourself in the face and instead use SSE because no one in their right mind should be writing x87 code in 2021! My information is pretty old, but IIRC x87 still has specialized instructions for sin/cos/tan that are sometimes more performant than their equivalent implementations in SSE. x87 instructions are also very small, so trig-heavy workloads where I$ is a measurable performance component might unfortunately still be a good fit for x87.
- adrian_b 5y agoOn recent Intel/AMD CPUs the x87 transcedental functions are usually much slower than their equivalents using SSE/AVX instructions. For future CPUs, it is expected that the performance gap will increase. The x87 trigonometric functions require typically between 100 and 200 clock cycles. During that time a recent CPU can execute 200 to 500 instructions, enough to compute many values of a trigonometric function (using a polynomial approximation). When SIMD instructions can be used, several tens of values of a function could be computed during a single x87 instruction.
- lifthrasiir 5y agox87 trig instructions are pretty inaccurate even in the supported range mainly because of faulty range reduction [1] and can't be vectorized at all. If you have trig-heavy workloads nowadays you would want SIMD libm, not x87. [1] http://notabs.org/fpuaccuracy/ http://notabs.org/fpuaccuracy/
- cogman10 5y agoWhy can't the trig instructions be vectorized? I've never quiet understood why SSE/AVX didn't add trig functions to finally kill off any argument for using x87.
- adrian_b 5y agoUnlike simple operations like a floating-point multiplication, trigonometric functions and the other transcedental functions are too complex, so they must be split into many steps. If a trigonometric function is encoded as a single instruction, then it must launch a microprogram, to execute the many required steps. A microprogram cannot execute faster than when the same execution steps would have been encoded as separate instructions, the only advantage of encoding a trigonometric function in a single instruction would be to reduce the program size. Most programs contain few trigonometric functions, so the reduction in program size is not worthwhile. While a microprogrammed trigonometric function could be as fast as the equivalent sequence of instructions, in reality it is usually much slower. The reason is that the modern CPUs are optimized for the most frequent instructions and they dedicate a minimum of resources for the seldom used microprogrammed instructions, so these hit various limitations that do not exist for the simple instructions. The microprogrammed instructions usually have some phases whose execution cannot be overlapped in time with other instructions, which leads to lower performance.
- cogman10 5y agoGotcha, hence the reason they've added instructions like FMA. Those don't require microprograms but do make things like calculating a taylor series faster. Right?
- 5y ago
- Tuna-Fish 5y ago> My information is pretty old, but IIRC x87 still has specialized instructions for sin/cos/tan that are sometimes more performant than their equivalent implementations in SSE. They absolutely are not. If you accept the same, crappy precision, you can do an estimate in just a few cycles instead of the 60+ that the instructions take.
- gpderetta 5y agoFor some operations 80 bit accumulators are still useful, so a mix of SSE and x87 might be optimal.
- monocasa 5y agoLast time I checked, x87 (and MMX which uses the same register file) aren't renamed. There's only the architectural register file. At that point these sorts of concerns don't matter because you're not metaprogramming rename hardware through the veneer of the architectural registers.
- brucedawson 5y agoIf x87 and MMX registers don't support renaming then that means that they can't support OOO and speculative execution of these instructions. This is possible but seems unlikely to me. That is, even though not a lot of x87/MMX code is executed these days (and even less is written) I would be surprised if the OOO/speculative-execution of the processor gets halted whenever these instructions are encountered. Note that x87 instructions still get used in most 32-bit programs because the x87 registers are the defined way for functions to return floating-point results. Which is to say, citation needed.
- monocasa 5y agoIt looks like my information was woefully out of date. They're fully renamed and executed OoO now, and on Intel even share a PRF with AVX-512 K registers interestingly enough. Thanks for keeping me honest.
- Dylan16807 5y ago> That is, even though not a lot of x87/MMX code is executed these days (and even less is written) I would be surprised if the OOO/speculative-execution of the processor gets halted whenever these instructions are encountered. The implications aren't that bad. Only instructions using the outputs of x87/MMX instructions would have to stall. And especially if you're only using them to shift values during function returns, then that wouldn't have to have a huge impact on performance.
- gpderetta 5y agoThe x87 register stack has been renamed on Intel desktop/server OoO cpus since the original Pentium Pro, by special handilng of the fxch instruction. If anything, AMD improved its handling on recent Zens. Take a look at Agner manuals. It is plausible it will be dropped at some point but we are not there yet. [1] newer atoms, although a