2
0

constexp.pas 13 KB

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