mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-04-16 23:21:57 +02:00
* Improved sign(x) functions to be branchless in most cases. Resolves #14206.
git-svn-id: trunk@33067 -
This commit is contained in:
parent
56b6cedcf4
commit
a4ed9f3b54
@ -638,35 +638,35 @@ end;
|
||||
function Sign(const AValue: Integer): TValueSign;inline;
|
||||
|
||||
begin
|
||||
If Avalue<0 then
|
||||
Result:=NegativeValue
|
||||
else If Avalue>0 then
|
||||
Result:=PositiveValue
|
||||
else
|
||||
Result:=ZeroValue;
|
||||
result:=TValueSign(
|
||||
SarLongint(AValue,sizeof(AValue)*8-1) or { gives -1 for negative values, 0 otherwise }
|
||||
(-AValue shr (sizeof(AValue)*8-1)) { gives 1 for positive values, 0 otherwise }
|
||||
);
|
||||
end;
|
||||
|
||||
function Sign(const AValue: Int64): TValueSign;inline;
|
||||
|
||||
begin
|
||||
{$ifdef cpu64}
|
||||
result:=TValueSign(
|
||||
SarInt64(AValue,sizeof(AValue)*8-1) or
|
||||
(-AValue shr (sizeof(AValue)*8-1))
|
||||
);
|
||||
{$else cpu64}
|
||||
If Avalue<0 then
|
||||
Result:=NegativeValue
|
||||
else If Avalue>0 then
|
||||
Result:=PositiveValue
|
||||
else
|
||||
Result:=ZeroValue;
|
||||
{$endif}
|
||||
end;
|
||||
|
||||
{$ifdef FPC_HAS_TYPE_SINGLE}
|
||||
function Sign(const AValue: Single): TValueSign;inline;
|
||||
|
||||
begin
|
||||
If Avalue<0.0 then
|
||||
Result:=NegativeValue
|
||||
else If Avalue>0.0 then
|
||||
Result:=PositiveValue
|
||||
else
|
||||
Result:=ZeroValue;
|
||||
Result:=ord(AValue>0.0)-ord(AValue<0.0);
|
||||
end;
|
||||
{$endif}
|
||||
|
||||
@ -674,24 +674,14 @@ end;
|
||||
function Sign(const AValue: Double): TValueSign;inline;
|
||||
|
||||
begin
|
||||
If Avalue<0.0 then
|
||||
Result:=NegativeValue
|
||||
else If Avalue>0.0 then
|
||||
Result:=PositiveValue
|
||||
else
|
||||
Result:=ZeroValue;
|
||||
Result:=ord(AValue>0.0)-ord(AValue<0.0);
|
||||
end;
|
||||
|
||||
{$ifdef FPC_HAS_TYPE_EXTENDED}
|
||||
function Sign(const AValue: Extended): TValueSign;inline;
|
||||
|
||||
begin
|
||||
If Avalue<0.0 then
|
||||
Result:=NegativeValue
|
||||
else If Avalue>0.0 then
|
||||
Result:=PositiveValue
|
||||
else
|
||||
Result:=ZeroValue;
|
||||
Result:=ord(AValue>0.0)-ord(AValue<0.0);
|
||||
end;
|
||||
{$endif}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user