Преглед на файлове

Partial JS Int64 fixes.

Refs issue 1532.
Bruno Garcia преди 12 години
родител
ревизия
c2f47c1ea7
променени са 2 файла, в които са добавени 39 реда и са изтрити 14 реда
  1. 13 13
      std/haxe/Int64.hx
  2. 26 1
      tests/unit/TestInt64.hx

+ 13 - 13
std/haxe/Int64.hx

@@ -31,7 +31,7 @@ class Int64 {
 		this.low = i32(low);
 	}
 
-	@:extern inline function i32(i) {
+	@:extern static inline function i32(i) {
 		#if (php || js || flash8)
 		return i | 0;
 		#else
@@ -85,8 +85,8 @@ class Int64 {
 	}
 
 	public static function add( a : Int64, b : Int64 ) : Int64 {
-		var high = a.high + b.high;
-		var low = a.low + b.low;
+		var high = i32(a.high + b.high);
+		var low = i32(a.low + b.low);
 		if( uicompare(low,a.low) < 0 )
 			high++;
 		return new Int64(high, low);
@@ -104,16 +104,16 @@ class Int64 {
 		var mask = 0xFFFF;
 		var al = a.low & mask, ah = a.low >>> 16;
 		var bl = b.low & mask, bh = b.low >>> 16;
-		var p00 = al * bl;
-		var p10 = ah * bl;
-		var p01 = al * bh;
-		var p11 = ah * bh;
+		var p00 = i32(al * bl);
+		var p10 = i32(ah * bl);
+		var p01 = i32(al * bh);
+		var p11 = i32(ah * bh);
 		var low = p00;
-		var high = p11 + (p01 >>> 16) + (p10 >>> 16);
-		p01 = (p01 << 16); low += p01; if( uicompare(low,p01) < 0 ) high++;
-		p10 = (p10 << 16); low += p10; if( uicompare(low,p10) < 0 ) high++;
-		high += a.low * b.high;
-		high += a.high * b.low;
+		var high = i32(p11 + i32((p01 >>> 16) + (p10 >>> 16)));
+		p01 = (p01 << 16); low = i32(low + p01); if( uicompare(low,p01) < 0 ) high = i32(high + 1);
+		p10 = (p10 << 16); low = i32(low + p10); if( uicompare(low,p10) < 0 ) high = i32(high + 1);
+		high = i32(high + i32(a.low * b.high));
+		high = i32(high + i32(a.high * b.low));
 		return new Int64(high, low);
 	}
 
@@ -221,4 +221,4 @@ class Int64 {
 		return a.toString();
 	}
 
-}
+}

+ 26 - 1
tests/unit/TestInt64.hx

@@ -21,4 +21,29 @@ class TestInt64 extends Test {
 		eq(Int64.ofInt(0).toStr(), "0");
 	}
 
-}
+	public function testMath() {
+		var a = Int64.make(0, 0x239B0E13);
+		var b = Int64.make(0, 0x39193D1B);
+		var c = Int64.mul(a, b);
+		eq( c.toStr(), "572248275467371265" );
+		// trace(Int64.toStr(c) + " should be 572248275467371265"); // but gives 7572248271172403969 in javascript
+
+		var a = Int64.make(0, 0xD3F9C9F4);
+		var b = Int64.make(0, 0xC865C765);
+		var c = Int64.mul(a, b);
+		eq( c.toStr(), "-6489849317865727676" );
+
+		var a = Int64.make(0, 0x9E370301);
+		var b = Int64.make(0, 0xB0590000);
+		var c = Int64.add(a, b);
+		eq( Int64.toStr(c), "5613028097" );
+
+		// FIXME: Failing unit test!
+		// var a = Int64.make(0xFFF21CDA, 0x972E8BA3);
+		// var b = Int64.make(0x0098C29B, 0x81000001);
+		// var c = Int64.mul(a, b);
+		// var expected = Int64.make(0xDDE8A2E8, 0xBA2E8BA3);
+		// eq( expected.compare(c), 0 );
+	}
+
+}