There are two 32 bit numbers. The older 1 byte of these numbers is always 00. The bottom line is this, you need to find the Euclidean distance of these numbers byte-by-byte. Well, let's say these two numbers are $ 050703 and $ 060401. sqrt (($ 03- $ 01) ^ 2 + ($ 07- $ 04) ^ 2 + ($ 05- $ 06) ^ 2).

I wrote this function:

function test(a,b:integer):double; register; var d:integer; asm mov eax,a and eax,$FF mov d,eax fild d mov eax,b and eax,$FF mov d,eax fild d fsub fld st(0) fmul mov eax,a shr eax,8 and eax,$FF mov d,eax fild d mov eax,b shr eax,8 and eax,$FF mov d,eax fild d fsub fld st(0) fmul fadd mov eax,a shr eax,16 and eax,$FF mov d,eax fild d mov eax,b shr eax,16 and eax,$FF mov d,eax fild d fsub fld st(0) fmul fadd fsqrt end; 

It seems I could make it easier. I would be grateful for any advice.

  • one
    Well, subtraction could be the whole use. Yes, and squaring, too. Instead of three pieces could be done in a loop. And who so scoffs at youth? Euclidean distance byte by ... - alexlz
  • Yes, in fact, everything is simple, there are two color values ​​($ BGR) you need to find their difference. - Timenzzo
  • Thanks for the advice. PS This is what I am mocking myself about) - Timenzzo

2 answers 2

And the assembler is fundamentally?

Make it easier:

 var abytes: array[1..4] of byte absolute a; bbytes: array[1..4] of byte absolute b; begin result := sqrt(sqr(abytes[1] - bbytes[1]) + sqr(abytes[2] - bbytes[2]) + sqr(abytes[3] - bbytes[3]) + sqr(abytes[4] - bbytes[4])); end; 
  • @VladD a what happens if the bytes in a are larger than the ones in b? What does the corpse pascal count in sqr ((- 1) & $ FF)? - alexlz
  • @alexlz: sqr is a square, not a root. - VladD am
  • > Cadaveric Pascal> Cadaveric Delphi> blah blah blah as I am pleased with such sayings - teanICH
  • one
    @Timenzzo: Do ​​you run this code millions of times? If not, optimization is not needed. Because the difference in absolute units will be a couple of microseconds, you have the costs of starting the process more. - VladD am
  • 2
    @VladD the author wrote that he needs to determine the proximity of colors (by the way, @Timenzzo, and why extract the square root ?. The metric is slightly different, the more distant colors will give a greater difference - so be it ...). @ teanYCH where I wrote "corpse delphi"? “Cadaveric Pascal” is the somewhat distorted name of the Borland product “Turopasopal”. Well, advertising - it is for this and is given to laugh at her. (In general, it is difficult for me to imagine a programming language with a turbine. An apocalyptic picture). IDE "with wings", pancake. - alexlz

I decided to use the following metric: | r1-r2 | + | b1-b2 | + | g1-g2 |

This is the function:

 function testRaz(a,b:integer):integer; asm pxor mm0,mm0 movd mm1,a movd mm2,b punpcklbw mm0,mm1 pxor mm1,mm1 punpcklbw mm1,mm2 psadbw mm0,mm1 movd result,mm0 emms end; 

Although it is not the answer to the initial question, but in solving my problem, it suits me. Perhaps someone else will come in handy.

Thank you all for the tips!