constexp.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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>qword(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: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. {$Q-}
  287. aa:=qword(-a.svalue)
  288. {$ifdef ena_q}{$Q+}{$endif}
  289. else
  290. aa:=a.uvalue;
  291. sb:=b.signed and (b.svalue<0);
  292. if sb then
  293. {$Q-}
  294. bb:=qword(-b.svalue)
  295. {$ifdef ena_q}{$Q+}{$endif}
  296. else
  297. bb:=b.uvalue;
  298. if bb=0 then
  299. result.overflow:=true
  300. else
  301. begin
  302. r:=aa div bb;
  303. if sa xor sb then
  304. begin
  305. result.signed:=true;
  306. if r>qword(high(int64)) then
  307. result.overflow:=true
  308. else
  309. result.svalue:=-int64(r);
  310. end
  311. else
  312. begin
  313. result.signed:=false;
  314. result.uvalue:=r;
  315. end;
  316. end;
  317. end;
  318. operator mod (const a,b:Tconstexprint):Tconstexprint;
  319. var aa,bb,r:qword;
  320. sa,sb:boolean;
  321. begin
  322. if a.overflow or b.overflow then
  323. begin
  324. result.overflow:=true;
  325. exit;
  326. end;
  327. result.overflow:=false;
  328. sa:=a.signed and (a.svalue<0);
  329. if sa then
  330. {$Q-}
  331. aa:=qword(-a.svalue)
  332. {$ifdef ena_q}{$Q+}{$endif}
  333. else
  334. aa:=a.uvalue;
  335. sb:=b.signed and (b.svalue<0);
  336. if sb then
  337. {$Q-}
  338. bb:=qword(-b.svalue)
  339. {$ifdef ena_q}{$Q+}{$endif}
  340. else
  341. bb:=b.uvalue;
  342. if bb=0 then
  343. result.overflow:=true
  344. else
  345. begin
  346. { the sign of a modulo operation only depends on the sign of the
  347. dividend }
  348. r:=aa mod bb;
  349. result.signed:=sa;
  350. if not sa then
  351. result.uvalue:=r
  352. else
  353. result.svalue:=-int64(r);
  354. end;
  355. end;
  356. operator / (const a,b:Tconstexprint):bestreal;
  357. var aa,bb:bestreal;
  358. begin
  359. if a.overflow or b.overflow then
  360. internalerror(200706096);
  361. if a.signed then
  362. aa:=a.svalue
  363. else
  364. aa:=a.uvalue;
  365. if b.signed then
  366. bb:=b.svalue
  367. else
  368. bb:=b.uvalue;
  369. result:=aa/bb;
  370. end;
  371. operator = (const a,b:Tconstexprint):boolean;
  372. begin
  373. if a.signed and (a.svalue<0) then
  374. if b.signed and (b.svalue<0) then
  375. result:=a.svalue=b.svalue
  376. else if b.uvalue>qword(high(int64)) then
  377. result:=false
  378. else
  379. result:=a.svalue=b.svalue
  380. else
  381. if not (b.signed and (b.svalue<0)) then
  382. result:=a.uvalue=b.uvalue
  383. else if a.uvalue>qword(high(int64)) then
  384. result:=false
  385. else
  386. result:=a.svalue=b.svalue
  387. end;
  388. operator > (const a,b:Tconstexprint):boolean;
  389. begin
  390. if a.signed and (a.svalue<0) then
  391. if b.signed and (b.svalue<0) then
  392. result:=a.svalue>b.svalue
  393. else if b.uvalue>qword(high(int64)) then
  394. result:=false
  395. else
  396. result:=a.svalue>b.svalue
  397. else
  398. if not (b.signed and (b.svalue<0)) then
  399. result:=a.uvalue>b.uvalue
  400. else if a.uvalue>qword(high(int64)) then
  401. result:=true
  402. else
  403. result:=a.svalue>b.svalue
  404. end;
  405. operator >= (const a,b:Tconstexprint):boolean;
  406. begin
  407. if a.signed and (a.svalue<0) then
  408. if b.signed and (b.svalue<0) then
  409. result:=a.svalue>=b.svalue
  410. else if b.uvalue>qword(high(int64)) then
  411. result:=false
  412. else
  413. result:=a.svalue>=b.svalue
  414. else
  415. if not (b.signed and (b.svalue<0)) then
  416. result:=a.uvalue>=b.uvalue
  417. else if a.uvalue>qword(high(int64)) then
  418. result:=true
  419. else
  420. result:=a.svalue>=b.svalue
  421. end;
  422. operator < (const a,b:Tconstexprint):boolean;
  423. begin
  424. if a.signed and (a.svalue<0) then
  425. if b.signed and (b.svalue<0) then
  426. result:=a.svalue<b.svalue
  427. else if b.uvalue>qword(high(int64)) then
  428. result:=true
  429. else
  430. result:=a.svalue<b.svalue
  431. else
  432. if not (b.signed and (b.svalue<0)) then
  433. result:=a.uvalue<b.uvalue
  434. else if a.uvalue>qword(high(int64)) then
  435. result:=false
  436. else
  437. result:=a.svalue<b.svalue
  438. end;
  439. operator <= (const a,b:Tconstexprint):boolean;
  440. begin
  441. if a.signed and (a.svalue<0) then
  442. if b.signed and (b.svalue<0) then
  443. result:=a.svalue<=b.svalue
  444. else if b.uvalue>qword(high(int64)) then
  445. result:=true
  446. else
  447. result:=a.svalue<=b.svalue
  448. else
  449. if not (b.signed and (b.svalue<0)) then
  450. result:=a.uvalue<=b.uvalue
  451. else if a.uvalue>qword(high(int64)) then
  452. result:=false
  453. else
  454. result:=a.svalue<=b.svalue
  455. end;
  456. operator and (const a,b:Tconstexprint):Tconstexprint;
  457. begin
  458. result.overflow:=false;
  459. result.signed:=a.signed or b.signed;
  460. result.uvalue:=a.uvalue and b.uvalue;
  461. end;
  462. operator or (const a,b:Tconstexprint):Tconstexprint;
  463. begin
  464. result.overflow:=false;
  465. result.signed:=a.signed or b.signed;
  466. result.uvalue:=a.uvalue or b.uvalue;
  467. end;
  468. operator xor (const a,b:Tconstexprint):Tconstexprint;
  469. begin
  470. result.overflow:=false;
  471. result.signed:=a.signed or b.signed;
  472. result.uvalue:=a.uvalue xor b.uvalue;
  473. end;
  474. operator shl (const a,b:Tconstexprint):Tconstexprint;
  475. begin
  476. result.overflow:=false;
  477. result.signed:=a.signed;
  478. result.uvalue:=a.uvalue shl b.uvalue;
  479. end;
  480. operator shr (const a,b:Tconstexprint):Tconstexprint;
  481. begin
  482. result.overflow:=false;
  483. result.signed:=a.signed;
  484. result.uvalue:=a.uvalue shr b.uvalue;
  485. end;
  486. function tostr(const i:Tconstexprint):shortstring;overload;
  487. begin
  488. if i.signed then
  489. str(i.svalue,result)
  490. else
  491. str(i.uvalue,result);
  492. end;
  493. end.