ttmathint.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917
  1. /*
  2. * This file is a part of TTMath Bignum Library
  3. * and is distributed under the (new) BSD licence.
  4. * Author: Tomasz Sowa <[email protected]>
  5. */
  6. /*
  7. * Copyright (c) 2006-2011, Tomasz Sowa
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * * Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. *
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. *
  20. * * Neither the name Tomasz Sowa nor the names of contributors to this
  21. * project may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. * THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #ifndef headerfilettmathint
  37. #define headerfilettmathint
  38. /*!
  39. \file ttmathint.h
  40. \brief template class Int<uint>
  41. */
  42. #include "ttmathuint.h"
  43. namespace ttmath
  44. {
  45. /*!
  46. \brief Int implements a big integer value with a sign
  47. value_size - how many bytes specify our value
  48. on 32bit platforms: value_size=1 -> 4 bytes -> 32 bits
  49. on 64bit platforms: value_size=1 -> 8 bytes -> 64 bits
  50. value_size = 1,2,3,4,5,6....
  51. */
  52. template<uint value_size>
  53. class Int : public UInt<value_size>
  54. {
  55. public:
  56. /*!
  57. this method sets the max value which this class can hold
  58. (all bits will be one besides the last one)
  59. */
  60. void SetMax()
  61. {
  62. UInt<value_size>::SetMax();
  63. UInt<value_size>::table[value_size-1] = ~ TTMATH_UINT_HIGHEST_BIT;
  64. }
  65. /*!
  66. this method sets the min value which this class can hold
  67. (all bits will be zero besides the last one which is one)
  68. */
  69. void SetMin()
  70. {
  71. UInt<value_size>::SetZero();
  72. UInt<value_size>::table[value_size-1] = TTMATH_UINT_HIGHEST_BIT;
  73. }
  74. /*!
  75. this method sets -1 as the value
  76. (-1 is equal the max value in an unsigned type)
  77. */
  78. void SetSignOne()
  79. {
  80. UInt<value_size>::SetMax();
  81. }
  82. /*!
  83. we change the sign of the value
  84. if it isn't possible to change the sign this method returns 1
  85. else return 0 and changing the sign
  86. */
  87. uint ChangeSign()
  88. {
  89. /*
  90. if the value is equal that one which has been returned from SetMin
  91. (only the highest bit is set) that means we can't change sign
  92. because the value is too big (bigger about one)
  93. e.g. when value_size = 1 and value is -2147483648 we can't change it to the
  94. 2147483648 because the max value which can be held is 2147483647
  95. we don't change the value and we're using this fact somewhere in some methods
  96. (if we look on our value without the sign we get the correct value
  97. eg. -2147483648 in Int<1> will be 2147483648 on the UInt<1> type)
  98. */
  99. if( UInt<value_size>::IsOnlyTheHighestBitSet() )
  100. return 1;
  101. UInt<value_size> temp(*this);
  102. UInt<value_size>::SetZero();
  103. UInt<value_size>::Sub(temp);
  104. return 0;
  105. }
  106. /*!
  107. this method sets the sign
  108. e.g. 1 -> -1
  109. -2 -> -2
  110. from a positive value we make a negative value,
  111. if the value is negative we do nothing
  112. */
  113. void SetSign()
  114. {
  115. if( IsSign() )
  116. return;
  117. ChangeSign();
  118. }
  119. /*!
  120. this method returns true if there's the sign
  121. (the highest bit will be converted to the bool)
  122. */
  123. bool IsSign() const
  124. {
  125. return UInt<value_size>::IsTheHighestBitSet();
  126. }
  127. /*!
  128. it sets an absolute value
  129. it can return carry (1) (look on ChangeSign() for details)
  130. */
  131. uint Abs()
  132. {
  133. if( !IsSign() )
  134. return 0;
  135. return ChangeSign();
  136. }
  137. /*!
  138. *
  139. * basic mathematic functions
  140. *
  141. */
  142. private:
  143. uint CorrectCarryAfterAdding(bool p1_is_sign, bool p2_is_sign)
  144. {
  145. if( !p1_is_sign && !p2_is_sign )
  146. {
  147. if( UInt<value_size>::IsTheHighestBitSet() )
  148. return 1;
  149. }
  150. if( p1_is_sign && p2_is_sign )
  151. {
  152. if( ! UInt<value_size>::IsTheHighestBitSet() )
  153. return 1;
  154. }
  155. return 0;
  156. }
  157. public:
  158. /*!
  159. this method adds two value with a sign and returns a carry
  160. we're using methods from the base class because values are stored with U2
  161. we must only make the carry correction
  162. this = p1(=this) + p2
  163. when p1>=0 i p2>=0 carry is set when the highest bit of value is set
  164. when p1<0 i p2<0 carry is set when the highest bit of value is clear
  165. when p1>=0 i p2<0 carry will never be set
  166. when p1<0 i p2>=0 carry will never be set
  167. */
  168. uint Add(const Int<value_size> & ss2)
  169. {
  170. bool p1_is_sign = IsSign();
  171. bool p2_is_sign = ss2.IsSign();
  172. UInt<value_size>::Add(ss2);
  173. return CorrectCarryAfterAdding(p1_is_sign, p2_is_sign);
  174. }
  175. /*!
  176. this method adds one *unsigned* word (at a specific position)
  177. and returns a carry (if it was)
  178. look at a description in UInt<>::AddInt(...)
  179. */
  180. uint AddInt(uint value, uint index = 0)
  181. {
  182. bool p1_is_sign = IsSign();
  183. UInt<value_size>::AddInt(value, index);
  184. return CorrectCarryAfterAdding(p1_is_sign, false);
  185. }
  186. /*!
  187. this method adds two *unsigned* words to the existing value
  188. and these words begin on the 'index' position
  189. index should be equal or smaller than value_size-2 (index <= value_size-2)
  190. x1 - lower word, x2 - higher word
  191. look at a description in UInt<>::AddTwoInts(...)
  192. */
  193. uint AddTwoInts(uint x2, uint x1, uint index)
  194. {
  195. bool p1_is_sign = IsSign();
  196. UInt<value_size>::AddTwoInts(x2, x1, index);
  197. return CorrectCarryAfterAdding(p1_is_sign, false);
  198. }
  199. private:
  200. uint CorrectCarryAfterSubtracting(bool p1_is_sign, bool p2_is_sign)
  201. {
  202. if( !p1_is_sign && p2_is_sign )
  203. {
  204. if( UInt<value_size>::IsTheHighestBitSet() )
  205. return 1;
  206. }
  207. if( p1_is_sign && !p2_is_sign )
  208. {
  209. if( ! UInt<value_size>::IsTheHighestBitSet() )
  210. return 1;
  211. }
  212. return 0;
  213. }
  214. public:
  215. /*!
  216. this method subtracts two values with a sign
  217. we don't use the previous Add because the method ChangeSign can
  218. sometimes return carry
  219. this = p1(=this) - p2
  220. when p1>=0 i p2>=0 carry will never be set
  221. when p1<0 i p2<0 carry will never be set
  222. when p1>=0 i p2<0 carry is set when the highest bit of value is set
  223. when p1<0 i p2>=0 carry is set when the highest bit of value is clear
  224. */
  225. uint Sub(const Int<value_size> & ss2)
  226. {
  227. bool p1_is_sign = IsSign();
  228. bool p2_is_sign = ss2.IsSign();
  229. UInt<value_size>::Sub(ss2);
  230. return CorrectCarryAfterSubtracting(p1_is_sign, p2_is_sign);
  231. }
  232. /*!
  233. this method subtracts one *unsigned* word (at a specific position)
  234. and returns a carry (if it was)
  235. */
  236. uint SubInt(uint value, uint index = 0)
  237. {
  238. bool p1_is_sign = IsSign();
  239. UInt<value_size>::SubInt(value, index);
  240. return CorrectCarryAfterSubtracting(p1_is_sign, false);
  241. }
  242. /*!
  243. this method adds one to the value and returns carry
  244. */
  245. uint AddOne()
  246. {
  247. bool p1_is_sign = IsSign();
  248. UInt<value_size>::AddOne();
  249. return CorrectCarryAfterAdding(p1_is_sign, false);
  250. }
  251. /*!
  252. this method subtracts one from the value and returns carry
  253. */
  254. uint SubOne()
  255. {
  256. bool p1_is_sign = IsSign();
  257. UInt<value_size>::SubOne();
  258. return CorrectCarryAfterSubtracting(p1_is_sign, false);
  259. }
  260. private:
  261. uint CheckMinCarry(bool ss1_is_sign, bool ss2_is_sign)
  262. {
  263. /*
  264. we have to examine the sign of the result now
  265. but if the result is with the sign then:
  266. 1. if the signs were the same that means the result is too big
  267. (the result must be without a sign)
  268. 2. if the signs were different that means if the result
  269. is different from that one which has been returned from SetMin()
  270. that is carry (result too big) but if the result is equal SetMin()
  271. there'll be ok (and the next SetSign will has no effect because
  272. the value is actually negative -- look at description of that case
  273. in ChangeSign())
  274. */
  275. if( IsSign() )
  276. {
  277. if( ss1_is_sign != ss2_is_sign )
  278. {
  279. /*
  280. there can be one case where signs are different and
  281. the result will be equal the value from SetMin() (only the highest bit is set)
  282. (this situation is ok)
  283. */
  284. if( !UInt<value_size>::IsOnlyTheHighestBitSet() )
  285. return 1;
  286. }
  287. else
  288. {
  289. // signs were the same
  290. return 1;
  291. }
  292. }
  293. return 0;
  294. }
  295. public:
  296. /*!
  297. multiplication: this = this * ss2
  298. it can return a carry
  299. */
  300. uint MulInt(sint ss2)
  301. {
  302. bool ss1_is_sign, ss2_is_sign;
  303. uint c;
  304. ss1_is_sign = IsSign();
  305. /*
  306. we don't have to check the carry from Abs (values will be correct
  307. because next we're using the method MulInt from the base class UInt
  308. which is without a sign)
  309. */
  310. Abs();
  311. if( ss2 < 0 )
  312. {
  313. ss2 = -ss2;
  314. ss2_is_sign = true;
  315. }
  316. else
  317. {
  318. ss2_is_sign = false;
  319. }
  320. c = UInt<value_size>::MulInt((uint)ss2);
  321. c += CheckMinCarry(ss1_is_sign, ss2_is_sign);
  322. if( ss1_is_sign != ss2_is_sign )
  323. SetSign();
  324. return c;
  325. }
  326. /*!
  327. multiplication this = this * ss2
  328. it returns carry if the result is too big
  329. (we're using the method from the base class but we have to make
  330. one correction in account of signs)
  331. */
  332. uint Mul(Int<value_size> ss2)
  333. {
  334. bool ss1_is_sign, ss2_is_sign;
  335. uint c;
  336. ss1_is_sign = IsSign();
  337. ss2_is_sign = ss2.IsSign();
  338. /*
  339. we don't have to check the carry from Abs (values will be correct
  340. because next we're using the method Mul from the base class UInt
  341. which is without a sign)
  342. */
  343. Abs();
  344. ss2.Abs();
  345. c = UInt<value_size>::Mul(ss2);
  346. c += CheckMinCarry(ss1_is_sign, ss2_is_sign);
  347. if( ss1_is_sign != ss2_is_sign )
  348. SetSign();
  349. return c;
  350. }
  351. /*!
  352. division this = this / ss2
  353. returned values:
  354. 0 - ok
  355. 1 - division by zero
  356. for example: (result means 'this')
  357. 20 / 3 --> result: 6 remainder: 2
  358. -20 / 3 --> result: -6 remainder: -2
  359. 20 / -3 --> result: -6 remainder: 2
  360. -20 / -3 --> result: 6 remainder: -2
  361. in other words: this(old) = ss2 * this(new)(result) + remainder
  362. */
  363. uint Div(Int<value_size> ss2, Int<value_size> * remainder = 0)
  364. {
  365. bool ss1_is_sign, ss2_is_sign;
  366. ss1_is_sign = IsSign();
  367. ss2_is_sign = ss2.IsSign();
  368. /*
  369. we don't have to test the carry from Abs as well as in Mul
  370. */
  371. Abs();
  372. ss2.Abs();
  373. uint c = UInt<value_size>::Div(ss2, remainder);
  374. if( ss1_is_sign != ss2_is_sign )
  375. SetSign();
  376. if( ss1_is_sign && remainder )
  377. remainder->SetSign();
  378. return c;
  379. }
  380. uint Div(const Int<value_size> & ss2, Int<value_size> & remainder)
  381. {
  382. return Div(ss2, &remainder);
  383. }
  384. /*!
  385. division this = this / ss2 (ss2 is int)
  386. returned values:
  387. 0 - ok
  388. 1 - division by zero
  389. for example: (result means 'this')
  390. 20 / 3 --> result: 6 remainder: 2
  391. -20 / 3 --> result: -6 remainder: -2
  392. 20 / -3 --> result: -6 remainder: 2
  393. -20 / -3 --> result: 6 remainder: -2
  394. in other words: this(old) = ss2 * this(new)(result) + remainder
  395. */
  396. uint DivInt(sint ss2, sint * remainder = 0)
  397. {
  398. bool ss1_is_sign, ss2_is_sign;
  399. ss1_is_sign = IsSign();
  400. /*
  401. we don't have to test the carry from Abs as well as in Mul
  402. */
  403. Abs();
  404. if( ss2 < 0 )
  405. {
  406. ss2 = -ss2;
  407. ss2_is_sign = true;
  408. }
  409. else
  410. {
  411. ss2_is_sign = false;
  412. }
  413. uint rem;
  414. uint c = UInt<value_size>::DivInt((uint)ss2, &rem);
  415. if( ss1_is_sign != ss2_is_sign )
  416. SetSign();
  417. if( remainder )
  418. {
  419. if( ss1_is_sign )
  420. *remainder = -sint(rem);
  421. else
  422. *remainder = sint(rem);
  423. }
  424. return c;
  425. }
  426. uint DivInt(sint ss2, sint & remainder)
  427. {
  428. return DivInt(ss2, &remainder);
  429. }
  430. private:
  431. /*!
  432. power this = this ^ pow
  433. this can be negative
  434. pow is >= 0
  435. */
  436. uint Pow2(const Int<value_size> & pow)
  437. {
  438. bool was_sign = IsSign();
  439. uint c = 0;
  440. if( was_sign )
  441. c += Abs();
  442. uint c_temp = UInt<value_size>::Pow(pow);
  443. if( c_temp > 0 )
  444. return c_temp; // c_temp can be: 0, 1 or 2
  445. if( was_sign && (pow.table[0] & 1) == 1 )
  446. // negative value to the power of odd number is negative
  447. c += ChangeSign();
  448. return (c==0)? 0 : 1;
  449. }
  450. public:
  451. /*!
  452. power this = this ^ pow
  453. return values:
  454. 0 - ok
  455. 1 - carry
  456. 2 - incorrect arguments 0^0 or 0^(-something)
  457. */
  458. uint Pow(Int<value_size> pow)
  459. {
  460. if( !pow.IsSign() )
  461. return Pow2(pow);
  462. if( UInt<value_size>::IsZero() )
  463. // if 'pow' is negative then
  464. // 'this' must be different from zero
  465. return 2;
  466. if( pow.ChangeSign() )
  467. return 1;
  468. Int<value_size> t(*this);
  469. uint c_temp = t.Pow2(pow);
  470. if( c_temp > 0 )
  471. return c_temp;
  472. UInt<value_size>::SetOne();
  473. if( Div(t) )
  474. return 1;
  475. return 0;
  476. }
  477. /*!
  478. *
  479. * convertion methods
  480. *
  481. */
  482. private:
  483. /*!
  484. an auxiliary method for converting both from UInt and Int
  485. */
  486. template<uint argument_size>
  487. uint FromUIntOrInt(const UInt<argument_size> & p, bool UInt_type)
  488. {
  489. uint min_size = (value_size < argument_size)? value_size : argument_size;
  490. uint i;
  491. for(i=0 ; i<min_size ; ++i)
  492. UInt<value_size>::table[i] = p.table[i];
  493. if( value_size > argument_size )
  494. {
  495. uint fill;
  496. if( UInt_type )
  497. fill = 0;
  498. else
  499. fill = (p.table[argument_size-1] & TTMATH_UINT_HIGHEST_BIT)?
  500. TTMATH_UINT_MAX_VALUE : 0;
  501. // 'this' is longer than 'p'
  502. for( ; i<value_size ; ++i)
  503. UInt<value_size>::table[i] = fill;
  504. }
  505. else
  506. {
  507. uint test = (UInt<value_size>::table[value_size-1] & TTMATH_UINT_HIGHEST_BIT)?
  508. TTMATH_UINT_MAX_VALUE : 0;
  509. if( UInt_type && test!=0 )
  510. return 1;
  511. for( ; i<argument_size ; ++i)
  512. if( p.table[i] != test )
  513. return 1;
  514. }
  515. return 0;
  516. }
  517. public:
  518. /*!
  519. this method converts an Int<another_size> type into this class
  520. this operation has mainly sense if the value from p
  521. can be held in this type
  522. it returns a carry if the value 'p' is too big
  523. */
  524. template<uint argument_size>
  525. uint FromInt(const Int<argument_size> & p)
  526. {
  527. return FromUIntOrInt(p, false);
  528. }
  529. /*!
  530. this method converts the sint type into this class
  531. */
  532. uint FromInt(sint value)
  533. {
  534. uint fill = ( value<0 ) ? TTMATH_UINT_MAX_VALUE : 0;
  535. for(uint i=1 ; i<value_size ; ++i)
  536. UInt<value_size>::table[i] = fill;
  537. UInt<value_size>::table[0] = uint(value);
  538. // there'll never be a carry here
  539. return 0;
  540. }
  541. /*!
  542. this method converts UInt<another_size> into this class
  543. */
  544. template<uint argument_size>
  545. uint FromUInt(const UInt<argument_size> & p)
  546. {
  547. return FromUIntOrInt(p, true);
  548. }
  549. /*!
  550. this method converts UInt<another_size> into this class
  551. */
  552. template<uint argument_size>
  553. uint FromInt(const UInt<argument_size> & p)
  554. {
  555. return FromUIntOrInt(p, true);
  556. }
  557. /*!
  558. this method converts the uint type into this class
  559. */
  560. uint FromUInt(uint value)
  561. {
  562. for(uint i=1 ; i<value_size ; ++i)
  563. UInt<value_size>::table[i] = 0;
  564. UInt<value_size>::table[0] = value;
  565. // there can be a carry here when the size of this value is equal one word
  566. // and the 'value' has the highest bit set
  567. if( value_size==1 && (value & TTMATH_UINT_HIGHEST_BIT)!=0 )
  568. return 1;
  569. return 0;
  570. }
  571. /*!
  572. this method converts the uint type into this class
  573. */
  574. uint FromInt(uint value)
  575. {
  576. return FromUInt(value);
  577. }
  578. /*!
  579. the default assignment operator
  580. */
  581. /* Int<value_size> & operator=(const Int<value_size> & p)
  582. {
  583. FromInt(p);
  584. return *this;
  585. }
  586. */
  587. /*!
  588. this operator converts an Int<another_size> type to this class
  589. it doesn't return a carry
  590. */
  591. /* template<uint argument_size>
  592. Int<value_size> & operator=(const Int<argument_size> & p)
  593. {
  594. FromInt(p);
  595. return *this;
  596. }
  597. */
  598. /*!
  599. this method converts the sint type to this class
  600. */
  601. Int<value_size> & operator=(sint i)
  602. {
  603. FromInt(i);
  604. return *this;
  605. }
  606. /*!
  607. a constructor for converting the uint to this class
  608. */
  609. /* Int(sint i)
  610. {
  611. FromInt(i);
  612. }
  613. */
  614. /*!
  615. a copy constructor
  616. */
  617. /* Int(const Int<value_size> & u)
  618. {
  619. FromInt(u);
  620. }
  621. */
  622. /*!
  623. a constructor for copying from another types
  624. */
  625. /* template<uint argument_size>
  626. Int(const Int<argument_size> & u)
  627. {
  628. // look that 'size' we still set as 'value_size' and not as u.value_size
  629. FromInt(u);
  630. }
  631. */
  632. /*!
  633. this operator converts an UInt<another_size> type to this class
  634. it doesn't return a carry
  635. */
  636. template<uint argument_size>
  637. Int<value_size> & operator=(const UInt<argument_size> & p)
  638. {
  639. FromUInt(p);
  640. return *this;
  641. }
  642. /*!
  643. this method converts the Uint type to this class
  644. */
  645. Int<value_size> & operator=(uint i)
  646. {
  647. FromUInt(i);
  648. return *this;
  649. }
  650. /*!
  651. a constructor for converting the uint to this class
  652. */
  653. /* Int(uint i)
  654. {
  655. FromUInt(i);
  656. }
  657. */
  658. /*!
  659. a constructor for copying from another types
  660. */
  661. /* template<uint argument_size>
  662. Int(const UInt<argument_size> & u)
  663. {
  664. // look that 'size' we still set as 'value_size' and not as u.value_size
  665. FromUInt(u);
  666. }
  667. */
  668. #ifdef TTMATH_PLATFORM32
  669. /*!
  670. this method converts unsigned 64 bit int type to this class
  671. ***this method is created only on a 32bit platform***
  672. */
  673. uint FromUInt(ulint n)
  674. {
  675. uint c = UInt<value_size>::FromUInt(n);
  676. if( c )
  677. return 1;
  678. if( value_size == 1 )
  679. return ((UInt<value_size>::table[0] & TTMATH_UINT_HIGHEST_BIT) == 0) ? 0 : 1;
  680. if( value_size == 2 )
  681. return ((UInt<value_size>::table[1] & TTMATH_UINT_HIGHEST_BIT) == 0) ? 0 : 1;
  682. return 0;
  683. }
  684. /*!
  685. this method converts unsigned 64 bit int type to this class
  686. ***this method is created only on a 32bit platform***
  687. */
  688. uint FromInt(ulint n)
  689. {
  690. return FromUInt(n);
  691. }
  692. /*!
  693. this method converts signed 64 bit int type to this class
  694. ***this method is created only on a 32bit platform***
  695. */
  696. uint FromInt(slint n)
  697. {
  698. uint mask = (n < 0) ? TTMATH_UINT_MAX_VALUE : 0;
  699. UInt<value_size>::table[0] = (uint)(ulint)n;
  700. if( value_size == 1 )
  701. {
  702. if( uint(ulint(n) >> 32) != mask )
  703. return 1;
  704. return ((UInt<value_size>::table[0] & TTMATH_UINT_HIGHEST_BIT) == (mask & TTMATH_UINT_HIGHEST_BIT)) ? 0 : 1;
  705. }
  706. UInt<value_size>::table[1] = (uint)(ulint(n) >> 32);
  707. for(uint i=2 ; i<value_size ; ++i)
  708. UInt<value_size>::table[i] = mask;
  709. return 0;
  710. }
  711. /*!
  712. this operator converts unsigned 64 bit int type to this class
  713. ***this operator is created only on a 32bit platform***
  714. */
  715. Int<value_size> & operator=(ulint n)
  716. {
  717. FromUInt(n);
  718. return *this;
  719. }
  720. /*!
  721. a constructor for converting unsigned 64 bit int to this class
  722. ***this constructor is created only on a 32bit platform***
  723. */
  724. /* Int(ulint n)
  725. {
  726. FromUInt(n);
  727. }
  728. */
  729. /*!
  730. this operator converts signed 64 bit int type to this class
  731. ***this operator is created only on a 32bit platform***
  732. */
  733. Int<value_size> & operator=(slint n)
  734. {
  735. FromInt(n);
  736. return *this;
  737. }
  738. /*!
  739. a constructor for converting signed 64 bit int to this class
  740. ***this constructor is created only on a 32bit platform***
  741. */
  742. /* Int(slint n)
  743. {
  744. FromInt(n);
  745. }
  746. */
  747. #endif
  748. #ifdef TTMATH_PLATFORM64
  749. /*!
  750. this method converts 32 bit unsigned int type to this class
  751. ***this operator is created only on a 64bit platform***
  752. */
  753. uint FromUInt(unsigned int i)
  754. {
  755. return FromUInt(uint(i));
  756. }
  757. /*!
  758. this method converts 32 bit unsigned int type to this class
  759. ***this operator is created only on a 64bit platform***
  760. */
  761. uint FromInt(unsigned int i)
  762. {
  763. return FromUInt(i);
  764. }
  765. /*!
  766. this method converts 32 bit signed int type to this class
  767. ***this operator is created only on a 64bit platform***
  768. */
  769. uint FromInt(signed int i)
  770. {
  771. return FromInt(sint(i));
  772. }
  773. /*!
  774. this method converts 32 bit unsigned int type to this class
  775. ***this operator is created only on a 64bit platform***
  776. */
  777. Int<value_size> & operator=(unsigned int i)
  778. {
  779. FromUInt(i);
  780. return *this;
  781. }
  782. /*!
  783. a constructor for converting 32 bit unsigned int to this class
  784. ***this constructor is created only on a 64bit platform***
  785. */
  786. /* Int(unsigned int i)
  787. {
  788. FromUInt(i);
  789. }
  790. */
  791. /*!
  792. this operator converts 32 bit signed int type to this class
  793. ***this operator is created only on a 64bit platform***
  794. */
  795. Int<value_size> & operator=(signed int i)
  796. {
  797. FromInt(i);
  798. return *this;
  799. }
  800. /*!
  801. a constructor for converting 32 bit signed int to this class
  802. ***this constructor is created only on a 64bit platform***
  803. */
  804. /* Int(signed int i)
  805. {
  806. FromInt(i);
  807. }
  808. */
  809. #endif
  810. /*!
  811. a constructor for converting string to this class (with the base=10)
  812. */
  813. /* Int(const char * s)
  814. {
  815. FromString(s);
  816. }
  817. */
  818. /*!
  819. a constructor for converting a string to this class (with the base=10)
  820. */
  821. /* Int(const std::string & s)
  822. {
  823. FromString( s.c_str() );
  824. }
  825. */
  826. #ifndef TTMATH_DONT_USE_WCHAR
  827. /*!
  828. a constructor for converting string to this class (with the base=10)
  829. */
  830. Int(const wchar_t * s)
  831. {
  832. FromString(s);
  833. }
  834. /*!
  835. a constructor for converting a string to this class (with the base=10)
  836. */
  837. Int(const std::wstring & s)
  838. {
  839. FromString( s.c_str() );
  840. }
  841. #endif
  842. /*!
  843. a default constructor
  844. we don't clear table etc.
  845. */
  846. /* Int()
  847. {
  848. }
  849. */
  850. /*!
  851. the destructor
  852. */
  853. /* ~Int()
  854. {
  855. }
  856. */
  857. /*!
  858. this method returns the lowest value from table with a sign
  859. we must be sure when we using this method whether the value
  860. will hold in an sint type or not (the rest value from table must be zero or -1)
  861. */
  862. sint ToInt() const
  863. {
  864. return sint( UInt<value_size>::table[0] );
  865. }
  866. /*!
  867. this method converts the value to uint type
  868. can return a carry if the value is too long to store it in uint type
  869. */
  870. uint ToUInt(uint & result) const
  871. {
  872. uint c = UInt<value_size>::ToUInt(result);
  873. if( value_size == 1 )
  874. return (result & TTMATH_UINT_HIGHEST_BIT) == 0 ? 0 : 1;
  875. return c;
  876. }
  877. /*!
  878. this method converts the value to uint type
  879. can return a carry if the value is too long to store it in uint type
  880. */
  881. uint ToInt(uint & result) const
  882. {
  883. return ToUInt(result);
  884. }
  885. /*!
  886. this method converts the value to sint type
  887. can return a carry if the value is too long to store it in sint type
  888. */
  889. uint ToInt(sint & result) const
  890. {
  891. result = sint( UInt<value_size>::table[0] );
  892. uint mask = IsSign() ? TTMATH_UINT_MAX_VALUE : 0;
  893. if( (result & TTMATH_UINT_HIGHEST_BIT) != (mask & TTMATH_UINT_HIGHEST_BIT) )
  894. return 1;
  895. for(uint i=1 ; i<value_size ; ++i)
  896. if( UInt<value_size>::table[i] != mask )
  897. return 1;
  898. return 0;
  899. }
  900. #ifdef TTMATH_PLATFORM32
  901. /*!
  902. this method converts the value to ulint type (64 bit unsigned integer)
  903. can return a carry if the value is too long to store it in ulint type
  904. *** this method is created only on a 32 bit platform ***
  905. */
  906. uint ToUInt(ulint & result) const
  907. {
  908. uint c = UInt<value_size>::ToUInt(result);
  909. if( value_size == 1 )
  910. return (UInt<value_size>::table[0] & TTMATH_UINT_HIGHEST_BIT) == 0 ? 0 : 1;
  911. if( value_size == 2 )
  912. return (UInt<value_size>::table[1] & TTMATH_UINT_HIGHEST_BIT) == 0 ? 0 : 1;
  913. return c;
  914. }
  915. /*!
  916. this method converts the value to ulint type (64 bit unsigned integer)
  917. can return a carry if the value is too long to store it in ulint type
  918. *** this method is created only on a 32 bit platform ***
  919. */
  920. uint ToInt(ulint & result) const
  921. {
  922. return ToUInt(result);
  923. }
  924. /*!
  925. this method converts the value to slint type (64 bit signed integer)
  926. can return a carry if the value is too long to store it in slint type
  927. *** this method is created only on a 32 bit platform ***
  928. */
  929. uint ToInt(slint & result) const
  930. {
  931. if( value_size == 1 )
  932. {
  933. result = slint(sint(UInt<value_size>::table[0]));
  934. }
  935. else
  936. {
  937. uint low = UInt<value_size>::table[0];
  938. uint high = UInt<value_size>::table[1];
  939. result = low;
  940. result |= (ulint(high) << TTMATH_BITS_PER_UINT);
  941. uint mask = IsSign() ? TTMATH_UINT_MAX_VALUE : 0;
  942. if( (high & TTMATH_UINT_HIGHEST_BIT) != (mask & TTMATH_UINT_HIGHEST_BIT) )
  943. return 1;
  944. for(uint i=2 ; i<value_size ; ++i)
  945. if( UInt<value_size>::table[i] != mask )
  946. return 1;
  947. }
  948. return 0;
  949. }
  950. #endif
  951. #ifdef TTMATH_PLATFORM64
  952. /*!
  953. this method converts the value to a 32 bit unsigned integer
  954. can return a carry if the value is too long to store it in this type
  955. *** this method is created only on a 64 bit platform ***
  956. */
  957. uint ToUInt(unsigned int & result) const
  958. {
  959. uint c = UInt<value_size>::ToUInt(result);
  960. if( c || IsSign() )
  961. return 1;
  962. return 0;
  963. }
  964. /*!
  965. this method converts the value to a 32 bit unsigned integer
  966. can return a carry if the value is too long to store it in this type
  967. *** this method is created only on a 64 bit platform ***
  968. */
  969. uint ToInt(unsigned int & result) const
  970. {
  971. return ToUInt(result);
  972. }
  973. /*!
  974. this method converts the value to a 32 bit signed integer
  975. can return a carry if the value is too long to store it in this type
  976. *** this method is created only on a 64 bit platform ***
  977. */
  978. uint ToInt(int & result) const
  979. {
  980. uint first = UInt<value_size>::table[0];
  981. result = int(first);
  982. uint mask = IsSign() ? TTMATH_UINT_MAX_VALUE : 0;
  983. if( (first >> 31) != (mask >> 31) )
  984. return 1;
  985. for(uint i=1 ; i<value_size ; ++i)
  986. if( UInt<value_size>::table[i] != mask )
  987. return 1;
  988. return 0;
  989. }
  990. #endif
  991. /*!
  992. an auxiliary method for converting to a string
  993. */
  994. template<class string_type>
  995. void ToStringBase(string_type & result, uint b = 10) const
  996. {
  997. if( IsSign() )
  998. {
  999. Int<value_size> temp(*this);
  1000. temp.Abs();
  1001. temp.UInt<value_size>::ToStringBase(result, b, true);
  1002. }
  1003. else
  1004. {
  1005. UInt<value_size>::ToStringBase(result, b, false);
  1006. }
  1007. }
  1008. /*!
  1009. this method converts the value to a string with a base equal 'b'
  1010. */
  1011. void ToString(std::string & result, uint b = 10) const
  1012. {
  1013. return ToStringBase(result, b);
  1014. }
  1015. /*!
  1016. this method converts the value to a string with a base equal 'b'
  1017. */
  1018. std::string ToString(uint b = 10) const
  1019. {
  1020. std::string result;
  1021. ToStringBase(result, b);
  1022. return result;
  1023. }
  1024. #ifndef TTMATH_DONT_USE_WCHAR
  1025. /*!
  1026. this method converts the value to a string with a base equal 'b'
  1027. */
  1028. void ToString(std::wstring & result, uint b = 10) const
  1029. {
  1030. return ToStringBase(result, b);
  1031. }
  1032. /*!
  1033. this method converts the value to a string with a base equal 'b'
  1034. */
  1035. std::wstring ToWString(uint b = 10) const
  1036. {
  1037. std::wstring result;
  1038. ToStringBase(result, b);
  1039. return result;
  1040. }
  1041. #endif
  1042. private:
  1043. /*!
  1044. an auxiliary method for converting from a string
  1045. */
  1046. template<class char_type>
  1047. uint FromStringBase(const char_type * s, uint b = 10, const char_type ** after_source = 0, bool * value_read = 0)
  1048. {
  1049. bool is_sign = false;
  1050. Misc::SkipWhiteCharacters(s);
  1051. if( *s == '-' )
  1052. {
  1053. is_sign = true;
  1054. Misc::SkipWhiteCharacters(++s);
  1055. }
  1056. else
  1057. if( *s == '+' )
  1058. {
  1059. Misc::SkipWhiteCharacters(++s);
  1060. }
  1061. if( UInt<value_size>::FromString(s,b,after_source,value_read) )
  1062. return 1;
  1063. if( is_sign )
  1064. {
  1065. Int<value_size> mmin;
  1066. mmin.SetMin();
  1067. /*
  1068. the reference to mmin will be automatically converted to the reference
  1069. to UInt type
  1070. (this value can be equal mmin -- look at a description in ChangeSign())
  1071. */
  1072. if( UInt<value_size>::operator>( mmin ) )
  1073. return 1;
  1074. /*
  1075. if the value is equal mmin the method ChangeSign() does nothing (only returns 1 but we ignore it)
  1076. */
  1077. ChangeSign();
  1078. }
  1079. else
  1080. {
  1081. Int<value_size> mmax;
  1082. mmax.SetMax();
  1083. if( UInt<value_size>::operator>( mmax ) )
  1084. return 1;
  1085. }
  1086. return 0;
  1087. }
  1088. public:
  1089. /*!
  1090. this method converts a string into its value
  1091. it returns carry=1 if the value will be too big or an incorrect base 'b' is given
  1092. string is ended with a non-digit value, for example:
  1093. "-12" will be translated to -12
  1094. as well as:
  1095. "- 12foo" will be translated to -12 too
  1096. existing first white characters will be ommited
  1097. (between '-' and a first digit can be white characters too)
  1098. after_source (if exists) is pointing at the end of the parsed string
  1099. value_read (if exists) tells whether something has actually been read (at least one digit)
  1100. */
  1101. uint FromString(const char * s, uint b = 10, const char ** after_source = 0, bool * value_read = 0)
  1102. {
  1103. return FromStringBase(s, b, after_source, value_read);
  1104. }
  1105. /*!
  1106. this method converts a string into its value
  1107. */
  1108. uint FromString(const wchar_t * s, uint b = 10, const wchar_t ** after_source = 0, bool * value_read = 0)
  1109. {
  1110. return FromStringBase(s, b, after_source, value_read);
  1111. }
  1112. /*!
  1113. this method converts a string into its value
  1114. it returns carry=1 if the value will be too big or an incorrect base 'b' is given
  1115. */
  1116. uint FromString(const std::string & s, uint b = 10)
  1117. {
  1118. return FromString( s.c_str(), b );
  1119. }
  1120. /*!
  1121. this operator converts a string into its value (with base = 10)
  1122. */
  1123. Int<value_size> & operator=(const char * s)
  1124. {
  1125. FromString(s);
  1126. return *this;
  1127. }
  1128. #ifndef TTMATH_DONT_USE_WCHAR
  1129. /*!
  1130. this method converts a string into its value
  1131. it returns carry=1 if the value will be too big or an incorrect base 'b' is given
  1132. */
  1133. uint FromString(const std::wstring & s, uint b = 10)
  1134. {
  1135. return FromString( s.c_str(), b );
  1136. }
  1137. /*!
  1138. this operator converts a string into its value (with base = 10)
  1139. */
  1140. Int<value_size> & operator=(const wchar_t * s)
  1141. {
  1142. FromString(s);
  1143. return *this;
  1144. }
  1145. /*!
  1146. this operator converts a string into its value (with base = 10)
  1147. */
  1148. Int<value_size> & operator=(const std::wstring & s)
  1149. {
  1150. FromString( s.c_str() );
  1151. return *this;
  1152. }
  1153. #endif
  1154. /*!
  1155. this operator converts a string into its value (with base = 10)
  1156. */
  1157. Int<value_size> & operator=(const std::string & s)
  1158. {
  1159. FromString( s.c_str() );
  1160. return *this;
  1161. }
  1162. /*!
  1163. *
  1164. * methods for comparing
  1165. *
  1166. *
  1167. */
  1168. bool operator==(const Int<value_size> & l) const
  1169. {
  1170. return UInt<value_size>::operator==(l);
  1171. }
  1172. bool operator!=(const Int<value_size> & l) const
  1173. {
  1174. return UInt<value_size>::operator!=(l);
  1175. }
  1176. bool operator<(const Int<value_size> & l) const
  1177. {
  1178. sint i=value_size-1;
  1179. sint a1 = sint(UInt<value_size>::table[i]);
  1180. sint a2 = sint(l.table[i]);
  1181. if( a1 != a2 )
  1182. return a1 < a2;
  1183. for(--i ; i>=0 ; --i)
  1184. {
  1185. if( UInt<value_size>::table[i] != l.table[i] )
  1186. // comparison as unsigned int
  1187. return UInt<value_size>::table[i] < l.table[i];
  1188. }
  1189. // they're equal
  1190. return false;
  1191. }
  1192. bool operator>(const Int<value_size> & l) const
  1193. {
  1194. sint i=value_size-1;
  1195. sint a1 = sint(UInt<value_size>::table[i]);
  1196. sint a2 = sint(l.table[i]);
  1197. if( a1 != a2 )
  1198. return a1 > a2;
  1199. for(--i ; i>=0 ; --i)
  1200. {
  1201. if( UInt<value_size>::table[i] != l.table[i] )
  1202. // comparison as unsigned int
  1203. return UInt<value_size>::table[i] > l.table[i];
  1204. }
  1205. // they're equal
  1206. return false;
  1207. }
  1208. bool operator<=(const Int<value_size> & l) const
  1209. {
  1210. sint i=value_size-1;
  1211. sint a1 = sint(UInt<value_size>::table[i]);
  1212. sint a2 = sint(l.table[i]);
  1213. if( a1 != a2 )
  1214. return a1 < a2;
  1215. for(--i ; i>=0 ; --i)
  1216. {
  1217. if( UInt<value_size>::table[i] != l.table[i] )
  1218. // comparison as unsigned int
  1219. return UInt<value_size>::table[i] < l.table[i];
  1220. }
  1221. // they're equal
  1222. return true;
  1223. }
  1224. bool operator>=(const Int<value_size> & l) const
  1225. {
  1226. sint i=value_size-1;
  1227. sint a1 = sint(UInt<value_size>::table[i]);
  1228. sint a2 = sint(l.table[i]);
  1229. if( a1 != a2 )
  1230. return a1 > a2;
  1231. for(--i ; i>=0 ; --i)
  1232. {
  1233. if( UInt<value_size>::table[i] != l.table[i] )
  1234. // comparison as unsigned int
  1235. return UInt<value_size>::table[i] > l.table[i];
  1236. }
  1237. // they're equal
  1238. return true;
  1239. }
  1240. /*!
  1241. *
  1242. * standard mathematical operators
  1243. *
  1244. */
  1245. /*!
  1246. an operator for changing the sign
  1247. it's not changing 'this' but the changed value will be returned
  1248. */
  1249. Int<value_size> operator-() const
  1250. {
  1251. Int<value_size> temp(*this);
  1252. temp.ChangeSign();
  1253. return temp;
  1254. }
  1255. Int<value_size> operator-(const Int<value_size> & p2) const
  1256. {
  1257. Int<value_size> temp(*this);
  1258. temp.Sub(p2);
  1259. return temp;
  1260. }
  1261. Int<value_size> & operator-=(const Int<value_size> & p2)
  1262. {
  1263. Sub(p2);
  1264. return *this;
  1265. }
  1266. Int<value_size> operator+(const Int<value_size> & p2) const
  1267. {
  1268. Int<value_size> temp(*this);
  1269. temp.Add(p2);
  1270. return temp;
  1271. }
  1272. Int<value_size> & operator+=(const Int<value_size> & p2)
  1273. {
  1274. Add(p2);
  1275. return *this;
  1276. }
  1277. Int<value_size> operator*(const Int<value_size> & p2) const
  1278. {
  1279. Int<value_size> temp(*this);
  1280. temp.Mul(p2);
  1281. return temp;
  1282. }
  1283. Int<value_size> & operator*=(const Int<value_size> & p2)
  1284. {
  1285. Mul(p2);
  1286. return *this;
  1287. }
  1288. Int<value_size> operator/(const Int<value_size> & p2) const
  1289. {
  1290. Int<value_size> temp(*this);
  1291. temp.Div(p2);
  1292. return temp;
  1293. }
  1294. Int<value_size> & operator/=(const Int<value_size> & p2)
  1295. {
  1296. Div(p2);
  1297. return *this;
  1298. }
  1299. Int<value_size> operator%(const Int<value_size> & p2) const
  1300. {
  1301. Int<value_size> temp(*this);
  1302. Int<value_size> remainder;
  1303. temp.Div(p2, remainder);
  1304. return remainder;
  1305. }
  1306. Int<value_size> & operator%=(const Int<value_size> & p2)
  1307. {
  1308. Int<value_size> remainder;
  1309. Div(p2, remainder);
  1310. operator=(remainder);
  1311. return *this;
  1312. }
  1313. /*!
  1314. Prefix operator e.g. ++variable
  1315. */
  1316. UInt<value_size> & operator++()
  1317. {
  1318. AddOne();
  1319. return *this;
  1320. }
  1321. /*!
  1322. Postfix operator e.g. variable++
  1323. */
  1324. UInt<value_size> operator++(int)
  1325. {
  1326. UInt<value_size> temp( *this );
  1327. AddOne();
  1328. return temp;
  1329. }
  1330. UInt<value_size> & operator--()
  1331. {
  1332. SubOne();
  1333. return *this;
  1334. }
  1335. UInt<value_size> operator--(int)
  1336. {
  1337. UInt<value_size> temp( *this );
  1338. SubOne();
  1339. return temp;
  1340. }
  1341. /*!
  1342. *
  1343. * input/output operators for standard streams
  1344. *
  1345. */
  1346. private:
  1347. /*!
  1348. an auxiliary method for outputing to standard streams
  1349. */
  1350. template<class ostream_type, class string_type>
  1351. static ostream_type & OutputToStream(ostream_type & s, const Int<value_size> & l)
  1352. {
  1353. string_type ss;
  1354. l.ToString(ss);
  1355. s << ss;
  1356. return s;
  1357. }
  1358. public:
  1359. /*!
  1360. output to standard streams
  1361. */
  1362. friend std::ostream & operator<<(std::ostream & s, const Int<value_size> & l)
  1363. {
  1364. return OutputToStream<std::ostream, std::string>(s, l);
  1365. }
  1366. #ifndef TTMATH_DONT_USE_WCHAR
  1367. /*!
  1368. output to standard streams
  1369. */
  1370. friend std::wostream & operator<<(std::wostream & s, const Int<value_size> & l)
  1371. {
  1372. return OutputToStream<std::wostream, std::wstring>(s, l);
  1373. }
  1374. #endif
  1375. private:
  1376. /*!
  1377. an auxiliary method for converting from a string
  1378. */
  1379. template<class istream_type, class string_type, class char_type>
  1380. static istream_type & InputFromStream(istream_type & s, Int<value_size> & l)
  1381. {
  1382. string_type ss;
  1383. // char or wchar_t for operator>>
  1384. char_type z;
  1385. // operator>> omits white characters if they're set for ommiting
  1386. s >> z;
  1387. if( z=='-' || z=='+' )
  1388. {
  1389. ss += z;
  1390. s >> z; // we're reading a next character (white characters can be ommited)
  1391. }
  1392. // we're reading only digits (base=10)
  1393. while( s.good() && Misc::CharToDigit(z, 10)>=0 )
  1394. {
  1395. ss += z;
  1396. z = static_cast<char_type>(s.get());
  1397. }
  1398. // we're leaving the last readed character
  1399. // (it's not belonging to the value)
  1400. s.unget();
  1401. l.FromString(ss);
  1402. return s;
  1403. }
  1404. public:
  1405. /*!
  1406. input from standard streams
  1407. */
  1408. friend std::istream & operator>>(std::istream & s, Int<value_size> & l)
  1409. {
  1410. return InputFromStream<std::istream, std::string, char>(s, l);
  1411. }
  1412. #ifndef TTMATH_DONT_USE_WCHAR
  1413. /*!
  1414. input from standard streams
  1415. */
  1416. friend std::wistream & operator>>(std::wistream & s, Int<value_size> & l)
  1417. {
  1418. return InputFromStream<std::wistream, std::wstring, wchar_t>(s, l);
  1419. }
  1420. #endif
  1421. };
  1422. } // namespace
  1423. #endif