Recent

Author Topic: Sqr function  (Read 2202 times)

glorfin

  • Full Member
  • ***
  • Posts: 148
  • LMath supporter
Sqr function
« on: January 27, 2021, 01:51:25 pm »
Hi!
What is the advantage of using Sqr(A) over just A*A?
Treatment of some special cases, protection from underflow/overflow errors? Documentation looks really very succinct...

Thank you!

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Sqr function
« Reply #1 on: January 27, 2021, 02:10:55 pm »
Hi!
What is the advantage of using Sqr(A) over just A*A?
I think it's the same.
Code: Pascal  [Select][+][-]
  1. {$RANGECHECKS ON}
  2. {$OVERFLOWCHECKS ON}
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. var
  6.   D2: ValReal = 2.0;
  7.   DS, DM: ValReal;
  8. begin
  9.   DS := Sqr(D2);
  10.   DM := D2 * D2;
  11.   Caption := DS.ToString + ' ' + DM.ToString;
  12. end;

Code: ASM  [Select][+][-]
  1. ...
  2. # [30] var
  3.         movq    TC_$UNIT1$_$TFORM1_$_BUTTON1CLICK$TOBJECT_$$_defaultD2(%rip),%rax
  4.         movq    %rax,-24(%rbp)
  5. .Ll7:
  6. # [34] DS := Sqr(D2);
  7.         movsd   -24(%rbp),%xmm0
  8.         mulsd   %xmm0,%xmm0
  9.         movsd   %xmm0,-32(%rbp)
  10. .Ll8:
  11. # [35] DM := D2 * D2;
  12.         movsd   -24(%rbp),%xmm0
  13.         mulsd   -24(%rbp),%xmm0
  14.         movsd   %xmm0,-40(%rbp)
  15. .Ll9:
  16. # [36] Caption := DS.ToString + ' ' + DM.ToString;
  17. ...

glorfin

  • Full Member
  • ***
  • Posts: 148
  • LMath supporter
Re: Sqr function
« Reply #2 on: January 27, 2021, 02:21:26 pm »
Thanks! Indeed, looks like that. Which means that for long expressions Sqr is better and for short *, just to type less :)

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Sqr function
« Reply #3 on: January 27, 2021, 11:15:36 pm »
you need to look at the generated code for a 32 bit target..

as I see it now, it looks like it saves a little by using registers instead of the stack always but this is in 64 bit mode, you may want to see the results in 32 bit mode which I still write lots of..
The only true wisdom is knowing you know nothing

Peter H

  • Sr. Member
  • ****
  • Posts: 272
Re: Sqr function
« Reply #4 on: January 27, 2021, 11:38:04 pm »
If I remember it correctly, some CPUs or their FPUs (68020) have square machine instructions.
Reason: the microcode algorithm for binary squaring is faster than multiplication.
And with emulated FPU it should be definitely faster.
« Last Edit: January 28, 2021, 12:00:29 am by Peter H »

marcio2003

  • Jr. Member
  • **
  • Posts: 69
Re: Sqr function
« Reply #5 on: January 28, 2021, 02:43:17 am »
For example if we have
SQR (a · X² + b · X + c)
how long is the processing time to
(a · X² + b · X + c) · (a · X² + b · X + c) ?
Lazarus 2.0.10 Windows 10 64bits

Kays

  • Hero Member
  • *****
  • Posts: 569
  • Whasup!?
    • KaiBurghardt.de
Re: Sqr function
« Reply #6 on: January 28, 2021, 03:00:34 am »
[…] how long is the processing time […]
There is no general statement. It depends. If it’s all constants, the FPC will calculate everything at compile-time (“constant folding”).

[…]Which means that for long expressions Sqr is better and for short *, just to type less :)
I hope it’s needless to say: sqr(myFunction()) differs from myFunction() * myFunction(), the latter calling myFunction twice.

But other than that, you already said it, sqr(…) mitigates the chance of spelling mistakes and is just simply shorter (if the expression exceeds a certain number of characters).
Yours Sincerely
Kai Burghardt

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Sqr function
« Reply #7 on: January 28, 2021, 09:27:38 am »
In some conditions the compiler optimizes x * x to Sqr(x). To quote from compiler/nadd.pas around line 1060:

Quote
using sqr(x) for reals instead of x*x might reduces register pressure and/or memory accesses while sqr(<real>) has no drawback }

glorfin

  • Full Member
  • ***
  • Posts: 148
  • LMath supporter
Re: Sqr function
« Reply #8 on: January 28, 2021, 04:16:01 pm »
Thank you for all the answers!

marcio2003

  • Jr. Member
  • **
  • Posts: 69
Re: Sqr function
« Reply #9 on: January 28, 2021, 08:44:47 pm »
I wanted to show the author of the topic (glorfin) another clear advantage in using SQR function: It is not necessary to duplicate the typing.

I also believe that using SQR (f (x)) the compiler will only execute the function once, while (f (x) * f (x)) will execute the function twice. But i don't sure about it.
Lazarus 2.0.10 Windows 10 64bits

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Sqr function
« Reply #10 on: January 29, 2021, 01:28:00 pm »
I also believe that using SQR (f (x)) the compiler will only execute the function once, while (f (x) * f (x)) will execute the function twice. But i don't sure about it.

Correct, that's what Kays already wrote.

glorfin

  • Full Member
  • ***
  • Posts: 148
  • LMath supporter
Re: Sqr function
« Reply #11 on: January 29, 2021, 03:49:21 pm »
I wanted to show the author of the topic (glorfin) another clear advantage in using SQR function: It is not necessary to duplicate the typing.

I also believe that using SQR (f (x)) the compiler will only execute the function once, while (f (x) * f (x)) will execute the function twice. But i don't sure about it.
Well, duplication of typing is important if a squared expression is long, otherwise K*K is shorter. In case of a squared function it becomes important for sure, when I asked a question I was thinking about a variable.

 

TinyPortal © 2005-2018