constexp.pas 13 KB

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