The fastest integer type? lazarus / 62-General ---------------------------------------------------------------------- Zoran · 2019-08-09 06:26 I want to choose the integer on which calculations will perform fastest. I thought that PtrInt would always be the best choice. So, as a start, I wrote this: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 type // fastest integer, but at least 32-bit:  IntFast32 = {$if SizeOf(Pointer) <= 4}Int32{$else}PtrInt{$endif};  UIntFast32 = {$if SizeOf(Pointer) <= 4}UInt32{$else}PtrUInt{$endif}; // fastest, at least 16-bit:  IntFast16 = {$if SizeOf(Pointer) <= 2}Int16{$else}IntFast32{$endif};  UIntFast16 = {$if SizeOf(Pointer) <= 2}UInt16{$else}UIntFast32{$endif};  However, I am not sure any more, as I read somewhere that on 64-bit CPU's, 32-bit integer is still the fastest. ---------------------------------------------------------------------- lucamar · 2019-08-09 06:34 Can't you test it? A loop doing some realtively simple calculation should do the trick. If you want the test to be more thorough you could add a call to some function or proc passing the integer by value. That would test the speed of using that integer with the stack and, in most cases, prevent the test being "tainted" by caching in the CPU. ---------------------------------------------------------------------- Thaddy · 2019-08-09 07:12 The fasted integer type is the native type for the CPU. That's all there is to it. On Intel it may be slightly different, but access vs calculations is something to keep in mind. On Intel, the micro code still has to mask out the upper part. ---------------------------------------------------------------------- PascalDragon · 2019-08-09 07:14 Quote from: Zoran on August 09, 2019, 08:26:18 amI want to choose the integer on which calculations will perform fastest. I thought that PtrInt would always be the best choice. So, as a start, I wrote this: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 type // fastest integer, but at least 32-bit:  IntFast32 = {$if SizeOf(Pointer) <= 4}Int32{$else}PtrInt{$endif};  UIntFast32 = {$if SizeOf(Pointer) <= 4}UInt32{$else}PtrUInt{$endif}; // fastest, at least 16-bit:  IntFast16 = {$if SizeOf(Pointer) <= 2}Int16{$else}IntFast32{$endif};  UIntFast16 = {$if SizeOf(Pointer) <= 2}UInt16{$else}UIntFast32{$endif};  However, I am not sure any more, as I read somewhere that on 64-bit CPU's, 32-bit integer is still the fastest. Please note that your check for the size of Pointer in the 16-bit case is not entirely correct as the size depends on the selected memory model (on i8086 obviously) and thus you might have a pointer type of size 4 on a 16-bit system (e.g. in Huge memory model). Thus your IntFast16 would be IntFast32 there which would not be the fastest type. ---------------------------------------------------------------------- wp · 2019-08-09 08:17 Quote from: Zoran on August 09, 2019, 08:26:18 amI want to choose the integer on which calculations will perform fastest. I think this is almost useless micro-optimization. There's much more to gain in speed when you focus on the algorithm and the data structures used by the calculation, or on simple loop errors such as calculating something in a loop which does not change there. ---------------------------------------------------------------------- LemonParty · 2019-08-09 08:20 Quote from: Thaddy on August 09, 2019, 09:12:12 amThe fasted integer type is the native type for the CPU. That's all there is to it. No. If we talking about performance then the fastest integer is the smallest. 8 bit integer needs 4 times less cache space then 32 bit integer. And L1 cache is ~80 times faster then memory. ---------------------------------------------------------------------- Zoran · 2019-08-09 08:23 Thank you all. Quote from: PascalDragon on August 09, 2019, 09:14:31 am Please note that your check for the size of Pointer in the 16-bit case is not entirely correct as the size depends on the selected memory model (on i8086 obviously) and thus you might have a pointer type of size 4 on a 16-bit system (e.g. in Huge memory model). Thus your IntFast16 would be IntFast32 there which would not be the fastest type. Would it be better then to ask SizeOf(SizeInt) in condition, and then to use SizeInt instead of PtrInt? ---------------------------------------------------------------------- lucamar · 2019-08-09 09:22 Quote from: LemonParty on August 09, 2019, 10:20:18 am8 bit integer needs 4 times less cache space then 32 bit integer. That's not exactly true. The processor will cache the whole 32(64) bit data in the bus and there will be further speed-downs in the internal pipelines caused by the need to discard the unused bytes on the data before passing it on. I would consider all that negligible (we're talking fractions of nanoseconds here) but since we are taliking about absolute speed ... ---------------------------------------------------------------------- Thaddy · 2019-08-09 09:29 Quote from: LemonParty on August 09, 2019, 10:20:18 amQuote from: Thaddy on August 09, 2019, 09:12:12 amThe fasted integer type is the native type for the CPU. That's all there is to it. No. If we talking about performance then the fastest integer is the smallest. 8 bit integer needs 4 times less cache space then 32 bit integer. And L1 cache is ~80 times faster then memory. That is simply not true.  The caches - in fact all of the CPU caches - are aligned to the native (register) type. Even on modern processors that allow unaligned data. Caches are speed efficient, not space efficient. ---------------------------------------------------------------------- jamie · 2019-08-09 14:28 The fastest integer type would be that on the CPU register size because Fpc will do a lot of conversions between them.  Since FPC does a lot of upshift conversion in code it's best to use the natural number size.  this is true for 32 bit targets however, in the 64bit target its upshifted to 64 bit regs most of the time when ever any processing is done, so there is a trade off there.  I suppose if you used all 64int in a 64bit target it would scream! ---------------------------------------------------------------------- LemonParty · 2019-08-10 10:06 I put 50$, that you can't write a faster code of processing buffer of integers than mine. I mean two buffers of the same amount of elements but one contain 8 bit int and another 32 bit integers. (arch x86) ---------------------------------------------------------------------- Thaddy · 2019-08-10 10:41 send me the $50 in advance plz. And plz specify what size of integer you mean and what architecture of x86: 8, 16, 32, 64, 128,256? (Latter two for AVXx/SSEx) ---------------------------------------------------------------------- marcov · 2019-08-10 10:41 Quote from: LemonParty on August 10, 2019, 12:06:58 pmI put 50$, that you can't write a faster code of processing buffer of integers than mine. I mean two buffers of the same amount of elements but one contain 8 bit int and another 32 bit integers. (arch x86) The code already is avx2 ? ---------------------------------------------------------------------- Thaddy · 2019-08-10 10:44 Quote from: marcov on August 10, 2019, 12:41:38 pmQuote from: LemonParty on August 10, 2019, 12:06:58 pmI put 50$, that you can't write a faster code of processing buffer of integers  than mine. I mean two buffers of the same amount of elements but one contain 8 bit int and another 32 bit integers. (arch x86) The code already is avx2 ? You can also send the $50 to Marco ---------------------------------------------------------------------- LemonParty · 2019-08-10 11:07 Quote from: marcov on August 10, 2019, 12:41:38 pmThe code already is avx2 ? It's enough SSE2 to beat AVX2 that working with Int32. ---------------------------------------------------------------------- marcov · 2019-08-10 11:36 Quote from: LemonParty on August 10, 2019, 01:07:41 pmQuote from: marcov on August 10, 2019, 12:41:38 pmThe code already is avx2 ? It's enough SSE2 to beat AVX2 that working with Int32. Why? Show and explain, let us learn. Do you miss certain instructions in AVX2? Do you have a processor (like Ryzen<3000 series) that implements avx2 with two pipes? Is the 128-bit lane shuffle limit somehow a problem? (hitting shuffle limits?) I mostly do bytewise SSE2 (and in rare cases avx2), but inbetween results are often 16-bit. ---------------------------------------------------------------------- Thaddy · 2019-08-10 11:52 Quote from: LemonParty on August 10, 2019, 01:07:41 pmQuote from: marcov on August 10, 2019, 12:41:38 pmThe code already is avx2 ? It's enough SSE2 to beat AVX2 that working with Int32. Please answer my question! You are completely incomprehensable. Usually caused by language problems (fair) or having no clue at all (worrying). For now I assume the latter. ---------------------------------------------------------------------- LemonParty · 2019-08-10 12:07 Quote from: marcov on August 10, 2019, 01:36:45 pmWhy? There is an explanation in this answer. In short processor can do calculations fast until the cache not limit him. For example you can get 64 bytes from cache or 16 dwords, that's mean you can process 16 or 64 integers per portion of time. This doesn't depend on instructions because the speed of RAM is constant. ---------------------------------------------------------------------- marcov · 2019-08-10 12:33 Quote from: LemonParty on August 10, 2019, 02:07:23 pmQuote from: marcov on August 10, 2019, 01:36:45 pmWhy? There is an explanation in this answer. In short processor can do calculations fast until the cache not limit him. For example you can get 64 bytes from cache or 16 dwords, that's mean you can process 16 or 64 integers per portion of time. This doesn't depend on instructions because the speed of RAM is constant. Yes. That is possible, if your dataset is much larger than your cache, so that it can be assumed cold, and with relatively simple instructions (unpack - add - pack cycle a few times unrolled, or not even that I assume). Memory sizes, cache size etc, ARE considerably increasing though. Todays cold load might still be in cache tomorrow, look at the sizes on these puppies I have some SIMD code for work, mostly dealing with image format transformation and kernel operations. It is simplified (64-bit only, aligned only, only widths that are multiples of 32px etc) Most of it is still SSE2 for similar reasons. Only color distance and YUV/HSV conversions are AVX2. For three reasons: simple code doesn't benefit as muchI use Delphi which @$*@$HYQ#E@# still doesn't support AVX2. The avx2 code is in FPC generated DLLs, but I only do it when it mattersThe shuffle units of AVX2 still have some limitations for 1 and 2-byte quantities to shift them over 128-bit lanes. Probably it is possible, but a whole lot more complicated ---------------------------------------------------------------------- LemonParty · 2019-08-10 12:47 CPUs with 8+ cores can open a potential of AVX2 instructions in multithread code, because the bigger cache you have the more data you can prefetch. ---------------------------------------------------------------------- circular · 2019-08-19 12:43 I would agree that cache is the main limit. I have been trying to use 64 bit integers instead of 32 bit integers but as a matter of fact, it did not increase the speed significantly. But hitting the cache limit make a very big difference. ---------------------------------------------------------------------- marcov · 2019-08-19 14:58 Quote from: LemonParty on August 10, 2019, 02:47:13 pmCPUs with 8+ cores can open a potential of AVX2 instructions in multithread code, because the bigger cache you have the more data you can prefetch. On AMD afaik cores can only use cache on the same core complex. Larger numbers are typically fragmented over multiple core complexes. ---------------------------------------------------------------------- munair · 2019-09-20 18:40 I wonder if a CPU cache would make much difference here. Generally the fastest integer is the native bit width of a system. So on a 16 bits system two byte integers are fastest, on a 32 bits system that would be 4 bytes etc. ---------------------------------------------------------------------- BeniBela · 2019-09-20 21:43 Not everyone has AVX btw Till last week I have been using an i5-520M without any AVX ---------------------------------------------------------------------- VTwin · 2019-09-20 23:49 I love this forum. Looking forward to the shootout. It won't change my code though, I agree with wp. ---------------------------------------------------------------------- valdir.marcos · 2019-09-21 13:10 Quote from: VTwin on September 21, 2019, 01:49:34 amI love this forum. Looking forward to the shootout. It won't change my code though, I agree with wp.+1