Recent

Author Topic: Confused by a line in matth.inc (2.6.2 rtl)  (Read 1868 times)

Pasqualish

  • Jr. Member
  • **
  • Posts: 68
Confused by a line in matth.inc (2.6.2 rtl)
« on: September 19, 2017, 09:22:47 am »
This line is in mathh.inc in /usr/share/fpcsrc/2.6.2/rtl

Code: Pascal  [Select][+][-]
  1.     function pi : ValReal;[internproc:fpc_in_pi_real];

I see a function named pi that takes no arguments and returns a ValReal type, but I don't know how to interpret the rest of that declaration.

Thaddy

  • Hero Member
  • *****
  • Posts: 14376
  • Sensorship about opinions does not belong here.
Re: Confused by a line in matth.inc (2.6.2 rtl)
« Reply #1 on: September 19, 2017, 09:38:11 am »
[internproc:fpc_in_pi_real] means that valreal will be resolved by the compiler into pi with as resolution the correct max real type (single, double, extended, etc) for the platform: eg. i386 = extended, x86_64 = double.
It is related to the compilerproc modifiier that is added in the implementation section if it is not a compiler const (here it is a compiler const) See e.g. declaration for Abs and implementation for Abs.
You can only use this construct in the interface section of a unit and only if there is a corresponding  compiler intrinsic or const for that.
Basically never use it yourself unless you are a compiler dev.. It is as magical as magic comes.... :D O:-)
Code: Pascal  [Select][+][-]
  1. //mathh.inc interface
  2. function abs(d : ValReal) : ValReal;[internproc:fpc_in_abs_real];
  3. // compproc.inc implementation
  4. function fpc_abs_real(d : ValReal) : ValReal;compilerproc;
  5.  

In the case of pi, the const is first resolved through a  index for known constants and then a FPU instruction if available and subsequently through the compiler function getpi in ninl.pas translated at compile time  into a const of the correct size.
Code: Pascal  [Select][+][-]
  1.  
  2.     function getpi : bestreal;
  3.       begin
  4.       {$ifdef x86}
  5.         { x86 has pi in hardware }
  6.         result:=pi;
  7.       {$else x86}
  8.         {$ifdef cpuextended}
  9.           result:=MathPiExtended.Value;
  10.         {$else cpuextended}
  11.           result:=MathPi.Value;
  12.         {$endif cpuextended}
  13.       {$endif x86}
  14.       end;

ValReal itself is determined through a set of defines based on the resolution of the cpu or fpu for the platform.

So basically: nice syntax, but not for you (or me)...
« Last Edit: September 19, 2017, 01:27:51 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Pasqualish

  • Jr. Member
  • **
  • Posts: 68
Re: Confused by a line in matth.inc (2.6.2 rtl)
« Reply #2 on: September 19, 2017, 10:48:15 am »
Thanks! 

 

TinyPortal © 2005-2018