constexp.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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:qword;
  320. begin
  321. if a.overflow or b.overflow then
  322. begin
  323. result.overflow:=true;
  324. exit;
  325. end;
  326. result.overflow:=false;
  327. if a.signed then
  328. aa:=qword(a.svalue)
  329. else
  330. aa:=a.uvalue;
  331. if b.signed then
  332. bb:=qword(b.svalue)
  333. else
  334. bb:=b.uvalue;
  335. if bb=0 then
  336. result.overflow:=true
  337. else
  338. begin
  339. result.signed:=false;
  340. result.uvalue:=aa mod bb;
  341. end;
  342. end;
  343. operator / (const a,b:Tconstexprint):bestreal;
  344. var aa,bb:bestreal;
  345. begin
  346. if a.overflow or b.overflow then
  347. internalerror(200706096);
  348. if a.signed then
  349. aa:=a.svalue
  350. else
  351. aa:=a.uvalue;
  352. if b.signed then
  353. bb:=b.svalue
  354. else
  355. bb:=b.uvalue;
  356. result:=aa/bb;
  357. end;
  358. operator = (const a,b:Tconstexprint):boolean;
  359. begin
  360. if a.signed and (a.svalue<0) then
  361. if b.signed and (b.svalue<0) then
  362. result:=a.svalue=b.svalue
  363. else if b.uvalue>qword(high(int64)) then
  364. result:=false
  365. else
  366. result:=a.svalue=b.svalue
  367. else
  368. if not (b.signed and (b.svalue<0)) then
  369. result:=a.uvalue=b.uvalue
  370. else if a.uvalue>qword(high(int64)) then
  371. result:=false
  372. else
  373. result:=a.svalue=b.svalue
  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:=true
  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:=true
  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:=false
  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 and (const a,b:Tconstexprint):Tconstexprint;
  444. begin
  445. result.overflow:=false;
  446. result.signed:=a.signed or b.signed;
  447. result.uvalue:=a.uvalue and b.uvalue;
  448. end;
  449. operator or (const a,b:Tconstexprint):Tconstexprint;
  450. begin
  451. result.overflow:=false;
  452. result.signed:=a.signed or b.signed;
  453. result.uvalue:=a.uvalue or b.uvalue;
  454. end;
  455. operator xor (const a,b:Tconstexprint):Tconstexprint;
  456. begin
  457. result.overflow:=false;
  458. result.signed:=a.signed or b.signed;
  459. result.uvalue:=a.uvalue xor b.uvalue;
  460. end;
  461. operator shl (const a,b:Tconstexprint):Tconstexprint;
  462. begin
  463. result.overflow:=false;
  464. result.signed:=a.signed;
  465. result.uvalue:=a.uvalue shl b.uvalue;
  466. end;
  467. operator shr (const a,b:Tconstexprint):Tconstexprint;
  468. begin
  469. result.overflow:=false;
  470. result.signed:=a.signed;
  471. result.uvalue:=a.uvalue shr b.uvalue;
  472. end;
  473. function tostr(const i:Tconstexprint):shortstring;overload;
  474. begin
  475. if i.signed then
  476. str(i.svalue,result)
  477. else
  478. str(i.uvalue,result);
  479. end;
  480. end.