constexp.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. {
  2. Copyright (c) 2007 by Daniel Mantione
  3. This unit implements a Tconstexprint type. This type simulates an integer
  4. type that can handle numbers from low(int64) to high(qword) calculations.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit constexp;
  19. {$i fpcdefs.inc}
  20. interface
  21. { bootstrapping with 2.0.x }
  22. {$ifdef VER2_0}
  23. {$Q-}
  24. {$R-}
  25. {$endif}
  26. {$ifopt q+}
  27. {$define ena_q}
  28. {$endif}
  29. type Tconstexprint=record
  30. overflow:boolean;
  31. case signed:boolean of
  32. false:
  33. (uvalue:qword);
  34. true:
  35. (svalue:int64);
  36. end;
  37. errorproc=procedure (i:longint);
  38. {"Uses verbose" gives a dependency on cpuinfo through globals. This leads
  39. build trouble when compiling the directory utils, since the cpu directory
  40. isn't searched there. Therefore we use a procvar and make verbose install
  41. the errorhandler. A dependency from verbose on this unit is no problem.}
  42. var internalerror:errorproc;
  43. {Same issue, avoid dependency on cpuinfo because the cpu directory isn't
  44. searched during utils building.}
  45. {$ifdef x86}
  46. type bestreal=extended;
  47. {$else}
  48. type bestreal=double;
  49. {$endif}
  50. operator := (const u:qword):Tconstexprint;inline;
  51. operator := (const s:int64):Tconstexprint;inline;
  52. operator := (const c:Tconstexprint):qword;
  53. operator := (const c:Tconstexprint):int64;
  54. operator := (const c:Tconstexprint):bestreal;
  55. operator + (const a,b:Tconstexprint):Tconstexprint;
  56. operator - (const a,b:Tconstexprint):Tconstexprint;
  57. operator - (const a:Tconstexprint):Tconstexprint;
  58. operator * (const a,b:Tconstexprint):Tconstexprint;
  59. operator div (const a,b:Tconstexprint):Tconstexprint;
  60. operator mod (const a,b:Tconstexprint):Tconstexprint;
  61. operator / (const a,b:Tconstexprint):bestreal;
  62. operator = (const a,b:Tconstexprint):boolean;
  63. operator > (const a,b:Tconstexprint):boolean;
  64. operator >= (const a,b:Tconstexprint):boolean;
  65. operator < (const a,b:Tconstexprint):boolean;
  66. operator <= (const a,b:Tconstexprint):boolean;
  67. operator and (const a,b:Tconstexprint):Tconstexprint;
  68. operator or (const a,b:Tconstexprint):Tconstexprint;
  69. operator xor (const a,b:Tconstexprint):Tconstexprint;
  70. operator shl (const a,b:Tconstexprint):Tconstexprint;
  71. operator shr (const a,b:Tconstexprint):Tconstexprint;
  72. function tostr(const i:Tconstexprint):shortstring;overload;
  73. {****************************************************************************}
  74. implementation
  75. {****************************************************************************}
  76. operator := (const u:qword):Tconstexprint;
  77. begin
  78. result.overflow:=false;
  79. result.signed:=false;
  80. result.uvalue:=u;
  81. end;
  82. operator := (const s:int64):Tconstexprint;
  83. begin
  84. result.overflow:=false;
  85. result.signed:=true;
  86. result.svalue:=s;
  87. end;
  88. operator := (const c:Tconstexprint):qword;
  89. begin
  90. if c.overflow then
  91. internalerror(200706091)
  92. else if not c.signed then
  93. result:=c.uvalue
  94. else if c.svalue<0 then
  95. internalerror(200706092)
  96. else
  97. result:=qword(c.svalue);
  98. end;
  99. operator := (const c:Tconstexprint):int64;
  100. begin
  101. if c.overflow then
  102. internalerror(200706093)
  103. else if c.signed then
  104. result:=c.svalue
  105. else if c.uvalue>high(int64) then
  106. internalerror(200706094)
  107. else
  108. result:=int64(c.uvalue);
  109. end;
  110. operator := (const c:Tconstexprint):bestreal;
  111. begin
  112. if c.overflow then
  113. internalerror(200706095)
  114. else if c.signed then
  115. result:=c.svalue
  116. else
  117. result:=c.uvalue;
  118. end;
  119. function add_to(const a:Tconstexprint;b:qword):Tconstexprint;
  120. var sspace,uspace:qword;
  121. label try_qword;
  122. begin
  123. result.overflow:=false;
  124. {Try if the result fits in an int64.}
  125. if (a.signed) and (a.svalue<0) then
  126. {$Q-}
  127. sspace:=qword(high(int64))-qword(-a.svalue)
  128. {$ifdef ena_q}{$Q+}{$endif}
  129. else if not a.signed and (a.uvalue>qword(high(int64))) then
  130. goto try_qword
  131. else
  132. sspace:=qword(high(int64))-a.svalue;
  133. if sspace>=b then
  134. begin
  135. result.signed:=true;
  136. {$Q-}
  137. result.svalue:=a.svalue+int64(b);
  138. {$ifdef ena_q}{$Q+}{$endif}
  139. exit;
  140. end;
  141. {Try if the result fits in a qword.}
  142. try_qword:
  143. if (a.signed) and (a.svalue<0) then
  144. uspace:=high(qword)-qword(-a.svalue)
  145. { else if not a.signed and (a.uvalue>qword(high(int64))) then
  146. uspace:=high(qword)-a.uvalue}
  147. else
  148. uspace:=high(qword)-a.uvalue;
  149. if uspace>=b then
  150. begin
  151. result.signed:=false;
  152. {$Q-}
  153. result.uvalue:=a.uvalue+b;
  154. {$ifdef ena_q}{$Q+}{$endif}
  155. exit;
  156. end;
  157. result.overflow:=true;
  158. end;
  159. function sub_from(const a:Tconstexprint;b:qword):Tconstexprint;
  160. const abs_low_int64=qword(9223372036854775808); {abs(low(int64)) -> overflow error}
  161. var sspace,uspace:qword;
  162. label try_qword,ov;
  163. begin
  164. result.overflow:=false;
  165. {Try if the result fits in an int64.}
  166. if (a.signed) and (a.svalue<0) then
  167. {$Q-}
  168. sspace:=qword(a.svalue)+abs_low_int64
  169. {$ifdef ena_q}{$Q+}{$endif}
  170. else if not a.signed and (a.uvalue>qword(high(int64))) then
  171. goto try_qword
  172. else
  173. sspace:=a.uvalue+qword(abs(low(int64)));
  174. if sspace>=b then
  175. begin
  176. result.signed:=true;
  177. {$Q-}
  178. result.svalue:=a.svalue-int64(b);
  179. {$ifdef ena_q}{$Q+}{$endif}
  180. exit;
  181. end;
  182. {Try if the result fits in a qword.}
  183. try_qword:
  184. if not(a.signed and (a.svalue<0)) and (a.uvalue>=b) then
  185. begin
  186. result.signed:=false;
  187. {$Q-}
  188. result.uvalue:=a.uvalue-b;
  189. {$ifdef ena_q}{$Q+}{$endif}
  190. exit;
  191. end;
  192. ov:
  193. result.overflow:=true;
  194. end;
  195. operator + (const a,b:Tconstexprint):Tconstexprint;
  196. begin
  197. if a.overflow or b.overflow then
  198. begin
  199. result.overflow:=true;
  200. exit;
  201. end;
  202. if b.signed and (b.svalue<0) then
  203. {$Q-}
  204. result:=sub_from(a,qword(-b.svalue))
  205. {$ifdef ena_q}{$Q+}{$endif}
  206. else
  207. result:=add_to(a,b.uvalue);
  208. end;
  209. operator - (const a,b:Tconstexprint):Tconstexprint;
  210. begin
  211. if a.overflow or b.overflow then
  212. begin
  213. result.overflow:=true;
  214. exit;
  215. end;
  216. if b.signed and (b.svalue<0) then
  217. {$Q-}
  218. result:=add_to(a,qword(-b.svalue))
  219. {$ifdef ena_q}{$Q+}{$endif}
  220. else
  221. result:=sub_from(a,b.uvalue);
  222. end;
  223. operator - (const a:Tconstexprint):Tconstexprint;
  224. begin
  225. if not a.signed and (a.uvalue>qword(high(int64))) then
  226. result.overflow:=true
  227. else
  228. begin
  229. result.overflow:=false;
  230. result.signed:=true;
  231. result.svalue:=-a.svalue;
  232. end;
  233. end;
  234. operator * (const a,b:Tconstexprint):Tconstexprint;
  235. var aa,bb,r:qword;
  236. sa,sb:boolean;
  237. begin
  238. if a.overflow or b.overflow then
  239. begin
  240. result.overflow:=true;
  241. exit;
  242. end;
  243. result.overflow:=false;
  244. sa:=a.signed and (a.svalue<0);
  245. if sa then
  246. aa:=qword(-a.svalue)
  247. else
  248. aa:=a.uvalue;
  249. sb:=b.signed and (b.svalue<0);
  250. if sb then
  251. bb:=qword(-b.svalue)
  252. else
  253. bb:=b.uvalue;
  254. if (bb<>0) and (high(qword) div bb<aa) then
  255. result.overflow:=true
  256. else
  257. begin
  258. r:=aa*bb;
  259. if sa xor sb then
  260. begin
  261. result.signed:=true;
  262. if r>qword(high(int64)) then
  263. result.overflow:=true
  264. else
  265. result.svalue:=-int64(r);
  266. end
  267. else
  268. begin
  269. result.signed:=false;
  270. result.uvalue:=r;
  271. end;
  272. end;
  273. end;
  274. operator div (const a,b:Tconstexprint):Tconstexprint;
  275. var aa,bb,r:qword;
  276. sa,sb:boolean;
  277. begin
  278. if a.overflow or b.overflow then
  279. begin
  280. result.overflow:=true;
  281. exit;
  282. end;
  283. result.overflow:=false;
  284. sa:=a.signed and (a.svalue<0);
  285. if sa then
  286. aa:=qword(-a.svalue)
  287. else
  288. aa:=a.uvalue;
  289. sb:=b.signed and (b.svalue<0);
  290. if sb then
  291. bb:=qword(-b.svalue)
  292. else
  293. bb:=b.uvalue;
  294. if bb=0 then
  295. result.overflow:=true
  296. else
  297. begin
  298. r:=aa div bb;
  299. if sa xor sb then
  300. begin
  301. result.signed:=true;
  302. if r>qword(high(int64)) then
  303. result.overflow:=true
  304. else
  305. result.svalue:=-int64(r);
  306. end
  307. else
  308. begin
  309. result.signed:=false;
  310. result.uvalue:=r;
  311. end;
  312. end;
  313. end;
  314. operator mod (const a,b:Tconstexprint):Tconstexprint;
  315. var aa,bb:qword;
  316. sa,sb:boolean;
  317. begin
  318. if a.overflow or b.overflow then
  319. begin
  320. result.overflow:=true;
  321. exit;
  322. end;
  323. result.overflow:=false;
  324. if a.signed then
  325. begin
  326. aa:=qword(a.svalue);
  327. sa:=a.svalue<0;
  328. end
  329. else
  330. begin
  331. aa:=a.uvalue;
  332. sa:=false;
  333. end;
  334. if b.signed then
  335. begin
  336. bb:=qword(b.svalue);
  337. sb:=b.svalue<0;
  338. end
  339. else
  340. begin
  341. bb:=b.uvalue;
  342. sb:=false;
  343. end;
  344. if bb=0 then
  345. result.overflow:=true
  346. else
  347. begin
  348. result.signed:=false;
  349. result.uvalue:=aa mod bb;
  350. end;
  351. end;
  352. operator / (const a,b:Tconstexprint):bestreal;
  353. var aa,bb:bestreal;
  354. begin
  355. if a.overflow or b.overflow then
  356. internalerror(200706096);
  357. if a.signed then
  358. aa:=a.svalue
  359. else
  360. aa:=a.uvalue;
  361. if b.signed then
  362. bb:=b.svalue
  363. else
  364. bb:=b.uvalue;
  365. result:=aa/bb;
  366. end;
  367. operator = (const a,b:Tconstexprint):boolean;
  368. begin
  369. if a.signed and (a.svalue<0) then
  370. if b.signed and (b.svalue<0) then
  371. result:=a.svalue=b.svalue
  372. else if b.uvalue>qword(high(int64)) then
  373. result:=false
  374. else
  375. result:=a.svalue=b.svalue
  376. else
  377. if not (b.signed and (b.svalue<0)) then
  378. result:=a.uvalue=b.uvalue
  379. else if a.uvalue>qword(high(int64)) then
  380. result:=false
  381. else
  382. result:=a.svalue=b.svalue
  383. end;
  384. operator > (const a,b:Tconstexprint):boolean;
  385. begin
  386. if a.signed and (a.svalue<0) then
  387. if b.signed and (b.svalue<0) then
  388. result:=a.svalue>b.svalue
  389. else if b.uvalue>qword(high(int64)) then
  390. result:=false
  391. else
  392. result:=a.svalue>b.svalue
  393. else
  394. if not (b.signed and (b.svalue<0)) then
  395. result:=a.uvalue>b.uvalue
  396. else if a.uvalue>qword(high(int64)) then
  397. result:=true
  398. else
  399. result:=a.svalue>b.svalue
  400. end;
  401. operator >= (const a,b:Tconstexprint):boolean;
  402. begin
  403. if a.signed and (a.svalue<0) then
  404. if b.signed and (b.svalue<0) then
  405. result:=a.svalue>=b.svalue
  406. else if b.uvalue>qword(high(int64)) then
  407. result:=false
  408. else
  409. result:=a.svalue>=b.svalue
  410. else
  411. if not (b.signed and (b.svalue<0)) then
  412. result:=a.uvalue>=b.uvalue
  413. else if a.uvalue>qword(high(int64)) then
  414. result:=true
  415. else
  416. result:=a.svalue>=b.svalue
  417. end;
  418. operator < (const a,b:Tconstexprint):boolean;
  419. begin
  420. if a.signed and (a.svalue<0) then
  421. if b.signed and (b.svalue<0) then
  422. result:=a.svalue<b.svalue
  423. else if b.uvalue>qword(high(int64)) then
  424. result:=true
  425. else
  426. result:=a.svalue<b.svalue
  427. else
  428. if not (b.signed and (b.svalue<0)) then
  429. result:=a.uvalue<b.uvalue
  430. else if a.uvalue>qword(high(int64)) then
  431. result:=false
  432. else
  433. result:=a.svalue<b.svalue
  434. end;
  435. operator <= (const a,b:Tconstexprint):boolean;
  436. begin
  437. if a.signed and (a.svalue<0) then
  438. if b.signed and (b.svalue<0) then
  439. result:=a.svalue<=b.svalue
  440. else if b.uvalue>qword(high(int64)) then
  441. result:=true
  442. else
  443. result:=a.svalue<=b.svalue
  444. else
  445. if not (b.signed and (b.svalue<0)) then
  446. result:=a.uvalue<=b.uvalue
  447. else if a.uvalue>qword(high(int64)) then
  448. result:=false
  449. else
  450. result:=a.svalue<=b.svalue
  451. end;
  452. operator and (const a,b:Tconstexprint):Tconstexprint;
  453. begin
  454. result.overflow:=false;
  455. result.signed:=a.signed or b.signed;
  456. result.uvalue:=a.uvalue and b.uvalue;
  457. end;
  458. operator or (const a,b:Tconstexprint):Tconstexprint;
  459. begin
  460. result.overflow:=false;
  461. result.signed:=a.signed or b.signed;
  462. result.uvalue:=a.uvalue or b.uvalue;
  463. end;
  464. operator xor (const a,b:Tconstexprint):Tconstexprint;
  465. begin
  466. result.overflow:=false;
  467. result.signed:=a.signed or b.signed;
  468. result.uvalue:=a.uvalue xor b.uvalue;
  469. end;
  470. operator shl (const a,b:Tconstexprint):Tconstexprint;
  471. begin
  472. result.overflow:=false;
  473. result.signed:=a.signed;
  474. result.uvalue:=a.uvalue shl b.uvalue;
  475. end;
  476. operator shr (const a,b:Tconstexprint):Tconstexprint;
  477. begin
  478. result.overflow:=false;
  479. result.signed:=a.signed;
  480. result.uvalue:=a.uvalue shr b.uvalue;
  481. end;
  482. function tostr(const i:Tconstexprint):shortstring;overload;
  483. begin
  484. if i.signed then
  485. str(i.svalue,result)
  486. else
  487. str(i.uvalue,result);
  488. end;
  489. end.