constexp.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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 internalerrorproc: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. { use a separate procedure here instead of calling internalerrorproc directly because
  73. - procedure variables cannot have a noreturn directive
  74. - having a procedure and a procedure variable with the same name in the interfaces of different units is confusing }
  75. procedure internalerror(i:longint);{$ifndef VER2_6}noreturn;{$endif VER2_6}
  76. begin
  77. internalerrorproc(i);
  78. end;
  79. operator := (const u:qword):Tconstexprint;
  80. begin
  81. result.overflow:=false;
  82. result.signed:=false;
  83. result.uvalue:=u;
  84. end;
  85. operator := (const s:int64):Tconstexprint;
  86. begin
  87. result.overflow:=false;
  88. result.signed:=true;
  89. result.svalue:=s;
  90. end;
  91. operator := (const c:Tconstexprint):qword;
  92. begin
  93. if c.overflow then
  94. internalerror(200706091)
  95. else if not c.signed then
  96. result:=c.uvalue
  97. else if c.svalue<0 then
  98. internalerror(200706092)
  99. else
  100. result:=qword(c.svalue);
  101. end;
  102. operator := (const c:Tconstexprint):int64;
  103. begin
  104. if c.overflow then
  105. internalerror(200706093)
  106. else if c.signed then
  107. result:=c.svalue
  108. else if c.uvalue>qword(high(int64)) then
  109. internalerror(200706094)
  110. else
  111. result:=int64(c.uvalue);
  112. end;
  113. operator := (const c:Tconstexprint):bestreal;
  114. begin
  115. if c.overflow then
  116. internalerror(200706095)
  117. else if c.signed then
  118. result:=c.svalue
  119. else
  120. result:=c.uvalue;
  121. end;
  122. function add_to(const a:Tconstexprint;b:qword):Tconstexprint;
  123. var sspace,uspace:qword;
  124. label try_qword;
  125. begin
  126. result.overflow:=false;
  127. {Try if the result fits in an int64.}
  128. if (a.signed) and (a.svalue<0) then
  129. {$push}{$Q-}
  130. sspace:=qword(high(int64))+qword(-a.svalue)
  131. {$pop}
  132. else if not a.signed and (a.uvalue>qword(high(int64))) then
  133. goto try_qword
  134. else
  135. sspace:=qword(high(int64))-a.svalue;
  136. if sspace>=b then
  137. begin
  138. result.signed:=true;
  139. {$push} {$Q-}
  140. result.svalue:=a.svalue+int64(b);
  141. {$pop}
  142. exit;
  143. end;
  144. {Try if the result fits in a qword.}
  145. try_qword:
  146. if (a.signed) and (a.svalue<0) then
  147. uspace:=high(qword)-qword(-a.svalue)
  148. { else if not a.signed and (a.uvalue>qword(high(int64))) then
  149. uspace:=high(qword)-a.uvalue}
  150. else
  151. uspace:=high(qword)-a.uvalue;
  152. if uspace>=b then
  153. begin
  154. result.signed:=false;
  155. {$push} {$Q-}
  156. result.uvalue:=a.uvalue+b;
  157. {$pop}
  158. exit;
  159. end;
  160. result.overflow:=true;
  161. end;
  162. { workaround for 2.6.x bug }
  163. {$ifdef VER2_6}
  164. {$push} {$Q-}
  165. {$endif VER2_6}
  166. function sub_from(const a:Tconstexprint;b:qword):Tconstexprint;
  167. const abs_low_int64=qword(9223372036854775808); {abs(low(int64)) -> overflow error}
  168. var sspace:qword;
  169. label try_qword,ov;
  170. begin
  171. result.overflow:=false;
  172. {Try if the result fits in an int64.}
  173. if (a.signed) and (a.svalue<0) then
  174. {$push} {$Q-}
  175. sspace:=qword(a.svalue)+abs_low_int64
  176. {$pop}
  177. else if not a.signed and (a.uvalue>qword(high(int64))) then
  178. goto try_qword
  179. else
  180. sspace:=a.uvalue+qword(abs(low(int64)));
  181. if sspace>=b then
  182. begin
  183. result.signed:=true;
  184. {$push} {$Q-}
  185. result.svalue:=a.svalue-int64(b);
  186. {$pop}
  187. exit;
  188. end;
  189. {Try if the result fits in a qword.}
  190. try_qword:
  191. if not(a.signed and (a.svalue<0)) and (a.uvalue>=b) then
  192. begin
  193. result.signed:=false;
  194. {$push} {$Q-}
  195. result.uvalue:=a.uvalue-b;
  196. {$pop}
  197. exit;
  198. end;
  199. ov:
  200. result.overflow:=true;
  201. end;
  202. { workaround for 2.6.x bug }
  203. {$ifdef VER2_6}
  204. {$pop}
  205. {$endif VER2_6}
  206. operator + (const a,b:Tconstexprint):Tconstexprint;
  207. begin
  208. if a.overflow or b.overflow then
  209. begin
  210. result.overflow:=true;
  211. exit;
  212. end;
  213. if b.signed and (b.svalue<0) then
  214. {$push} {$Q-}
  215. result:=sub_from(a,qword(-b.svalue))
  216. {$pop}
  217. else
  218. result:=add_to(a,b.uvalue);
  219. end;
  220. operator - (const a,b:Tconstexprint):Tconstexprint;
  221. begin
  222. if a.overflow or b.overflow then
  223. begin
  224. result.overflow:=true;
  225. exit;
  226. end;
  227. if b.signed and (b.svalue<0) then
  228. {$push} {$Q-}
  229. result:=add_to(a,qword(-b.svalue))
  230. {$pop}
  231. else
  232. result:=sub_from(a,b.uvalue);
  233. end;
  234. operator - (const a:Tconstexprint):Tconstexprint;
  235. begin
  236. if not a.signed and (a.uvalue>qword(high(int64))) then
  237. result.overflow:=true
  238. else
  239. begin
  240. result.overflow:=false;
  241. result.signed:=true;
  242. result.svalue:=-a.svalue;
  243. end;
  244. end;
  245. operator * (const a,b:Tconstexprint):Tconstexprint;
  246. var aa,bb,r:qword;
  247. sa,sb:boolean;
  248. begin
  249. if a.overflow or b.overflow then
  250. begin
  251. result.overflow:=true;
  252. exit;
  253. end;
  254. result.overflow:=false;
  255. sa:=a.signed and (a.svalue<0);
  256. if sa then
  257. aa:=qword(-a.svalue)
  258. else
  259. aa:=a.uvalue;
  260. sb:=b.signed and (b.svalue<0);
  261. if sb then
  262. bb:=qword(-b.svalue)
  263. else
  264. bb:=b.uvalue;
  265. if (bb<>0) and (high(qword) div bb<aa) then
  266. result.overflow:=true
  267. else
  268. begin
  269. r:=aa*bb;
  270. if sa xor sb then
  271. begin
  272. result.signed:=true;
  273. if r>qword(high(int64)) then
  274. result.overflow:=true
  275. else
  276. result.svalue:=-int64(r);
  277. end
  278. else
  279. begin
  280. result.signed:=false;
  281. result.uvalue:=r;
  282. end;
  283. end;
  284. end;
  285. operator div (const a,b:Tconstexprint):Tconstexprint;
  286. var aa,bb,r:qword;
  287. sa,sb:boolean;
  288. begin
  289. if a.overflow or b.overflow then
  290. begin
  291. result.overflow:=true;
  292. exit;
  293. end;
  294. result.overflow:=false;
  295. sa:=a.signed and (a.svalue<0);
  296. if sa then
  297. {$push} {$Q-}
  298. aa:=qword(-a.svalue)
  299. {$pop}
  300. else
  301. aa:=a.uvalue;
  302. sb:=b.signed and (b.svalue<0);
  303. if sb then
  304. {$push} {$Q-}
  305. bb:=qword(-b.svalue)
  306. {$pop}
  307. else
  308. bb:=b.uvalue;
  309. if bb=0 then
  310. result.overflow:=true
  311. else
  312. begin
  313. r:=aa div bb;
  314. if sa xor sb then
  315. begin
  316. result.signed:=true;
  317. if r>qword(high(int64)) then
  318. result.overflow:=true
  319. else
  320. result.svalue:=-int64(r);
  321. end
  322. else
  323. begin
  324. result.signed:=false;
  325. result.uvalue:=r;
  326. end;
  327. end;
  328. end;
  329. operator mod (const a,b:Tconstexprint):Tconstexprint;
  330. var aa,bb,r:qword;
  331. sa,sb:boolean;
  332. begin
  333. if a.overflow or b.overflow then
  334. begin
  335. result.overflow:=true;
  336. exit;
  337. end;
  338. result.overflow:=false;
  339. sa:=a.signed and (a.svalue<0);
  340. if sa then
  341. {$push} {$Q-}
  342. aa:=qword(-a.svalue)
  343. {$pop}
  344. else
  345. aa:=a.uvalue;
  346. sb:=b.signed and (b.svalue<0);
  347. if sb then
  348. {$push} {$Q-}
  349. bb:=qword(-b.svalue)
  350. {$pop}
  351. else
  352. bb:=b.uvalue;
  353. if bb=0 then
  354. result.overflow:=true
  355. else
  356. begin
  357. { the sign of a modulo operation only depends on the sign of the
  358. dividend }
  359. r:=aa mod bb;
  360. result.signed:=sa;
  361. if not sa then
  362. result.uvalue:=r
  363. else
  364. result.svalue:=-int64(r);
  365. end;
  366. end;
  367. operator / (const a,b:Tconstexprint):bestreal;
  368. var aa,bb:bestreal;
  369. begin
  370. if a.overflow or b.overflow then
  371. internalerror(200706096);
  372. if a.signed then
  373. aa:=a.svalue
  374. else
  375. aa:=a.uvalue;
  376. if b.signed then
  377. bb:=b.svalue
  378. else
  379. bb:=b.uvalue;
  380. result:=aa/bb;
  381. end;
  382. operator = (const a,b:Tconstexprint):boolean;
  383. begin
  384. if a.signed and (a.svalue<0) then
  385. if b.signed and (b.svalue<0) then
  386. result:=a.svalue=b.svalue
  387. else if b.uvalue>qword(high(int64)) then
  388. result:=false
  389. else
  390. result:=a.svalue=b.svalue
  391. else
  392. if not (b.signed and (b.svalue<0)) then
  393. result:=a.uvalue=b.uvalue
  394. else if a.uvalue>qword(high(int64)) then
  395. result:=false
  396. else
  397. result:=a.svalue=b.svalue
  398. end;
  399. operator > (const a,b:Tconstexprint):boolean;
  400. begin
  401. if a.signed and (a.svalue<0) then
  402. if b.signed and (b.svalue<0) then
  403. result:=a.svalue>b.svalue
  404. else if b.uvalue>qword(high(int64)) then
  405. result:=false
  406. else
  407. result:=a.svalue>b.svalue
  408. else
  409. if not (b.signed and (b.svalue<0)) then
  410. result:=a.uvalue>b.uvalue
  411. else if a.uvalue>qword(high(int64)) then
  412. result:=true
  413. else
  414. result:=a.svalue>b.svalue
  415. end;
  416. operator >= (const a,b:Tconstexprint):boolean;
  417. begin
  418. if a.signed and (a.svalue<0) then
  419. if b.signed and (b.svalue<0) then
  420. result:=a.svalue>=b.svalue
  421. else if b.uvalue>qword(high(int64)) then
  422. result:=false
  423. else
  424. result:=a.svalue>=b.svalue
  425. else
  426. if not (b.signed and (b.svalue<0)) then
  427. result:=a.uvalue>=b.uvalue
  428. else if a.uvalue>qword(high(int64)) then
  429. result:=true
  430. else
  431. result:=a.svalue>=b.svalue
  432. end;
  433. operator < (const a,b:Tconstexprint):boolean;
  434. begin
  435. if a.signed and (a.svalue<0) then
  436. if b.signed and (b.svalue<0) then
  437. result:=a.svalue<b.svalue
  438. else if b.uvalue>qword(high(int64)) then
  439. result:=true
  440. else
  441. result:=a.svalue<b.svalue
  442. else
  443. if not (b.signed and (b.svalue<0)) then
  444. result:=a.uvalue<b.uvalue
  445. else if a.uvalue>qword(high(int64)) then
  446. result:=false
  447. else
  448. result:=a.svalue<b.svalue
  449. end;
  450. operator <= (const a,b:Tconstexprint):boolean;
  451. begin
  452. if a.signed and (a.svalue<0) then
  453. if b.signed and (b.svalue<0) then
  454. result:=a.svalue<=b.svalue
  455. else if b.uvalue>qword(high(int64)) then
  456. result:=true
  457. else
  458. result:=a.svalue<=b.svalue
  459. else
  460. if not (b.signed and (b.svalue<0)) then
  461. result:=a.uvalue<=b.uvalue
  462. else if a.uvalue>qword(high(int64)) then
  463. result:=false
  464. else
  465. result:=a.svalue<=b.svalue
  466. end;
  467. operator and (const a,b:Tconstexprint):Tconstexprint;
  468. begin
  469. result.overflow:=false;
  470. result.signed:=a.signed or b.signed;
  471. result.uvalue:=a.uvalue and b.uvalue;
  472. end;
  473. operator or (const a,b:Tconstexprint):Tconstexprint;
  474. begin
  475. result.overflow:=false;
  476. result.signed:=a.signed or b.signed;
  477. result.uvalue:=a.uvalue or b.uvalue;
  478. end;
  479. operator xor (const a,b:Tconstexprint):Tconstexprint;
  480. begin
  481. result.overflow:=false;
  482. result.signed:=a.signed or b.signed;
  483. result.uvalue:=a.uvalue xor b.uvalue;
  484. end;
  485. operator shl (const a,b:Tconstexprint):Tconstexprint;
  486. begin
  487. result.overflow:=false;
  488. result.signed:=a.signed;
  489. result.uvalue:=a.uvalue shl b.uvalue;
  490. end;
  491. operator shr (const a,b:Tconstexprint):Tconstexprint;
  492. begin
  493. result.overflow:=false;
  494. result.signed:=a.signed;
  495. result.uvalue:=a.uvalue shr b.uvalue;
  496. end;
  497. function tostr(const i:Tconstexprint):shortstring;overload;
  498. begin
  499. if i.signed then
  500. str(i.svalue,result)
  501. else
  502. str(i.uvalue,result);
  503. end;
  504. end.