math.inc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_INT}
  46. function fpc_int_real(d : valreal) : valreal;compilerproc; assembler; nostackframe;
  47. asm
  48. fctidz f2, f1
  49. fcfid f1, f2
  50. end;
  51. {$define FPC_SYSTEM_HAS_ROUND}
  52. function fpc_round_real(d : valreal) : int64; compilerproc;assembler; nostackframe;
  53. { exactly the same as trunc, except that one fctiwz has become fctiw }
  54. { input: d in fr1 }
  55. { output: result in r3 }
  56. asm
  57. fctid f1, f1
  58. stfd f1, -8(r1)
  59. ld r3, -8(r1)
  60. end;
  61. {****************************************************************************
  62. Int to real helpers
  63. ****************************************************************************}
  64. {$define FPC_SYSTEM_HAS_INT64_TO_DOUBLE}
  65. function fpc_int64_to_double(i: int64): double; compilerproc;assembler;
  66. { input: i in r3 }
  67. { output: double(i) in f0 }
  68. {from "PowerPC Microprocessor Family: Programming Environments Manual for 64 and 32-Bit Microprocessors", v2.0, pg. 698 }
  69. var temp : int64;
  70. asm
  71. std r3,temp // store dword
  72. lfd f0,temp // load float
  73. fcfid f0,f0 // convert to fpu int
  74. end;
  75. {$define FPC_SYSTEM_HAS_QWORD_TO_DOUBLE}
  76. function fpc_qword_to_double(q: qword): double; compilerproc;assembler;
  77. const
  78. longint_to_real_helper: qword = $80000000;
  79. {from "PowerPC Microprocessor Family: Programming Environments Manual for
  80. 64 and 32-Bit Microprocessors", v2.0, pg. 698, *exact version* }
  81. { input: q in r3 }
  82. { output: double(q) in f0 }
  83. var
  84. temp1, temp2: qword;
  85. asm
  86. // load 2^32 into f4
  87. lis r4, longint_to_real_helper@highesta
  88. ori r4, r4, longint_to_real_helper@highera
  89. sldi r4, r4, 32
  90. oris r4, r4, longint_to_real_helper@ha
  91. lfd f4, longint_to_real_helper@l(r4)
  92. rldicl r4,r3,32,32 // isolate high half
  93. rldicl r0,r3,0,32 // isolate low half
  94. std r4,temp1 // store dword both
  95. std r0,temp2
  96. lfd f2,temp1 // load float both
  97. lfd f0,temp2 // load float both
  98. fcfid f2,f2 // convert each half to
  99. fcfid f0,f0 // fpu int (no rnd)
  100. fmadd f0,f4,f2,f0 // (2**32)*high+low (only add can rnd)
  101. end;