constexp.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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 GENERIC_CPU}
  47. type bestreal=extended;
  48. {$else}
  49. {$ifdef x86}
  50. type bestreal=extended;
  51. {$else}
  52. type bestreal=double;
  53. {$endif}
  54. {$endif}
  55. operator := (const u:qword):Tconstexprint;inline;
  56. operator := (const s:int64):Tconstexprint;inline;
  57. operator := (const c:Tconstexprint):qword;
  58. operator := (const c:Tconstexprint):int64;
  59. operator := (const c:Tconstexprint):bestreal;
  60. operator + (const a,b:Tconstexprint):Tconstexprint;
  61. operator - (const a,b:Tconstexprint):Tconstexprint;
  62. operator - (const a:Tconstexprint):Tconstexprint;
  63. operator * (const a,b:Tconstexprint):Tconstexprint;
  64. operator div (const a,b:Tconstexprint):Tconstexprint;
  65. operator mod (const a,b:Tconstexprint):Tconstexprint;
  66. operator / (const a,b:Tconstexprint):bestreal;
  67. operator = (const a,b:Tconstexprint):boolean;
  68. operator > (const a,b:Tconstexprint):boolean;
  69. operator >= (const a,b:Tconstexprint):boolean;
  70. operator < (const a,b:Tconstexprint):boolean;
  71. operator <= (const a,b:Tconstexprint):boolean;
  72. operator and (const a,b:Tconstexprint):Tconstexprint;
  73. operator or (const a,b:Tconstexprint):Tconstexprint;
  74. operator xor (const a,b:Tconstexprint):Tconstexprint;
  75. operator shl (const a,b:Tconstexprint):Tconstexprint;
  76. operator shr (const a,b:Tconstexprint):Tconstexprint;
  77. function tostr(const i:Tconstexprint):shortstring;overload;
  78. {****************************************************************************}
  79. implementation
  80. {****************************************************************************}
  81. operator := (const u:qword):Tconstexprint;
  82. begin
  83. result.overflow:=false;
  84. result.signed:=false;
  85. result.uvalue:=u;
  86. end;
  87. operator := (const s:int64):Tconstexprint;
  88. begin
  89. result.overflow:=false;
  90. result.signed:=true;
  91. result.svalue:=s;
  92. end;
  93. operator := (const c:Tconstexprint):qword;
  94. begin
  95. if c.overflow then
  96. internalerror(200706091)
  97. else if not c.signed then
  98. result:=c.uvalue
  99. else if c.svalue<0 then
  100. internalerror(200706092)
  101. else
  102. result:=qword(c.svalue);
  103. end;
  104. operator := (const c:Tconstexprint):int64;
  105. begin
  106. if c.overflow then
  107. internalerror(200706093)
  108. else if c.signed then
  109. result:=c.svalue
  110. else if c.uvalue>qword(high(int64)) then
  111. internalerror(200706094)
  112. else
  113. result:=int64(c.uvalue);
  114. end;
  115. operator := (const c:Tconstexprint):bestreal;
  116. begin
  117. if c.overflow then
  118. internalerror(200706095)
  119. else if c.signed then
  120. result:=c.svalue
  121. else
  122. result:=c.uvalue;
  123. end;
  124. function add_to(const a:Tconstexprint;b:qword):Tconstexprint;
  125. var sspace,uspace:qword;
  126. label try_qword;
  127. begin
  128. result.overflow:=false;
  129. {Try if the result fits in an int64.}
  130. if (a.signed) and (a.svalue<0) then
  131. {$Q-}
  132. sspace:=qword(high(int64))+qword(-a.svalue)
  133. {$ifdef ena_q}{$Q+}{$endif}
  134. else if not a.signed and (a.uvalue>qword(high(int64))) then
  135. goto try_qword
  136. else
  137. sspace:=qword(high(int64))-a.svalue;
  138. if sspace>=b then
  139. begin
  140. result.signed:=true;
  141. {$Q-}
  142. result.svalue:=a.svalue+int64(b);
  143. {$ifdef ena_q}{$Q+}{$endif}
  144. exit;
  145. end;
  146. {Try if the result fits in a qword.}
  147. try_qword:
  148. if (a.signed) and (a.svalue<0) then
  149. uspace:=high(qword)-qword(-a.svalue)
  150. { else if not a.signed and (a.uvalue>qword(high(int64))) then
  151. uspace:=high(qword)-a.uvalue}
  152. else
  153. uspace:=high(qword)-a.uvalue;
  154. if uspace>=b then
  155. begin
  156. result.signed:=false;
  157. {$Q-}
  158. result.uvalue:=a.uvalue+b;
  159. {$ifdef ena_q}{$Q+}{$endif}
  160. exit;
  161. end;
  162. result.overflow:=true;
  163. end;
  164. function sub_from(const a:Tconstexprint;b:qword):Tconstexprint;
  165. const abs_low_int64=qword(9223372036854775808); {abs(low(int64)) -> overflow error}
  166. var sspace:qword;
  167. label try_qword,ov;
  168. begin
  169. result.overflow:=false;
  170. {Try if the result fits in an int64.}
  171. if (a.signed) and (a.svalue<0) then
  172. {$Q-}
  173. sspace:=qword(a.svalue)+abs_low_int64
  174. {$ifdef ena_q}{$Q+}{$endif}
  175. else if not a.signed and (a.uvalue>qword(high(int64))) then
  176. goto try_qword
  177. else
  178. sspace:=a.uvalue+qword(abs(low(int64)));
  179. if sspace>=b then
  180. begin
  181. result.signed:=true;
  182. {$Q-}
  183. result.svalue:=a.svalue-int64(b);
  184. {$ifdef ena_q}{$Q+}{$endif}
  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. {$Q-}
  193. result.uvalue:=a.uvalue-b;
  194. {$ifdef ena_q}{$Q+}{$endif}
  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. {$Q-}
  209. result:=sub_from(a,qword(-b.svalue))
  210. {$ifdef ena_q}{$Q+}{$endif}
  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. {$Q-}
  223. result:=add_to(a,qword(-b.svalue))
  224. {$ifdef ena_q}{$Q+}{$endif}
  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. result.svalue:=-a.svalue;
  237. end;
  238. end;
  239. operator * (const a,b:Tconstexprint):Tconstexprint;
  240. var aa,bb,r:qword;
  241. sa,sb:boolean;
  242. begin
  243. if a.overflow or b.overflow then
  244. begin
  245. result.overflow:=true;
  246. exit;
  247. end;
  248. result.overflow:=false;
  249. sa:=a.signed and (a.svalue<0);
  250. if sa then
  251. aa:=qword(-a.svalue)
  252. else
  253. aa:=a.uvalue;
  254. sb:=b.signed and (b.svalue<0);
  255. if sb then
  256. bb:=qword(-b.svalue)
  257. else
  258. bb:=b.uvalue;
  259. if (bb<>0) and (high(qword) div bb<aa) then
  260. result.overflow:=true
  261. else
  262. begin
  263. r:=aa*bb;
  264. if sa xor sb then
  265. begin
  266. result.signed:=true;
  267. if r>qword(high(int64)) then
  268. result.overflow:=true
  269. else
  270. result.svalue:=-int64(r);
  271. end
  272. else
  273. begin
  274. result.signed:=false;
  275. result.uvalue:=r;
  276. end;
  277. end;
  278. end;
  279. operator div (const a,b:Tconstexprint):Tconstexprint;
  280. var aa,bb,r:qword;
  281. sa,sb:boolean;
  282. begin
  283. if a.overflow or b.overflow then
  284. begin
  285. result.overflow:=true;
  286. exit;
  287. end;
  288. result.overflow:=false;
  289. sa:=a.signed and (a.svalue<0);
  290. if sa then
  291. {$Q-}
  292. aa:=qword(-a.svalue)
  293. {$ifdef ena_q}{$Q+}{$endif}
  294. else
  295. aa:=a.uvalue;
  296. sb:=b.signed and (b.svalue<0);
  297. if sb then
  298. {$Q-}
  299. bb:=qword(-b.svalue)
  300. {$ifdef ena_q}{$Q+}{$endif}
  301. else
  302. bb:=b.uvalue;
  303. if bb=0 then
  304. result.overflow:=true
  305. else
  306. begin
  307. r:=aa div bb;
  308. if sa xor sb then
  309. begin
  310. result.signed:=true;
  311. if r>qword(high(int64)) then
  312. result.overflow:=true
  313. else
  314. result.svalue:=-int64(r);
  315. end
  316. else
  317. begin
  318. result.signed:=false;
  319. result.uvalue:=r;
  320. end;
  321. end;
  322. end;
  323. operator mod (const a,b:Tconstexprint):Tconstexprint;
  324. var aa,bb,r:qword;
  325. sa,sb:boolean;
  326. begin
  327. if a.overflow or b.overflow then
  328. begin
  329. result.overflow:=true;
  330. exit;
  331. end;
  332. result.overflow:=false;
  333. sa:=a.signed and (a.svalue<0);
  334. if sa then
  335. {$Q-}
  336. aa:=qword(-a.svalue)
  337. {$ifdef ena_q}{$Q+}{$endif}
  338. else
  339. aa:=a.uvalue;
  340. sb:=b.signed and (b.svalue<0);
  341. if sb then
  342. {$Q-}
  343. bb:=qword(-b.svalue)
  344. {$ifdef ena_q}{$Q+}{$endif}
  345. else
  346. bb:=b.uvalue;
  347. if bb=0 then
  348. result.overflow:=true
  349. else
  350. begin
  351. { the sign of a modulo operation only depends on the sign of the
  352. dividend }
  353. r:=aa mod bb;
  354. result.signed:=sa;
  355. if not sa then
  356. result.uvalue:=r
  357. else
  358. result.svalue:=-int64(r);
  359. end;
  360. end;
  361. operator / (const a,b:Tconstexprint):bestreal;
  362. var aa,bb:bestreal;
  363. begin
  364. if a.overflow or b.overflow then
  365. internalerror(200706096);
  366. if a.signed then
  367. aa:=a.svalue
  368. else
  369. aa:=a.uvalue;
  370. if b.signed then
  371. bb:=b.svalue
  372. else
  373. bb:=b.uvalue;
  374. result:=aa/bb;
  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:=false
  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:=false
  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:=true
  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 <= (const a,b:Tconstexprint):boolean;
  445. begin
  446. if a.signed and (a.svalue<0) then
  447. if b.signed and (b.svalue<0) then
  448. result:=a.svalue<=b.svalue
  449. else if b.uvalue>qword(high(int64)) then
  450. result:=true
  451. else
  452. result:=a.svalue<=b.svalue
  453. else
  454. if not (b.signed and (b.svalue<0)) then
  455. result:=a.uvalue<=b.uvalue
  456. else if a.uvalue>qword(high(int64)) then
  457. result:=false
  458. else
  459. result:=a.svalue<=b.svalue
  460. end;
  461. operator and (const a,b:Tconstexprint):Tconstexprint;
  462. begin
  463. result.overflow:=false;
  464. result.signed:=a.signed or b.signed;
  465. result.uvalue:=a.uvalue and b.uvalue;
  466. end;
  467. operator or (const a,b:Tconstexprint):Tconstexprint;
  468. begin
  469. result.overflow:=false;
  470. result.signed:=a.signed or b.signed;
  471. result.uvalue:=a.uvalue or b.uvalue;
  472. end;
  473. operator xor (const a,b:Tconstexprint):Tconstexprint;
  474. begin
  475. result.overflow:=false;
  476. result.signed:=a.signed or b.signed;
  477. result.uvalue:=a.uvalue xor b.uvalue;
  478. end;
  479. operator shl (const a,b:Tconstexprint):Tconstexprint;
  480. begin
  481. result.overflow:=false;
  482. result.signed:=a.signed;
  483. result.uvalue:=a.uvalue shl b.uvalue;
  484. end;
  485. operator shr (const a,b:Tconstexprint):Tconstexprint;
  486. begin
  487. result.overflow:=false;
  488. result.signed:=a.signed;
  489. result.uvalue:=a.uvalue shr b.uvalue;
  490. end;
  491. function tostr(const i:Tconstexprint):shortstring;overload;
  492. begin
  493. if i.signed then
  494. str(i.svalue,result)
  495. else
  496. str(i.uvalue,result);
  497. end;
  498. end.