constexp.pas 13 KB

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