Explorar el Código

Pointer subtraction using stride instead of size

Brian Fiete hace 2 años
padre
commit
200bb6453c

+ 1 - 1
IDEHelper/Compiler/BfExprEvaluator.cpp

@@ -24256,7 +24256,7 @@ void BfExprEvaluator::PerformBinaryOperation(BfAstNode* leftExpression, BfAstNod
 				convLeftValue = mModule->CastToValue(leftExpression, leftValue, intPtrType, (BfCastFlags)(BfCastFlags_Explicit | BfCastFlags_FromCompiler));
 				convLeftValue = mModule->CastToValue(leftExpression, leftValue, intPtrType, (BfCastFlags)(BfCastFlags_Explicit | BfCastFlags_FromCompiler));
 				convRightValue = mModule->CastToValue(rightExpression, rightValue, intPtrType, (BfCastFlags)(BfCastFlags_Explicit | BfCastFlags_FromCompiler));
 				convRightValue = mModule->CastToValue(rightExpression, rightValue, intPtrType, (BfCastFlags)(BfCastFlags_Explicit | BfCastFlags_FromCompiler));
 				BfIRValue diffValue = mModule->mBfIRBuilder->CreateSub(convLeftValue, convRightValue);
 				BfIRValue diffValue = mModule->mBfIRBuilder->CreateSub(convLeftValue, convRightValue);
-				diffValue = mModule->mBfIRBuilder->CreateDiv(diffValue, mModule->GetConstValue(resultPointerType->mElementType->mSize, intPtrType), true);
+				diffValue = mModule->mBfIRBuilder->CreateDiv(diffValue, mModule->GetConstValue(resultPointerType->mElementType->GetStride(), intPtrType), true);
 				mResult = BfTypedValue(diffValue, intPtrType);
 				mResult = BfTypedValue(diffValue, intPtrType);
 			}
 			}
 			return;
 			return;

+ 15 - 0
IDEHelper/Tests/src/Loops.bf

@@ -134,9 +134,24 @@ namespace Tests
 			Test.Assert(!(1..<3).Contains(1..<4));
 			Test.Assert(!(1..<3).Contains(1..<4));
 
 
 			List<int> iList = scope .() { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
 			List<int> iList = scope .() { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
+			int itrCount = 0;
 			total = 0;
 			total = 0;
 			for (int i in iList[...])
 			for (int i in iList[...])
+			{
+				itrCount++;
+				total += i;
+			}
+			Test.Assert(itrCount == 10);
+			Test.Assert(total == 10+20+30+40+50+60+70+80+90+100);
+
+			total = 0;
+			itrCount = 0;
+			for (int i in iList[...].Reversed)
+			{
+				itrCount++;
 				total += i;
 				total += i;
+			}
+			Test.Assert(itrCount == 10);
 			Test.Assert(total == 10+20+30+40+50+60+70+80+90+100);
 			Test.Assert(total == 10+20+30+40+50+60+70+80+90+100);
 
 
 			total = 0;
 			total = 0;

+ 14 - 0
IDEHelper/Tests/src/Structs.bf

@@ -189,6 +189,20 @@ namespace Tests
 
 
 			StructN sn = GetStructN();
 			StructN sn = GetStructN();
 			Test.Assert(sn.mA == 123);
 			Test.Assert(sn.mA == 123);
+
+			List<StructD> list = scope .();
+			for (int i < 10)
+				list.Add(default);
+			var ptr0 = &list[0];
+			var ptr9 = &list[9];
+
+			int count = ptr9 - ptr0;
+			Test.Assert(count == 9);
+
+			var ptr9B = ptr0 + 9;
+			count = ptr9B - ptr0;
+			Test.Assert(ptr9 == ptr9B);
+			Test.Assert(count == 9);
 		}
 		}
 
 
 		[Align(16)]
 		[Align(16)]