constexp.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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. { workaround for 2.6.x bug }
  156. {$ifdef VER2_6}
  157. {$push} {$Q-}
  158. {$endif VER2_6}
  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: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. {$push} {$Q-}
  168. sspace:=qword(a.svalue)+abs_low_int64
  169. {$pop}
  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. {$push} {$Q-}
  178. result.svalue:=a.svalue-int64(b);
  179. {$pop}
  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. {$push} {$Q-}
  188. result.uvalue:=a.uvalue-b;
  189. {$pop}
  190. exit;
  191. end;
  192. ov:
  193. result.overflow:=true;
  194. end;
  195. { workaround for 2.6.x bug }
  196. {$ifdef VER2_6}
  197. {$pop}
  198. {$endif VER2_6}
  199. operator + (const a,b:Tconstexprint):Tconstexprint;
  200. begin
  201. if a.overflow or b.overflow then
  202. begin
  203. result.overflow:=true;
  204. exit;
  205. end;
  206. if b.signed and (b.svalue<0) then
  207. {$push} {$Q-}
  208. result:=sub_from(a,qword(-b.svalue))
  209. {$pop}
  210. else
  211. result:=add_to(a,b.uvalue);
  212. end;
  213. operator - (const a,b:Tconstexprint):Tconstexprint;
  214. begin
  215. if a.overflow or b.overflow then
  216. begin
  217. result.overflow:=true;
  218. exit;
  219. end;
  220. if b.signed and (b.svalue<0) then
  221. {$push} {$Q-}
  222. result:=add_to(a,qword(-b.svalue))
  223. {$pop}
  224. else
  225. result:=sub_from(a,b.uvalue);
  226. end;
  227. operator - (const a:Tconstexprint):Tconstexprint;
  228. begin
  229. if not a.signed and (a.uvalue>qword(high(int64))) then
  230. result.overflow:=true
  231. else
  232. begin
  233. result.overflow:=false;
  234. result.signed:=true;
  235. result.svalue:=-a.svalue;
  236. end;
  237. end;
  238. operator * (const a,b:Tconstexprint):Tconstexprint;
  239. var aa,bb,r:qword;
  240. sa,sb:boolean;
  241. begin
  242. if a.overflow or b.overflow then
  243. begin
  244. result.overflow:=true;
  245. exit;
  246. end;
  247. result.overflow:=false;
  248. sa:=a.signed and (a.svalue<0);
  249. if sa then
  250. aa:=qword(-a.svalue)
  251. else
  252. aa:=a.uvalue;
  253. sb:=b.signed and (b.svalue<0);
  254. if sb then
  255. bb:=qword(-b.svalue)
  256. else
  257. bb:=b.uvalue;
  258. if (bb<>0) and (high(qword) div bb<aa) then
  259. result.overflow:=true
  260. else
  261. begin
  262. r:=aa*bb;
  263. if sa xor sb then
  264. begin
  265. result.signed:=true;
  266. if r>qword(high(int64)) then
  267. result.overflow:=true
  268. else
  269. result.svalue:=-int64(r);
  270. end
  271. else
  272. begin
  273. result.signed:=false;
  274. result.uvalue:=r;
  275. end;
  276. end;
  277. end;
  278. operator div (const a,b:Tconstexprint):Tconstexprint;
  279. var aa,bb,r:qword;
  280. sa,sb:boolean;
  281. begin
  282. if a.overflow or b.overflow then
  283. begin
  284. result.overflow:=true;
  285. exit;
  286. end;
  287. result.overflow:=false;
  288. sa:=a.signed and (a.svalue<0);
  289. if sa then
  290. {$push} {$Q-}
  291. aa:=qword(-a.svalue)
  292. {$pop}
  293. else
  294. aa:=a.uvalue;
  295. sb:=b.signed and (b.svalue<0);
  296. if sb then
  297. {$push} {$Q-}
  298. bb:=qword(-b.svalue)
  299. {$pop}
  300. else
  301. bb:=b.uvalue;
  302. if bb=0 then
  303. result.overflow:=true
  304. else
  305. begin
  306. r:=aa div bb;
  307. if sa xor sb then
  308. begin
  309. result.signed:=true;
  310. if r>qword(high(int64)) then
  311. result.overflow:=true
  312. else
  313. result.svalue:=-int64(r);
  314. end
  315. else
  316. begin
  317. result.signed:=false;
  318. result.uvalue:=r;
  319. end;
  320. end;
  321. end;
  322. operator mod (const a,b:Tconstexprint):Tconstexprint;
  323. var aa,bb,r:qword;
  324. sa,sb:boolean;
  325. begin
  326. if a.overflow or b.overflow then
  327. begin
  328. result.overflow:=true;
  329. exit;
  330. end;
  331. result.overflow:=false;
  332. sa:=a.signed and (a.svalue<0);
  333. if sa then
  334. {$push} {$Q-}
  335. aa:=qword(-a.svalue)
  336. {$pop}
  337. else
  338. aa:=a.uvalue;
  339. sb:=b.signed and (b.svalue<0);
  340. if sb then
  341. {$push} {$Q-}
  342. bb:=qword(-b.svalue)
  343. {$pop}
  344. else
  345. bb:=b.uvalue;
  346. if bb=0 then
  347. result.overflow:=true
  348. else
  349. begin
  350. { the sign of a modulo operation only depends on the sign of the
  351. dividend }
  352. r:=aa mod bb;
  353. result.signed:=sa;
  354. if not sa then
  355. result.uvalue:=r
  356. else
  357. result.svalue:=-int64(r);
  358. end;
  359. end;
  360. operator / (const a,b:Tconstexprint):bestreal;
  361. var aa,bb:bestreal;
  362. begin
  363. if a.overflow or b.overflow then
  364. internalerror(200706096);
  365. if a.signed then
  366. aa:=a.svalue
  367. else
  368. aa:=a.uvalue;
  369. if b.signed then
  370. bb:=b.svalue
  371. else
  372. bb:=b.uvalue;
  373. result:=aa/bb;
  374. end;
  375. operator = (const a,b:Tconstexprint):boolean;
  376. begin
  377. if a.signed and (a.svalue<0) then
  378. if b.signed and (b.svalue<0) then
  379. result:=a.svalue=b.svalue
  380. else if b.uvalue>qword(high(int64)) then
  381. result:=false
  382. else
  383. result:=a.svalue=b.svalue
  384. else
  385. if not (b.signed and (b.svalue<0)) then
  386. result:=a.uvalue=b.uvalue
  387. else if a.uvalue>qword(high(int64)) then
  388. result:=false
  389. else
  390. result:=a.svalue=b.svalue
  391. end;
  392. operator > (const a,b:Tconstexprint):boolean;
  393. begin
  394. if a.signed and (a.svalue<0) then
  395. if b.signed and (b.svalue<0) then
  396. result:=a.svalue>b.svalue
  397. else if b.uvalue>qword(high(int64)) then
  398. result:=false
  399. else
  400. result:=a.svalue>b.svalue
  401. else
  402. if not (b.signed and (b.svalue<0)) then
  403. result:=a.uvalue>b.uvalue
  404. else if a.uvalue>qword(high(int64)) then
  405. result:=true
  406. else
  407. result:=a.svalue>b.svalue
  408. end;
  409. operator >= (const a,b:Tconstexprint):boolean;
  410. begin
  411. if a.signed and (a.svalue<0) then
  412. if b.signed and (b.svalue<0) then
  413. result:=a.svalue>=b.svalue
  414. else if b.uvalue>qword(high(int64)) then
  415. result:=false
  416. else
  417. result:=a.svalue>=b.svalue
  418. else
  419. if not (b.signed and (b.svalue<0)) then
  420. result:=a.uvalue>=b.uvalue
  421. else if a.uvalue>qword(high(int64)) then
  422. result:=true
  423. else
  424. result:=a.svalue>=b.svalue
  425. end;
  426. operator < (const a,b:Tconstexprint):boolean;
  427. begin
  428. if a.signed and (a.svalue<0) then
  429. if b.signed and (b.svalue<0) then
  430. result:=a.svalue<b.svalue
  431. else if b.uvalue>qword(high(int64)) then
  432. result:=true
  433. else
  434. result:=a.svalue<b.svalue
  435. else
  436. if not (b.signed and (b.svalue<0)) then
  437. result:=a.uvalue<b.uvalue
  438. else if a.uvalue>qword(high(int64)) then
  439. result:=false
  440. else
  441. result:=a.svalue<b.svalue
  442. end;
  443. operator <= (const a,b:Tconstexprint):boolean;
  444. begin
  445. if a.signed and (a.svalue<0) then
  446. if b.signed and (b.svalue<0) then
  447. result:=a.svalue<=b.svalue
  448. else if b.uvalue>qword(high(int64)) then
  449. result:=true
  450. else
  451. result:=a.svalue<=b.svalue
  452. else
  453. if not (b.signed and (b.svalue<0)) then
  454. result:=a.uvalue<=b.uvalue
  455. else if a.uvalue>qword(high(int64)) then
  456. result:=false
  457. else
  458. result:=a.svalue<=b.svalue
  459. end;
  460. operator and (const a,b:Tconstexprint):Tconstexprint;
  461. begin
  462. result.overflow:=false;
  463. result.signed:=a.signed or b.signed;
  464. result.uvalue:=a.uvalue and b.uvalue;
  465. end;
  466. operator or (const a,b:Tconstexprint):Tconstexprint;
  467. begin
  468. result.overflow:=false;
  469. result.signed:=a.signed or b.signed;
  470. result.uvalue:=a.uvalue or b.uvalue;
  471. end;
  472. operator xor (const a,b:Tconstexprint):Tconstexprint;
  473. begin
  474. result.overflow:=false;
  475. result.signed:=a.signed or b.signed;
  476. result.uvalue:=a.uvalue xor b.uvalue;
  477. end;
  478. operator shl (const a,b:Tconstexprint):Tconstexprint;
  479. begin
  480. result.overflow:=false;
  481. result.signed:=a.signed;
  482. result.uvalue:=a.uvalue shl b.uvalue;
  483. end;
  484. operator shr (const a,b:Tconstexprint):Tconstexprint;
  485. begin
  486. result.overflow:=false;
  487. result.signed:=a.signed;
  488. result.uvalue:=a.uvalue shr b.uvalue;
  489. end;
  490. function tostr(const i:Tconstexprint):shortstring;overload;
  491. begin
  492. if i.signed then
  493. str(i.svalue,result)
  494. else
  495. str(i.uvalue,result);
  496. end;
  497. end.