A very simple and basic question about avx2 in fpc lazarus / 21-General ---------------------------------------------------------------------- beria2 · 2025-04-10 11:27 A big request for help... I've been sitting here for twenty-four hours and I can't solve the problem. How to load data into avx2 registers in the internal fpc assembled correctly. In the program below, they are initialized with garbage, and the program outputs the value -1.2556805098698515E+249, which is not true.  :'( :'( :'( :'( :'( :'( :'( :'( :'( :'( :'( :'(   function AsmTest: double; assembler;   asm            VMOVAPD YMM0, ymmword ptr [rip + @Data]            VEXTRACTF128 XMM0, YMM0, 1            JMP     @Exit            ALIGN   32            @Data:            DB      $40, $08, $FA, $29, $E1, $70, $A4, $F1            DB      $41, $09, $FB, $2A, $E2, $71, $A5, $F2            DB      $42, $0A, $FC, $2B, $E3, $72, $A6, $F3            DB      $43, $0B, $FD, $2C, $E4, $73, $A7, $F4            @Exit:   end; ---------------------------------------------------------------------- ALLIGATOR · 2025-04-10 13:07 Hmmm, at first glance, it looks like the value loads normally ---------------------------------------------------------------------- beria2 · 2025-04-10 13:30 Quote from: ALLIGATOR on April 10, 2025, 03:07:29 pmHmmm, at first glance, it looks like the value loads normally And where do you have such a beautiful debugger in Lazarus? This is how I look at the contents of the processor registers.  According to him, there is garbage there. And the test result, that is, the second number from the array, is garbage, not the one whose binary contents are typed in there.. It should be 3.26711296000000E+05. In my opinion, the code is correct, but the result is strange.... ---------------------------------------------------------------------- LV · 2025-04-10 13:52 Maybe use the data as a double? The result should be 10Pi ~ 31.4159265359. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1; {$asmmode intel}uses  SysUtils, Math; var  RawPtr: Pointer;  AlignedPtr: Pointer;  ResultValue: Double; const  ALIGNMENT = 32; type  TAVXData = array[0..3] of Double; procedure AllocateAlignedMemory(var Raw, Aligned: Pointer; Size, Align: PtrUInt);var  p: PtrUInt;begin  GetMem(Raw, Size + Align);            // Reserve extra space for alignment  p := (PtrUInt(Raw) + Align - 1) and not (Align - 1); // Align upwards  Aligned := Pointer(p);end; function AsmTest(p: Pointer): Double; assembler;asm  mov     rax, p                      // Address of the array  vmovapd ymm0, ymmword ptr [rax]     // Load 4 double values into ymm0   // Add lower and upper halves of ymm0  vextractf128 xmm1, ymm0, 1          // Extract upper 2 double values into xmm1  vaddpd  xmm0, xmm0, xmm1          // xmm0 = (a0+a2, a1+a3)   // Horizontally add remaining values  vhaddpd xmm0, xmm0, xmm0            // xmm0 = (a0+a2+a1+a3, ...)   // Return the result  movsd   xmm1, xmm0                  // Move result to xmm1end;   var  Data: TAVXData = (pi, 2 * pi, 3 * pi, 4 * pi); begin  // Allocate memory  AllocateAlignedMemory(RawPtr, AlignedPtr, SizeOf(Data), ALIGNMENT);   // Copy data to aligned memory  Move(Data, AlignedPtr^, SizeOf(Data));   Writeln('RawPtr     = ', PtrUInt(RawPtr));  Writeln('AlignedPtr = ', PtrUInt(AlignedPtr));   // Check if AlignedPtr is a multiple of 32  if (PtrUInt(AlignedPtr) mod ALIGNMENT) <> 0 then    raise Exception.Create('Alignment error!');   // Call assembler function  ResultValue := AsmTest(AlignedPtr);  Writeln('Returned value = ', ResultValue :0:10);   FreeMem(RawPtr);   Readln;end.  Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i RawPtr     = 22902112AlignedPtr = 22902112Returned value = 31.4159265359 ---------------------------------------------------------------------- ALLIGATOR · 2025-04-10 13:56 Quote from: beria2 on April 10, 2025, 03:30:56 pmQuote from: ALLIGATOR on April 10, 2025, 03:07:29 pmHmmm, at first glance, it looks like the value loads normally And where do you have such a beautiful debugger in Lazarus? This is how I look at the contents of the processor registers.  According to him, there is garbage there. And the test result, that is, the second number from the array, is garbage, not the one whose binary contents are typed in there.. It should be 3.26711296000000E+05. In my opinion, the code is correct, but the result is strange....   It's right here: https://forum.lazarus.freepascal.org/index.php?topic=68962.0 ---------------------------------------------------------------------- beria2 · 2025-04-10 15:12 Quote from: beria2 on April 10, 2025, 01:27:08 pmA big request for help... I've been sitting here for twenty-four hours and I can't solve the problem. How to load data into avx2 registers in the internal fpc assembled correctly. In the program below, they are initialized with garbage, and the program outputs the value -1.2556805098698515E+249, which is not true.  :'( :'( :'( :'( :'( :'( :'( :'( :'( :'( :'( :'(   function AsmTest: double; assembler;   asm            VMOVAPD YMM0, ymmword ptr [rip + @Data]            VEXTRACTF128 XMM0, YMM0, 1            JMP     @Exit            ALIGN   32            @Data:            DB      $40, $08, $FA, $29, $E1, $70, $A4, $F1            DB      $41, $09, $FB, $2A, $E2, $71, $A5, $F2            DB      $42, $0A, $FC, $2B, $E3, $72, $A6, $F3            DB      $43, $0B, $FD, $2C, $E4, $73, $A7, $F4            @Exit:   end;     I did it!!!   function AsmTest: double; assembler;   asm            VMOVAPD YMM0, ymmword ptr [rip + @Data]            VPERMILPD XMM1, XMM0, 1            MOVAPS  XMM0, XMM1            JMP     @Exit            ALIGN   32            @Data:            DB      $00, $00, $00, $00, $00, $00, $F0, $3F  // 1.0            DB      $00, $00, $00, $00, $00, $00, $00, $40  // 2.0            DB      $00, $00, $00, $00, $00, $00, $08, $40  // 3.0            DB      $00, $00, $00, $00, $00, $00, $10, $40  // 4.0            @Exit:   end; ---------------------------------------------------------------------- marcov · 2025-04-20 11:13 You don't need to stuff it in a procedure, just use a const. Code: [Select]const       splitsh6 :  array[0..31] of byte = (  $00,$04,$08,$0C,$01,$05,$09,$0d,                                             $02,$06,$0A,$0E,$03,$07,$0B,$0F,                                             $00,$04,$08,$0C,$01,$05,$09,$0d,                                             $02,$06,$0A,$0E,$03,$07,$0B,$0F); procedure xx; asm  ...        // in FPC code, loads of constants should now be 32-byte aligned (constmin)        vmovdqa  ymm9, [rip+splitsh6] ... end; [/code]