Jump to content
wqmeng

SetLength of TArray<double> in ASM get strange result

Recommended Posts

SetLength of TArray<double> in ASM get strange result.

 

Hello,

 

I have a procedure which try to setlength for 3 TArray<Double>. But seems cause unexcpeted result.

procedure OpArrSSE2(const srcData: TArray<Double>; A1, A2, A3: Integer;
  var dst1, dst2, dst3: TArray<Double>;
  var P1, P2, P3: Double;
  var F1, F2, F3: Double;
  const B1: Boolean);
asm
  ...
  
  // start to setlength of dst1, dst2, dst3.
  {
  mov eax, dst1     // dst1 Pointer
  mov edx, edi
  call System.@SetLength 
  }
  
  // procedure DynArraySetLength(var a: Pointer; typeInfo: Pointer; dimCnt: NativeInt; lengthVec: PNativeint);
  mov eax, dst1     // dst1 Pointer
  mov edx, offset TypeInfoArrayDouble   // Type info: Get the type information pointer for TArray<Double>
  mov ecx, 1            // DimCnt (number of dimensions), for a one-dimensional array it's 1
  push edi              // New length (NativeInt), edi = newlength_value
  call System.@DynArraySetLength // procedure DynArraySetLength(var a: Pointer; typeInfo: Pointer; dimCnt: NativeInt; lengthVec: PNativeint);
  add esp, 4            // pop edi
  
  // fill 0
  mov eax, dst1        // dst1 Pointer -> @dst1[0]
  mov eax, [eax]        // @dst1[0]
  mov edx, edi          // Number of elements
  shl edx, 3            // Multiply by SizeOf(Double) (which is 8)
  xor ecx, ecx          // Value to fill (zero)
  call System.@FillChar // Call FillChar function  
  
  // then repeat the same operate on dst2, dst3.
  problem is here, after FillChar dst3.
  the dst2 array become empty, and then some exception will raise up.
  Seems the fillchar of dst3 override some memory data of dst2, and other unknown data which cause AV error.  
end.

Please see the above problem descritption at the bottom of the code.

 

I have tried SetLength at first, but seems it not work, as SetLength is a overload function, I do not know how to call it correctly, then try DynArraySetLength, which seems could setlength correctly, but after fillchar, it shows that the memory which get may has confulic with other data, what's wrong of my codes? Any advice will be appreciated.

 

Thank you.

Share this post


Link to post

The best thing you can do is debug the identical procedure write in Pascal and see how the compiler acts ...

  • Like 1

Share this post


Link to post

Can you just include the pascal code of what you are trying to size. Never mind implement it... The term "setlength for 3 TArray<Double>" is unclear. Are you trying to size

 

arr_3D: Array of array of array of double?

 

if so, SetLength(arr_3d, a,b,c) should work (where a,b.c are the dimensions you want)

Note, SetLength will set the newly allocated memory to zero.

 

or 3 different 1D array of double

Share this post


Link to post
Just now, Dave Novo said:

The term "setlength for 3 TArray<Double>" is unclear.

It's in the source:

3 hours ago, wqmeng said:

var dst1, dst2, dst3: TArray<Double>;

 

Share this post


Link to post
36 minutes ago, Dave Novo said:

Can you just include the pascal code of what you are trying to size. Never mind implement it... The term "setlength for 3 TArray<Double>" is unclear. Are you trying to size

 

arr_3D: Array of array of array of double?

 

if so, SetLength(arr_3d, a,b,c) should work (where a,b.c are the dimensions you want)

Note, SetLength will set the newly allocated memory to zero.

 

or 3 different 1D array of double

Hello,

 

They are 3 differnt 1 D array which passed in as parameters var dst1, dst2, dst3.

In asm, I would like to check if the dst1, dst2, dst3 have already set for enough length to hold results get from the srcData. So need Resize dst1, dst2, dst3 if needed.

 

If I setlength of dst1, dst2, dst3 before call the procedure, there will be no problem.  If dst1, dst2, dst3 has not setlength of the length is less than srcData, will call the asm code to DynArraySetLength of them, then various exceptions will raise up. I gusss the problem is caused by the DynArraySetLength and fillchar which clear of the wrong memorys.

 

Thank you.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×