pastojs: bigint shl/shr int

This commit is contained in:
mattias 2019-03-05 13:16:43 +00:00
parent 205431fb27
commit b376ebcd55
3 changed files with 39 additions and 25 deletions

View File

@ -569,6 +569,8 @@ type
pbifnAsExt,
pbifnBitwiseNativeIntAnd,
pbifnBitwiseNativeIntOr,
pbifnBitwiseNativeIntShl,
pbifnBitwiseNativeIntShr,
pbifnBitwiseNativeIntXor,
pbifnCheckMethodCall,
pbifnCheckVersion,
@ -728,6 +730,8 @@ const
'asExt', // rtl.asExt
'and', // pbifnBitwiseNativeIntAnd,
'or', // pbifnBitwiseNativeIntOr,
'shl', // pbifnBitwiseNativeIntShl,
'shr', // pbifnBitwiseNativeIntShr,
'xor', // pbifnBitwiseNativeIntXor,
'checkMethodCall',
'checkVersion',
@ -7050,10 +7054,17 @@ begin
FreeAndNil(B);
exit;
end;
// ToDo: BigInt shl const
end;
aResolver.LogMsg(20190228220225,mtWarning,nBitWiseOperationIs32Bit,
sBitWiseOperationIs32Bit,[],El);
// use rtl.shl(a,b)
Call:=CreateCallExpression(El);
Result:=Call;
if El.OpCode=eopShl then
Call.Expr:=CreateMemberExpression([GetBIName(pbivnRTL),GetBIName(pbifnBitwiseNativeIntShl)])
else
Call.Expr:=CreateMemberExpression([GetBIName(pbivnRTL),GetBIName(pbifnBitwiseNativeIntShr)]);
Call.AddArg(A); A:=nil;
Call.AddArg(B); B:=nil;
exit;
end;
end
else if (LeftResolved.BaseType=btCurrency) or (RightResolved.BaseType=btCurrency) then

View File

@ -265,7 +265,6 @@ type
Procedure TestIntegerTypecasts;
Procedure TestInteger_BitwiseShrNativeInt;
Procedure TestInteger_BitwiseShlNativeInt;
Procedure TestInteger_BitwiseShlNativeIntWarn;
Procedure TestCurrency;
Procedure TestForBoolDo;
Procedure TestForIntDo;
@ -6435,24 +6434,27 @@ begin
StartProgram(false);
Add([
'var',
' i: nativeint;',
' i,j: nativeint;',
'begin',
' i:=i shr 0;',
' i:=i shr 1;',
' i:=i shr 3;',
' i:=i shr 54;',
' i:=j shr i;',
'']);
ConvertProgram;
CheckResolverUnexpectedHints;
CheckSource('TestInteger_BitwiseShrNativeInt',
LinesToStr([
'this.i = 0;',
'this.j = 0;',
'']),
LinesToStr([
'$mod.i = $mod.i;',
'$mod.i = Math.floor($mod.i / 2);',
'$mod.i = Math.floor($mod.i / 8);',
'$mod.i = 0;',
'$mod.i = rtl.shr($mod.j, $mod.i);',
'']));
end;
@ -6466,6 +6468,7 @@ begin
' i:=i shl 0;',
' i:=i shl 54;',
' i:=123456789012 shl 1;',
' i:=i shl 1;',
'']);
ConvertProgram;
CheckResolverUnexpectedHints;
@ -6477,29 +6480,10 @@ begin
'$mod.i = $mod.i;',
'$mod.i = 0;',
'$mod.i = 246913578024;',
'$mod.i = rtl.shl($mod.i, 1);',
'']));
end;
procedure TTestModule.TestInteger_BitwiseShlNativeIntWarn;
begin
StartProgram(false);
Add([
'var',
' i: nativeint;',
'begin',
' i:=i shl 3;',
'']);
ConvertProgram;
CheckSource('TestInteger_BitwiseShlNativeIntWarn',
LinesToStr([
'this.i = 0;',
'']),
LinesToStr([
'$mod.i = $mod.i << 3;',
'']));
CheckHint(mtWarning,nBitWiseOperationIs32Bit,sBitWiseOperationIs32Bit);
end;
procedure TTestModule.TestCurrency;
begin
StartProgram(false);

View File

@ -26,6 +26,8 @@ var rtl = {
if (rtl.version != v) throw "expected rtl version "+v+", but found "+rtl.version;
},
hiInt: Math.pow(2,53),
hasString: function(s){
return rtl.isString(s) && (s.length>0);
},
@ -1082,6 +1084,23 @@ var rtl = {
return h*hi + l;
},
shr: function(a,b){
if (a<0) a += rtl.hiInt;
if (a<0x80000000) return a >> b;
if (b<=0) return a;
if (b>54) return 0;
return Math.floor(a / Math.pow(2,b));
},
shl: function(a,b){
if (a<0) a += rtl.hiInt;
if (b<=0) return a;
if (b>54) return 0;
var r = a * (2**b);
if (r <= rtl.hiInt) return r;
return r % rtl.hiInt;
},
initRTTI: function(){
if (rtl.debug_rtti) rtl.debug('initRTTI');