Code speed in 64bit compiler vs 32bit compiler lazarus / 62-General ---------------------------------------------------------------------- ad1mt · 2024-11-10 09:44 I have written some code using 64bit integers, which I then compiled with both the 64bit and 32bit compilers. I expected the code produced by the 64bit compiler to run faster, but that is not the case. The code produced by the 32bit compiler runs slightly faster. Does anyone have any insight into why this is? Thanks. ---------------------------------------------------------------------- 440bx · 2024-11-10 10:07 Quote from: ad1mt on November 10, 2024, 10:44:28 amDoes anyone have any insight into why this is? Without seeing the code, only guesses can be provided.  Apparently the code you wrote takes fewer clock cycles to execute in 32 bit than in 64 bit.  The reason why ?... (without seeing the code) who knows! ---------------------------------------------------------------------- marcov · 2024-11-10 10:54 Or, if you calculate large tables or parse large data structures, the cache/memory might be the bottleneck, not the CPU. ---------------------------------------------------------------------- Thaddy · 2024-11-10 11:17 Or, the fpu settiing for that matter. The compiler is sooo conservative upto and including -O2. There is also a noticable difference between ARM32/AARCH64, it is not only Intel. ---------------------------------------------------------------------- MathMan · 2024-11-10 11:45 Or the target - if the target is a 32 bit CPU, then ... Regarding source I think ad1mt is refering to his Multi-Int package, as he posted the same comment here - https://forum.lazarus.freepascal. org/index.php/topic,68290.msg537657/topicseen.html#new ---------------------------------------------------------------------- LV · 2024-11-10 13:44 The floating point results are also interesting (FPC 3.2.2. no optimization).  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; uses  SysUtils; var  start: int64;  i, j, k: integer;  x: double;  p: Pointer;begin  writeln('system ', SizeOf(p) * 8, '  bit');  for k := 1 to 10 do  begin    start := gettickcount64;    for j := 0 to 10000 do      for i := 0 to 10000 do        x := ln(i + 1);    writeln(k, '  finished', ' took: ', gettickcount64 - start);  end;  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 system 32  bit1  finished took: 14852  finished took: 14373  finished took: 14224  finished took: 14225  finished took: 14376  finished took: 14227  finished took: 14388  finished took: 14069  finished took: 143710  finished took: 1422 system 64  bit1  finished took: 27342  finished took: 26413  finished took: 26414  finished took: 26405  finished took: 26256  finished took: 26417  finished took: 26408  finished took: 26259  finished took: 264110  finished took: 2625  Windows 11. Lazarus 2.2.6 (rev lazarus_2_2_6) FPC 3.2.2 x86_64-win64-win32/win64 ---------------------------------------------------------------------- LV · 2024-11-10 14:36 Another test. 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; uses  SysUtils; const  MN = 8000; type  MyArr = array of array of double; var  Arrx, Arry, Arrz: MyArr;  start: int64;  i, j, k, l: integer;  x: double;  p: Pointer;   procedure Computing(i: integer; j: integer);  begin    Arry[i, j] := Arrx[i, j] - (Arrx[i, j] * Arrx[i, j] - Arrx[i - 1, j] *      Arrx[i - 1, j] / 2);    Arrz[i, j] := Arrx[i, j] - (Arrx[i, j] * Arrx[i, j] - Arrx[i, j - 1] *      Arrx[i, j - 1] / 2);  end; begin  writeln('system ', SizeOf(p) * 8, '  bit');  SetLength(Arrx, MN + 1, MN + 1);  SetLength(Arry, MN + 1, MN + 1);  SetLength(Arrz, MN + 1, MN + 1);  for l := 1 to 5 do  begin    start := gettickcount64;    for k := 1 to 5 do      for i := 1 to MN do        for j := 1 to MN do          Computing(i, j);    writeln(l, '  finished', ' took: ', gettickcount64 - start);  end;  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 system 32  bit1  finished took: 32812  finished took: 32503  finished took: 32504  finished took: 32665  finished took: 3234 system 64  bit1  finished took: 29532  finished took: 29063  finished took: 29064  finished took: 29065  finished took: 2891 ---------------------------------------------------------------------- LV · 2024-11-10 15:15 I have a multithreaded program that I tested on a computer with an i7 8700 processor running Windows 11. I set it to run with 6 threads. In the Win32 compilation mode (cross-compilation target: i386), the execution time was 16 seconds. In the Win64 compilation mode (target: Default), the execution time was 8 seconds. ---------------------------------------------------------------------- tetrastes · 2024-11-10 15:33 Quote from: LV on November 10, 2024, 02:44:13 pmThe floating point results are also interesting (FPC 3.2.2. no optimization).  This is because ppcx64 does not use x87 FPU. ---------------------------------------------------------------------- BrunoK · 2024-11-10 15:43 Quote from: LV on November 10, 2024, 02:44:13 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i bk_1 : x := ln(i + 1); bk_2 :  procedure Computing(i: integer; j: integer);Notes :    bk_1 : the 32 bit mode uses the in processor x87 ln intrinsic operation while the 64 bit mode uses a more exacting compiler procedure in 3.2.2\rtl\inc\genmath.inc from line 1370. Seems that the intrinsic x87 floating point is twice faster than the windows x86_64 implementation.   bk_2 :  On my system, the 64 bit mode is slightly faster than the x87 fpu mode. Probably due to Double (64 bits) matching size of register (8 bit 8 bytes). As for compilation, try to compile with -O1 -OoREGVAR. The generated code remains very debugable but is nearly as fast as with higher optimization. ---------------------------------------------------------------------- Thaddy · 2024-11-10 16:06 Quote from: tetrastes on November 10, 2024, 04:33:49 pmQuote from: LV on November 10, 2024, 02:44:13 pmThe floating point results are also interesting (FPC 3.2.2. no optimization).  This is because ppcx64 does not use x87 FPU. That is a misconception. SSE3+, AVX and X86-64-V are much faster and even with better precision in some cases. But all these do not belong to the default optimization upto level O2. You can get upwards of 128 bit - here upto 512 - precision from those and that is a lot more than 80 bit. It just takes a bit of understanding what you want exactly...and what the fpu settings do... e.g. avx2+ is a lot faster and with more precision than the olden x87 concept if required. Although it might not very well be integrated in the compiler yet. ---------------------------------------------------------------------- tetrastes · 2024-11-10 16:47 Quote from: Thaddy on November 10, 2024, 05:06:56 pmQuote from: tetrastes on November 10, 2024, 04:33:49 pmQuote from: LV on November 10, 2024, 02:44:13 pmThe floating point results are also interesting (FPC 3.2.2. no optimization).  This is because ppcx64 does not use x87 FPU. That is a misconception. SSE3+, AVX and X86-64-V are much faster and even with better precision in some cases. But all these do not belong to the default optimization upto level O2. I have written about the particular code, and why here x87 is faster despite all optimizations, you may read in answer of BrunoK. Quote from: Thaddy on November 10, 2024, 05:06:56 pmYou can get upwards of 128 bit - here upto 512 - precision from those and that is a lot more than 80 bit. It just takes a bit of understanding what you want exactly...and what the fpu settings do... e.g. avx2+ is a lot faster and with more precision than the olden x87 concept if required. Although it might not very well be integrated in the compiler yet. That is the point. You cannot achieve anything without using assembler. Not to say, that there are no floating point types of high precisions, even Extended is removed, may be it can be used to some extend (?) for all that cool things, at least to have 80-bit precision. Though talks about precision have no matter to this code. ---------------------------------------------------------------------- MathMan · 2024-11-10 17:57 @BrunoK Quote bk_1 : the 32 bit mode uses the in processor x87 ln intrinsic operation while the 64 bit mode uses a more exacting compiler procedure in 3.2.2\rtl\inc\genmath.inc from line 1370. Seems that the intrinsic x87 floating point is twice faster than the windows x86_64 implementation. Nearly correct - it uses 'FYL2X FYL2XP1' plus a multiplication by constant - taking ~80-90 cycles, as per Agner Fogs measurements. The 64 bit mode variant for Double is however not more exact - the Intel microcode implementation of x87 'FYL2X FYL2XP1' extended are correct to ~1.5 units in the last place which is much better than anything achievable for Double. See the Intel x87 documentation - e.g. here http://www.infophysics.net/x87.pdf. Edit: Oops, sorry - maybe I misread 'intrinsic' as 'mnemonic' here? In general terms there has been a lot of progress recently (last 10-15 years) on floating point libraries in terms of speed and accuracy for Single & Double algebraic and transcendental functions. Search for the 'Core Math' project of Paul Zimmermann if you want to take a peek. Modern libraries are now faster than the x87 microcode for Single & Double. If the library in FPC RTL is slower, as you suggest, then it probably hasn't been updated for quite some time. The Extended type nowadays is on a major retreat as far as I can see. @Thaddy QuoteThat is a misconception. SSE3+, AVX and X86-64-V are much faster and even with better precision in some cases. But all these do not belong to the default optimization upto level O2. You can get upwards of 128 bit - here upto 512 - precision from those and that is a lot more than 80 bit. It just takes a bit of understanding what you want exactly...and what the fpu settings do... I think the misconception is on your side here, sorry. All vector extensions of Intel (and also ARM / RISK-V and others afaia) tops out at Double (IEEE FP64 bit floating point type). What they do offer is the ability to do operations on multiple instances of FP32 or FP64 at the same time, but not providing added precision due to that. You can do e.g. 2 / 4 / 8 multiplications of FP64 in parallel on SSE / AVX2 / AVX512 - but this is not the same as doing 1 FP512 multiplication! They are faster on the basic operations like +,-,* than the x87 but at the same have lost all algebraic / transzendental functions (with exception of SQRT and INVSQRT) which now must be implemented by libraries. @tetrastes: QuoteNot to say, that there are no floating point types of high precisions, even Extended is removed, may be it can be used to some extend (?) for all that cool things, at least to have 80-bit precision. Yes they can - there are some libraries implementing what is called double quad or quad quad - a non IEEE floating point format with 106 / 212 bit precision. However this is stitched together from 2 / 4 IEEE FP64, and though fast seems to have been neglected recently (<= I may be on error here, haven't followed thoroughly). ---------------------------------------------------------------------- LV · 2024-11-10 18:10 Quote from: BrunoK on November 10, 2024, 04:43:04 pmAs for compilation, try to compile with -O1 -OoREGVAR. The generated code remains very debugable but is nearly as fast as with higher optimization. @BrunoK, thanks. That's what I usually do. In the test (Reply #7), a fairly large program for Win64 was compiled with the (-O1 + quick optimizations) (-O2) option. Out of curiosity, I checked it for Win32 with the same option. For the sake of completeness, here are the results of tests with optimization (Reply #5): 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 system 32  bit   (-O1 + quick optimizations) (-O2)1  finished took: 13912  finished took: 13753  finished took: 13754  finished took: 13595  finished took: 1391 system 64  bit   (-O1 + quick optimizations) (-O2)1  finished took: 27192  finished took: 26873  finished took: 26414  finished took: 27035  finished took: 2703  and (Reply #6): 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 system 32  bit   (-O1 + quick optimizations) (-O2)1  finished took: 15002  finished took: 14843  finished took: 14694  finished took: 14695  finished took: 1468 system 64  bit   (-O1 + quick optimizations) (-O2)1  finished took: 10472  finished took: 10313  finished took: 10474  finished took: 10315  finished took: 1031  @MathMan, thank you; I have expanded my horizons. Best regards ---------------------------------------------------------------------- ad1mt · 2024-11-10 18:17 It appears this might be rather complicated... A very simple code test I just did, adding integers together, is sometimes faster in the 32bit compiler, and sometimes faster in the 64bit compiler. One of things that the speed depends on is the -O optimisation level. Another thing is mixing 32bit variables and 64bit variables in assignments or expressions. ---------------------------------------------------------------------- tetrastes · 2024-11-10 19:04 @ad1mt Just in case, this may be interesting, if you didn't read it https://f orum.lazarus.freepascal.org/index.php/topic,40099.msg276637.html#msg27 6637 ---------------------------------------------------------------------- Martin_fr · 2024-11-10 19:34 Quote from: ad1mt on November 10, 2024, 07:17:22 pmA very simple code test I just did, adding integers together, is sometimes faster in the 32bit compiler, and sometimes faster in the 64bit compiler. Well, there are lots of factors. Both code and data may change in size and by that may be differently aligned, and that can make surprising speed differences. Say you have a loop in your code, then its alignment depends on how much other code is in front => but depending on its size, if the loop has a specific alignment, it may run faster. In the end it may happen more likely under 32 bits. Similar stuff can happen with data, if data that is used together ends up in a single cache line. There are some directives to set up alignment for those things. You can play around with that. In some cases alignment to boundaries up to 32byte bounds can change speed. But its not a guarantee.  It can go the other way to.