Delphi Sytem单元 Set8087CW、Get8087CW、Default8087CW - FPU浮点值运算器
Delphi Sytem单元 Set8087CW、Get8087CW、Default8087CW - FPU浮点值运算器
FPU:浮点运算器,Floating Point Unit的简写,浮点运算单元 是专门用于浮点运算的处理器,集成于CPU之内,效率高、执行快 (以前的FPU是一种单独芯片,在486之后,英特尔把FPU集成在CPU之内。)
原型:
procedure Set8087CW(NewCW: Word);
begin
Default8087CW := NewCW;
asm
FNCLEX // don't raise pending exceptions enabled by the new flags
{$IFDEF PIC}
MOV EAX,[EBX].OFFSET Default8087CW
FLDCW [EAX]
{$ELSE}
FLDCW Default8087CW
{$ENDIF}
end;
end;
function Get8087CW: Word;
asm
PUSH 0
FNSTCW [ESP].Word
POP EAX
end;
Default8087CW: Word = $1332;{ Default 8087 control word. FPU control register is set to this value. 默认8087控制字。FPU控制寄存器设置为该值。
CAUTION: Setting this to an invalid value could cause unpredictable behavior. 警告:将此值设置为无效值可能会导致不可预知的行为。 }
示例:
Set8087CW($1372): //开FPU, 默认值(例外情况除外)为 $1372 =4978 $137F=4991 Set8087cw($133F): //闭FPU ,禁用所有FPU异常 $133F=4927 $1332 =4914 Get8087CW; //获取当前的FPU值 Default8087CW; //获取FPU的默认值,系统运行后,默认是开启,即4978
示例及测试:
//滔Roy 2020.12.14 var Form1: TForm1; Saved8087CW: Word; implementation {$R *.dfm} procedure TForm1.RadioGroup1Click(Sender: TObject); begin if Saved8087CW=0 then Exit; //防止没设定之前加载 ,否则报错: Invalid floating point operation. if RadioGroup1.Items[RadioGroup1.ItemIndex] = 'No FPU Exceptions' then //禁用后,不会抛出异常 结果:INF Set8087CW($133f); { Disable all fpu exceptions. 禁用所有fpu异常。 $133F=4927 } if RadioGroup1.Items[RadioGroup1.ItemIndex] = 'FPU Exceptions' then //FPU开启后,运算会抛出异常 Floating point division by zero Set8087CW(Saved8087CW); end; procedure TForm1.Button1Click(Sender: TObject); begin Edit3.Text := FloatToStr(StrToFloat(Edit1.Text) / StrToFloat(Edit2.Text)); end; procedure TForm1.FormCreate(Sender: TObject); begin RadioGroup1.Items.Add('No FPU Exceptions'); RadioGroup1.Items.Add('FPU Exceptions'); RadioGroup1.ItemIndex := 2; Saved8087CW := Default8087CW; // Save this because Set8087CW changes it. 保存此项是因为Set8087CW更改了它。 end; procedure TForm1.FormDestroy(Sender: TObject); begin Set8087CW(Saved8087CW); // Default value (with exceptions) is $1372. 默认值(例外情况除外)为 $1372 end;
创建时间:2020.12.14 更新时间:
博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你有所帮助,谢谢!