int64.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by the Free Pascal development team
  5. This file contains some helper routines for int64 and qword
  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. {$Q- no overflow checking }
  13. {$R- no range checking }
  14. type
  15. tqwordrec = packed record
  16. low : dword;
  17. high : dword;
  18. end;
  19. function count_leading_zeros(q : qword) : longint;
  20. var
  21. r,i : longint;
  22. begin
  23. r:=0;
  24. for i:=0 to 31 do
  25. begin
  26. if (tqwordrec(q).high and ($80000000 shr i))<>0 then
  27. begin
  28. count_leading_zeros:=r;
  29. exit;
  30. end;
  31. inc(r);
  32. end;
  33. for i:=0 to 31 do
  34. begin
  35. if (tqwordrec(q).low and ($80000000 shr i))<>0 then
  36. begin
  37. count_leading_zeros:=r;
  38. exit;
  39. end;
  40. inc(r);
  41. end;
  42. count_leading_zeros:=r;
  43. end;
  44. function divqword(n,z : qword) : qword;[public,alias: 'FPC_DIV_QWORD'];
  45. var
  46. shift,lzz,lzn : longint;
  47. { one : qword; }
  48. begin
  49. divqword:=0;
  50. if n=0 then
  51. HandleErrorFrame(200,get_frame);
  52. lzz:=count_leading_zeros(z);
  53. lzn:=count_leading_zeros(n);
  54. { if the denominator contains less zeros }
  55. { then the numerator }
  56. { the d is greater than the n }
  57. if lzn<lzz then
  58. exit;
  59. shift:=lzn-lzz;
  60. n:=n shl shift;
  61. repeat
  62. if z>=n then
  63. begin
  64. z:=z-n;
  65. divqword:=divqword+(qword(1) shl shift);
  66. end;
  67. dec(shift);
  68. n:=n shr 1;
  69. until shift<0;
  70. end;
  71. function modqword(n,z : qword) : qword;[public,alias: 'FPC_MOD_QWORD'];
  72. var
  73. shift,lzz,lzn : longint;
  74. begin
  75. modqword:=0;
  76. if n=0 then
  77. HandleErrorFrame(200,get_frame);
  78. lzz:=count_leading_zeros(z);
  79. lzn:=count_leading_zeros(n);
  80. { if the denominator contains less zeros }
  81. { then the numerator }
  82. { the d is greater than the n }
  83. if lzn<lzz then
  84. begin
  85. modqword:=z;
  86. exit;
  87. end;
  88. shift:=lzn-lzz;
  89. n:=n shl shift;
  90. repeat
  91. if z>=n then
  92. z:=z-n;
  93. dec(shift);
  94. n:=n shr 1;
  95. until shift<0;
  96. modqword:=z;
  97. end;
  98. function divint64(n,z : int64) : int64;[public,alias: 'FPC_DIV_INT64'];
  99. var
  100. sign : boolean;
  101. q1,q2 : qword;
  102. c : comp;
  103. begin
  104. if n=0 then
  105. HandleErrorFrame(200,get_frame);
  106. { can the fpu do the work? }
  107. if fpuint64 then
  108. begin
  109. // the c:=comp(...) is necessary to shut up the compiler
  110. c:=comp(comp(z)/comp(n));
  111. divint64:=qword(c);
  112. end
  113. else
  114. begin
  115. sign:=false;
  116. if z<0 then
  117. begin
  118. sign:=not(sign);
  119. q1:=qword(-z);
  120. end
  121. else
  122. q1:=z;
  123. if n<0 then
  124. begin
  125. sign:=not(sign);
  126. q2:=qword(-n);
  127. end
  128. else
  129. q2:=n;
  130. { the div is coded by the compiler as call to divqword }
  131. if sign then
  132. divint64:=-(q1 div q2)
  133. else
  134. divint64:=q1 div q2;
  135. end;
  136. end;
  137. function modint64(n,z : int64) : int64;[public,alias: 'FPC_MOD_INT64'];
  138. var
  139. signed : boolean;
  140. r,nq,zq : qword;
  141. begin
  142. if n=0 then
  143. HandleErrorFrame(200,get_frame);
  144. if n<0 then
  145. begin
  146. nq:=-n;
  147. signed:=true;
  148. end
  149. else
  150. begin
  151. signed:=false;
  152. nq:=n;
  153. end;
  154. if z<0 then
  155. begin
  156. zq:=qword(-z);
  157. signed:=not(signed);
  158. end
  159. else
  160. zq:=z;
  161. r:=zq mod nq;
  162. if signed then
  163. modint64:=-int64(r)
  164. else
  165. modint64:=r;
  166. end;
  167. { multiplies two qwords
  168. the longbool for checkoverflow avoids a misaligned stack
  169. }
  170. function mulqword(f1,f2 : qword;checkoverflow : longbool) : qword;[public,alias: 'FPC_MUL_QWORD'];
  171. var
  172. _f1,bitpos : qword;
  173. l : longint;
  174. {$ifdef i386}
  175. r : qword;
  176. {$endif i386}
  177. begin
  178. {$ifdef i386}
  179. if not(checkoverflow) then
  180. begin
  181. asm
  182. movl f1+4,%edx
  183. movl f2+4,%ecx
  184. orl %ecx,%edx
  185. movl f2,%edx
  186. movl f1,%eax
  187. jnz .Lqwordmultwomul
  188. mull %edx
  189. jmp .Lqwordmulready
  190. .Lqwordmultwomul:
  191. imul f1+4,%edx
  192. imul %eax,%ecx
  193. addl %edx,%ecx
  194. mull f2
  195. add %ecx,%edx
  196. .Lqwordmulready:
  197. movl %eax,r
  198. movl %edx,r+4
  199. end;
  200. mulqword:=r;
  201. end
  202. else
  203. {$endif i386}
  204. begin
  205. mulqword:=0;
  206. bitpos:=1;
  207. // store f1 for overflow checking
  208. _f1:=f1;
  209. for l:=0 to 63 do
  210. begin
  211. if (f2 and bitpos)<>0 then
  212. mulqword:=mulqword+f1;
  213. f1:=f1 shl 1;
  214. bitpos:=bitpos shl 1;
  215. end;
  216. { if one of the operands is greater than the result an }
  217. { overflow occurs }
  218. if checkoverflow and ((_f1>mulqword) or (f2>mulqword)) then
  219. HandleErrorFrame(215,get_frame);
  220. end;
  221. end;
  222. { multiplies two int64 ....
  223. fpuint64 = false:
  224. ... using the the qword multiplication
  225. fpuint64 = true:
  226. ... using the comp multiplication
  227. the longbool for checkoverflow avoids a misaligned stack
  228. }
  229. function mulint64(f1,f2 : int64;checkoverflow : longbool) : int64;[public,alias: 'FPC_MUL_INT64'];
  230. var
  231. sign : boolean;
  232. q1,q2,q3 : qword;
  233. c : comp;
  234. begin
  235. { can the fpu do the work ? }
  236. if fpuint64 and not(checkoverflow) then
  237. begin
  238. // the c:=comp(...) is necessary to shut up the compiler
  239. c:=comp(comp(f1)*comp(f2));
  240. mulint64:=int64(c);
  241. end
  242. else
  243. begin
  244. sign:=false;
  245. if f1<0 then
  246. begin
  247. sign:=not(sign);
  248. q1:=qword(-f1);
  249. end
  250. else
  251. q1:=f1;
  252. if f2<0 then
  253. begin
  254. sign:=not(sign);
  255. q2:=qword(-f2);
  256. end
  257. else
  258. q2:=f2;
  259. { the q1*q2 is coded as call to mulqword }
  260. q3:=q1*q2;
  261. if checkoverflow and ((q1>q3) or (q2>q3) or
  262. { the bit 63 can be only set if we have $80000000 00000000 }
  263. { and sign is true }
  264. ((tqwordrec(q3).high and $80000000)<>0) and
  265. ((q3<>(qword(1) shl 63)) or not(sign))
  266. ) then
  267. HandleErrorFrame(215,get_frame);
  268. if sign then
  269. mulint64:=-q3
  270. else
  271. mulint64:=q3;
  272. end;
  273. end;
  274. procedure qword_str(value : qword;var s : string);
  275. var
  276. hs : string;
  277. begin
  278. hs:='';
  279. repeat
  280. hs:=chr(longint(value mod 10)+48)+hs;
  281. value:=value div 10;
  282. until value=0;
  283. s:=hs;
  284. end;
  285. procedure int64_str(value : int64;var s : string);
  286. var
  287. hs : string;
  288. q : qword;
  289. begin
  290. if value<0 then
  291. begin
  292. q:=qword(-value);
  293. qword_str(q,hs);
  294. s:='-'+hs;
  295. end
  296. else
  297. qword_str(qword(value),s);
  298. end;
  299. procedure int_str_qword(v : qword;len : longint;var s : shortstring);[public,alias:'FPC_SHORTSTR_QWORD'];
  300. begin
  301. qword_str(v,s);
  302. if length(s)<len then
  303. s:=space(len-length(s))+s;
  304. end;
  305. procedure int_str_int64(v : int64;len : longint;var s : shortstring);[public,alias:'FPC_SHORTSTR_INT64'];
  306. begin
  307. int64_str(v,s);
  308. if length(s)<len then
  309. s:=space(len-length(s))+s;
  310. end;
  311. procedure int_str_qword(v : qword;len : longint;var s : ansistring);[public,alias:'FPC_ANSISTR_QWORD'];
  312. var
  313. ss : shortstring;
  314. begin
  315. int_str_qword(v,len,ss);
  316. s:=ss;
  317. end;
  318. procedure int_str_int64(v : int64;len : longint;var s : ansistring);[public,alias:'FPC_ANSISTR_INT64'];
  319. var
  320. ss : shortstring;
  321. begin
  322. int_str_int64(v,len,ss);
  323. s:=ss;
  324. end;
  325. Function ValInt64(DestSize: longint; Const S: ShortString; var Code: ValSInt): Int64; [public, alias:'FPC_VAL_INT64_SHORTSTR'];
  326. var
  327. u, temp, prev : Int64;
  328. base : byte;
  329. negative : boolean;
  330. begin
  331. ValInt64 := 0;
  332. Temp:=0;
  333. Code:=InitVal(s,negative,base);
  334. if Code>length(s) then
  335. exit;
  336. if negative and (s='-9223372036854775808') then
  337. begin
  338. Code:=0;
  339. ValInt64:=Int64($80000000) shl 32;
  340. exit;
  341. end;
  342. while Code<=Length(s) do
  343. begin
  344. case s[Code] of
  345. '0'..'9' : u:=Ord(S[Code])-Ord('0');
  346. 'A'..'F' : u:=Ord(S[Code])-(Ord('A')-10);
  347. 'a'..'f' : u:=Ord(S[Code])-(Ord('a')-10);
  348. else
  349. u:=16;
  350. end;
  351. Prev:=Temp;
  352. Temp:=Temp*Int64(base);
  353. if (Temp<prev) Then
  354. Begin
  355. ValInt64:=0;
  356. Exit
  357. End;
  358. prev:=temp;
  359. Temp:=Temp+u;
  360. if prev>temp then
  361. begin
  362. ValInt64:=0;
  363. exit;
  364. end;
  365. inc(code);
  366. end;
  367. code:=0;
  368. ValInt64:=Temp;
  369. If Negative Then
  370. ValInt64:=-ValInt64;
  371. end;
  372. Function ValQWord(Const S: ShortString; var Code: ValSInt): QWord; [public, alias:'FPC_VAL_QWORD_SHORTSTR'];
  373. var
  374. u, prev: QWord;
  375. base : byte;
  376. negative : boolean;
  377. begin
  378. ValQWord:=0;
  379. Code:=InitVal(s,negative,base);
  380. If Negative or (Code>length(s)) Then
  381. Exit;
  382. while Code<=Length(s) do
  383. begin
  384. case s[Code] of
  385. '0'..'9' : u:=Ord(S[Code])-Ord('0');
  386. 'A'..'F' : u:=Ord(S[Code])-(Ord('A')-10);
  387. 'a'..'f' : u:=Ord(S[Code])-(Ord('a')-10);
  388. else
  389. u:=16;
  390. end;
  391. prev := ValQWord;
  392. ValQWord:=ValQWord*QWord(base);
  393. If (prev>ValQWord) or (u>base) Then
  394. Begin
  395. ValQWord := 0;
  396. Exit
  397. End;
  398. prev:=ValQWord;
  399. ValQWord:=ValQWord+u;
  400. if prev>ValQWord then
  401. begin
  402. ValQWord:=0;
  403. exit;
  404. end;
  405. inc(code);
  406. end;
  407. code := 0;
  408. end;
  409. {
  410. $Log$
  411. Revision 1.19 2000-02-09 22:19:24 florian
  412. + helper routine for <int64> mod <in64> added
  413. Revision 1.18 2000/02/09 16:59:30 peter
  414. * truncated log
  415. Revision 1.17 2000/01/27 15:43:02 florian
  416. * improved qword*qword code, if no overflow checking is done
  417. Revision 1.16 2000/01/23 12:27:39 florian
  418. * int64/int64 and int64*int64 is now done by the fpu if possible
  419. Revision 1.15 2000/01/23 12:22:37 florian
  420. * reading of 64 bit type implemented
  421. Revision 1.14 2000/01/07 16:41:34 daniel
  422. * copyright 2000
  423. }