math.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2000 by Jonas Maebe and other members of the
  4. Free Pascal development team
  5. Implementation of mathematical Routines (only for real)
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {****************************************************************************
  13. EXTENDED data type routines
  14. ****************************************************************************}
  15. {$define FPC_SYSTEM_HAS_PI}
  16. function fpc_pi_real : valreal;compilerproc;
  17. begin
  18. { Function is handled internal in the compiler }
  19. runerror(207);
  20. result:=0;
  21. end;
  22. {$define FPC_SYSTEM_HAS_ABS}
  23. function fpc_abs_real(d : valreal) : valreal;compilerproc;
  24. begin
  25. { Function is handled internal in the compiler }
  26. runerror(207);
  27. result:=0;
  28. end;
  29. {$define FPC_SYSTEM_HAS_SQR}
  30. function fpc_sqr_real(d : valreal) : valreal;compilerproc;
  31. begin
  32. { Function is handled internal in the compiler }
  33. runerror(207);
  34. result:=0;
  35. end;
  36. {$define FPC_SYSTEM_HAS_TRUNC}
  37. function fpc_trunc_real(d : valreal) : int64;compilerproc; assembler; nostackframe;
  38. { input: d in fr1 }
  39. { output: result in r3 }
  40. asm
  41. fctidz f1, f1
  42. stfd f1, -8(r1)
  43. ld r3, -8(r1)
  44. end;
  45. {$define FPC_SYSTEM_HAS_ROUND}
  46. function fpc_round_real(d : valreal) : int64; compilerproc;assembler; nostackframe;
  47. { exactly the same as trunc, except that one fctiwz has become fctiw }
  48. { input: d in fr1 }
  49. { output: result in r3 }
  50. asm
  51. fctid f1, f1
  52. stfd f1, -8(r1)
  53. ld r3, -8(r1)
  54. end;
  55. {****************************************************************************
  56. Int to real helpers
  57. ****************************************************************************}
  58. {$define FPC_SYSTEM_HAS_INT64_TO_DOUBLE}
  59. function fpc_int64_to_double(i: int64): double; compilerproc;assembler;
  60. { input: i in r3 }
  61. { output: double(i) in f0 }
  62. {from "PowerPC Microprocessor Family: Programming Environments Manual for 64 and 32-Bit Microprocessors", v2.0, pg. 698 }
  63. var temp : int64;
  64. asm
  65. std r3,temp // store dword
  66. lfd f0,temp // load float
  67. fcfid f0,f0 // convert to fpu int
  68. end;
  69. {$define FPC_SYSTEM_HAS_QWORD_TO_DOUBLE}
  70. function fpc_qword_to_double(q: qword): double; compilerproc;assembler;
  71. const
  72. longint_to_real_helper: qword = $80000000;
  73. {from "PowerPC Microprocessor Family: Programming Environments Manual for
  74. 64 and 32-Bit Microprocessors", v2.0, pg. 698, *exact version* }
  75. { input: q in r3 }
  76. { output: double(q) in f0 }
  77. var
  78. temp1, temp2: qword;
  79. asm
  80. // load 2^32 into f4
  81. lis r4, longint_to_real_helper@highesta
  82. ori r4, r4, longint_to_real_helper@highera
  83. sldi r4, r4, 32
  84. oris r4, r4, longint_to_real_helper@ha
  85. lfd f4, longint_to_real_helper@l(r4)
  86. rldicl r4,r3,32,32 // isolate high half
  87. rldicl r0,r3,0,32 // isolate low half
  88. std r4,temp1 // store dword both
  89. std r0,temp2
  90. lfd f2,temp1 // load float both
  91. lfd f0,temp2 // load float both
  92. fcfid f2,f2 // convert each half to
  93. fcfid f0,f0 // fpu int (no rnd)
  94. fmadd f0,f4,f2,f0 // (2**32)*high+low (only add can rnd)
  95. end;