int64.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. This file contains some helper routines for int64 and qword
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$Q- no overflow checking }
  12. {$R- no range checking }
  13. type
  14. {$ifdef ENDIAN_LITTLE}
  15. tqwordrec = packed record
  16. low : dword;
  17. high : dword;
  18. end;
  19. {$endif ENDIAN_LITTLE}
  20. {$ifdef ENDIAN_BIG}
  21. tqwordrec = packed record
  22. high : dword;
  23. low : dword;
  24. end;
  25. {$endif ENDIAN_BIG}
  26. {$ifdef FPC_INCLUDE_SOFTWARE_SHIFT_INT64}
  27. {$ifndef FPC_SYSTEM_HAS_SHL_QWORD}
  28. function fpc_shl_qword(value,shift : qword) : qword; [public,alias: 'FPC_SHL_QWORD']; compilerproc;
  29. begin
  30. shift:=shift and 63;
  31. if shift=0 then
  32. result:=value
  33. else if shift>31 then
  34. begin
  35. tqwordrec(result).low:=0;
  36. tqwordrec(result).high:=tqwordrec(value).low shl (shift-32);
  37. end
  38. else
  39. begin
  40. tqwordrec(result).low:=tqwordrec(value).low shl shift;
  41. tqwordrec(result).high:=(tqwordrec(value).high shl shift) or (tqwordrec(value).low shr (32-shift));
  42. end;
  43. end;
  44. {$endif FPC_SYSTEM_HAS_SHL_QWORD}
  45. {$ifndef FPC_SYSTEM_HAS_SHR_QWORD}
  46. function fpc_shr_qword(value,shift : qword) : qword; [public,alias: 'FPC_SHR_QWORD']; compilerproc;
  47. begin
  48. shift:=shift and 63;
  49. if shift=0 then
  50. result:=value
  51. else if shift>31 then
  52. begin
  53. tqwordrec(result).high:=0;
  54. tqwordrec(result).low:=tqwordrec(value).high shr (shift-32);
  55. end
  56. else
  57. begin
  58. tqwordrec(result).high:=tqwordrec(value).high shr shift;
  59. tqwordrec(result).low:=(tqwordrec(value).low shr shift) or (tqwordrec(value).high shl (32-shift));
  60. end;
  61. end;
  62. {$endif FPC_SYSTEM_HAS_SHR_QWORD}
  63. {$ifndef FPC_SYSTEM_HAS_SHL_INT64}
  64. function fpc_shl_int64(value,shift : int64) : int64; [public,alias: 'FPC_SHL_INT64']; compilerproc;
  65. begin
  66. shift:=shift and 63;
  67. if shift=0 then
  68. result:=value
  69. else if shift>31 then
  70. begin
  71. tqwordrec(result).low:=0;
  72. tqwordrec(result).high:=tqwordrec(value).low shl (shift-32);
  73. end
  74. else
  75. begin
  76. tqwordrec(result).low:=tqwordrec(value).low shl shift;
  77. tqwordrec(result).high:=(tqwordrec(value).high shl shift) or (tqwordrec(value).low shr (32-shift));
  78. end;
  79. end;
  80. {$endif FPC_SYSTEM_HAS_SHL_INT64}
  81. {$ifndef FPC_SYSTEM_HAS_SHR_INT64}
  82. function fpc_shr_int64(value,shift : int64) : int64; [public,alias: 'FPC_SHR_INT64']; compilerproc;
  83. begin
  84. shift:=shift and 63;
  85. if shift=0 then
  86. result:=value
  87. else if shift>31 then
  88. begin
  89. tqwordrec(result).high:=0;
  90. tqwordrec(result).low:=tqwordrec(value).high shr (shift-32);
  91. end
  92. else
  93. begin
  94. tqwordrec(result).high:=tqwordrec(value).high shr shift;
  95. tqwordrec(result).low:=(tqwordrec(value).low shr shift) or (tqwordrec(value).high shl (32-shift));
  96. end;
  97. end;
  98. {$endif FPC_SYSTEM_HAS_SHR_INT64}
  99. {$endif FPC_INCLUDE_SOFTWARE_SHIFT_INT64}
  100. function count_leading_zeros(q : qword) : longint;
  101. var
  102. r,i : longint;
  103. begin
  104. r:=0;
  105. for i:=0 to 31 do
  106. begin
  107. if (tqwordrec(q).high and (dword($80000000) shr i))<>0 then
  108. begin
  109. count_leading_zeros:=r;
  110. exit;
  111. end;
  112. inc(r);
  113. end;
  114. for i:=0 to 31 do
  115. begin
  116. if (tqwordrec(q).low and (dword($80000000) shr i))<>0 then
  117. begin
  118. count_leading_zeros:=r;
  119. exit;
  120. end;
  121. inc(r);
  122. end;
  123. count_leading_zeros:=r;
  124. end;
  125. {$ifndef FPC_SYSTEM_HAS_DIV_QWORD}
  126. function fpc_div_qword(n,z : qword) : qword;[public,alias: 'FPC_DIV_QWORD']; compilerproc;
  127. var
  128. shift,lzz,lzn : longint;
  129. begin
  130. fpc_div_qword:=0;
  131. if n=0 then
  132. HandleErrorFrame(200,get_frame);
  133. lzz:=count_leading_zeros(z);
  134. lzn:=count_leading_zeros(n);
  135. { if the denominator contains less zeros }
  136. { then the numerator }
  137. { the d is greater than the n }
  138. if lzn<lzz then
  139. exit;
  140. shift:=lzn-lzz;
  141. n:=n shl shift;
  142. repeat
  143. if z>=n then
  144. begin
  145. z:=z-n;
  146. fpc_div_qword:=fpc_div_qword+(qword(1) shl shift);
  147. end;
  148. dec(shift);
  149. n:=n shr 1;
  150. until shift<0;
  151. end;
  152. {$endif FPC_SYSTEM_HAS_DIV_QWORD}
  153. {$ifndef FPC_SYSTEM_HAS_MOD_QWORD}
  154. function fpc_mod_qword(n,z : qword) : qword;[public,alias: 'FPC_MOD_QWORD']; compilerproc;
  155. var
  156. shift,lzz,lzn : longint;
  157. begin
  158. fpc_mod_qword:=0;
  159. if n=0 then
  160. HandleErrorFrame(200,get_frame);
  161. lzz:=count_leading_zeros(z);
  162. lzn:=count_leading_zeros(n);
  163. { if the denominator contains less zeros }
  164. { then the numerator }
  165. { the d is greater than the n }
  166. if lzn<lzz then
  167. begin
  168. fpc_mod_qword:=z;
  169. exit;
  170. end;
  171. shift:=lzn-lzz;
  172. n:=n shl shift;
  173. repeat
  174. if z>=n then
  175. z:=z-n;
  176. dec(shift);
  177. n:=n shr 1;
  178. until shift<0;
  179. fpc_mod_qword:=z;
  180. end;
  181. {$endif FPC_SYSTEM_HAS_MOD_QWORD}
  182. {$ifndef FPC_SYSTEM_HAS_DIV_INT64}
  183. function fpc_div_int64(n,z : int64) : int64;[public,alias: 'FPC_DIV_INT64']; compilerproc;
  184. var
  185. sign : boolean;
  186. q1,q2 : qword;
  187. begin
  188. if n=0 then
  189. HandleErrorFrame(200,get_frame);
  190. { can the fpu do the work? }
  191. begin
  192. sign:=false;
  193. if z<0 then
  194. begin
  195. sign:=not(sign);
  196. q1:=qword(-z);
  197. end
  198. else
  199. q1:=z;
  200. if n<0 then
  201. begin
  202. sign:=not(sign);
  203. q2:=qword(-n);
  204. end
  205. else
  206. q2:=n;
  207. { the div is coded by the compiler as call to divqword }
  208. if sign then
  209. fpc_div_int64:=-(q1 div q2)
  210. else
  211. fpc_div_int64:=q1 div q2;
  212. end;
  213. end;
  214. {$endif FPC_SYSTEM_HAS_DIV_INT64}
  215. {$ifndef FPC_SYSTEM_HAS_MOD_INT64}
  216. function fpc_mod_int64(n,z : int64) : int64;[public,alias: 'FPC_MOD_INT64']; compilerproc;
  217. var
  218. signed : boolean;
  219. r,nq,zq : qword;
  220. begin
  221. if n=0 then
  222. HandleErrorFrame(200,get_frame);
  223. if n<0 then
  224. nq:=-n
  225. else
  226. nq:=n;
  227. if z<0 then
  228. begin
  229. signed:=true;
  230. zq:=qword(-z)
  231. end
  232. else
  233. begin
  234. signed:=false;
  235. zq:=z;
  236. end;
  237. r:=zq mod nq;
  238. if signed then
  239. fpc_mod_int64:=-int64(r)
  240. else
  241. fpc_mod_int64:=r;
  242. end;
  243. {$endif FPC_SYSTEM_HAS_MOD_INT64}
  244. {$ifndef FPC_SYSTEM_HAS_MUL_QWORD}
  245. { multiplies two qwords
  246. the longbool for checkoverflow avoids a misaligned stack
  247. }
  248. function fpc_mul_qword(f1,f2 : qword;checkoverflow : longbool) : qword;[public,alias: 'FPC_MUL_QWORD']; compilerproc;
  249. var
  250. _f1,bitpos : qword;
  251. l : longint;
  252. f1overflowed : boolean;
  253. begin
  254. fpc_mul_qword:=0;
  255. bitpos:=1;
  256. f1overflowed:=false;
  257. for l:=0 to 63 do
  258. begin
  259. if (f2 and bitpos)<>0 then
  260. begin
  261. _f1:=fpc_mul_qword;
  262. fpc_mul_qword:=fpc_mul_qword+f1;
  263. { if one of the operands is greater than the result an
  264. overflow occurs }
  265. if checkoverflow and (f1overflowed or ((_f1<>0) and (f1<>0) and
  266. ((_f1>fpc_mul_qword) or (f1>fpc_mul_qword)))) then
  267. HandleErrorFrame(215,get_frame);
  268. end;
  269. { when bootstrapping, we forget about overflow checking for qword :) }
  270. f1overflowed:=f1overflowed or ((f1 and (1 shl 63))<>0);
  271. f1:=f1 shl 1;
  272. bitpos:=bitpos shl 1;
  273. end;
  274. end;
  275. {$endif FPC_SYSTEM_HAS_MUL_QWORD}
  276. {$ifndef FPC_SYSTEM_HAS_MUL_INT64}
  277. function fpc_mul_int64(f1,f2 : int64;checkoverflow : longbool) : int64;[public,alias: 'FPC_MUL_INT64']; compilerproc;
  278. var
  279. sign : boolean;
  280. q1,q2,q3 : qword;
  281. begin
  282. begin
  283. sign:=false;
  284. if f1<0 then
  285. begin
  286. sign:=not(sign);
  287. q1:=qword(-f1);
  288. end
  289. else
  290. q1:=f1;
  291. if f2<0 then
  292. begin
  293. sign:=not(sign);
  294. q2:=qword(-f2);
  295. end
  296. else
  297. q2:=f2;
  298. { the q1*q2 is coded as call to mulqword }
  299. q3:=q1*q2;
  300. if checkoverflow and (q1 <> 0) and (q2 <>0) and
  301. ((q1>q3) or (q2>q3) or
  302. { the bit 63 can be only set if we have $80000000 00000000 }
  303. { and sign is true }
  304. (q3 shr 63<>0) and
  305. ((q3<>qword(qword(1) shl 63)) or not(sign))
  306. ) then
  307. HandleErrorFrame(215,get_frame);
  308. if sign then
  309. fpc_mul_int64:=-q3
  310. else
  311. fpc_mul_int64:=q3;
  312. end;
  313. end;
  314. {$endif FPC_SYSTEM_HAS_MUL_INT64}