constexp.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. Tconststring = type pchar;
  38. errorproc=procedure (i:longint);
  39. {"Uses verbose" gives a dependency on cpuinfo through globals. This leads
  40. build trouble when compiling the directory utils, since the cpu directory
  41. isn't searched there. Therefore we use a procvar and make verbose install
  42. the errorhandler. A dependency from verbose on this unit is no problem.}
  43. var internalerror:errorproc;
  44. {Same issue, avoid dependency on cpuinfo because the cpu directory isn't
  45. searched during utils building.}
  46. {$ifdef x86}
  47. type bestreal=extended;
  48. {$else}
  49. type bestreal=double;
  50. {$endif}
  51. operator := (const u:qword):Tconstexprint;inline;
  52. operator := (const s:int64):Tconstexprint;inline;
  53. operator := (const c:Tconstexprint):qword;
  54. operator := (const c:Tconstexprint):int64;
  55. operator := (const c:Tconstexprint):bestreal;
  56. operator + (const a,b:Tconstexprint):Tconstexprint;
  57. operator - (const a,b:Tconstexprint):Tconstexprint;
  58. operator - (const a:Tconstexprint):Tconstexprint;
  59. operator * (const a,b:Tconstexprint):Tconstexprint;
  60. operator div (const a,b:Tconstexprint):Tconstexprint;
  61. operator mod (const a,b:Tconstexprint):Tconstexprint;
  62. operator / (const a,b:Tconstexprint):bestreal;
  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 <= (const a,b:Tconstexprint):boolean;
  68. operator and (const a,b:Tconstexprint):Tconstexprint;
  69. operator or (const a,b:Tconstexprint):Tconstexprint;
  70. operator xor (const a,b:Tconstexprint):Tconstexprint;
  71. operator shl (const a,b:Tconstexprint):Tconstexprint;
  72. operator shr (const a,b:Tconstexprint):Tconstexprint;
  73. function tostr(const i:Tconstexprint):shortstring;overload;
  74. {****************************************************************************}
  75. implementation
  76. {****************************************************************************}
  77. operator := (const u:qword):Tconstexprint;
  78. begin
  79. result.overflow:=false;
  80. result.signed:=false;
  81. result.uvalue:=u;
  82. end;
  83. operator := (const s:int64):Tconstexprint;
  84. begin
  85. result.overflow:=false;
  86. result.signed:=true;
  87. result.svalue:=s;
  88. end;
  89. operator := (const c:Tconstexprint):qword;
  90. begin
  91. if c.overflow then
  92. internalerror(200706091)
  93. else if not c.signed then
  94. result:=c.uvalue
  95. else if c.svalue<0 then
  96. internalerror(200706092)
  97. else
  98. result:=qword(c.svalue);
  99. end;
  100. operator := (const c:Tconstexprint):int64;
  101. begin
  102. if c.overflow then
  103. internalerror(200706093)
  104. else if c.signed then
  105. result:=c.svalue
  106. else if c.uvalue>qword(high(int64)) then
  107. internalerror(200706094)
  108. else
  109. result:=int64(c.uvalue);
  110. end;
  111. operator := (const c:Tconstexprint):bestreal;
  112. begin
  113. if c.overflow then
  114. internalerror(200706095)
  115. else if c.signed then
  116. result:=c.svalue
  117. else
  118. result:=c.uvalue;
  119. end;
  120. function add_to(const a:Tconstexprint;b:qword):Tconstexprint;
  121. var sspace,uspace:qword;
  122. label try_qword;
  123. begin
  124. result.overflow:=false;
  125. {Try if the result fits in an int64.}
  126. if (a.signed) and (a.svalue<0) then
  127. {$Q-}
  128. sspace:=qword(high(int64))+qword(-a.svalue)
  129. {$ifdef ena_q}{$Q+}{$endif}
  130. else if not a.signed and (a.uvalue>qword(high(int64))) then
  131. goto try_qword
  132. else
  133. sspace:=qword(high(int64))-a.svalue;
  134. if sspace>=b then
  135. begin
  136. result.signed:=true;
  137. {$Q-}
  138. result.svalue:=a.svalue+int64(b);
  139. {$ifdef ena_q}{$Q+}{$endif}
  140. exit;
  141. end;
  142. {Try if the result fits in a qword.}
  143. try_qword:
  144. if (a.signed) and (a.svalue<0) then
  145. uspace:=high(qword)-qword(-a.svalue)
  146. { else if not a.signed and (a.uvalue>qword(high(int64))) then
  147. uspace:=high(qword)-a.uvalue}
  148. else
  149. uspace:=high(qword)-a.uvalue;
  150. if uspace>=b then
  151. begin
  152. result.signed:=false;
  153. {$Q-}
  154. result.uvalue:=a.uvalue+b;
  155. {$ifdef ena_q}{$Q+}{$endif}
  156. exit;
  157. end;
  158. result.overflow:=true;
  159. end;
  160. function sub_from(const a:Tconstexprint;b:qword):Tconstexprint;
  161. const abs_low_int64=qword(9223372036854775808); {abs(low(int64)) -> overflow error}
  162. var sspace:qword;
  163. label try_qword,ov;
  164. begin
  165. result.overflow:=false;
  166. {Try if the result fits in an int64.}
  167. if (a.signed) and (a.svalue<0) then
  168. {$Q-}
  169. sspace:=qword(a.svalue)+abs_low_int64
  170. {$ifdef ena_q}{$Q+}{$endif}
  171. else if not a.signed and (a.uvalue>qword(high(int64))) then
  172. goto try_qword
  173. else
  174. sspace:=a.uvalue+qword(abs(low(int64)));
  175. if sspace>=b then
  176. begin
  177. result.signed:=true;
  178. {$Q-}
  179. result.svalue:=a.svalue-int64(b);
  180. {$ifdef ena_q}{$Q+}{$endif}
  181. exit;
  182. end;
  183. {Try if the result fits in a qword.}
  184. try_qword:
  185. if not(a.signed and (a.svalue<0)) and (a.uvalue>=b) then
  186. begin
  187. result.signed:=false;
  188. {$Q-}
  189. result.uvalue:=a.uvalue-b;
  190. {$ifdef ena_q}{$Q+}{$endif}
  191. exit;
  192. end;
  193. ov:
  194. result.overflow:=true;
  195. end;
  196. operator + (const a,b:Tconstexprint):Tconstexprint;
  197. begin
  198. if a.overflow or b.overflow then
  199. begin
  200. result.overflow:=true;
  201. exit;
  202. end;
  203. if b.signed and (b.svalue<0) then
  204. {$Q-}
  205. result:=sub_from(a,qword(-b.svalue))
  206. {$ifdef ena_q}{$Q+}{$endif}
  207. else
  208. result:=add_to(a,b.uvalue);
  209. end;
  210. operator - (const a,b:Tconstexprint):Tconstexprint;
  211. begin
  212. if a.overflow or b.overflow then
  213. begin
  214. result.overflow:=true;
  215. exit;
  216. end;
  217. if b.signed and (b.svalue<0) then
  218. {$Q-}
  219. result:=add_to(a,qword(-b.svalue))
  220. {$ifdef ena_q}{$Q+}{$endif}
  221. else
  222. result:=sub_from(a,b.uvalue);
  223. end;
  224. operator - (const a:Tconstexprint):Tconstexprint;
  225. begin
  226. if not a.signed and (a.uvalue>qword(high(int64))) then
  227. result.overflow:=true
  228. else
  229. begin
  230. result.overflow:=false;
  231. result.signed:=true;
  232. result.svalue:=-a.svalue;
  233. end;
  234. end;
  235. operator * (const a,b:Tconstexprint):Tconstexprint;
  236. var aa,bb,r:qword;
  237. sa,sb:boolean;
  238. begin
  239. if a.overflow or b.overflow then
  240. begin
  241. result.overflow:=true;
  242. exit;
  243. end;
  244. result.overflow:=false;
  245. sa:=a.signed and (a.svalue<0);
  246. if sa then
  247. aa:=qword(-a.svalue)
  248. else
  249. aa:=a.uvalue;
  250. sb:=b.signed and (b.svalue<0);
  251. if sb then
  252. bb:=qword(-b.svalue)
  253. else
  254. bb:=b.uvalue;
  255. if (bb<>0) and (high(qword) div bb<aa) then
  256. result.overflow:=true
  257. else
  258. begin
  259. r:=aa*bb;
  260. if sa xor sb then
  261. begin
  262. result.signed:=true;
  263. if r>qword(high(int64)) then
  264. result.overflow:=true
  265. else
  266. result.svalue:=-int64(r);
  267. end
  268. else
  269. begin
  270. result.signed:=false;
  271. result.uvalue:=r;
  272. end;
  273. end;
  274. end;
  275. operator div (const a,b:Tconstexprint):Tconstexprint;
  276. var aa,bb,r:qword;
  277. sa,sb:boolean;
  278. begin
  279. if a.overflow or b.overflow then
  280. begin
  281. result.overflow:=true;
  282. exit;
  283. end;
  284. result.overflow:=false;
  285. sa:=a.signed and (a.svalue<0);
  286. if sa then
  287. {$Q-}
  288. aa:=qword(-a.svalue)
  289. {$ifdef ena_q}{$Q+}{$endif}
  290. else
  291. aa:=a.uvalue;
  292. sb:=b.signed and (b.svalue<0);
  293. if sb then
  294. {$Q-}
  295. bb:=qword(-b.svalue)
  296. {$ifdef ena_q}{$Q+}{$endif}
  297. else
  298. bb:=b.uvalue;
  299. if bb=0 then
  300. result.overflow:=true
  301. else
  302. begin
  303. r:=aa div bb;
  304. if sa xor sb then
  305. begin
  306. result.signed:=true;
  307. if r>qword(high(int64)) then
  308. result.overflow:=true
  309. else
  310. result.svalue:=-int64(r);
  311. end
  312. else
  313. begin
  314. result.signed:=false;
  315. result.uvalue:=r;
  316. end;
  317. end;
  318. end;
  319. operator mod (const a,b:Tconstexprint):Tconstexprint;
  320. var aa,bb:qword;
  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. if a.signed then
  329. aa:=qword(a.svalue)
  330. else
  331. aa:=a.uvalue;
  332. if b.signed then
  333. bb:=qword(b.svalue)
  334. else
  335. bb:=b.uvalue;
  336. if bb=0 then
  337. result.overflow:=true
  338. else
  339. begin
  340. result.signed:=false;
  341. result.uvalue:=aa mod bb;
  342. end;
  343. end;
  344. operator / (const a,b:Tconstexprint):bestreal;
  345. var aa,bb:bestreal;
  346. begin
  347. if a.overflow or b.overflow then
  348. internalerror(200706096);
  349. if a.signed then
  350. aa:=a.svalue
  351. else
  352. aa:=a.uvalue;
  353. if b.signed then
  354. bb:=b.svalue
  355. else
  356. bb:=b.uvalue;
  357. result:=aa/bb;
  358. end;
  359. operator = (const a,b:Tconstexprint):boolean;
  360. begin
  361. if a.signed and (a.svalue<0) then
  362. if b.signed and (b.svalue<0) then
  363. result:=a.svalue=b.svalue
  364. else if b.uvalue>qword(high(int64)) then
  365. result:=false
  366. else
  367. result:=a.svalue=b.svalue
  368. else
  369. if not (b.signed and (b.svalue<0)) then
  370. result:=a.uvalue=b.uvalue
  371. else if a.uvalue>qword(high(int64)) then
  372. result:=false
  373. else
  374. result:=a.svalue=b.svalue
  375. end;
  376. operator > (const a,b:Tconstexprint):boolean;
  377. begin
  378. if a.signed and (a.svalue<0) then
  379. if b.signed and (b.svalue<0) then
  380. result:=a.svalue>b.svalue
  381. else if b.uvalue>qword(high(int64)) then
  382. result:=false
  383. else
  384. result:=a.svalue>b.svalue
  385. else
  386. if not (b.signed and (b.svalue<0)) then
  387. result:=a.uvalue>b.uvalue
  388. else if a.uvalue>qword(high(int64)) then
  389. result:=true
  390. else
  391. result:=a.svalue>b.svalue
  392. end;
  393. operator >= (const a,b:Tconstexprint):boolean;
  394. begin
  395. if a.signed and (a.svalue<0) then
  396. if b.signed and (b.svalue<0) then
  397. result:=a.svalue>=b.svalue
  398. else if b.uvalue>qword(high(int64)) then
  399. result:=false
  400. else
  401. result:=a.svalue>=b.svalue
  402. else
  403. if not (b.signed and (b.svalue<0)) then
  404. result:=a.uvalue>=b.uvalue
  405. else if a.uvalue>qword(high(int64)) then
  406. result:=true
  407. else
  408. result:=a.svalue>=b.svalue
  409. end;
  410. operator < (const a,b:Tconstexprint):boolean;
  411. begin
  412. if a.signed and (a.svalue<0) then
  413. if b.signed and (b.svalue<0) then
  414. result:=a.svalue<b.svalue
  415. else if b.uvalue>qword(high(int64)) then
  416. result:=true
  417. else
  418. result:=a.svalue<b.svalue
  419. else
  420. if not (b.signed and (b.svalue<0)) then
  421. result:=a.uvalue<b.uvalue
  422. else if a.uvalue>qword(high(int64)) then
  423. result:=false
  424. else
  425. result:=a.svalue<b.svalue
  426. end;
  427. operator <= (const a,b:Tconstexprint):boolean;
  428. begin
  429. if a.signed and (a.svalue<0) then
  430. if b.signed and (b.svalue<0) then
  431. result:=a.svalue<=b.svalue
  432. else if b.uvalue>qword(high(int64)) then
  433. result:=true
  434. else
  435. result:=a.svalue<=b.svalue
  436. else
  437. if not (b.signed and (b.svalue<0)) then
  438. result:=a.uvalue<=b.uvalue
  439. else if a.uvalue>qword(high(int64)) then
  440. result:=false
  441. else
  442. result:=a.svalue<=b.svalue
  443. end;
  444. operator and (const a,b:Tconstexprint):Tconstexprint;
  445. begin
  446. result.overflow:=false;
  447. result.signed:=a.signed or b.signed;
  448. result.uvalue:=a.uvalue and b.uvalue;
  449. end;
  450. operator or (const a,b:Tconstexprint):Tconstexprint;
  451. begin
  452. result.overflow:=false;
  453. result.signed:=a.signed or b.signed;
  454. result.uvalue:=a.uvalue or b.uvalue;
  455. end;
  456. operator xor (const a,b:Tconstexprint):Tconstexprint;
  457. begin
  458. result.overflow:=false;
  459. result.signed:=a.signed or b.signed;
  460. result.uvalue:=a.uvalue xor b.uvalue;
  461. end;
  462. operator shl (const a,b:Tconstexprint):Tconstexprint;
  463. begin
  464. result.overflow:=false;
  465. result.signed:=a.signed;
  466. result.uvalue:=a.uvalue shl b.uvalue;
  467. end;
  468. operator shr (const a,b:Tconstexprint):Tconstexprint;
  469. begin
  470. result.overflow:=false;
  471. result.signed:=a.signed;
  472. result.uvalue:=a.uvalue shr b.uvalue;
  473. end;
  474. function tostr(const i:Tconstexprint):shortstring;overload;
  475. begin
  476. if i.signed then
  477. str(i.svalue,result)
  478. else
  479. str(i.uvalue,result);
  480. end;
  481. end.