Using SSE for rounding in Delphi

I wrote this function to round single numbers to integers:

function Round(const Val: Single): Integer;
begin
  asm
    cvtss2si eax,Val
    mov Result,eax
  end;
end;

This works, but I need to change the rounding mode. Apparently, behind this , I need to set the MXCSR register.

How to do it in Delphi?

The reason why I do this in the first place is the "rounding from zero" rounding (as in C #), which is not possible even with SetRoundingMode.

+4
source share
1 answer

In modern Delphi, you can call SetMXCSRfrom block to install MXCSR System. To read the current value, use GetMXCSR.

, SetMXCSR, Set8087CW, . Embarcadero , , .

Delphi LDMXCSR STMXCSR. :

function GetMXCSR: LongWord;
asm
  PUSH    EAX
  STMXCSR [ESP].DWord
  POP     EAX
end;

procedure SetMXCSR(NewMXCSR: LongWord);
//thread-safe version that does not abuse the global variable DefaultMXCSR
var
  MXCSR: LongWord;
asm
  AND     EAX, $FFC0 // Remove flag bits
  MOV     MXCSR, EAX
  LDMXCSR MXCSR
end;

, , Delphi.

, Round . .

, Intel, Intel (x87, SSE) , IEEE754. :

  • ()
  • ( -โˆž)
  • ( + โˆž)
  • ()

, .

+8

All Articles