2
0

constexp.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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);noreturn;
  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. function sub_from(const a:Tconstexprint;b:qword):Tconstexprint;
  163. const abs_low_int64=qword(9223372036854775808); {abs(low(int64)) -> overflow error}
  164. var sspace:qword;
  165. label try_qword,ov;
  166. begin
  167. result.overflow:=false;
  168. {Try if the result fits in an int64.}
  169. if (a.signed) and (a.svalue<0) then
  170. {$push} {$Q-}
  171. sspace:=qword(a.svalue)+abs_low_int64
  172. {$pop}
  173. else if not a.signed and (a.uvalue>qword(high(int64))) then
  174. goto try_qword
  175. else
  176. {$push} {$Q-}
  177. sspace:=a.uvalue+abs_low_int64;
  178. {$pop}
  179. if sspace>=b then
  180. begin
  181. result.signed:=true;
  182. {$push} {$Q-}
  183. result.svalue:=a.svalue-int64(b);
  184. {$pop}
  185. exit;
  186. end;
  187. {Try if the result fits in a qword.}
  188. try_qword:
  189. if not(a.signed and (a.svalue<0)) and (a.uvalue>=b) then
  190. begin
  191. result.signed:=false;
  192. {$push} {$Q-}
  193. result.uvalue:=a.uvalue-b;
  194. {$pop}
  195. exit;
  196. end;
  197. ov:
  198. result.overflow:=true;
  199. end;
  200. operator + (const a,b:Tconstexprint):Tconstexprint;
  201. begin
  202. if a.overflow or b.overflow then
  203. begin
  204. result.overflow:=true;
  205. exit;
  206. end;
  207. if b.signed and (b.svalue<0) then
  208. {$push} {$Q-}
  209. result:=sub_from(a,qword(-b.svalue))
  210. {$pop}
  211. else
  212. result:=add_to(a,b.uvalue);
  213. end;
  214. operator - (const a,b:Tconstexprint):Tconstexprint;
  215. begin
  216. if a.overflow or b.overflow then
  217. begin
  218. result.overflow:=true;
  219. exit;
  220. end;
  221. if b.signed and (b.svalue<0) then
  222. {$push} {$Q-}
  223. result:=add_to(a,qword(-b.svalue))
  224. {$pop}
  225. else
  226. result:=sub_from(a,b.uvalue);
  227. end;
  228. operator - (const a:Tconstexprint):Tconstexprint;
  229. begin
  230. if not a.signed and (a.uvalue>qword(high(int64))) then
  231. result.overflow:=true
  232. else
  233. begin
  234. result.overflow:=false;
  235. result.signed:=true;
  236. {$push} {$Q-}
  237. result.svalue:=-a.svalue;
  238. {$pop}
  239. end;
  240. end;
  241. operator * (const a,b:Tconstexprint):Tconstexprint;
  242. var aa,bb,r:qword;
  243. sa,sb:boolean;
  244. begin
  245. if a.overflow or b.overflow then
  246. begin
  247. result.overflow:=true;
  248. exit;
  249. end;
  250. result.overflow:=false;
  251. sa:=a.signed and (a.svalue<0);
  252. if sa then
  253. aa:=qword(-a.svalue)
  254. else
  255. aa:=a.uvalue;
  256. sb:=b.signed and (b.svalue<0);
  257. if sb then
  258. bb:=qword(-b.svalue)
  259. else
  260. bb:=b.uvalue;
  261. if (bb<>0) and (high(qword) div bb<aa) then
  262. result.overflow:=true
  263. else
  264. begin
  265. r:=aa*bb;
  266. if sa xor sb then
  267. begin
  268. result.signed:=true;
  269. if r>qword(high(int64)) then
  270. result.overflow:=true
  271. else
  272. result.svalue:=-int64(r);
  273. end
  274. else
  275. begin
  276. result.signed:=false;
  277. result.uvalue:=r;
  278. end;
  279. end;
  280. end;
  281. operator div (const a,b:Tconstexprint):Tconstexprint;
  282. var aa,bb,r:qword;
  283. sa,sb:boolean;
  284. begin
  285. if a.overflow or b.overflow then
  286. begin
  287. result.overflow:=true;
  288. exit;
  289. end;
  290. result.overflow:=false;
  291. sa:=a.signed and (a.svalue<0);
  292. if sa then
  293. {$push} {$Q-}
  294. aa:=qword(-a.svalue)
  295. {$pop}
  296. else
  297. aa:=a.uvalue;
  298. sb:=b.signed and (b.svalue<0);
  299. if sb then
  300. {$push} {$Q-}
  301. bb:=qword(-b.svalue)
  302. {$pop}
  303. else
  304. bb:=b.uvalue;
  305. if bb=0 then
  306. result.overflow:=true
  307. else
  308. begin
  309. r:=aa div bb;
  310. if sa xor sb then
  311. begin
  312. result.signed:=true;
  313. if r>qword(high(int64)) then
  314. result.overflow:=true
  315. else
  316. result.svalue:=-int64(r);
  317. end
  318. else
  319. begin
  320. result.signed:=false;
  321. result.uvalue:=r;
  322. end;
  323. end;
  324. end;
  325. operator mod (const a,b:Tconstexprint):Tconstexprint;
  326. var aa,bb,r:qword;
  327. sa,sb:boolean;
  328. begin
  329. if a.overflow or b.overflow then
  330. begin
  331. result.overflow:=true;
  332. exit;
  333. end;
  334. result.overflow:=false;
  335. sa:=a.signed and (a.svalue<0);
  336. if sa then
  337. {$push} {$Q-}
  338. aa:=qword(-a.svalue)
  339. {$pop}
  340. else
  341. aa:=a.uvalue;
  342. sb:=b.signed and (b.svalue<0);
  343. if sb then
  344. {$push} {$Q-}
  345. bb:=qword(-b.svalue)
  346. {$pop}
  347. else
  348. bb:=b.uvalue;
  349. if bb=0 then
  350. result.overflow:=true
  351. else
  352. begin
  353. { the sign of a modulo operation only depends on the sign of the
  354. dividend }
  355. r:=aa mod bb;
  356. result.signed:=sa;
  357. if not sa then
  358. result.uvalue:=r
  359. else
  360. result.svalue:=-int64(r);
  361. end;
  362. end;
  363. operator / (const a,b:Tconstexprint):bestreal;
  364. var aa,bb:bestreal;
  365. begin
  366. if a.overflow or b.overflow then
  367. internalerror(200706096);
  368. if a.signed then
  369. aa:=a.svalue
  370. else
  371. aa:=a.uvalue;
  372. if b.signed then
  373. bb:=b.svalue
  374. else
  375. bb:=b.uvalue;
  376. result:=aa/bb;
  377. end;
  378. operator = (const a,b:Tconstexprint):boolean;
  379. begin
  380. if a.signed and (a.svalue<0) then
  381. if b.signed and (b.svalue<0) then
  382. result:=a.svalue=b.svalue
  383. else if b.uvalue>qword(high(int64)) then
  384. result:=false
  385. else
  386. result:=a.svalue=b.svalue
  387. else
  388. if not (b.signed and (b.svalue<0)) then
  389. result:=a.uvalue=b.uvalue
  390. else if a.uvalue>qword(high(int64)) then
  391. result:=false
  392. else
  393. result:=a.svalue=b.svalue
  394. end;
  395. operator > (const a,b:Tconstexprint):boolean;
  396. begin
  397. if a.signed and (a.svalue<0) then
  398. if b.signed and (b.svalue<0) then
  399. result:=a.svalue>b.svalue
  400. else if b.uvalue>qword(high(int64)) then
  401. result:=false
  402. else
  403. result:=a.svalue>b.svalue
  404. else
  405. if not (b.signed and (b.svalue<0)) then
  406. result:=a.uvalue>b.uvalue
  407. else if a.uvalue>qword(high(int64)) then
  408. result:=true
  409. else
  410. result:=a.svalue>b.svalue
  411. end;
  412. operator >= (const a,b:Tconstexprint):boolean;
  413. begin
  414. if a.signed and (a.svalue<0) then
  415. if b.signed and (b.svalue<0) then
  416. result:=a.svalue>=b.svalue
  417. else if b.uvalue>qword(high(int64)) then
  418. result:=false
  419. else
  420. result:=a.svalue>=b.svalue
  421. else
  422. if not (b.signed and (b.svalue<0)) then
  423. result:=a.uvalue>=b.uvalue
  424. else if a.uvalue>qword(high(int64)) then
  425. result:=true
  426. else
  427. result:=a.svalue>=b.svalue
  428. end;
  429. operator < (const a,b:Tconstexprint):boolean;
  430. begin
  431. if a.signed and (a.svalue<0) then
  432. if b.signed and (b.svalue<0) then
  433. result:=a.svalue<b.svalue
  434. else if b.uvalue>qword(high(int64)) then
  435. result:=true
  436. else
  437. result:=a.svalue<b.svalue
  438. else
  439. if not (b.signed and (b.svalue<0)) then
  440. result:=a.uvalue<b.uvalue
  441. else if a.uvalue>qword(high(int64)) then
  442. result:=false
  443. else
  444. result:=a.svalue<b.svalue
  445. end;
  446. operator <= (const a,b:Tconstexprint):boolean;
  447. begin
  448. if a.signed and (a.svalue<0) then
  449. if b.signed and (b.svalue<0) then
  450. result:=a.svalue<=b.svalue
  451. else if b.uvalue>qword(high(int64)) then
  452. result:=true
  453. else
  454. result:=a.svalue<=b.svalue
  455. else
  456. if not (b.signed and (b.svalue<0)) then
  457. result:=a.uvalue<=b.uvalue
  458. else if a.uvalue>qword(high(int64)) then
  459. result:=false
  460. else
  461. result:=a.svalue<=b.svalue
  462. end;
  463. operator and (const a,b:Tconstexprint):Tconstexprint;
  464. begin
  465. result.overflow:=false;
  466. result.signed:=a.signed or b.signed;
  467. result.uvalue:=a.uvalue and b.uvalue;
  468. end;
  469. operator or (const a,b:Tconstexprint):Tconstexprint;
  470. begin
  471. result.overflow:=false;
  472. result.signed:=a.signed or b.signed;
  473. result.uvalue:=a.uvalue or b.uvalue;
  474. end;
  475. operator xor (const a,b:Tconstexprint):Tconstexprint;
  476. begin
  477. result.overflow:=false;
  478. result.signed:=a.signed or b.signed;
  479. result.uvalue:=a.uvalue xor b.uvalue;
  480. end;
  481. operator shl (const a,b:Tconstexprint):Tconstexprint;
  482. begin
  483. result.overflow:=false;
  484. result.signed:=a.signed;
  485. result.uvalue:=a.uvalue shl b.uvalue;
  486. end;
  487. operator shr (const a,b:Tconstexprint):Tconstexprint;
  488. begin
  489. result.overflow:=false;
  490. result.signed:=a.signed;
  491. result.uvalue:=a.uvalue shr b.uvalue;
  492. end;
  493. function tostr(const i:Tconstexprint):shortstring;overload;
  494. begin
  495. if i.signed then
  496. str(i.svalue,result)
  497. else
  498. str(i.uvalue,result);
  499. end;
  500. end.