constexp.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. type Tconstexprint=record
  22. overflow:boolean;
  23. case signed:boolean of
  24. false:
  25. (uvalue:qword);
  26. true:
  27. (svalue:int64);
  28. end;
  29. errorproc=procedure (i:longint);
  30. {"Uses verbose" gives a dependency on cpuinfo through globals. This leads
  31. build trouble when compiling the directory utils, since the cpu directory
  32. isn't searched there. Therefore we use a procvar and make verbose install
  33. the errorhandler. A dependency from verbose on this unit is no problem.}
  34. var internalerror:errorproc;
  35. {Same issue, avoid dependency on cpuinfo because the cpu directory isn't
  36. searched during utils building.}
  37. {$ifdef GENERIC_CPU}
  38. type bestreal=extended;
  39. {$else}
  40. {$ifdef x86}
  41. type bestreal=extended;
  42. {$else}
  43. type bestreal=double;
  44. {$endif}
  45. {$endif}
  46. operator := (const u:qword):Tconstexprint;inline;
  47. operator := (const s:int64):Tconstexprint;inline;
  48. operator := (const c:Tconstexprint):qword;
  49. operator := (const c:Tconstexprint):int64;
  50. operator := (const c:Tconstexprint):bestreal;
  51. operator + (const a,b:Tconstexprint):Tconstexprint;
  52. operator - (const a,b:Tconstexprint):Tconstexprint;
  53. operator - (const a:Tconstexprint):Tconstexprint;
  54. operator * (const a,b:Tconstexprint):Tconstexprint;
  55. operator div (const a,b:Tconstexprint):Tconstexprint;
  56. operator mod (const a,b:Tconstexprint):Tconstexprint;
  57. operator / (const a,b:Tconstexprint):bestreal;
  58. operator = (const a,b:Tconstexprint):boolean;
  59. operator > (const a,b:Tconstexprint):boolean;
  60. operator >= (const a,b:Tconstexprint):boolean;
  61. operator < (const a,b:Tconstexprint):boolean;
  62. operator <= (const a,b:Tconstexprint):boolean;
  63. operator and (const a,b:Tconstexprint):Tconstexprint;
  64. operator or (const a,b:Tconstexprint):Tconstexprint;
  65. operator xor (const a,b:Tconstexprint):Tconstexprint;
  66. operator shl (const a,b:Tconstexprint):Tconstexprint;
  67. operator shr (const a,b:Tconstexprint):Tconstexprint;
  68. function tostr(const i:Tconstexprint):shortstring;overload;
  69. {****************************************************************************}
  70. implementation
  71. {****************************************************************************}
  72. operator := (const u:qword):Tconstexprint;
  73. begin
  74. result.overflow:=false;
  75. result.signed:=false;
  76. result.uvalue:=u;
  77. end;
  78. operator := (const s:int64):Tconstexprint;
  79. begin
  80. result.overflow:=false;
  81. result.signed:=true;
  82. result.svalue:=s;
  83. end;
  84. operator := (const c:Tconstexprint):qword;
  85. begin
  86. if c.overflow then
  87. internalerror(200706091)
  88. else if not c.signed then
  89. result:=c.uvalue
  90. else if c.svalue<0 then
  91. internalerror(200706092)
  92. else
  93. result:=qword(c.svalue);
  94. end;
  95. operator := (const c:Tconstexprint):int64;
  96. begin
  97. if c.overflow then
  98. internalerror(200706093)
  99. else if c.signed then
  100. result:=c.svalue
  101. else if c.uvalue>qword(high(int64)) then
  102. internalerror(200706094)
  103. else
  104. result:=int64(c.uvalue);
  105. end;
  106. operator := (const c:Tconstexprint):bestreal;
  107. begin
  108. if c.overflow then
  109. internalerror(200706095)
  110. else if c.signed then
  111. result:=c.svalue
  112. else
  113. result:=c.uvalue;
  114. end;
  115. function add_to(const a:Tconstexprint;b:qword):Tconstexprint;
  116. var sspace,uspace:qword;
  117. label try_qword;
  118. begin
  119. result.overflow:=false;
  120. {Try if the result fits in an int64.}
  121. if (a.signed) and (a.svalue<0) then
  122. {$push}{$Q-}
  123. sspace:=qword(high(int64))+qword(-a.svalue)
  124. {$pop}
  125. else if not a.signed and (a.uvalue>qword(high(int64))) then
  126. goto try_qword
  127. else
  128. sspace:=qword(high(int64))-a.svalue;
  129. if sspace>=b then
  130. begin
  131. result.signed:=true;
  132. {$push} {$Q-}
  133. result.svalue:=a.svalue+int64(b);
  134. {$pop}
  135. exit;
  136. end;
  137. {Try if the result fits in a qword.}
  138. try_qword:
  139. if (a.signed) and (a.svalue<0) then
  140. uspace:=high(qword)-qword(-a.svalue)
  141. { else if not a.signed and (a.uvalue>qword(high(int64))) then
  142. uspace:=high(qword)-a.uvalue}
  143. else
  144. uspace:=high(qword)-a.uvalue;
  145. if uspace>=b then
  146. begin
  147. result.signed:=false;
  148. {$push} {$Q-}
  149. result.uvalue:=a.uvalue+b;
  150. {$pop}
  151. exit;
  152. end;
  153. result.overflow:=true;
  154. end;
  155. function sub_from(const a:Tconstexprint;b:qword):Tconstexprint;
  156. const abs_low_int64=qword(9223372036854775808); {abs(low(int64)) -> overflow error}
  157. var sspace:qword;
  158. label try_qword,ov;
  159. begin
  160. result.overflow:=false;
  161. {Try if the result fits in an int64.}
  162. if (a.signed) and (a.svalue<0) then
  163. {$push} {$Q-}
  164. sspace:=qword(a.svalue)+abs_low_int64
  165. {$pop}
  166. else if not a.signed and (a.uvalue>qword(high(int64))) then
  167. goto try_qword
  168. else
  169. sspace:=a.uvalue+qword(abs(low(int64)));
  170. if sspace>=b then
  171. begin
  172. result.signed:=true;
  173. {$push} {$Q-}
  174. result.svalue:=a.svalue-int64(b);
  175. {$pop}
  176. exit;
  177. end;
  178. {Try if the result fits in a qword.}
  179. try_qword:
  180. if not(a.signed and (a.svalue<0)) and (a.uvalue>=b) then
  181. begin
  182. result.signed:=false;
  183. {$push} {$Q-}
  184. result.uvalue:=a.uvalue-b;
  185. {$pop}
  186. exit;
  187. end;
  188. ov:
  189. result.overflow:=true;
  190. end;
  191. operator + (const a,b:Tconstexprint):Tconstexprint;
  192. begin
  193. if a.overflow or b.overflow then
  194. begin
  195. result.overflow:=true;
  196. exit;
  197. end;
  198. if b.signed and (b.svalue<0) then
  199. {$push} {$Q-}
  200. result:=sub_from(a,qword(-b.svalue))
  201. {$pop}
  202. else
  203. result:=add_to(a,b.uvalue);
  204. end;
  205. operator - (const a,b:Tconstexprint):Tconstexprint;
  206. begin
  207. if a.overflow or b.overflow then
  208. begin
  209. result.overflow:=true;
  210. exit;
  211. end;
  212. if b.signed and (b.svalue<0) then
  213. {$push} {$Q-}
  214. result:=add_to(a,qword(-b.svalue))
  215. {$pop}
  216. else
  217. result:=sub_from(a,b.uvalue);
  218. end;
  219. operator - (const a:Tconstexprint):Tconstexprint;
  220. begin
  221. if not a.signed and (a.uvalue>qword(high(int64))) then
  222. result.overflow:=true
  223. else
  224. begin
  225. result.overflow:=false;
  226. result.signed:=true;
  227. result.svalue:=-a.svalue;
  228. end;
  229. end;
  230. operator * (const a,b:Tconstexprint):Tconstexprint;
  231. var aa,bb,r:qword;
  232. sa,sb:boolean;
  233. begin
  234. if a.overflow or b.overflow then
  235. begin
  236. result.overflow:=true;
  237. exit;
  238. end;
  239. result.overflow:=false;
  240. sa:=a.signed and (a.svalue<0);
  241. if sa then
  242. aa:=qword(-a.svalue)
  243. else
  244. aa:=a.uvalue;
  245. sb:=b.signed and (b.svalue<0);
  246. if sb then
  247. bb:=qword(-b.svalue)
  248. else
  249. bb:=b.uvalue;
  250. if (bb<>0) and (high(qword) div bb<aa) then
  251. result.overflow:=true
  252. else
  253. begin
  254. r:=aa*bb;
  255. if sa xor sb then
  256. begin
  257. result.signed:=true;
  258. if r>qword(high(int64)) then
  259. result.overflow:=true
  260. else
  261. result.svalue:=-int64(r);
  262. end
  263. else
  264. begin
  265. result.signed:=false;
  266. result.uvalue:=r;
  267. end;
  268. end;
  269. end;
  270. operator div (const a,b:Tconstexprint):Tconstexprint;
  271. var aa,bb,r:qword;
  272. sa,sb:boolean;
  273. begin
  274. if a.overflow or b.overflow then
  275. begin
  276. result.overflow:=true;
  277. exit;
  278. end;
  279. result.overflow:=false;
  280. sa:=a.signed and (a.svalue<0);
  281. if sa then
  282. {$push} {$Q-}
  283. aa:=qword(-a.svalue)
  284. {$pop}
  285. else
  286. aa:=a.uvalue;
  287. sb:=b.signed and (b.svalue<0);
  288. if sb then
  289. {$push} {$Q-}
  290. bb:=qword(-b.svalue)
  291. {$pop}
  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,r: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. sa:=a.signed and (a.svalue<0);
  325. if sa then
  326. {$push} {$Q-}
  327. aa:=qword(-a.svalue)
  328. {$pop}
  329. else
  330. aa:=a.uvalue;
  331. sb:=b.signed and (b.svalue<0);
  332. if sb then
  333. {$push} {$Q-}
  334. bb:=qword(-b.svalue)
  335. {$pop}
  336. else
  337. bb:=b.uvalue;
  338. if bb=0 then
  339. result.overflow:=true
  340. else
  341. begin
  342. { the sign of a modulo operation only depends on the sign of the
  343. dividend }
  344. r:=aa mod bb;
  345. result.signed:=sa;
  346. if not sa then
  347. result.uvalue:=r
  348. else
  349. result.svalue:=-int64(r);
  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.