math.inc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. const
  13. longint_to_real_helper: int64 = $4330000080000000;
  14. cardinal_to_real_helper: int64 = $4330000000000000;
  15. int_to_real_factor: double = double(high(cardinal))+1.0;
  16. {****************************************************************************
  17. EXTENDED data type routines
  18. ****************************************************************************}
  19. {$define FPC_SYSTEM_HAS_PI}
  20. function fpc_pi_real : valreal;compilerproc;
  21. begin
  22. { Function is handled internal in the compiler }
  23. runerror(207);
  24. result:=0;
  25. end;
  26. {$define FPC_SYSTEM_HAS_ABS}
  27. function fpc_abs_real(d : valreal) : valreal;compilerproc;
  28. begin
  29. { Function is handled internal in the compiler }
  30. runerror(207);
  31. result:=0;
  32. end;
  33. {$define FPC_SYSTEM_HAS_SQR}
  34. function fpc_sqr_real(d : valreal) : valreal;compilerproc;
  35. begin
  36. { Function is handled internal in the compiler }
  37. runerror(207);
  38. result:=0;
  39. end;
  40. const
  41. factor: double = double(int64(1) shl 32);
  42. factor2: double = double(int64(1) shl 31);
  43. {$ifndef FPC_SYSTEM_HAS_TRUNC}
  44. {$define FPC_SYSTEM_HAS_TRUNC}
  45. function fpc_trunc_real(d : valreal) : int64;assembler;compilerproc;
  46. { input: d in fr1 }
  47. { output: result in r3 }
  48. assembler;
  49. var
  50. temp: packed record
  51. case byte of
  52. 0: (l1,l2: longint);
  53. 1: (d: double);
  54. end;
  55. asm
  56. // store d in temp
  57. stfd f1,temp
  58. // extract sign bit (record in cr0)
  59. lwz r3,temp
  60. rlwinm. r3,r3,1,31,31
  61. // make d positive
  62. fabs f1,f1
  63. // load 2^32 in f2
  64. {$ifndef macos}
  65. lis r4,factor@ha
  66. lfd f2,factor@l(r4)
  67. {$else}
  68. lwz r4,factor(r2)
  69. lfd f2,0(r4)
  70. {$endif}
  71. // check if value is < 0
  72. // f3 := d / 2^32;
  73. fdiv f3,f1,f2
  74. // round
  75. fctiwz f4,f3
  76. // store
  77. stfd f4,temp
  78. // and load into r4
  79. lwz r3,temp+4
  80. // convert back to float
  81. lis r0,0x4330
  82. stw r0,temp
  83. xoris r0,r3,0x8000
  84. stw r0,temp+4
  85. {$ifndef macos}
  86. lis r4,longint_to_real_helper@ha
  87. lfd f0,longint_to_real_helper@l(r4)
  88. {$else}
  89. lwz r4,longint_to_real_helper(r2)
  90. lfd f0,0(r4)
  91. {$endif}
  92. lfd f3,temp
  93. fsub f3,f3,f0
  94. // f4 := d "mod" 2^32 ( = d - ((d / 2^32) * 2^32))
  95. fnmsub f4,f3,f2,f1
  96. // now, convert to unsigned 32 bit
  97. // load 2^31 in f2
  98. {$ifndef macos}
  99. lis r4,factor2@ha
  100. lfd f2,factor2@l(r4)
  101. {$else}
  102. lwz r4,factor2(r2)
  103. lfd f2,0(r4)
  104. {$endif}
  105. // subtract 2^31
  106. fsub f3,f4,f2
  107. // was the value > 2^31?
  108. fcmpu cr1,f4,f2
  109. // use diff if >= 2^31
  110. fsel f4,f3,f3,f4
  111. // next part same as conversion to signed integer word
  112. fctiwz f4,f4
  113. stfd f4,temp
  114. lwz r4,temp+4
  115. // add 2^31 if value was >=2^31
  116. blt cr1, .LTruncNoAdd
  117. xoris r4,r4,0x8000
  118. .LTruncNoAdd:
  119. // negate value if it was negative to start with
  120. beq cr0,.LTruncPositive
  121. subfic r4,r4,0
  122. subfze r3,r3
  123. .LTruncPositive:
  124. end;
  125. {$endif not FPC_SYSTEM_HAS_TRUNC}
  126. (*
  127. {$ifndef FPC_SYSTEM_HAS_ROUND}
  128. {$define FPC_SYSTEM_HAS_ROUND}
  129. function round(d : extended) : int64;
  130. function fpc_round(d : extended) : int64;assembler;[public, alias:'FPC_ROUND'];compilerproc;
  131. { exactly the same as trunc, except that one fctiwz has become fctiw }
  132. { input: d in fr1 }
  133. { output: result in r3 }
  134. assembler;
  135. var
  136. temp: packed record
  137. case byte of
  138. 0: (l1,l2: longint);
  139. 1: (d: double);
  140. end;
  141. asm
  142. // store d in temp
  143. stfd f1, temp
  144. // extract sign bit (record in cr0)
  145. lwz r4,temp
  146. rlwinm. r4,r4,1,31,31
  147. // make d positive
  148. fabs f1,f1
  149. // load 2^32 in f2
  150. {$ifndef macos}
  151. lis r4,factor@ha
  152. lfd f2,factor@l(r4)
  153. {$else}
  154. lwz r4,factor(r2)
  155. lfd f2,0(r4)
  156. {$endif}
  157. // check if value is < 0
  158. // f3 := d / 2^32;
  159. fdiv f3,f1,f2
  160. // round
  161. fctiwz f4,f3
  162. // store
  163. stfd f4,temp
  164. // and load into r4
  165. lwz r3,temp+4
  166. // convert back to float
  167. lis r0,0x4330
  168. stw r0,temp
  169. xoris r0,r3,0x8000
  170. stw r0,temp+4
  171. {$ifndef macos}
  172. lis r4,longint_to_real_helper@ha
  173. lfd f0,longint_to_real_helper@l(r4)
  174. {$else}
  175. lwz r4,longint_to_real_helper(r2)
  176. lfd f0,0(r4)
  177. {$endif}
  178. lfd f3,temp
  179. fsub f3,f3,f0
  180. // f4 := d "mod" 2^32 ( = d - ((d / 2^32) * 2^32))
  181. fnmsub f4,f3,f2,f1
  182. // now, convert to unsigned 32 bit
  183. // load 2^31 in f2
  184. {$ifndef macos}
  185. lis r4,factor2@ha
  186. lfd f2,factor2@l(r4)
  187. {$else}
  188. lwz r4,factor2(r2)
  189. lfd f2,0(r4)
  190. {$endif}
  191. // subtract 2^31
  192. fsub f3,f4,f2
  193. // was the value > 2^31?
  194. fcmpu cr1,f4,f2
  195. // use diff if >= 2^31
  196. fsel f4,f3,f3,f4
  197. // next part same as conversion to signed integer word
  198. fctiw f4,f4
  199. stfd f4,temp
  200. lwz r4,temp+4
  201. // add 2^31 if value was >=2^31
  202. blt cr1, .LRoundNoAdd
  203. xoris r4,r4,0x8000
  204. .LRoundNoAdd:
  205. // negate value if it was negative to start with
  206. beq cr0,.LRoundPositive
  207. subfic r4,r4,0
  208. subfze r3,r3
  209. .LRoundPositive:
  210. end;
  211. {$endif not FPC_SYSTEM_HAS_ROUND}
  212. *)
  213. {****************************************************************************
  214. Int to real helpers
  215. ****************************************************************************}
  216. {$define FPC_SYSTEM_HAS_INT64_TO_DOUBLE}
  217. function fpc_int64_to_double(i: int64): double; compilerproc;
  218. assembler;
  219. { input: high(i) in r4, low(i) in r3 }
  220. { output: double(i) in f0 }
  221. var
  222. temp: packed record
  223. case byte of
  224. 0: (l1,l2: cardinal);
  225. 1: (d: double);
  226. end;
  227. asm
  228. lis r0,0x4330
  229. stw r0,temp
  230. xoris r3,r3,0x8000
  231. stw r3,temp+4
  232. {$ifndef macos}
  233. lis r3,longint_to_real_helper@ha
  234. lfd f1,longint_to_real_helper@l(r3)
  235. {$else}
  236. lwz r3,longint_to_real_helper(r2)
  237. lfd f1,0(r3)
  238. {$endif}
  239. lfd f0,temp
  240. stw r4,temp+4
  241. fsub f0,f0,f1
  242. {$ifndef macos}
  243. lis r4,cardinal_to_real_helper@ha
  244. lfd f1,cardinal_to_real_helper@l(r4)
  245. lis r4,int_to_real_factor@ha
  246. lfd f3,temp
  247. lfd f2,int_to_real_factor@l(r4)
  248. {$else}
  249. lwz r4,cardinal_to_real_helper(r2)
  250. lwz r3,int_to_real_factor(r2)
  251. lfd f3,temp
  252. lfd f1,0(r4)
  253. lfd f2,0(r3)
  254. {$endif}
  255. fsub f3,f3,f1
  256. fmadd f1,f0,f2,f3
  257. end;
  258. {$define FPC_SYSTEM_HAS_QWORD_TO_DOUBLE}
  259. function fpc_qword_to_double(q: qword): double; compilerproc;
  260. assembler;
  261. { input: high(q) in r4, low(q) in r3 }
  262. { output: double(q) in f0 }
  263. var
  264. temp: packed record
  265. case byte of
  266. 0: (l1,l2: cardinal);
  267. 1: (d: double);
  268. end;
  269. asm
  270. lis r0,0x4330
  271. stw r0,temp
  272. stw r3,temp+4
  273. lfd f0,temp
  274. {$ifndef macos}
  275. lis r3,cardinal_to_real_helper@ha
  276. lfd f1,cardinal_to_real_helper@l(r3)
  277. {$else}
  278. lwz r3,longint_to_real_helper(r2)
  279. lfd f1,0(r3)
  280. {$endif}
  281. stw r4,temp+4
  282. fsub f0,f0,f1
  283. lfd f3,temp
  284. {$ifndef macos}
  285. lis r4,int_to_real_factor@ha
  286. lfd f2,int_to_real_factor@l(r4)
  287. {$else}
  288. lwz r4,int_to_real_factor(r2)
  289. lfd f2,0(r4)
  290. {$endif}
  291. fsub f3,f3,f1
  292. fmadd f1,f0,f2,f3
  293. end;