constexp.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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+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. {$push} {$Q-}
  243. result.svalue:=-a.svalue;
  244. {$pop}
  245. end;
  246. end;
  247. operator * (const a,b:Tconstexprint):Tconstexprint;
  248. var aa,bb,r:qword;
  249. sa,sb:boolean;
  250. begin
  251. if a.overflow or b.overflow then
  252. begin
  253. result.overflow:=true;
  254. exit;
  255. end;
  256. result.overflow:=false;
  257. sa:=a.signed and (a.svalue<0);
  258. if sa then
  259. aa:=qword(-a.svalue)
  260. else
  261. aa:=a.uvalue;
  262. sb:=b.signed and (b.svalue<0);
  263. if sb then
  264. bb:=qword(-b.svalue)
  265. else
  266. bb:=b.uvalue;
  267. if (bb<>0) and (high(qword) div bb<aa) then
  268. result.overflow:=true
  269. else
  270. begin
  271. r:=aa*bb;
  272. if sa xor sb then
  273. begin
  274. result.signed:=true;
  275. if r>qword(high(int64)) then
  276. result.overflow:=true
  277. else
  278. result.svalue:=-int64(r);
  279. end
  280. else
  281. begin
  282. result.signed:=false;
  283. result.uvalue:=r;
  284. end;
  285. end;
  286. end;
  287. operator div (const a,b:Tconstexprint):Tconstexprint;
  288. var aa,bb,r:qword;
  289. sa,sb:boolean;
  290. begin
  291. if a.overflow or b.overflow then
  292. begin
  293. result.overflow:=true;
  294. exit;
  295. end;
  296. result.overflow:=false;
  297. sa:=a.signed and (a.svalue<0);
  298. if sa then
  299. {$push} {$Q-}
  300. aa:=qword(-a.svalue)
  301. {$pop}
  302. else
  303. aa:=a.uvalue;
  304. sb:=b.signed and (b.svalue<0);
  305. if sb then
  306. {$push} {$Q-}
  307. bb:=qword(-b.svalue)
  308. {$pop}
  309. else
  310. bb:=b.uvalue;
  311. if bb=0 then
  312. result.overflow:=true
  313. else
  314. begin
  315. r:=aa div bb;
  316. if sa xor sb then
  317. begin
  318. result.signed:=true;
  319. if r>qword(high(int64)) then
  320. result.overflow:=true
  321. else
  322. result.svalue:=-int64(r);
  323. end
  324. else
  325. begin
  326. result.signed:=false;
  327. result.uvalue:=r;
  328. end;
  329. end;
  330. end;
  331. operator mod (const a,b:Tconstexprint):Tconstexprint;
  332. var aa,bb,r:qword;
  333. sa,sb:boolean;
  334. begin
  335. if a.overflow or b.overflow then
  336. begin
  337. result.overflow:=true;
  338. exit;
  339. end;
  340. result.overflow:=false;
  341. sa:=a.signed and (a.svalue<0);
  342. if sa then
  343. {$push} {$Q-}
  344. aa:=qword(-a.svalue)
  345. {$pop}
  346. else
  347. aa:=a.uvalue;
  348. sb:=b.signed and (b.svalue<0);
  349. if sb then
  350. {$push} {$Q-}
  351. bb:=qword(-b.svalue)
  352. {$pop}
  353. else
  354. bb:=b.uvalue;
  355. if bb=0 then
  356. result.overflow:=true
  357. else
  358. begin
  359. { the sign of a modulo operation only depends on the sign of the
  360. dividend }
  361. r:=aa mod bb;
  362. result.signed:=sa;
  363. if not sa then
  364. result.uvalue:=r
  365. else
  366. result.svalue:=-int64(r);
  367. end;
  368. end;
  369. operator / (const a,b:Tconstexprint):bestreal;
  370. var aa,bb:bestreal;
  371. begin
  372. if a.overflow or b.overflow then
  373. internalerror(200706096);
  374. if a.signed then
  375. aa:=a.svalue
  376. else
  377. aa:=a.uvalue;
  378. if b.signed then
  379. bb:=b.svalue
  380. else
  381. bb:=b.uvalue;
  382. result:=aa/bb;
  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:=false
  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:=false
  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:=true
  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 <= (const a,b:Tconstexprint):boolean;
  453. begin
  454. if a.signed and (a.svalue<0) then
  455. if b.signed and (b.svalue<0) then
  456. result:=a.svalue<=b.svalue
  457. else if b.uvalue>qword(high(int64)) then
  458. result:=true
  459. else
  460. result:=a.svalue<=b.svalue
  461. else
  462. if not (b.signed and (b.svalue<0)) then
  463. result:=a.uvalue<=b.uvalue
  464. else if a.uvalue>qword(high(int64)) then
  465. result:=false
  466. else
  467. result:=a.svalue<=b.svalue
  468. end;
  469. operator and (const a,b:Tconstexprint):Tconstexprint;
  470. begin
  471. result.overflow:=false;
  472. result.signed:=a.signed or b.signed;
  473. result.uvalue:=a.uvalue and b.uvalue;
  474. end;
  475. operator or (const a,b:Tconstexprint):Tconstexprint;
  476. begin
  477. result.overflow:=false;
  478. result.signed:=a.signed or b.signed;
  479. result.uvalue:=a.uvalue or b.uvalue;
  480. end;
  481. operator xor (const a,b:Tconstexprint):Tconstexprint;
  482. begin
  483. result.overflow:=false;
  484. result.signed:=a.signed or b.signed;
  485. result.uvalue:=a.uvalue xor b.uvalue;
  486. end;
  487. operator shl (const a,b:Tconstexprint):Tconstexprint;
  488. begin
  489. result.overflow:=false;
  490. result.signed:=a.signed;
  491. result.uvalue:=a.uvalue shl b.uvalue;
  492. end;
  493. operator shr (const a,b:Tconstexprint):Tconstexprint;
  494. begin
  495. result.overflow:=false;
  496. result.signed:=a.signed;
  497. result.uvalue:=a.uvalue shr b.uvalue;
  498. end;
  499. function tostr(const i:Tconstexprint):shortstring;overload;
  500. begin
  501. if i.signed then
  502. str(i.svalue,result)
  503. else
  504. str(i.uvalue,result);
  505. end;
  506. end.