fmtbcd.pp 106 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2005-2006 by the Free Pascal development team
  4. and Gehard Scholz
  5. It contains the Free Pascal BCD implementation
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS for A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. { "Programming is the time between two bugs" }
  13. { (last words of the unknown programmer) }
  14. { this program was a good test for the compiler: some bugs have been found.
  15. 1. WITH in inline funcs produces a compiler error AFTER producing an .exe file
  16. (was already known; I didn't see it in the bug list)
  17. 2. macro names were checked for being a keyword, even when starting with
  18. an '_' (produces range check when compiler is compiled with { $r+ }-mode
  19. fixed.
  20. 3. { $define program } was not possible in { $macro on } mode
  21. (keywords not allowed: doesn't make sense here)
  22. fixed.
  23. 4. the Inc/Dec ( unsigned, signed ) problem (has been similar in the
  24. bug list already)
  25. 5. when the result of an overloaded (inline) operator is ABSOLUTEd:
  26. compiler error 200110205
  27. happens only when operator is defined in a unit.
  28. 6. two range check errors in scanner.pas
  29. a) array subscripting
  30. b) value out ouf range
  31. }
  32. { $define debug_version}
  33. {$r+,q+,s+}
  34. { $r-,q-,s-}
  35. {$mode objfpc}
  36. {$h-}
  37. {$inline on}
  38. {$macro on}
  39. {$define BCDMaxDigits := 64 } { should be even }
  40. { the next defines must be defined by hand,
  41. unless someone shows me a way how to to it with macros }
  42. {$define BCDgr4} { define this if MCDMaxDigits is greater 4, else undefine! }
  43. {$define BCDgr9} { define this if MCDMaxDigits is greater 9, else undefine! }
  44. {$define BCDgr18} { define this if MCDMaxDigits is greater 18, else undefine! }
  45. { $define BCDgr64} { define this if MCDMaxDigits is greater 64, else undefine! }
  46. { $define BCDgr180} { define this if MCDMaxDigits is greater 180, else undefine! }
  47. {$ifdef BCDgr4}
  48. {$note BCD Digits > 4}
  49. {$endif}
  50. {$ifdef BCDgr9}
  51. {$note BCD Digits > 9}
  52. {$endif}
  53. {$ifdef BCDgr18}
  54. {$note BCD Digits > 18}
  55. {$endif}
  56. {$ifdef BCDgr64}
  57. {$note BCD Digits > 64}
  58. {$endif}
  59. {$ifdef BCDgr180}
  60. {$note BCD Digits > 180}
  61. {$endif}
  62. { $smartlink on}
  63. {$define some_packed} { enable this to keep some local structures PACKED }
  64. { $define as_object} { to define the tBCD record as object instead;
  65. fields then are private }
  66. { not done yet! }
  67. {$define additional_routines} { to create additional routines and operators }
  68. (* only define one of them! *)
  69. { $define integ32}
  70. {$define integ64}
  71. (* only define one of them! *)
  72. { $define real8}
  73. {$define real10}
  74. {check}
  75. {$ifndef integ32}
  76. {$ifndef integ64}
  77. {$define integ64}
  78. {$endif}
  79. {$endif}
  80. {$ifdef integ32}
  81. {$ifdef integ64}
  82. {$undef integ32}
  83. {$endif}
  84. {$endif}
  85. {check}
  86. {$ifndef real8}
  87. {$ifndef real10}
  88. {$define real8}
  89. {$endif}
  90. {$endif}
  91. {$ifdef real8}
  92. {$ifdef real10}
  93. {$undef real10}
  94. {$endif}
  95. {$endif}
  96. {$ifdef some_packed}
  97. {$define maybe_packed := packed}
  98. {$else}
  99. {$define maybe_packed := (**)}
  100. {$endif}
  101. UNIT FmtBCD;
  102. INTERFACE
  103. USES
  104. SysUtils,
  105. { dateutils,}
  106. Variants;
  107. const
  108. MaxStringDigits = 100; { not used ! }
  109. _NoDecimal = -255; { not used ! }
  110. _DefaultDecimals = 10; { not used ! }
  111. { From DB.pas }
  112. { Max supported by Midas } { must be EVEN }
  113. MaxFmtBCDFractionSize = BCDMaxDigits + Ord ( Odd ( BCDMaxDigits ) );
  114. { Max supported by Midas }
  115. MaxFmtBCDDigits = 32; { not used ! }
  116. DefaultFmtBCDScale = 6; { not used ! }
  117. MaxBCDPrecision = 18; { not used ! }
  118. MaxBCDScale = 4; { not used ! }
  119. {$ifdef BCDgr64}
  120. { $fatal big 1}
  121. {$define bigger_BCD} { must be defined
  122. if MaxFmtBCDFractionSize > 64 }
  123. { not usable in the moment }
  124. {$endif}
  125. {$ifdef BCDgr180}
  126. { $fatal big 2}
  127. type
  128. FmtBCDStringtype = AnsiString;
  129. {$define use_Ansistring}
  130. {$else}
  131. type
  132. FmtBCDStringtype = string [ 255 ];
  133. {$undef use_Ansistring}
  134. {$endif}
  135. {$ifdef use_ansistring}
  136. {$note ansi}
  137. {$else}
  138. {$note -ansi}
  139. {$endif}
  140. {$ifdef integ32}
  141. {$define myInttype := LongInt}
  142. {$endif}
  143. {$ifdef integ64}
  144. {$define myInttype := int64}
  145. {$endif}
  146. {$ifdef real8}
  147. {$define myRealtype := double}
  148. {$endif}
  149. {$ifdef real10}
  150. {$define myRealtype := extended}
  151. {$endif}
  152. {$ifdef SUPPORT_COMP}
  153. {$define comproutines}
  154. {$endif SUPPORT_COMP}
  155. {$define __low_Fraction := 0 }
  156. {$define __high_Fraction := ( ( MaxFmtBCDFractionSize DIV 2 ) - 1 ) }
  157. type
  158. pBCD = ^ tBCD;
  159. tBCD = packed {$ifdef as_object} OBJECT {$else} record {$endif}
  160. {$ifdef as_object} PRIVATE {$endif}
  161. Precision : 0..maxfmtbcdfractionsize; { 1 (joke?)..64 }
  162. {$ifndef bigger_BCD}
  163. SignSpecialPlaces : Byte; { Sign:1, Special:1, Places:6 }
  164. {$else}
  165. Negativ : Boolean;
  166. {
  167. Special : Boolean;
  168. }
  169. Places : 0..maxfmtbcdfractionsize - 1;
  170. {$endif}
  171. Fraction : packed array [ __low_Fraction..__high_Fraction ] of Byte;
  172. { BCD Nibbles, 00..99 per Byte, high Nibble 1st }
  173. end;
  174. type
  175. tDecimalPoint = ( DecimalPoint_is_Point, DecimalPoint_is_Comma, DecimalPoint_is_System );
  176. { Exception classes }
  177. type
  178. eBCDException = CLASS ( Exception );
  179. eBCDOverflowException = CLASS ( eBCDException );
  180. eBCDNotImplementedException = CLASS ( eBCDException );
  181. var
  182. DecimalPoint : tDecimalPoint = DecimalPoint_is_Point;
  183. { Utility functions for TBCD access }
  184. function BCDPrecision ( const BCD : tBCD ) : Word; Inline;
  185. function BCDScale ( const BCD : tBCD ) : Word; Inline;
  186. function IsBCDNegative ( const BCD : tBCD ) : Boolean; Inline;
  187. { BCD Arithmetic}
  188. procedure BCDNegate ( var BCD : tBCD ); Inline;
  189. { !!!!!!!!!! most routines are intentionally NOT inline !!!!!!!!!! }
  190. { Returns True if successful, False if Int Digits needed to be truncated }
  191. function NormalizeBCD ( const InBCD : tBCD;
  192. var OutBCD : tBCD;
  193. const Prec,
  194. Scale : Word ) : Boolean;
  195. procedure BCDAdd ( const BCDin1,
  196. BCDin2 : tBCD;
  197. var BCDout : tBCD );
  198. procedure BCDSubtract ( const BCDin1,
  199. BCDin2 : tBCD;
  200. var BCDout : tBCD );
  201. procedure BCDMultiply ( const BCDin1,
  202. BCDin2 : tBCD;
  203. var BCDout : tBCD );
  204. procedure BCDMultiply ( const BCDIn : tBCD;
  205. const DoubleIn : myRealtype;
  206. var BCDout : tBCD ); Inline;
  207. procedure BCDMultiply ( const BCDIn : tBCD;
  208. const StringIn : FmtBCDStringtype;
  209. var BCDout : tBCD ); Inline;
  210. { !!! params changed to const, shouldn't give a problem }
  211. procedure BCDMultiply ( const StringIn1,
  212. StringIn2 : FmtBCDStringtype;
  213. var BCDout : tBCD ); Inline;
  214. procedure BCDDivide ( const Dividend,
  215. Divisor : tBCD;
  216. var BCDout : tBCD );
  217. procedure BCDDivide ( const Dividend : tBCD;
  218. const Divisor : myRealtype;
  219. var BCDout : tBCD ); Inline;
  220. procedure BCDDivide ( const Dividend : tBCD;
  221. const Divisor : FmtBCDStringtype;
  222. var BCDout : tBCD ); Inline;
  223. { !!! params changed to const, shouldn't give a problem }
  224. procedure BCDDivide ( const Dividend,
  225. Divisor : FmtBCDStringtype;
  226. var BCDout : tBCD ); Inline;
  227. { TBCD variant creation utils }
  228. procedure VarFmtBCDCreate ( var aDest : Variant;
  229. const aBCD : tBCD );
  230. function VarFmtBCDCreate : Variant;
  231. function VarFmtBCDCreate ( const aValue : FmtBCDStringtype;
  232. Precision,
  233. Scale : Word ) : Variant;
  234. function VarFmtBCDCreate ( const aValue : myRealtype;
  235. Precision : Word = 18;
  236. Scale : Word = 4 ) : Variant;
  237. function VarFmtBCDCreate ( const aBCD : tBCD ) : Variant;
  238. function VarIsFmtBCD ( const aValue : Variant ) : Boolean;
  239. function VarFmtBCD : TVartype;
  240. { Convert string/Double/Integer to BCD struct }
  241. function StrToBCD ( const aValue : FmtBCDStringtype ) : tBCD;
  242. function TryStrToBCD ( const aValue : FmtBCDStringtype;
  243. var BCD : tBCD ) : Boolean;
  244. function DoubleToBCD ( const aValue : myRealtype ) : tBCD; Inline;
  245. procedure DoubleToBCD ( const aValue : myRealtype;
  246. var BCD : tBCD );
  247. function IntegerToBCD ( const aValue : myInttype ) : tBCD;
  248. function VarToBCD ( const aValue : Variant ) : tBCD;
  249. { From DB.pas }
  250. function CurrToBCD ( const Curr : currency;
  251. var BCD : tBCD;
  252. Precision : Integer = 32;
  253. Decimals : Integer = 4 ) : Boolean;
  254. { Convert BCD struct to string/Double/Integer }
  255. function BCDToStr ( const BCD : tBCD ) : FmtBCDStringtype;
  256. function BCDToDouble ( const BCD : tBCD ) : myRealtype;
  257. function BCDToInteger ( const BCD : tBCD;
  258. Truncate : Boolean = False ) : myInttype;
  259. { From DB.pas }
  260. function BCDToCurr ( const BCD : tBCD;
  261. var Curr : currency ) : Boolean;
  262. { Formatting BCD as string }
  263. function BCDToStrF ( const BCD : tBCD;
  264. Format : TFloatFormat;
  265. const Precision,
  266. Digits : Integer ) : FmtBCDStringtype;
  267. function FormatBCD ( const Format : string;
  268. BCD : tBCD ) : FmtBCDStringtype;
  269. { returns -1 if BCD1 < BCD2, 0 if BCD1 = BCD2, 1 if BCD1 > BCD2 }
  270. function BCDCompare ( const BCD1,
  271. BCD2 : tBCD ) : Integer;
  272. {$ifdef additional_routines}
  273. function CurrToBCD ( const Curr : currency ) : tBCD; Inline;
  274. {$ifdef comproutines}
  275. function CompToBCD ( const Curr : Comp ) : tBCD; Inline;
  276. function BCDToComp ( const BCD : tBCD ) : Comp; Inline;
  277. {$endif}
  278. procedure BCDAdd ( const BCDIn : tBCD;
  279. const IntIn : myInttype;
  280. var BCDout : tBCD );
  281. procedure BCDAdd ( const IntIn : myInttype;
  282. const BCDIn : tBCD;
  283. var BCDout : tBCD ); Inline;
  284. procedure BCDAdd ( const BCDIn : tBCD;
  285. const DoubleIn : myRealtype;
  286. var BCDout : tBCD ); Inline;
  287. procedure BCDAdd ( const DoubleIn : myRealtype;
  288. const BCDIn : tBCD;
  289. var BCDout : tBCD ); Inline;
  290. procedure BCDAdd ( const BCDIn : tBCD;
  291. const Currin : currency;
  292. var BCDout : tBCD ); Inline;
  293. procedure BCDAdd ( const Currin : currency;
  294. const BCDIn : tBCD;
  295. var BCDout : tBCD ); Inline;
  296. {$ifdef comproutines}
  297. procedure BCDAdd ( const BCDIn : tBCD;
  298. const Compin : Comp;
  299. var BCDout : tBCD ); Inline;
  300. procedure BCDAdd ( const Compin : Comp;
  301. const BCDIn : tBCD;
  302. var BCDout : tBCD ); Inline;
  303. {$endif}
  304. procedure BCDAdd ( const BCDIn : tBCD;
  305. const StringIn : FmtBCDStringtype;
  306. var BCDout : tBCD ); Inline;
  307. procedure BCDAdd ( const StringIn : FmtBCDStringtype;
  308. const BCDIn : tBCD;
  309. var BCDout : tBCD ); Inline;
  310. procedure BCDAdd ( const StringIn1,
  311. StringIn2 : FmtBCDStringtype;
  312. var BCDout : tBCD ); Inline;
  313. procedure BCDSubtract ( const BCDIn : tBCD;
  314. const IntIn : myInttype;
  315. var BCDout : tBCD );
  316. procedure BCDSubtract ( const IntIn : myInttype;
  317. const BCDIn : tBCD;
  318. var BCDout : tBCD ); Inline;
  319. procedure BCDSubtract ( const BCDIn : tBCD;
  320. const DoubleIn : myRealtype;
  321. var BCDout : tBCD ); Inline;
  322. procedure BCDSubtract ( const DoubleIn : myRealtype;
  323. const BCDIn : tBCD;
  324. var BCDout : tBCD ); Inline;
  325. procedure BCDSubtract ( const BCDIn : tBCD;
  326. const Currin : currency;
  327. var BCDout : tBCD ); Inline;
  328. procedure BCDSubtract ( const Currin : currency;
  329. const BCDIn : tBCD;
  330. var BCDout : tBCD ); Inline;
  331. {$ifdef comproutines}
  332. procedure BCDSubtract ( const BCDIn : tBCD;
  333. const Compin : Comp;
  334. var BCDout : tBCD ); Inline;
  335. procedure BCDSubtract ( const Compin : Comp;
  336. const BCDIn : tBCD;
  337. var BCDout : tBCD ); Inline;
  338. {$endif}
  339. procedure BCDSubtract ( const BCDIn : tBCD;
  340. const StringIn : FmtBCDStringtype;
  341. var BCDout : tBCD ); Inline;
  342. procedure BCDSubtract ( const StringIn : FmtBCDStringtype;
  343. const BCDIn : tBCD;
  344. var BCDout : tBCD ); Inline;
  345. procedure BCDSubtract ( const StringIn1,
  346. StringIn2 : FmtBCDStringtype;
  347. var BCDout : tBCD ); Inline;
  348. procedure BCDMultiply ( const BCDIn : tBCD;
  349. const IntIn : myInttype;
  350. var BCDout : tBCD );
  351. procedure BCDMultiply ( const IntIn : myInttype;
  352. const BCDIn : tBCD;
  353. var BCDout : tBCD ); Inline;
  354. procedure BCDMultiply ( const DoubleIn : myRealtype;
  355. const BCDIn : tBCD;
  356. var BCDout : tBCD ); Inline;
  357. procedure BCDMultiply ( const BCDIn : tBCD;
  358. const Currin : currency;
  359. var BCDout : tBCD ); Inline;
  360. procedure BCDMultiply ( const Currin : currency;
  361. const BCDIn : tBCD;
  362. var BCDout : tBCD ); Inline;
  363. {$ifdef comproutines}
  364. procedure BCDMultiply ( const BCDIn : tBCD;
  365. const Compin : Comp;
  366. var BCDout : tBCD ); Inline;
  367. procedure BCDMultiply ( const Compin : Comp;
  368. const BCDIn : tBCD;
  369. var BCDout : tBCD ); Inline;
  370. {$endif}
  371. procedure BCDMultiply ( const StringIn : FmtBCDStringtype;
  372. const BCDIn : tBCD;
  373. var BCDout : tBCD ); Inline;
  374. procedure BCDDivide ( const Dividend : tBCD;
  375. const Divisor : myInttype;
  376. var BCDout : tBCD ); Inline;
  377. procedure BCDDivide ( const Dividend : myInttype;
  378. const Divisor : tBCD;
  379. var BCDout : tBCD ); Inline;
  380. procedure BCDDivide ( const Dividend : myRealtype;
  381. const Divisor : tBCD;
  382. var BCDout : tBCD ); Inline;
  383. procedure BCDDivide ( const BCDIn : tBCD;
  384. const Currin : currency;
  385. var BCDout : tBCD ); Inline;
  386. procedure BCDDivide ( const Currin : currency;
  387. const BCDIn : tBCD;
  388. var BCDout : tBCD ); Inline;
  389. {$ifdef comproutines}
  390. procedure BCDDivide ( const BCDIn : tBCD;
  391. const Compin : Comp;
  392. var BCDout : tBCD ); Inline;
  393. procedure BCDDivide ( const Compin : Comp;
  394. const BCDIn : tBCD;
  395. var BCDout : tBCD ); Inline;
  396. {$endif}
  397. procedure BCDDivide ( const Dividend : FmtBCDStringtype;
  398. const Divisor : tBCD;
  399. var BCDout : tBCD ); Inline;
  400. operator = ( const BCD1,
  401. BCD2 : tBCD ) z : Boolean; Inline;
  402. operator < ( const BCD1,
  403. BCD2 : tBCD ) z : Boolean; Inline;
  404. operator > ( const BCD1,
  405. BCD2 : tBCD ) z : Boolean; Inline;
  406. operator <= ( const BCD1,
  407. BCD2 : tBCD ) z : Boolean; Inline;
  408. operator >= ( const BCD1,
  409. BCD2 : tBCD ) z : Boolean; Inline;
  410. (* ######################## not allowed: why?
  411. operator + ( const BCD : tBCD ) z : tBCD; make_Inline
  412. ##################################################### *)
  413. operator - ( const BCD : tBCD ) z : tBCD; Inline;
  414. operator + ( const BCD1,
  415. BCD2 : tBCD ) z : tBCD; Inline;
  416. operator + ( const BCD : tBCD;
  417. const i : myInttype ) z : tBCD; Inline;
  418. operator + ( const i : myInttype;
  419. const BCD : tBCD ) z : tBCD; Inline;
  420. operator + ( const BCD : tBCD;
  421. const r : myRealtype ) z : tBCD; Inline;
  422. operator + ( const r : myRealtype;
  423. const BCD : tBCD ) z : tBCD; Inline;
  424. operator + ( const BCD : tBCD;
  425. const c : currency ) z : tBCD; Inline;
  426. operator + ( const c : currency;
  427. const BCD : tBCD ) z : tBCD; Inline;
  428. {$ifdef comproutines}
  429. operator + ( const BCD : tBCD;
  430. const c : Comp ) z : tBCD; Inline;
  431. operator + ( const c : Comp;
  432. const BCD : tBCD ) z : tBCD; Inline;
  433. {$endif}
  434. operator + ( const BCD : tBCD;
  435. const s : FmtBCDStringtype ) z : tBCD; Inline;
  436. operator + ( const s : FmtBCDStringtype;
  437. const BCD : tBCD ) z : tBCD; Inline;
  438. operator - ( const BCD1,
  439. BCD2 : tBCD ) z : tBCD; Inline;
  440. operator - ( const BCD : tBCD;
  441. const i : myInttype ) z : tBCD; Inline;
  442. operator - ( const i : myInttype;
  443. const BCD : tBCD ) z : tBCD; Inline;
  444. operator - ( const BCD : tBCD;
  445. const r : myRealtype ) z : tBCD; Inline;
  446. operator - ( const r : myRealtype;
  447. const BCD : tBCD ) z : tBCD; Inline;
  448. operator - ( const BCD : tBCD;
  449. const c : currency ) z : tBCD; Inline;
  450. operator - ( const c : currency;
  451. const BCD : tBCD ) z : tBCD; Inline;
  452. {$ifdef comproutines}
  453. operator - ( const BCD : tBCD;
  454. const c : Comp ) z : tBCD; Inline;
  455. operator - ( const c : Comp;
  456. const BCD : tBCD ) z : tBCD; Inline;
  457. {$endif}
  458. operator - ( const BCD : tBCD;
  459. const s : FmtBCDStringtype ) z : tBCD; Inline;
  460. operator - ( const s : FmtBCDStringtype;
  461. const BCD : tBCD ) z : tBCD; Inline;
  462. operator * ( const BCD1,
  463. BCD2 : tBCD ) z : tBCD; Inline;
  464. operator * ( const BCD : tBCD;
  465. const i : myInttype ) z : tBCD; Inline;
  466. operator * ( const i : myInttype;
  467. const BCD : tBCD ) z : tBCD; Inline;
  468. operator * ( const BCD : tBCD;
  469. const r : myRealtype ) z : tBCD; Inline;
  470. operator * ( const r : myRealtype;
  471. const BCD : tBCD ) z : tBCD; Inline;
  472. operator * ( const BCD : tBCD;
  473. const c : currency ) z : tBCD; Inline;
  474. operator * ( const c : currency;
  475. const BCD : tBCD ) z : tBCD; Inline;
  476. {$ifdef comproutines}
  477. operator * ( const BCD : tBCD;
  478. const c : Comp ) z : tBCD; Inline;
  479. operator * ( const c : Comp;
  480. const BCD : tBCD ) z : tBCD; Inline;
  481. {$endif}
  482. operator * ( const BCD : tBCD;
  483. const s : FmtBCDStringtype ) z : tBCD; Inline;
  484. operator * ( const s : FmtBCDStringtype;
  485. const BCD : tBCD ) z : tBCD; Inline;
  486. operator / ( const BCD1,
  487. BCD2 : tBCD ) z : tBCD; Inline;
  488. operator / ( const BCD : tBCD;
  489. const i : myInttype ) z : tBCD; Inline;
  490. operator / ( const i : myInttype;
  491. const BCD : tBCD ) z : tBCD; Inline;
  492. operator / ( const BCD : tBCD;
  493. const r : myRealtype ) z : tBCD; Inline;
  494. operator / ( const r : myRealtype;
  495. const BCD : tBCD ) z : tBCD; Inline;
  496. operator / ( const BCD : tBCD;
  497. const c : currency ) z : tBCD; Inline;
  498. operator / ( const c : currency;
  499. const BCD : tBCD ) z : tBCD; Inline;
  500. {$ifdef comproutines}
  501. operator / ( const BCD : tBCD;
  502. const c : Comp ) z : tBCD; Inline;
  503. operator / ( const c : Comp;
  504. const BCD : tBCD ) z : tBCD; Inline;
  505. {$endif}
  506. operator / ( const BCD : tBCD;
  507. const s : FmtBCDStringtype ) z : tBCD; Inline;
  508. operator / ( const s : FmtBCDStringtype;
  509. const BCD : tBCD ) z : tBCD; Inline;
  510. operator := ( const i : Byte ) z : tBCD; Inline;
  511. operator := ( const BCD : tBCD ) z : Byte; Inline;
  512. operator := ( const i : Word ) z : tBCD; Inline;
  513. operator := ( const BCD : tBCD ) z : Word; Inline;
  514. operator := ( const i : longword ) z : tBCD; Inline;
  515. operator := ( const BCD : tBCD ) z : longword; Inline;
  516. {$if declared ( qword ) }
  517. operator := ( const i : qword ) z : tBCD; Inline;
  518. operator := ( const BCD : tBCD ) z : qword; Inline;
  519. {$endif}
  520. operator := ( const i : ShortInt ) z : tBCD; Inline;
  521. operator := ( const BCD : tBCD ) z : ShortInt; Inline;
  522. operator := ( const i : smallint ) z : tBCD; Inline;
  523. operator := ( const BCD : tBCD ) z : smallint; Inline;
  524. operator := ( const i : LongInt ) z : tBCD; Inline;
  525. operator := ( const BCD : tBCD ) z : LongInt; Inline;
  526. {$if declared ( int64 ) }
  527. operator := ( const i : int64 ) z : tBCD; Inline;
  528. operator := ( const BCD : tBCD ) z : int64; Inline;
  529. {$endif}
  530. operator := ( const r : Single ) z : tBCD; Inline;
  531. operator := ( const BCD : tBCD ) z : Single; Inline;
  532. operator := ( const r : Double ) z : tBCD; Inline;
  533. operator := ( const BCD : tBCD ) z : Double; Inline;
  534. {$if sizeof ( extended ) <> sizeof ( double )}
  535. operator := ( const r : Extended ) z : tBCD; Inline;
  536. operator := ( const BCD : tBCD ) z : Extended; Inline;
  537. {$endif}
  538. operator := ( const c : currency ) z : tBCD; Inline;
  539. operator := ( const BCD : tBCD ) z : currency; Inline;
  540. {$ifdef comproutines}
  541. operator := ( const c : Comp ) z : tBCD; Inline;
  542. operator := ( const BCD : tBCD ) z : Comp; Inline;
  543. {$endif}
  544. operator := ( const s : string ) z : tBCD; Inline;
  545. operator := ( const BCD : tBCD ) z : string; Inline;
  546. operator := ( const s : AnsiString ) z : tBCD; Inline;
  547. operator := ( const BCD : tBCD ) z : AnsiString; Inline;
  548. {$endif}
  549. function __get_null : tBCD; Inline;
  550. function __get_one : tBCD; Inline;
  551. PROPERTY
  552. NullBCD : tBCD Read __get_null;
  553. OneBCD : tBCD Read __get_one;
  554. //{$define __lo_bh := 1 * ( -( MaxFmtBCDFractionSize * 1 + 2 ) ) }
  555. //{$define __hi_bh := 1 * ( MaxFmtBCDFractionSize * 1 + 1 ) }
  556. {$define helper_declarations :=
  557. const
  558. __lo_bh = -( MaxFmtBCDFractionSize + 2 );
  559. __hi_bh = ( MaxFmtBCDFractionSize + 1 );
  560. type
  561. tBCD_helper = Maybe_Packed record
  562. Prec : {$ifopt r+} 0..( __hi_bh - __lo_bh + 1 ) {$else} Integer {$endif};
  563. Plac : {$ifopt r+} 0..( __hi_bh - __lo_bh + 1 ) {$else} Integer {$endif};
  564. FDig,
  565. LDig : {$ifopt r+} __lo_bh..__hi_bh {$else} Integer {$endif};
  566. Singles : Maybe_packed array [ __lo_bh..__hi_bh ]
  567. of {$ifopt r+} 0..9 {$else} Byte {$endif};
  568. Neg : Boolean;
  569. end;
  570. { in the tBCD_helper the bcd is stored for computations,
  571. shifted to the right position }
  572. { {$define __lo_bhb := 1 * ( __lo_bh + __lo_bh ) } }
  573. { {$define __hi_bhb := 1 * ( __hi_bh + __hi_bh + 1 ) } }
  574. const
  575. __lo_bhb = __lo_bh + __lo_bh - 1;
  576. __hi_bhb = __hi_bh + __hi_bh;
  577. type
  578. tBCD_helper_big = Maybe_Packed record
  579. Prec : {$ifopt r+} 0.. ( __hi_bhb - __lo_bhb + 1 ) {$else} Integer {$endif};
  580. Plac : {$ifopt r+} 0.. ( __hi_bhb - __lo_bhb + 1 ) {$else} Integer {$endif};
  581. FDig,
  582. LDig : {$ifopt r+} __lo_bhb..__hi_bhb {$else} Integer {$endif};
  583. Singles : Maybe_packed array [ __lo_bhb..__hi_bhb ]
  584. of {$ifopt r+} 0 * 0..9 * 9 * Pred ( MaxFmtBCDDigits ) {$else} Integer {$endif};
  585. Neg : Boolean;
  586. end;
  587. }
  588. {$ifdef debug_version}
  589. helper_declarations
  590. procedure unpack_BCD ( const BCD : tBCD;
  591. var bh : tBCD_helper );
  592. function pack_BCD ( var bh : tBCD_helper;
  593. var BCD : tBCD ) : Boolean;
  594. procedure dumpBCD ( const v : tBCD );
  595. {$endif}
  596. IMPLEMENTATION
  597. USES
  598. classes;
  599. type
  600. TFMTBcdFactory = CLASS(TPublishableVarianttype)
  601. PROTECTED
  602. function GetInstance(const v : TVarData): tObject; OVERRIDE;
  603. PUBLIC
  604. procedure BinaryOp(var Left: TVarData; const Right: TVarData; const Operation: TVarOp);
  605. end;
  606. TFMTBcdVarData = CLASS(TPersistent)
  607. PRIVATE
  608. FBcd : tBCD;
  609. PUBLIC
  610. constructor create;
  611. constructor create(const BCD : tBCD);
  612. PROPERTY BCD : tBCD Read FBcd Write FBcd;
  613. end;
  614. var
  615. NullBCD_ : tBCD;
  616. OneBCD_ : tBCD;
  617. function __get_null : tBCD; Inline;
  618. begin
  619. __get_null := NullBCD_;
  620. end;
  621. function __get_one : tBCD; Inline;
  622. begin
  623. __get_one := OneBCD_;
  624. end;
  625. type
  626. range_digits = 1..maxfmtbcdfractionsize;
  627. range_digits0 = 0..maxfmtbcdfractionsize;
  628. range_fracdigits = 0..pred ( MaxFmtBCDFractionSize );
  629. {$ifopt r+}
  630. var
  631. rcheck : 0..0;
  632. rbad : Byte = 1;
  633. {$endif}
  634. {$ifndef debug_version}
  635. helper_declarations
  636. {$endif}
  637. var
  638. null_ : record
  639. case Boolean of
  640. False: ( bh : tBCD_helper );
  641. True: ( bhb : tBCD_helper_big );
  642. end;
  643. FMTBcdFactory : TFMTBcdFactory = NIL;
  644. {$ifndef bigger_BCD}
  645. const
  646. NegBit = 1 SHL 7;
  647. SpecialBit = 1 SHL 6;
  648. PlacesMask = $ff XOR ( NegBit OR SpecialBit );
  649. {$endif}
  650. {$define _select := {$define _when := if {$define _when := end else if } }
  651. {$define _then := then begin }
  652. {$define _whenother := end else begin }
  653. {$define _endselect := end } }
  654. {$ifdef debug_version}
  655. procedure dumpBCD ( const v : tBCD );
  656. var
  657. i,
  658. j : Integer;
  659. const
  660. ft : ARRAY [ Boolean ] of Char = ( 'f', 't' );
  661. begin
  662. {$ifndef bigger_BCD}
  663. Write ( 'Prec:', v.Precision, ' ',
  664. 'Neg:', ft[( v.SignSpecialPlaces AND NegBit ) <> 0], ' ',
  665. 'Special:', ft[( v.SignSpecialPlaces AND SpecialBit ) <> 0], ' ',
  666. 'Places:', v.SignSpecialPlaces AND PlacesMask, ' ' );
  667. {$else}
  668. Write ( 'Prec:', v.Precision, ' ',
  669. 'Neg:', ft[v.Negativ], ' ',
  670. 'Places:', v.Places, ' ' );
  671. {$endif}
  672. j := 0;
  673. for i := 1 TO v.Precision do
  674. if Odd ( i )
  675. then Write ( ( v.Fraction[j] AND $f0 ) SHR 4 )
  676. else begin
  677. Write ( v.Fraction[j] AND $0f );
  678. Inc ( j );
  679. end;
  680. WriteLn;
  681. end;
  682. procedure dumpbh ( const v : tBCD_helper );
  683. var
  684. i : Integer;
  685. const
  686. ft : ARRAY [ Boolean ] of Char = ( 'f', 't' );
  687. begin
  688. Write ( 'Prec:', v.Prec, ' ',
  689. 'Neg:', ft[v.Neg], ' ',
  690. 'Places:', v.Plac, ' ',
  691. 'FDig:', v.FDig, ' ',
  692. 'LDig:', v.LDig, ' ',
  693. 'Digits:', v.LDig - v.FDig + 1, ' ' );
  694. for i := v.FDig TO v.LDig do
  695. Write ( v.Singles[i] );
  696. WriteLn;
  697. end;
  698. {$endif}
  699. {$if sizeof ( integer ) = 2 }
  700. {$ifdef BCDgr4 }
  701. var
  702. myMinIntBCD : tBCD;
  703. {$endif}
  704. {$else}
  705. {$if sizeof ( integer ) = 4 }
  706. {$ifdef BCDgr9 }
  707. var
  708. myMinIntBCD : tBCD;
  709. {$endif}
  710. {$else}
  711. {$if sizeof ( integer ) = 8 }
  712. {$ifdef BCDgr18 }
  713. var
  714. myMinIntBCD : tBCD;
  715. {$endif}
  716. {$else}
  717. {$fatal You have an interesting integer type! Sorry, not supported}
  718. {$endif}
  719. {$endif}
  720. {$endif}
  721. procedure not_implemented;
  722. begin
  723. RAISE eBCDNotImplementedException.create ( 'not implemented' );
  724. end;
  725. procedure unpack_BCD ( const BCD : tBCD;
  726. var bh : tBCD_helper );
  727. var
  728. i : {$ifopt r+} __lo_bh + 1 ..__hi_bh {$else} Integer {$endif};
  729. j : {$ifopt r+} -1..__high_fraction {$else} Integer {$endif};
  730. vv : {$ifopt r+} $00..$99 {$else} Integer {$endif};
  731. begin
  732. bh := null_.bh;
  733. WITH bh,
  734. BCD do
  735. begin
  736. Prec := Precision;
  737. if Prec > 0
  738. then begin
  739. {$ifndef bigger_BCD}
  740. Plac := SignSpecialPlaces AND PlacesMask;
  741. Neg := ( SignSpecialPlaces AND NegBit ) <> 0;
  742. {$else}
  743. Plac := Places;
  744. Neg := Negativ;
  745. {$endif}
  746. LDig := Plac;
  747. FDig := LDig - Prec + 1;
  748. j := -1;
  749. i := FDig;
  750. while i <= LDig do
  751. begin
  752. Inc ( j );
  753. vv := Fraction[j];
  754. Singles[i] := ( vv {AND $f0} ) SHR 4;
  755. if i < LDig
  756. then Singles[i+1] := vv AND $0f;
  757. Inc ( i, 2 );
  758. end;
  759. end;
  760. end;
  761. end;
  762. function pack_BCD ( var bh : tBCD_helper;
  763. var BCD : tBCD ) : Boolean;
  764. { return TRUE if successful (BCD valid) }
  765. var
  766. pre : {$ifopt r+} 0..__hi_bh - __lo_bh + 1 {$else} Integer {$endif};
  767. fra : {$ifopt r+} -1 * ( __hi_bh - __lo_bh + 1 )..__hi_bh - __lo_bh + 1 {$else} Integer {$endif};
  768. tm : {$ifopt r+} 0..__hi_bh - __lo_bh + 1 - Pred ( MaxFmtBCDFractionSize ) {$else} Integer {$endif};
  769. i : {$ifopt r+} low ( bh.FDig ) - 1..high ( bh.LDig ) {$else} Integer {$endif};
  770. rp : {$ifopt r+} low ( BCD.Fraction )..high ( BCD.Fraction ) + 1 {$else} Integer {$endif};
  771. ue : {$ifopt r+} 0..1 {$else} Integer {$endif};
  772. v : {$ifopt r+} 0..10 {$else} Integer {$endif};
  773. lnz : {$ifopt r+} low ( bh.FDig )..high ( bh.LDig ) {$else} Integer {$endif};
  774. doround,
  775. lnzf : Boolean;
  776. begin
  777. pack_BCD := False;
  778. BCD := NullBCD;
  779. WITH BCD,
  780. bh do
  781. begin
  782. lnzf := FDig < 0;
  783. while lnzf do
  784. if Singles[FDig] = 0
  785. then begin
  786. Inc ( FDig );
  787. if FDig = 0
  788. then lnzf := False;
  789. end
  790. else lnzf := False;
  791. pre := LDig - FDig + 1;
  792. fra := Plac;
  793. doround := False;
  794. if fra >= MaxFmtBCDFractionSize
  795. then begin
  796. doround := True;
  797. tm := fra - Pred ( MaxFmtBCDFractionSize );
  798. { dec ( pre, tm ); Dec/Inc error? }
  799. pre := pre - tm;
  800. { Dec ( fra, tm ); Dec/Inc error? }
  801. fra := fra - tm;
  802. { Dec ( LDig, tm ); Dec/Inc error? }
  803. LDig := LDig - tm;
  804. end;
  805. if pre > MaxFmtBCDFractionSize
  806. then begin
  807. doround := True;
  808. tm := pre - MaxFmtBCDFractionSize;
  809. { Dec ( pre, tm ); Dec/Inc error? }
  810. pre := pre - tm;
  811. { Dec ( fra, tm ); Dec/Inc error? }
  812. fra := fra - tm;
  813. { Dec ( LDig, tm ); Dec/Inc error? }
  814. LDig := LDig - tm;
  815. end;
  816. if fra < 0
  817. then EXIT;
  818. if doround
  819. then begin
  820. v := Singles[fra + 1];
  821. if v > 4
  822. then begin
  823. ue := 1;
  824. i := LDig;
  825. while ( i >= FDig ) AND ( ue <> 0 ) do
  826. begin
  827. v := Singles[i] + ue;
  828. ue := v DIV 10;
  829. Singles[i] := v MOD 10;
  830. Dec ( i );
  831. end;
  832. if ue <> 0
  833. then begin
  834. Dec ( FDig );
  835. Singles[FDig] := ue;
  836. Dec ( LDig );
  837. Dec ( fra );
  838. if fra < 0
  839. then EXIT;
  840. end;
  841. end;
  842. end;
  843. lnzf := False;
  844. i := LDig;
  845. while ( i >= FDig ) AND ( NOT lnzf ) do
  846. begin
  847. if Singles[i] <> 0
  848. then begin
  849. lnz := i;
  850. lnzf := True;
  851. end;
  852. Dec ( i );
  853. end;
  854. if lnzf
  855. then begin
  856. tm := LDig - lnz;
  857. if tm <> 0
  858. then begin
  859. { Dec ( pre, tm ); Dec/Inc error? }
  860. pre := pre - tm;
  861. { Dec ( fra, tm ); Dec/Inc error? }
  862. fra := fra - tm;
  863. { Dec ( LDig, tm ); Dec/Inc error? }
  864. LDig := LDig - tm;
  865. if fra < 0
  866. then begin
  867. { Dec ( pre, fra ); Dec/Inc error? }
  868. pre := pre - fra;
  869. { Dec ( LDig, fra ); Dec/Inc error? }
  870. LDig := LDig - fra;
  871. fra := 0;
  872. end;
  873. end;
  874. end
  875. else begin
  876. LDig := FDig;
  877. fra := 0;
  878. pre := 0;
  879. Neg := False;
  880. end;
  881. if pre <> 0
  882. then begin
  883. Precision := pre;
  884. rp := 0;
  885. i := FDig;
  886. while i <= LDig do
  887. begin
  888. if i < LDig
  889. then Fraction[rp] := ( Singles[i] SHL 4 ) OR Singles[i + 1]
  890. else Fraction[rp] := Singles[i] SHL 4;
  891. Inc ( rp );
  892. Inc ( i, 2 );
  893. end;
  894. {$ifndef bigger_BCD}
  895. if Neg
  896. then SignSpecialPlaces := NegBit;
  897. SignSpecialPlaces := SignSpecialPlaces OR fra;
  898. {$else}
  899. Negativ := Neg;
  900. Places := fra;
  901. {$endif}
  902. end;
  903. end;
  904. pack_BCD := True;
  905. end;
  906. procedure SetDecimals ( var dp,
  907. dc : Char );
  908. begin
  909. case DecimalPoint of
  910. DecimalPoint_is_Point: begin
  911. dp := '.';
  912. dc := ',';
  913. end;
  914. DecimalPoint_is_Comma: begin
  915. dp := ',';
  916. dc := '.';
  917. end;
  918. { find out language-specific ? }
  919. DecimalPoint_is_System: begin
  920. dp := DecimalSeparator;
  921. dc := ThousandSeparator;
  922. end;
  923. end;
  924. end;
  925. function BCDPrecision ( const BCD : tBCD ) : Word; Inline;
  926. begin
  927. BCDPrecision := BCD.Precision;
  928. end;
  929. function BCDScale ( const BCD : tBCD ) : Word; Inline;
  930. begin
  931. {$ifndef bigger_BCD}
  932. BCDScale := BCD.SignSpecialPlaces AND PlacesMask;
  933. {$else}
  934. BCDScale := BCD.Places;
  935. {$endif}
  936. end;
  937. function IsBCDNegative ( const BCD : tBCD ) : Boolean; Inline;
  938. begin
  939. {$ifndef bigger_BCD}
  940. IsBCDNegative := ( BCD.SignSpecialPlaces AND NegBit ) <> 0;
  941. {$else}
  942. IsBCDNegative := BCD.Negativ;
  943. {$endif}
  944. end;
  945. { BCD Arithmetic}
  946. procedure BCDNegate ( var BCD : tBCD ); Inline;
  947. begin
  948. { with-statement geht nicht !!
  949. with bcd do
  950. if precision <> 0
  951. then signspecialplaces := signspecialplaces xor negbit;
  952. }
  953. if BCD.Precision <> 0
  954. then
  955. {$ifndef bigger_BCD}
  956. BCD.SignSpecialPlaces := BCD.SignSpecialPlaces XOR NegBit;
  957. {$else}
  958. BCD.Negativ := NOT BCD.Negativ;
  959. {$endif}
  960. end;
  961. { returns -1 if BCD1 < BCD2, 0 if BCD1 = BCD2, 1 if BCD1 > BCD2 }
  962. function BCDCompare ( const BCD1,
  963. BCD2 : tBCD ) : Integer;
  964. var
  965. pl1 : {$ifopt r+} 0..maxfmtbcdfractionsize - 1 {$else} Integer {$endif};
  966. pl2 : {$ifopt r+} 0..maxfmtbcdfractionsize - 1 {$else} Integer {$endif};
  967. pr1 : {$ifopt r+} 0..maxfmtbcdfractionsize {$else} Integer {$endif};
  968. pr2 : {$ifopt r+} 0..maxfmtbcdfractionsize {$else} Integer {$endif};
  969. pr : {$ifopt r+} 0..maxfmtbcdfractionsize {$else} Integer {$endif};
  970. idig1 : {$ifopt r+} 0..maxfmtbcdfractionsize {$else} Integer {$endif};
  971. idig2 : {$ifopt r+} 0..maxfmtbcdfractionsize {$else} Integer {$endif};
  972. i : {$ifopt r+} __low_Fraction..__high_Fraction + 1 {$else} Integer {$endif};
  973. f1 : {$ifopt r+} $00..$99 {$else} Integer {$endif};
  974. f2 : {$ifopt r+} $00..$99 {$else} Integer {$endif};
  975. res : {$ifopt r+} -1..1 {$else} Integer {$endif};
  976. neg1,
  977. neg2 : Boolean;
  978. begin
  979. {$ifndef bigger_BCD}
  980. neg1 := ( BCD1.SignSpecialPlaces AND NegBit ) <> 0;
  981. neg2 := ( BCD2.SignSpecialPlaces AND NegBit ) <> 0;
  982. {$else}
  983. neg1 := BCD1.Negativ;
  984. neg2 := BCD2.Negativ;
  985. {$endif}
  986. _SELECT
  987. _WHEN neg1 AND ( NOT neg2 )
  988. _THEN result := -1;
  989. _WHEN ( NOT neg1 ) AND neg2
  990. _THEN result := +1;
  991. _WHENOTHER
  992. pr1 := BCD1.Precision;
  993. pr2 := BCD2.Precision;
  994. {$ifndef bigger_BCD}
  995. pl1 := BCD1.SignSpecialPlaces AND PlacesMask;
  996. pl2 := BCD2.SignSpecialPlaces AND PlacesMask;
  997. {$else}
  998. pl1 := BCD1.Places;
  999. pl2 := BCD2.Places;
  1000. {$endif}
  1001. idig1 := pr1 - pl1;
  1002. idig2 := pr2 - pl2;
  1003. if idig1 <> idig2
  1004. then begin
  1005. if ( idig1 > idig2 ) = neg1
  1006. then result := -1
  1007. else result := +1;
  1008. end
  1009. else begin
  1010. if pr1 < pr2
  1011. then pr := pr1
  1012. else pr := pr2;
  1013. res := 0;
  1014. i := __low_Fraction;
  1015. while ( res = 0 ) AND ( i < ( __low_Fraction + ( pr DIV 2 ) ) ) do
  1016. begin
  1017. {
  1018. if BCD1.Fraction[i] < BCD2.Fraction[i]
  1019. then res := -1
  1020. else
  1021. if BCD1.Fraction[i] > BCD2.Fraction[i]
  1022. then res := +1;
  1023. }
  1024. _SELECT
  1025. _WHEN BCD1.Fraction[i] < BCD2.Fraction[i]
  1026. _THEN res := -1
  1027. _WHEN BCD1.Fraction[i] > BCD2.Fraction[i]
  1028. _THEN res := +1;
  1029. _WHENOTHER
  1030. _endSELECT;
  1031. Inc ( i );
  1032. end;
  1033. if res = 0
  1034. then begin
  1035. if Odd ( pr )
  1036. then begin
  1037. f1 := BCD1.Fraction[i] AND $f0;
  1038. f2 := BCD2.Fraction[i] AND $f0;
  1039. {
  1040. if f1 < f2
  1041. then res := -1
  1042. else
  1043. if f1 > f2
  1044. then res := +1;
  1045. }
  1046. _SELECT
  1047. _WHEN f1 < f2
  1048. _THEN res := -1
  1049. _WHEN f1 > f2
  1050. _THEN res := +1;
  1051. _endSELECT;
  1052. end;
  1053. end;
  1054. if neg1
  1055. then result := 0 - res
  1056. else result := res;
  1057. end;
  1058. _endSELECT
  1059. end;
  1060. { Convert string/Double/Integer to BCD struct }
  1061. function TryStrToBCD ( const aValue : FmtBCDStringtype;
  1062. var BCD : tBCD ) : Boolean;
  1063. { shall this return TRUE when error and FALSE when o.k. or the other way round ? }
  1064. var
  1065. {$ifndef use_ansistring}
  1066. lav : {$ifopt r+} 0..high ( aValue ) {$else} Integer {$endif};
  1067. i : {$ifopt r+} 0..high ( aValue ) {$else} Integer {$endif};
  1068. {$else}
  1069. lav : {$ifopt r+} longword {$else} longword {$endif};
  1070. i : {$ifopt r+} longword {$else} longword {$endif};
  1071. {$endif}
  1072. ch : Char;
  1073. dp,
  1074. dc : Char;
  1075. type
  1076. ife = ( inint, infrac, inexp );
  1077. {$define max_exp_scanned := 9999 }
  1078. var
  1079. inife : ife;
  1080. lvars : record
  1081. fp,
  1082. lp : ARRAY [ ife ]
  1083. {$ifndef use_ansistring}
  1084. of {$ifopt r+} 0..high ( aValue ) {$else} Integer {$endif};
  1085. pfnb : {$ifopt r+} 0..high ( aValue ) {$else} Integer {$endif};
  1086. ps : {$ifopt r+} 0..high ( aValue ) {$else} Integer {$endif};
  1087. pse : {$ifopt r+} 0..high ( aValue ) {$else} Integer {$endif};
  1088. errp : {$ifopt r+} 0..high ( aValue ) {$else} Integer {$endif};
  1089. {$else}
  1090. of {$ifopt r+} longword {$else} longword {$endif};
  1091. pfnb : {$ifopt r+} longword {$else} longword {$endif};
  1092. ps : {$ifopt r+} longword {$else} longword {$endif};
  1093. pse : {$ifopt r+} longword {$else} longword {$endif};
  1094. errp : {$ifopt r+} longword {$else} longword {$endif};
  1095. {$endif}
  1096. exp : {$ifopt r+} -max_exp_scanned..max_exp_scanned {$else} Integer {$endif};
  1097. p : {$ifopt r+} -max_exp_scanned..max_exp_scanned {$else} Integer {$endif};
  1098. bh : tBCD_helper;
  1099. nbf : Boolean;
  1100. end;
  1101. begin
  1102. result := False;
  1103. FillChar ( lvars, SizeOf ( lvars ), #0 );
  1104. BCD := NullBCD;
  1105. lav := Length ( aValue );
  1106. if lav <> 0
  1107. then
  1108. WITH lvars,
  1109. bh do
  1110. begin
  1111. SetDecimals ( dp, dc );
  1112. while ( pfnb < lav ) AND ( NOT nbf ) do
  1113. begin
  1114. Inc ( pfnb );
  1115. nbf := aValue[pfnb] <> ' ';
  1116. end;
  1117. if nbf
  1118. then begin
  1119. if aValue[pfnb] IN [ '+', '-' ]
  1120. then begin
  1121. ps := pfnb;
  1122. Inc ( pfnb );
  1123. end;
  1124. inife := low ( inife );
  1125. for i := pfnb TO lav do
  1126. begin
  1127. ch := aValue[i];
  1128. case ch of
  1129. '0'..'9': begin
  1130. case inife of
  1131. inint,
  1132. inexp: if fp[inife] = 0
  1133. then begin
  1134. if ch <> '0'
  1135. then begin
  1136. fp[inife] := i;
  1137. lp[inife] := i;
  1138. end;
  1139. end
  1140. else lp[inife] := i;
  1141. infrac: begin
  1142. if fp[infrac] = 0
  1143. then fp[infrac] := i;
  1144. if ch <> '0'
  1145. then lp[infrac] := i;
  1146. end;
  1147. end;
  1148. end;
  1149. ',',
  1150. '.': if ch = dp
  1151. then begin
  1152. if inife <> inint
  1153. then result := True
  1154. else inife := infrac;
  1155. end;
  1156. 'e',
  1157. 'E': if inife = inexp
  1158. then result := True
  1159. else inife := inexp;
  1160. '+',
  1161. '-': if ( inife = inexp ) AND ( fp[inexp] = 0 )
  1162. then pse := i
  1163. else result := True;
  1164. else begin
  1165. result := True;
  1166. errp := i;
  1167. end;
  1168. end;
  1169. end;
  1170. if result
  1171. then begin
  1172. result := False;
  1173. for i := errp TO lav do
  1174. if aValue[i] <> ' '
  1175. then result := True;
  1176. end;
  1177. if result
  1178. then EXIT;
  1179. if ps <> 0
  1180. then Neg := aValue[ps] = '-';
  1181. if lp[infrac] = 0
  1182. then fp[infrac] := 0;
  1183. if fp[inexp] <> 0
  1184. then begin
  1185. exp := 0;
  1186. for i := fp[inexp] TO lp[inexp] do
  1187. if NOT result
  1188. then
  1189. if aValue[i] <> dc
  1190. then begin
  1191. exp := exp * 10 + ( Ord ( aValue[i] ) - Ord ( '0' ) );
  1192. if exp > 999
  1193. then result := True;
  1194. end;
  1195. if result
  1196. then EXIT;
  1197. if pse <> 0
  1198. then
  1199. if aValue[pse] = '-'
  1200. then exp := -exp;
  1201. end;
  1202. p := -exp;
  1203. if fp[infrac] <> 0
  1204. then begin
  1205. for i := fp[infrac] TO lp[infrac] do
  1206. if aValue[i] <> dc
  1207. then begin
  1208. if p < ( MaxFmtBCDFractionSize + 2 )
  1209. then begin
  1210. Inc ( p );
  1211. Singles[p] := Ord ( aValue[i] ) - Ord ( '0' );
  1212. end;
  1213. end;
  1214. end;
  1215. LDig := p;
  1216. p := 1 - exp;
  1217. if fp[inint] <> 0
  1218. then
  1219. for i := lp[inint] DOWNTO fp[inint] do
  1220. if aValue[i] <> dc
  1221. then begin
  1222. if p > - ( MaxFmtBCDFractionSize + 2 )
  1223. then begin
  1224. Dec ( p );
  1225. Singles[p] := Ord ( aValue[i] ) - Ord ( '0' );
  1226. end
  1227. else result := True;
  1228. end;
  1229. if result
  1230. then EXIT;
  1231. FDig := p;
  1232. if LDig < 0
  1233. then LDig := 0;
  1234. Plac := LDig;
  1235. result := NOT pack_BCD ( bh, BCD );
  1236. end;
  1237. end;
  1238. end;
  1239. function StrToBCD ( const aValue : FmtBCDStringtype ) : tBCD;
  1240. var
  1241. BCD : tBCD;
  1242. begin
  1243. if TryStrToBCD ( aValue, BCD )
  1244. then begin
  1245. RAISE eBCDOverflowException.create ( 'in StrToBCD' );
  1246. end
  1247. else StrToBCD := BCD;
  1248. end;
  1249. procedure DoubleToBCD ( const aValue : myRealtype;
  1250. var BCD : tBCD );
  1251. var
  1252. s : string [ 30 ];
  1253. dp : tDecimalPoint;
  1254. begin
  1255. Str ( aValue : 25, s );
  1256. dp := DecimalPoint;
  1257. DecimalPoint := DecimalPoint_is_Point;
  1258. BCD := StrToBCD ( s );
  1259. DecimalPoint := dp;
  1260. end;
  1261. function DoubleToBCD ( const aValue : myRealtype ) : tBCD; Inline;
  1262. begin
  1263. DoubleToBCD ( aValue, result );
  1264. end;
  1265. function IntegerToBCD ( const aValue : myInttype ) : tBCD;
  1266. var
  1267. bh : tBCD_helper;
  1268. v : {$ifopt r+} 0..high ( myInttype ) {$else} Integer {$endif};
  1269. p : {$ifopt r+} low ( bh.Singles ) - 1..0 {$else} Integer {$endif};
  1270. Error,
  1271. exitloop : Boolean;
  1272. begin
  1273. _SELECT
  1274. _WHEN aValue = 0
  1275. _THEN result := NullBCD;
  1276. _WHEN aValue = 1
  1277. _THEN result := OneBCD;
  1278. _WHEN aValue = low ( myInttype )
  1279. _THEN
  1280. {$if declared ( myMinIntBCD ) }
  1281. result := myMinIntBCD;
  1282. {$else}
  1283. RAISE eBCDOverflowException.create ( 'in IntegerToBCD' );
  1284. {$endif}
  1285. _WHENOTHER
  1286. bh := null_.bh;
  1287. WITH bh do
  1288. begin
  1289. Neg := aValue < 0;
  1290. if Neg
  1291. then v := -aValue
  1292. else v := +aValue;
  1293. LDig := 0;
  1294. p := 0;
  1295. Error := False;
  1296. REPEAT
  1297. Singles[p] := v MOD 10;
  1298. v := v DIV 10;
  1299. exitloop := v = 0;
  1300. Dec ( p );
  1301. if p < low ( Singles )
  1302. then begin
  1303. exitloop := True;
  1304. Error := True;
  1305. (* what to do if error occured? *)
  1306. RAISE eBCDOverflowException.create ( 'in IntegerToBCD' );
  1307. end;
  1308. UNTIL exitloop;
  1309. FDig := p + 1;
  1310. end;
  1311. pack_BCD ( bh, result );
  1312. _endSELECT;
  1313. end;
  1314. function VarToBCD ( const aValue : Variant ) : tBCD;
  1315. begin
  1316. not_implemented;
  1317. end;
  1318. function CurrToBCD ( const Curr : currency;
  1319. var BCD : tBCD;
  1320. Precision : Integer = 32;
  1321. Decimals : Integer = 4 ) : Boolean;
  1322. {
  1323. this works under the assumption that a currency is an int64,
  1324. except for scale of 10000
  1325. }
  1326. var
  1327. i : int64 absolute Curr;
  1328. begin
  1329. BCD := IntegerToBCD ( i );
  1330. {$ifndef bigger_BCD}
  1331. BCD.SignSpecialPlaces := 4 OR ( BCD.SignSpecialPlaces AND NegBit );
  1332. {$else}
  1333. BCD.Places := 4;
  1334. {$endif}
  1335. CurrToBCD := False;
  1336. if Decimals <> 4
  1337. then NormalizeBCD ( BCD, BCD, Precision, Decimals );
  1338. end;
  1339. {$ifdef comproutines}
  1340. function CompToBCD ( const Curr : Comp ) : tBCD; Inline;
  1341. var
  1342. cc : int64 absolute Curr;
  1343. begin
  1344. result := IntegerToBCD ( cc );
  1345. end;
  1346. function BCDToComp ( const BCD : tBCD ) : Comp; Inline;
  1347. var
  1348. zz : record
  1349. case Boolean of
  1350. False: ( i : int64 );
  1351. True: ( c : Comp );
  1352. end;
  1353. begin
  1354. zz.i := BCDToInteger ( BCD );
  1355. BCDToComp := zz.c;
  1356. end;
  1357. {$endif}
  1358. { Convert BCD struct to string/Double/Integer }
  1359. function BCDToStr ( const BCD : tBCD ) : FmtBCDStringtype;
  1360. var
  1361. bh : tBCD_helper;
  1362. l : {$ifopt r+} 0..maxfmtbcdfractionsize + 1 + 1 {$else} Integer {$endif};
  1363. i : {$ifopt r+} low ( bh.FDig )..high ( bh.LDig ) {$else} Integer {$endif};
  1364. pp : {$ifopt r+} low ( bh.FDig ) - 1..1 {$else} Integer {$endif};
  1365. begin
  1366. {$ifdef use_ansistring}
  1367. result := '';
  1368. {$endif}
  1369. unpack_BCD ( BCD, bh );
  1370. WITH bh do
  1371. begin
  1372. l := 0;
  1373. if Neg
  1374. then begin
  1375. {$ifndef use_ansistring}
  1376. Inc ( l );
  1377. result[1] := '-';
  1378. {$else}
  1379. result := result + '-';
  1380. {$endif}
  1381. end;
  1382. if Prec = Plac
  1383. then begin
  1384. {$ifndef use_ansistring}
  1385. Inc ( l );
  1386. result[1] := '0';
  1387. {$else}
  1388. result := result + '0';
  1389. {$endif}
  1390. end;
  1391. if Prec > 0
  1392. then begin
  1393. pp := low ( bh.FDig ) - 1;
  1394. if Plac > 0
  1395. then pp := 1;
  1396. for i := FDig TO LDig do
  1397. begin
  1398. if i = pp
  1399. then begin
  1400. {$ifndef use_ansistring}
  1401. Inc ( l );
  1402. result[l] := '.';
  1403. {$else}
  1404. result := result + '.';
  1405. {$endif}
  1406. end;
  1407. {$ifndef use_ansistring}
  1408. Inc ( l );
  1409. result[l] := Chr ( Singles[i] + Ord ( '0' ) );
  1410. {$else}
  1411. result := result + Chr ( Singles[i] + Ord ( '0' ) );
  1412. {$endif}
  1413. end;
  1414. end;
  1415. end;
  1416. {$ifndef use_ansistring}
  1417. result[0] := Chr ( l );
  1418. {$endif}
  1419. end;
  1420. function BCDToDouble ( const BCD : tBCD ) : myRealtype;
  1421. var
  1422. bh : tBCD_helper;
  1423. i : {$ifopt r+} low ( bh.FDig )..high ( bh.LDig ) {$else} Integer {$endif};
  1424. r,
  1425. e : myRealtype;
  1426. begin
  1427. unpack_BCD ( BCD, bh );
  1428. WITH bh do
  1429. begin
  1430. r := 0;
  1431. e := 1;
  1432. for i := 0 DOWNTO FDig do
  1433. begin
  1434. r := r + Singles[i] * e;
  1435. e := e * 10;
  1436. end;
  1437. e := 1;
  1438. for i := 1 TO LDig do
  1439. begin
  1440. e := e / 10;
  1441. r := r + Singles[i] * e;
  1442. end;
  1443. if Neg
  1444. then BCDToDouble := -r
  1445. else BCDToDouble := +r;
  1446. end;
  1447. end;
  1448. function BCDToInteger ( const BCD : tBCD;
  1449. Truncate : Boolean = False ) : myInttype;
  1450. var
  1451. bh : tBCD_helper;
  1452. res : myInttype;
  1453. i : {$ifopt r+} low ( bh.FDig )..0 {$else} Integer {$endif};
  1454. {
  1455. unclear: behaviour if overflow: abort? return 0? return something?
  1456. so: checks are missing yet
  1457. }
  1458. begin
  1459. unpack_BCD ( BCD, bh );
  1460. res := 0;
  1461. WITH bh do
  1462. begin
  1463. for i := FDig TO 0 do
  1464. res := res * 10 - Singles[i];
  1465. if NOT Truncate
  1466. then
  1467. if Plac > 0
  1468. then
  1469. if Singles[1] > 4
  1470. then Dec ( res );
  1471. if Neg
  1472. then BCDToInteger := +res
  1473. else BCDToInteger := -res;
  1474. end;
  1475. end;
  1476. { From DB.pas }
  1477. function BCDToCurr ( const BCD : tBCD;
  1478. var Curr : currency ) : Boolean;
  1479. var
  1480. bh : tBCD_helper;
  1481. res : int64;
  1482. c : currency absolute res;
  1483. i : {$ifopt r+} low ( bh.FDig )..4 {$else} Integer {$endif};
  1484. {
  1485. unclear: behaviour if overflow: abort? return 0? return something?
  1486. }
  1487. begin
  1488. BCDToCurr := True;
  1489. unpack_BCD ( BCD, bh );
  1490. res := 0;
  1491. WITH bh do
  1492. begin
  1493. for i := FDig TO 4 do
  1494. res := res * 10 + Singles[i];
  1495. if Plac > 4
  1496. then
  1497. if Singles[5] > 4
  1498. then Inc ( res );
  1499. if Neg
  1500. then Curr := -c
  1501. else Curr := +c;
  1502. end;
  1503. end;
  1504. procedure BCDAdd ( const BCDin1,
  1505. BCDin2 : tBCD;
  1506. var BCDout : tBCD );
  1507. var
  1508. bhr,
  1509. bh1,
  1510. bh2 : tBCD_helper;
  1511. ue : {$ifopt r+} 0..1 {$else} Integer {$endif};
  1512. i : {$ifopt r+} low ( bh1.FDig )..high ( bh1.LDig ) {$else} Integer {$endif};
  1513. v : {$ifopt r+} 0..9 + 9 + 1 {$else} Integer {$endif};
  1514. BCD : tBCD;
  1515. negate : Boolean;
  1516. begin
  1517. negate := IsBCDNegative ( BCDin1 );
  1518. if negate <> IsBCDNegative ( BCDin2 )
  1519. then begin
  1520. if negate
  1521. then begin
  1522. BCD := BCDin1;
  1523. BCDNegate ( BCD );
  1524. BCDSubtract ( BCDin2, BCD, BCDout );
  1525. EXIT;
  1526. end;
  1527. BCD := BCDin2;
  1528. BCDNegate ( BCD );
  1529. BCDSubtract ( BCDin1, BCD, BCDout );
  1530. EXIT;
  1531. end;
  1532. bhr := null_.bh;
  1533. WITH bhr do
  1534. begin
  1535. unpack_BCD ( BCDin1, bh1 );
  1536. unpack_BCD ( BCDin2, bh2 );
  1537. if bh1.FDig < bh2.FDig
  1538. then FDig := bh1.FDig
  1539. else FDig := bh2.FDig;
  1540. if bh1.LDig > bh2.LDig
  1541. then LDig := bh1.LDig
  1542. else LDig := bh2.LDig;
  1543. Plac := LDig;
  1544. ue := 0;
  1545. for i := LDig DOWNTO FDig do
  1546. begin
  1547. v := bh1.Singles[i] + bh2.Singles[i] + ue;
  1548. ue := v DIV 10;
  1549. Singles[i] := v MOD 10;
  1550. end;
  1551. if ue <> 0
  1552. then begin
  1553. Dec ( FDig );
  1554. Singles[FDig] := ue;
  1555. end;
  1556. Neg := negate;
  1557. end;
  1558. if NOT pack_BCD ( bhr, BCDout )
  1559. then begin
  1560. RAISE eBCDOverflowException.create ( 'in BCDAdd' );
  1561. end;
  1562. end;
  1563. procedure BCDSubtract ( const BCDin1,
  1564. BCDin2 : tBCD;
  1565. var BCDout : tBCD );
  1566. var
  1567. bhr,
  1568. bh1,
  1569. bh2 : tBCD_helper;
  1570. cmp : {$ifopt r+} -1..1 {$else} Integer {$endif};
  1571. ue : {$ifopt r+} 0..1 {$else} Integer {$endif};
  1572. i : {$ifopt r+} low ( bh1.FDig )..high ( bh1.LDig ) {$else} Integer {$endif};
  1573. v : {$ifopt r+} 0 - 9 - 1..9 - 0 - 0 {$else} Integer {$endif};
  1574. negate : Boolean;
  1575. BCD : tBCD;
  1576. begin
  1577. negate := IsBCDNegative ( BCDin1 );
  1578. if negate <> IsBCDNegative ( BCDin2 )
  1579. then begin
  1580. if negate
  1581. then begin
  1582. BCD := BCDin1;
  1583. BCDNegate ( BCD );
  1584. BCDAdd ( BCDin2, BCD, BCDout );
  1585. BCDNegate ( BCDout );
  1586. EXIT;
  1587. end;
  1588. BCD := BCDin2;
  1589. BCDNegate ( BCD );
  1590. BCDAdd ( BCDin1, BCD, BCDout );
  1591. EXIT;
  1592. end;
  1593. cmp := BCDCompare ( BCDin1, BCDin2 );
  1594. if cmp = 0
  1595. then begin
  1596. BCDout := NullBCD;
  1597. EXIT;
  1598. end;
  1599. bhr := null_.bh; { n n }
  1600. WITH bhr do { > < > < }
  1601. begin { }
  1602. if ( cmp > 0 ) = negate { +123 +12 -12 -123 }
  1603. then begin { - +12 - +123 - -123 - -12 }
  1604. unpack_BCD ( BCDin1, bh2 ); { x x }
  1605. unpack_BCD ( BCDin2, bh1 ); { s s s s }
  1606. negate := NOT negate; { nn n nn n }
  1607. end
  1608. else begin
  1609. unpack_BCD ( BCDin1, bh1 );
  1610. unpack_BCD ( BCDin2, bh2 );
  1611. end;
  1612. if bh1.FDig < bh2.FDig
  1613. then FDig := bh1.FDig
  1614. else FDig := bh2.FDig;
  1615. if bh1.LDig > bh2.LDig
  1616. then LDig := bh1.LDig
  1617. else LDig := bh2.LDig;
  1618. Plac := LDig;
  1619. ue := 0;
  1620. for i := LDig DOWNTO FDig do
  1621. begin
  1622. v := Integer ( bh1.Singles[i] ) - bh2.Singles[i] - ue;
  1623. ue := 0;
  1624. if v < 0
  1625. then begin
  1626. ue := 1;
  1627. Inc ( v, 10 );
  1628. end;
  1629. Singles[i] := v;
  1630. end;
  1631. Neg := negate;
  1632. if NOT pack_BCD ( bhr, BCDout )
  1633. then begin
  1634. {should never occur!}
  1635. RAISE eBCDOverflowException.create ( 'in BCDSubtract' );
  1636. end;
  1637. end;
  1638. end;
  1639. { Returns True if successful, False if Int Digits needed to be truncated }
  1640. function NormalizeBCD ( const InBCD : tBCD;
  1641. var OutBCD : tBCD;
  1642. const Prec,
  1643. Scale : Word ) : Boolean;
  1644. var
  1645. bh : tBCD_helper;
  1646. tm : {$ifopt r+} 1..maxfmtbcdfractionsize - 1 {$else} Integer {$endif};
  1647. begin
  1648. NormalizeBCD := True;
  1649. {$ifopt r+}
  1650. if ( Prec < 0 ) OR ( Prec > MaxFmtBCDFractionSize ) then rcheck := rbad;
  1651. if ( Scale < 0 ) OR ( Prec >= MaxFmtBCDFractionSize ) then rcheck := rbad;
  1652. {$endif}
  1653. if BCDScale ( InBCD ) > Scale
  1654. then begin
  1655. unpack_BCD ( InBCD, bh );
  1656. WITH bh do
  1657. begin
  1658. tm := Plac - Scale;
  1659. Plac := Scale;
  1660. { dec ( prec, tm ); Dec/Inc error? }
  1661. Prec := Prec - tm;
  1662. { dec ( ldig, tm ); Dec/Inc error? }
  1663. LDig := LDig - tm;
  1664. NormalizeBCD := False;
  1665. end;
  1666. if NOT pack_BCD ( bh, OutBCD )
  1667. then begin
  1668. RAISE eBCDOverflowException.create ( 'in BCDAdd' );
  1669. end;
  1670. end;
  1671. end;
  1672. procedure BCDMultiply ( const BCDin1,
  1673. BCDin2 : tBCD;
  1674. var BCDout : tBCD );
  1675. var
  1676. bh1,
  1677. bh2,
  1678. bhr : tBCD_helper;
  1679. bhrr : tBCD_helper_big;
  1680. i1 : {$ifopt r+} low ( bh1.FDig )..high ( bh1.LDig ) {$else} Integer {$endif};
  1681. i2 : {$ifopt r+} low ( bh2.FDig )..high ( bh2.LDig ) {$else} Integer {$endif};
  1682. i3 : {$ifopt r+} low ( bhrr.FDig )..high ( bhrr.LDig ) {$else} Integer {$endif};
  1683. v : {$ifopt r+} low ( bhrr.Singles[0] )..high ( bhrr.Singles[0] ) {$else} Integer {$endif};
  1684. ue : {$ifopt r+} low ( bhrr.Singles[0] ) DIV 10..high ( bhrr.Singles[0] ) DIV 10 {$else} Integer {$endif};
  1685. begin
  1686. unpack_BCD ( BCDin1, bh1 );
  1687. unpack_BCD ( BCDin2, bh2 );
  1688. if ( bh1.Prec = 0 ) OR ( bh2.Prec = 0 )
  1689. then begin
  1690. BCDout := NullBCD;
  1691. EXIT;
  1692. end;
  1693. bhr := null_.bh;
  1694. bhrr := null_.bhb;
  1695. WITH bhrr do
  1696. begin
  1697. Neg := bh1.Neg XOR bh2.Neg;
  1698. {
  1699. writeln ( __lo_bhb, ' ', __hi_bhb, ' ', bh1.fdig, ' ', bh2.fdig, ' ', low ( fdig ), ' ', low ( ldig ) );
  1700. }
  1701. FDig := bh1.FDig + bh2.FDig;
  1702. LDig := bh1.LDig + bh2.LDig;
  1703. for i1 := bh1.FDig TO bh1.LDig do
  1704. for i2 := bh2.FDig TO bh2.LDig do
  1705. begin
  1706. Inc ( Singles[i1 + i2],
  1707. bh1.Singles[i1]
  1708. * bh2.Singles[i2] );
  1709. {
  1710. write ( Singles[i1 + i2], ' ', bh1.Singles[i1], ' ', bh2.Singles[i2], ' : ' );
  1711. writeln ( Singles[i1 + i2] + bh1.Singles[i1] + bh2.Singles[i2] );
  1712. }
  1713. {
  1714. Singles[i1 + i2] := Singles[i1 + i2]
  1715. + bh1.Singles[i1]
  1716. * bh2.Singles[i2];
  1717. }
  1718. end;
  1719. {
  1720. for i3 := fdig to ldig do
  1721. write ( ' ', singles[i3] );
  1722. writeln;
  1723. }
  1724. if FDig < low ( bhr.Singles )
  1725. then RAISE eBCDOverflowException.create ( 'in BCDMultiply' );
  1726. ue := 0;
  1727. for i3 := LDig DOWNTO FDig do
  1728. begin
  1729. v := Singles[i3] + ue;
  1730. ue := v DIV 10;
  1731. v := v MOD 10;
  1732. bhr.Singles[i3] := v;
  1733. end;
  1734. while ue <> 0 do
  1735. begin
  1736. Dec ( FDig );
  1737. if FDig < low ( bhr.Singles )
  1738. then RAISE eBCDOverflowException.create ( 'in BCDMultiply' );
  1739. bhr.Singles[FDig] := ue MOD 10;
  1740. ue := ue DIV 10;
  1741. end;
  1742. bhr.Plac := LDig;
  1743. bhr.FDig := FDig;
  1744. if LDig > high ( bhr.Singles )
  1745. then bhr.LDig := high ( bhr.Singles )
  1746. else bhr.LDig := LDig;
  1747. end;
  1748. if NOT pack_BCD ( bhr, BCDout )
  1749. then begin
  1750. RAISE eBCDOverflowException.create ( 'in BCDMultiply' );
  1751. end;
  1752. end;
  1753. procedure BCDMultiply ( const BCDIn : tBCD;
  1754. const DoubleIn : myRealtype;
  1755. var BCDout : tBCD ); Inline;
  1756. begin
  1757. BCDMultiply ( BCDIn, DoubleToBCD ( DoubleIn ), BCDout );
  1758. end;
  1759. procedure BCDMultiply ( const BCDIn : tBCD;
  1760. const StringIn : FmtBCDStringtype;
  1761. var BCDout : tBCD ); Inline;
  1762. begin
  1763. BCDMultiply ( BCDIn, StrToBCD ( StringIn ), BCDout );
  1764. end;
  1765. procedure BCDMultiply ( const StringIn1,
  1766. StringIn2 : FmtBCDStringtype;
  1767. var BCDout : tBCD ); Inline;
  1768. begin
  1769. BCDMultiply ( StrToBCD ( StringIn1 ), StrToBCD ( StringIn2 ), BCDout );
  1770. end;
  1771. procedure BCDDivide ( const Dividend,
  1772. Divisor : tBCD;
  1773. var BCDout : tBCD );
  1774. var
  1775. bh1 : ARRAY [ Boolean ] of tBCD_helper;
  1776. bh2,
  1777. bh : tBCD_helper;
  1778. p : {$ifopt r+} low ( bh.FDig ) - high ( bh.FDig )..high ( bh.FDig ) - low ( bh.FDig ) {$else} Integer {$endif};
  1779. v1 : {$ifopt r+} low ( bh.Singles[0] )..high ( bh.Singles[0] ) {$else} Integer {$endif};
  1780. v2 : {$ifopt r+} low ( bh.Singles[0] )..high ( bh.Singles[0] ) {$else} Integer {$endif};
  1781. lFDig : {$ifopt r+} low ( bh.FDig )..high ( bh.FDig ) {$else} Integer {$endif};
  1782. d1 : {$ifopt r+} low ( bh.LDig ) - high ( bh.FDig )..high ( bh.LDig ) - low ( bh.FDig ) {$else} Integer {$endif};
  1783. d2 : {$ifopt r+} low ( bh.LDig ) - high ( bh.FDig )..high ( bh.LDig ) - low ( bh.FDig ) {$else} Integer {$endif};
  1784. d : {$ifopt r+} low ( bh.LDig ) - high ( bh.FDig )..high ( bh.LDig ) - low ( bh.FDig ) {$else} Integer {$endif};
  1785. lLdig : {$ifopt r+} low ( lFDig ) + low ( d )..high ( lFDig ) + high ( d ) {$else} Integer {$endif};
  1786. tm : {$ifopt r+} low ( lLdig ) - high ( bh2.Singles )..high ( lLdig ) - high ( bh2.Singles ) {$else} Integer {$endif};
  1787. i2 : {$ifopt r+} low ( lFDig )..high ( lLdig ) {$else} Integer {$endif};
  1788. i3 : {$ifopt r+} low ( lFDig )..high ( lLdig ) {$else} Integer {$endif};
  1789. ie : {$ifopt r+} low ( lFDig )..high ( lLdig ) {$else} Integer {$endif};
  1790. i4 : {$ifopt r+} low ( lFDig )..high ( lLdig ) {$else} Integer {$endif};
  1791. nFDig : {$ifopt r+} low ( i2 )..high ( i2 ) {$else} Integer {$endif};
  1792. nLDig : {$ifopt r+} low ( i2 )..high ( i2 ) {$else} Integer {$endif};
  1793. dd : {$ifopt r+} 0..9 {$else} Integer {$endif};
  1794. Add : {$ifopt r+} 0..99 {$else} Integer {$endif};
  1795. ue : {$ifopt r+} 0..99 {$else} Integer {$endif};
  1796. v3 : {$ifopt r+} low ( bh.Singles[0] ) - high ( bh2.singles[9] ) * high ( dd ) - high ( ue )..high ( bh.Singles[0] ) - low ( bh2.singles[9] ) * low ( dd ) - low ( ue ) {$else} Integer {$endif};
  1797. v4 : {$ifopt r+} low ( bh.Singles[0] ) + low ( add )..high ( bh.Singles[0] ) + high ( add ) {$else} Integer {$endif};
  1798. FlipFlop,
  1799. nz,
  1800. sf,
  1801. sh,
  1802. fdset : Boolean;
  1803. {
  1804. bh1p : ARRAY [ Boolean ] of ^ tBCD_helper;
  1805. }
  1806. begin
  1807. { test:
  1808. bh1p[false] := @ bh1[false];
  1809. bh1p[true] := @ bh1[true];
  1810. v := bh1[false].singles[0];
  1811. v := bh1[true].singles[0];
  1812. v := bh1p[false]^.singles[0];
  1813. v := bh1p[true]^.singles[0];
  1814. v := bh1[nz].singles[0];
  1815. v := bh1p[nz]^.singles[0];
  1816. }
  1817. unpack_BCD ( Divisor, bh2 );
  1818. unpack_BCD ( Dividend, bh1[False] );
  1819. p := bh1[False].FDig - bh2.FDig;
  1820. _SELECT
  1821. _WHEN bh2.Prec = 0
  1822. _THEN RAISE eBCDException.create ( 'Division by zero' );
  1823. _WHEN bh1[False].Prec = 0
  1824. _THEN BCDout := NullBCD;
  1825. _WHEN p < low ( bh2.Singles )
  1826. _THEN RAISE eBCDOverflowException.create ( 'in BCDDivide' );
  1827. _WHENOTHER
  1828. bh := null_.bh;
  1829. bh.Neg := bh1[False].Neg XOR bh2.Neg;
  1830. if p <= high ( bh.Singles )
  1831. then begin
  1832. bh1[True] := null_.bh;
  1833. FlipFlop := False;
  1834. fdset := p > 0;
  1835. if fdset
  1836. then bh.FDig := 0;
  1837. add := 0;
  1838. nz := True;
  1839. while nz do
  1840. WITH bh1[FlipFlop] do
  1841. begin
  1842. {
  1843. WriteLn('#####');
  1844. dumpbh ( bh1[flipflop] );
  1845. dumpbh ( bh2 );
  1846. dumpbh ( bh );
  1847. }
  1848. if ( Singles[FDig] + bh2.Singles[bh2.FDig] ) = 0
  1849. then begin
  1850. if ( FDig >= LDig )
  1851. OR ( bh2.FDig >= bh2.LDig )
  1852. then nz := False
  1853. else begin
  1854. Inc ( FDig );
  1855. Inc ( bh2.FDig );
  1856. end;
  1857. end
  1858. else begin
  1859. v1 := Singles[FDig];
  1860. v2 := bh2.Singles[bh2.FDig];
  1861. sh := v1 < v2;
  1862. if ( v1 = v2 )
  1863. then begin
  1864. nz := False;
  1865. i3 := Succ ( FDig );
  1866. ie := LDig;
  1867. while ( i3 <= ie ) AND ( NOT nz ) AND ( NOT sh ) do
  1868. begin
  1869. v1 := Singles[i3];
  1870. v2 := bh2.Singles[i3 - p];
  1871. if v1 <> v2
  1872. then begin
  1873. nz := True;
  1874. if v1 < v2
  1875. then sh := True;
  1876. end;
  1877. Inc ( i3 );
  1878. end;
  1879. end;
  1880. if NOT nz
  1881. then Add := 1
  1882. else begin
  1883. if sh
  1884. then begin
  1885. Inc ( p );
  1886. {
  1887. if p > 3 then halt;
  1888. }
  1889. if p > high ( bh.Singles )
  1890. then nz := False
  1891. else Dec ( bh2.FDig );
  1892. end
  1893. else begin
  1894. lFDig := FDig;
  1895. d1 := LDig - FDig;
  1896. d2 := bh2.LDig - bh2.FDig;
  1897. if d1 > d2
  1898. then d := d1
  1899. else d := d2;
  1900. lLdig := lFDig + d;
  1901. if lLdig > high ( bh2.Singles )
  1902. then begin
  1903. tm := ( lLdig ) - high ( bh2.Singles );
  1904. d := d - tm;
  1905. lLdig := lLdig - tm;
  1906. {runden?}
  1907. end;
  1908. sf := True;
  1909. Add := 0;
  1910. nFDig := 0;
  1911. nLDig := 0;
  1912. ue := 0;
  1913. dd := Singles[lFDig] DIV ( bh2.Singles[lFDig - p] + 1 );
  1914. {
  1915. dd := 1;
  1916. }
  1917. if dd < 1
  1918. then dd := 1;
  1919. {
  1920. writeln ( 'p=', p, ' dd=', dd, ' lFdig=', lfdig, ' lldig=', lldig );
  1921. }
  1922. for i2 := lLdig DOWNTO lFDig do
  1923. begin
  1924. v3 := Singles[i2] - bh2.Singles[i2 - p] * dd - ue;
  1925. ue := 0;
  1926. while v3 < 0 do
  1927. begin
  1928. Inc ( ue );;
  1929. v3 := v3 + 10;
  1930. end;
  1931. {
  1932. if v3 <> 0
  1933. then begin
  1934. }
  1935. bh1[NOT FlipFlop].Singles[i2] := v3;
  1936. {
  1937. nFDig := i2;
  1938. if sf
  1939. then begin
  1940. nLDig := i2;
  1941. sf := False;
  1942. end;
  1943. end;
  1944. }
  1945. end;
  1946. sf := False;
  1947. nfdig := lfdig;
  1948. nldig := lldig;
  1949. Inc ( Add, dd );
  1950. if NOT fdset
  1951. then begin
  1952. bh.FDig := p;
  1953. fdset := True;
  1954. end;
  1955. if bh.LDig < p
  1956. then begin
  1957. bh.LDig := p;
  1958. if ( bh.LDig - bh.FDig ) > Succ ( MaxFmtBCDFractionSize )
  1959. then nz := False;
  1960. end;
  1961. if sf
  1962. then nz := False
  1963. else begin
  1964. FillChar ( bh1[FlipFlop], SizeOf ( bh1[FlipFlop] ), #0 );
  1965. FlipFlop := NOT FlipFlop;
  1966. WITH bh1[FlipFlop] do
  1967. begin
  1968. FDig := nFDig;
  1969. LDig := nLDig;
  1970. end;
  1971. end;
  1972. end;
  1973. end;
  1974. if Add <> 0
  1975. then begin
  1976. i4 := p;
  1977. while ( Add <> 0 ) AND ( i4 >= bh.FDig ) do
  1978. begin
  1979. {
  1980. writeln ( '> ', i4, ' ', bh.Singles[i4], ' ', Add );
  1981. }
  1982. v4 := bh.Singles[i4] + Add;
  1983. Add := v4 DIV 10;
  1984. bh.Singles[i4] := v4 MOD 10;
  1985. Dec ( i4 );
  1986. end;
  1987. if Add <> 0
  1988. then begin
  1989. Dec ( bh.FDig );
  1990. bh.Singles[bh.FDig] := Add;
  1991. Add := 0;
  1992. end;
  1993. end;
  1994. end;
  1995. end;
  1996. end;
  1997. WITH bh do
  1998. begin
  1999. if LDig < 0
  2000. then LDig := 0;
  2001. if LDig > 0
  2002. then Plac := LDig
  2003. else Plac := 0;
  2004. end;
  2005. if NOT pack_BCD ( bh, BCDout )
  2006. then begin
  2007. RAISE eBCDOverflowException.create ( 'in BCDDivide' );
  2008. end;
  2009. _endSELECT
  2010. end;
  2011. procedure BCDDivide ( const Dividend,
  2012. Divisor : FmtBCDStringtype;
  2013. var BCDout : tBCD ); Inline;
  2014. begin
  2015. BCDDivide ( StrToBCD ( Dividend ), StrToBCD ( Divisor ), BCDout );
  2016. end;
  2017. procedure BCDDivide ( const Dividend : tBCD;
  2018. const Divisor : myRealtype;
  2019. var BCDout : tBCD ); Inline;
  2020. begin
  2021. BCDDivide ( Dividend, DoubleToBCD ( Divisor ), BCDout );
  2022. end;
  2023. procedure BCDDivide ( const Dividend : tBCD;
  2024. const Divisor : FmtBCDStringtype;
  2025. var BCDout : tBCD ); Inline;
  2026. begin
  2027. BCDDivide ( Dividend, StrToBCD ( Divisor ), BCDout );
  2028. end;
  2029. { TBCD variant creation utils }
  2030. procedure VarFmtBCDCreate ( var aDest : Variant;
  2031. const aBCD : tBCD );
  2032. begin
  2033. VarClear(aDest);
  2034. TVarData(aDest).Vtype:=FMTBcdFactory.Vartype;
  2035. TVarData(aDest).VPointer:=TFMTBcdVarData.create(aBCD);
  2036. end;
  2037. function VarFmtBCDCreate : Variant;
  2038. begin
  2039. VarFmtBCDCreate ( result, NullBCD );
  2040. end;
  2041. function VarFmtBCDCreate ( const aValue : FmtBCDStringtype;
  2042. Precision,
  2043. Scale : Word ) : Variant;
  2044. begin
  2045. VarFmtBCDCreate ( result, StrToBCD ( aValue ) );
  2046. end;
  2047. function VarFmtBCDCreate ( const aValue : myRealtype;
  2048. Precision : Word = 18;
  2049. Scale : Word = 4 ) : Variant;
  2050. begin
  2051. VarFmtBCDCreate ( result, DoubleToBCD ( aValue ) );
  2052. end;
  2053. function VarFmtBCDCreate ( const aBCD : tBCD ) : Variant;
  2054. begin
  2055. VarFmtBCDCreate ( result, aBCD );
  2056. end;
  2057. function VarIsFmtBCD ( const aValue : Variant ) : Boolean;
  2058. begin
  2059. result:=false;
  2060. not_implemented;
  2061. end;
  2062. function VarFmtBCD : TVartype;
  2063. begin
  2064. result:=0;
  2065. not_implemented;
  2066. end;
  2067. { Formatting BCD as string }
  2068. function BCDToStrF ( const BCD : tBCD;
  2069. Format : TFloatFormat;
  2070. const Precision,
  2071. Digits : Integer ) : FmtBCDStringtype;
  2072. begin
  2073. not_implemented;
  2074. result:='';
  2075. end;
  2076. function FormatBCD ( const Format : string;
  2077. BCD : tBCD ) : FmtBCDStringtype;
  2078. begin
  2079. not_implemented;
  2080. result:='';
  2081. end;
  2082. {$ifdef additional_routines}
  2083. function CurrToBCD ( const Curr : currency ) : tBCD; Inline;
  2084. begin
  2085. CurrToBCD ( Curr, result );
  2086. end;
  2087. procedure BCDAdd ( const BCDIn : tBCD;
  2088. const IntIn : myInttype;
  2089. var BCDout : tBCD );
  2090. var
  2091. BCD : tBCD;
  2092. bhr : tBCD_helper;
  2093. p : {$ifopt r+} low ( bhr.FDig ) - 1..0 {$else} Integer {$endif};
  2094. ue : {$ifopt r+} 0..high ( IntIn ) - 9 {$else} Integer {$endif};
  2095. v : {$ifopt r+} 0..{high ( ue ) + 9} high ( IntIn ) {$else} Integer {$endif};
  2096. nz : Boolean;
  2097. begin
  2098. if IntIn = 0
  2099. then begin
  2100. BCDout := BCDIn;
  2101. EXIT;
  2102. end;
  2103. if IntIn = low ( myInttype )
  2104. then begin
  2105. {$if declared ( myMinIntBCD ) }
  2106. BCDAdd ( BCDIn, myMinIntBCD, BCDout );
  2107. EXIT;
  2108. {$else}
  2109. RAISE eBCDOverflowException.create ( 'in BCDAdd' );
  2110. {$endif}
  2111. end;
  2112. if IsBCDNegative ( BCDIn )
  2113. then begin
  2114. BCD := BCDIn;
  2115. BCDNegate ( BCD );
  2116. if IntIn < 0
  2117. then BCDAdd ( BCD, -IntIn, BCDout )
  2118. else BCDSubtract ( BCD, IntIn, BCDout );
  2119. BCDNegate ( BCDout );
  2120. EXIT;
  2121. end;
  2122. if IntIn < 0
  2123. then begin
  2124. BCDSubtract ( BCDIn, -IntIn, BCDout );
  2125. EXIT;
  2126. end;
  2127. if IntIn > ( high ( IntIn ) - 9 )
  2128. then begin
  2129. BCDAdd ( BCDIn, IntegerToBCD ( IntIn ), BCDout );
  2130. EXIT;
  2131. end;
  2132. unpack_BCD ( BCDIn, bhr );
  2133. p := 0;
  2134. nz := True;
  2135. ue := IntIn;
  2136. while nz do
  2137. begin
  2138. v := bhr.Singles[p] + ue;
  2139. bhr.Singles[p] := v MOD 10;
  2140. ue := v DIV 10;
  2141. if ue = 0
  2142. then nz := False
  2143. else Dec ( p );
  2144. end;
  2145. if p < bhr.FDig
  2146. then begin
  2147. bhr.FDig := p;
  2148. bhr.Prec := bhr.Prec + ( bhr.FDig - p );
  2149. end;
  2150. if NOT pack_BCD ( bhr, BCDout )
  2151. then begin
  2152. RAISE eBCDOverflowException.create ( 'in BCDAdd' );
  2153. end;
  2154. end;
  2155. procedure BCDSubtract ( const BCDIn : tBCD;
  2156. const IntIn : myInttype;
  2157. var BCDout : tBCD );
  2158. {}
  2159. var
  2160. BCD : tBCD;
  2161. bhr : tBCD_helper;
  2162. p : {$ifopt r+} low ( bhr.FDig ) - 1..0 {$else} Integer {$endif};
  2163. ue : {$ifopt r+} 0..pred ( 100000000 ) {$else} Integer {$endif};
  2164. v : {$ifopt r+} -9..9 {$else} Integer {$endif};
  2165. direct : Boolean;
  2166. {}
  2167. begin
  2168. if IntIn = 0
  2169. then begin
  2170. BCDout := BCDIn;
  2171. EXIT;
  2172. end;
  2173. if IntIn = low ( myInttype )
  2174. then begin
  2175. {$if declared ( myMinIntBCD ) }
  2176. BCDSubtract ( BCDIn, myMinIntBCD, BCDout );
  2177. EXIT;
  2178. {$else}
  2179. RAISE eBCDOverflowException.create ( 'in BCDSubtract' );
  2180. {$endif}
  2181. end;
  2182. if IsBCDNegative ( BCDIn )
  2183. then begin
  2184. BCD := BCDIn;
  2185. BCDNegate ( BCD );
  2186. if IntIn < 0
  2187. then BCDSubtract ( BCD, -IntIn, BCDout )
  2188. else BCDAdd ( BCD, IntIn, BCDout );
  2189. BCDNegate ( BCDout );
  2190. EXIT;
  2191. end;
  2192. if IntIn < 0
  2193. then begin
  2194. BCDAdd ( BCDIn, -IntIn, BCDout );
  2195. EXIT;
  2196. end;
  2197. direct := False;
  2198. case BCDIn.Precision
  2199. -
  2200. {$ifndef bigger_BCD}
  2201. ( BCDIn.SignSpecialPlaces AND PlacesMask )
  2202. {$else}
  2203. BCDIn.Places
  2204. {$endif}
  2205. of
  2206. 2: direct := IntIn < 10;
  2207. 3: direct := IntIn < 100;
  2208. 4: direct := IntIn < 1000;
  2209. 5: direct := IntIn < 10000;
  2210. 6: direct := IntIn < 100000;
  2211. 7: direct := IntIn < 1000000;
  2212. 8: direct := IntIn < 10000000;
  2213. 9: direct := IntIn < 100000000;
  2214. end;
  2215. {
  2216. write(direct);dumpbcd(bcdin);write('[',intin,']');
  2217. }
  2218. if direct
  2219. then begin
  2220. unpack_BCD ( BCDIn, bhr );
  2221. WITH bhr do
  2222. begin
  2223. p := 0;
  2224. ue := IntIn;
  2225. while p >= FDig do
  2226. begin
  2227. v := Singles[p] - ue MOD 10;
  2228. ue := ue DIV 10;
  2229. if v < 0
  2230. then begin
  2231. v := v + 10;
  2232. ue := ue + 1;
  2233. end;
  2234. Singles[p] := v;
  2235. Dec ( p );
  2236. end;
  2237. end;
  2238. if NOT pack_BCD ( bhr, BCDout )
  2239. then begin
  2240. RAISE eBCDOverflowException.create ( 'in BCDSubtract' );
  2241. end;
  2242. end
  2243. else
  2244. {}
  2245. BCDSubtract ( BCDIn, IntegerToBCD ( IntIn ), BCDout );
  2246. end;
  2247. procedure BCDAdd ( const IntIn : myInttype;
  2248. const BCDIn : tBCD;
  2249. var BCDout : tBCD ); Inline;
  2250. begin
  2251. BCDAdd ( BCDIn, IntIn, BCDout );
  2252. end;
  2253. procedure BCDAdd ( const BCDIn : tBCD;
  2254. const DoubleIn : myRealtype;
  2255. var BCDout : tBCD ); Inline;
  2256. begin
  2257. BCDAdd ( BCDIn, DoubleToBCD ( DoubleIn ), BCDout );
  2258. end;
  2259. procedure BCDAdd ( const DoubleIn : myRealtype;
  2260. const BCDIn : tBCD;
  2261. var BCDout : tBCD ); Inline;
  2262. begin
  2263. BCDAdd ( DoubleToBCD ( DoubleIn ), BCDIn, BCDout );
  2264. end;
  2265. procedure BCDAdd ( const BCDIn : tBCD;
  2266. const Currin : currency;
  2267. var BCDout : tBCD ); Inline;
  2268. begin
  2269. BCDAdd ( BCDIn, CurrToBCD ( Currin ), BCDout );
  2270. end;
  2271. procedure BCDAdd ( const Currin : currency;
  2272. const BCDIn : tBCD;
  2273. var BCDout : tBCD ); Inline;
  2274. begin
  2275. BCDAdd ( CurrToBCD ( Currin ), BCDIn, BCDout );
  2276. end;
  2277. {$ifdef comproutines}
  2278. procedure BCDAdd ( const BCDIn : tBCD;
  2279. const Compin : Comp;
  2280. var BCDout : tBCD ); Inline;
  2281. begin
  2282. BCDAdd ( BCDIn, CompToBCD ( Compin ), BCDout );
  2283. end;
  2284. procedure BCDAdd ( const Compin : Comp;
  2285. const BCDIn : tBCD;
  2286. var BCDout : tBCD ); Inline;
  2287. begin
  2288. BCDAdd ( CompToBCD ( Compin ), BCDIn, BCDout );
  2289. end;
  2290. {$endif}
  2291. procedure BCDAdd ( const BCDIn : tBCD;
  2292. const StringIn : FmtBCDStringtype;
  2293. var BCDout : tBCD ); Inline;
  2294. begin
  2295. BCDAdd ( BCDIn, StrToBCD ( StringIn ), BCDout );
  2296. end;
  2297. procedure BCDAdd ( const StringIn : FmtBCDStringtype;
  2298. const BCDIn : tBCD;
  2299. var BCDout : tBCD ); Inline;
  2300. begin
  2301. BCDAdd ( StrToBCD ( StringIn ), BCDIn, BCDout );
  2302. end;
  2303. procedure BCDAdd ( const StringIn1,
  2304. StringIn2 : FmtBCDStringtype;
  2305. var BCDout : tBCD ); Inline;
  2306. begin
  2307. BCDAdd ( StrToBCD ( StringIn1 ), StrToBCD ( StringIn2 ), BCDout );
  2308. end;
  2309. procedure BCDSubtract ( const IntIn : myInttype;
  2310. const BCDIn : tBCD;
  2311. var BCDout : tBCD ); Inline;
  2312. begin
  2313. BCDSubtract ( BCDIn, IntIn, BCDout );
  2314. BCDNegate ( BCDout );
  2315. end;
  2316. procedure BCDSubtract ( const BCDIn : tBCD;
  2317. const DoubleIn : myRealtype;
  2318. var BCDout : tBCD ); Inline;
  2319. begin
  2320. BCDSubtract ( BCDIn, DoubleToBCD ( DoubleIn ), BCDout );
  2321. end;
  2322. procedure BCDSubtract ( const DoubleIn : myRealtype;
  2323. const BCDIn : tBCD;
  2324. var BCDout : tBCD ); Inline;
  2325. begin
  2326. BCDSubtract ( DoubleToBCD ( DoubleIn ), BCDIn, BCDout );
  2327. end;
  2328. procedure BCDSubtract ( const BCDIn : tBCD;
  2329. const Currin : currency;
  2330. var BCDout : tBCD ); Inline;
  2331. begin
  2332. BCDSubtract ( BCDIn, CurrToBCD ( Currin ), BCDout );
  2333. end;
  2334. procedure BCDSubtract ( const Currin : currency;
  2335. const BCDIn : tBCD;
  2336. var BCDout : tBCD ); Inline;
  2337. begin
  2338. BCDSubtract ( CurrToBCD ( Currin ), BCDIn, BCDout );
  2339. end;
  2340. {$ifdef comproutines}
  2341. procedure BCDSubtract ( const BCDIn : tBCD;
  2342. const Compin : Comp;
  2343. var BCDout : tBCD ); Inline;
  2344. begin
  2345. BCDSubtract ( BCDIn, CompToBCD ( Compin ), BCDout );
  2346. end;
  2347. procedure BCDSubtract ( const Compin : Comp;
  2348. const BCDIn : tBCD;
  2349. var BCDout : tBCD ); Inline;
  2350. begin
  2351. BCDSubtract ( CompToBCD ( Compin ), BCDIn, BCDout );
  2352. end;
  2353. {$endif}
  2354. procedure BCDSubtract ( const BCDIn : tBCD;
  2355. const StringIn : FmtBCDStringtype;
  2356. var BCDout : tBCD ); Inline;
  2357. begin
  2358. BCDSubtract ( BCDIn, StrToBCD ( StringIn ), BCDout );
  2359. end;
  2360. procedure BCDSubtract ( const StringIn : FmtBCDStringtype;
  2361. const BCDIn : tBCD;
  2362. var BCDout : tBCD ); Inline;
  2363. begin
  2364. BCDSubtract ( StrToBCD ( StringIn ), BCDIn, BCDout );
  2365. end;
  2366. procedure BCDSubtract ( const StringIn1,
  2367. StringIn2 : FmtBCDStringtype;
  2368. var BCDout : tBCD ); Inline;
  2369. begin
  2370. BCDSubtract ( StrToBCD ( StringIn1 ), StrToBCD ( StringIn2 ), BCDout );
  2371. end;
  2372. procedure BCDMultiply ( const BCDIn : tBCD;
  2373. const IntIn : myInttype;
  2374. var BCDout : tBCD );
  2375. var
  2376. bh : tBCD_helper;
  2377. bhr : tBCD_helper;
  2378. bhrr : tBCD_helper_big;
  2379. int : {$ifopt r+} 0..high ( bhrr.Singles[0] ) DIV 10 {$else} Integer {$endif};
  2380. i1 : {$ifopt r+} low ( bh.Singles )..high ( bh.Singles ) {$else} Integer {$endif};
  2381. i3 : {$ifopt r+} low ( bhr.Singles )..high ( bhr.Singles ) {$else} Integer {$endif};
  2382. v : {$ifopt r+} low ( bhrr.Singles[0] ) + low ( bhrr.Singles[0] ) DIV 10..high ( bhrr.Singles[0] ) + high ( bhrr.Singles[0] ) DIV 10 {$else} Integer {$endif};
  2383. ue : {$ifopt r+} 1 * ( low ( bhrr.Singles[0] ) + low ( bhrr.Singles[0] ) DIV 10 ) DIV 10
  2384. ..( high ( bhrr.Singles[0] ) + high ( bhrr.Singles[0] ) DIV 10 ) DIV 10 {$else} Integer {$endif};
  2385. begin
  2386. if IntIn = 0
  2387. then begin
  2388. BCDout := NullBCD;
  2389. EXIT;
  2390. end;
  2391. if IntIn = 1
  2392. then begin
  2393. BCDout := BCDIn;
  2394. EXIT;
  2395. end;
  2396. if IntIn = -1
  2397. then begin
  2398. BCDout := BCDIn;
  2399. BCDNegate ( BCDout );
  2400. EXIT;
  2401. end;
  2402. if IntIn = low ( myInttype )
  2403. then begin
  2404. {$if declared ( myMinIntBCD ) }
  2405. BCDMultiply ( BCDIn, myMinIntBCD, BCDout );
  2406. EXIT;
  2407. {$else}
  2408. RAISE eBCDOverflowException.create ( 'in BCDmultiply' );
  2409. {$endif}
  2410. end;
  2411. if Abs ( IntIn ) > low ( bhrr.Singles[0] ) DIV 10
  2412. then begin
  2413. BCDMultiply ( BCDIn, IntegerToBCD ( IntIn ), BCDout );
  2414. EXIT;
  2415. end;
  2416. unpack_BCD ( BCDIn, bh );
  2417. if bh.Prec = 0
  2418. then begin
  2419. BCDout := NullBCD;
  2420. EXIT;
  2421. end;
  2422. bhr := null_.bh;
  2423. bhrr := null_.bhb;
  2424. int := Abs ( IntIn );
  2425. WITH bhrr do
  2426. begin
  2427. Neg := bh.Neg XOR ( IntIn < 0 );
  2428. FDig := bh.FDig;
  2429. LDig := bh.LDig;
  2430. for i1 := bh.FDig TO bh.LDig do
  2431. Singles[i1] := bh.Singles[i1] * int;
  2432. {
  2433. for i3 := fdig to ldig do
  2434. write ( ' ', singles[i3] );
  2435. writeln;
  2436. }
  2437. ue := 0;
  2438. for i3 := LDig DOWNTO FDig do
  2439. begin
  2440. v := Singles[i3] + ue;
  2441. ue := v DIV 10;
  2442. v := v MOD 10;
  2443. bhr.Singles[i3] := v;
  2444. end;
  2445. while ue <> 0 do
  2446. begin
  2447. Dec ( FDig );
  2448. if FDig < low ( bhr.Singles )
  2449. then RAISE eBCDOverflowException.create ( 'in BCDMultiply' );
  2450. bhr.Singles[FDig] := ue MOD 10;
  2451. ue := ue DIV 10;
  2452. end;
  2453. bhr.Plac := LDig;
  2454. bhr.FDig := FDig;
  2455. if LDig > high ( bhr.Singles )
  2456. then bhr.LDig := high ( bhr.Singles )
  2457. else bhr.LDig := LDig;
  2458. end;
  2459. if NOT pack_BCD ( bhr, BCDout )
  2460. then begin
  2461. RAISE eBCDOverflowException.create ( 'in BCDMultiply' );
  2462. end;
  2463. end;
  2464. procedure BCDMultiply ( const IntIn : myInttype;
  2465. const BCDIn : tBCD;
  2466. var BCDout : tBCD ); Inline;
  2467. begin
  2468. BCDMultiply ( BCDIn, IntIn, BCDout );
  2469. end;
  2470. procedure BCDMultiply ( const DoubleIn : myRealtype;
  2471. const BCDIn : tBCD;
  2472. var BCDout : tBCD ); Inline;
  2473. begin
  2474. BCDMultiply ( DoubleToBCD ( DoubleIn ), BCDIn, BCDout );
  2475. end;
  2476. procedure BCDMultiply ( const BCDIn : tBCD;
  2477. const Currin : currency;
  2478. var BCDout : tBCD ); Inline;
  2479. begin
  2480. BCDMultiply ( BCDIn, CurrToBCD ( Currin ), BCDout );
  2481. end;
  2482. procedure BCDMultiply ( const Currin : currency;
  2483. const BCDIn : tBCD;
  2484. var BCDout : tBCD ); Inline;
  2485. begin
  2486. BCDMultiply ( CurrToBCD ( Currin ), BCDIn, BCDout );
  2487. end;
  2488. {$ifdef comproutines}
  2489. procedure BCDMultiply ( const BCDIn : tBCD;
  2490. const Compin : Comp;
  2491. var BCDout : tBCD ); Inline;
  2492. begin
  2493. BCDMultiply ( BCDIn, CompToBCD ( Compin ), BCDout );
  2494. end;
  2495. procedure BCDMultiply ( const Compin : Comp;
  2496. const BCDIn : tBCD;
  2497. var BCDout : tBCD ); Inline;
  2498. begin
  2499. BCDMultiply ( CompToBCD ( Compin ), BCDIn, BCDout );
  2500. end;
  2501. {$endif}
  2502. procedure BCDMultiply ( const StringIn : FmtBCDStringtype;
  2503. const BCDIn : tBCD;
  2504. var BCDout : tBCD ); Inline;
  2505. begin
  2506. BCDMultiply ( StrToBCD ( StringIn ), BCDIn, BCDout );
  2507. end;
  2508. procedure BCDDivide ( const Dividend : tBCD;
  2509. const Divisor : myInttype;
  2510. var BCDout : tBCD ); Inline;
  2511. begin
  2512. BCDDivide ( Dividend, IntegerToBCD ( Divisor ), BCDout );
  2513. end;
  2514. procedure BCDDivide ( const Dividend : myInttype;
  2515. const Divisor : tBCD;
  2516. var BCDout : tBCD ); Inline;
  2517. begin
  2518. BCDDivide ( IntegerToBCD ( Dividend ), Divisor, BCDout );
  2519. end;
  2520. procedure BCDDivide ( const Dividend : myRealtype;
  2521. const Divisor : tBCD;
  2522. var BCDout : tBCD ); Inline;
  2523. begin
  2524. BCDDivide ( DoubleToBCD ( Dividend ), Divisor, BCDout );
  2525. end;
  2526. procedure BCDDivide ( const BCDIn : tBCD;
  2527. const Currin : currency;
  2528. var BCDout : tBCD ); Inline;
  2529. begin
  2530. BCDDivide ( BCDIn, CurrToBCD ( Currin ), BCDout );
  2531. end;
  2532. procedure BCDDivide ( const Currin : currency;
  2533. const BCDIn : tBCD;
  2534. var BCDout : tBCD ); Inline;
  2535. begin
  2536. BCDDivide ( CurrToBCD ( Currin ), BCDIn, BCDout );
  2537. end;
  2538. {$ifdef comproutines}
  2539. procedure BCDDivide ( const BCDIn : tBCD;
  2540. const Compin : Comp;
  2541. var BCDout : tBCD ); Inline;
  2542. begin
  2543. BCDDivide ( BCDIn, CompToBCD ( Compin ), BCDout );
  2544. end;
  2545. procedure BCDDivide ( const Compin : Comp;
  2546. const BCDIn : tBCD;
  2547. var BCDout : tBCD ); Inline;
  2548. begin
  2549. BCDDivide ( CompToBCD ( Compin ), BCDIn, BCDout );
  2550. end;
  2551. {$endif}
  2552. procedure BCDDivide ( const Dividend : FmtBCDStringtype;
  2553. const Divisor : tBCD;
  2554. var BCDout : tBCD ); Inline;
  2555. begin
  2556. BCDDivide ( StrToBCD ( Dividend ), Divisor, BCDout );
  2557. end;
  2558. operator = ( const BCD1,
  2559. BCD2 : tBCD ) z : Boolean; Inline;
  2560. begin
  2561. z := BCDCompare ( BCD1, BCD2 ) = 0;
  2562. end;
  2563. operator < ( const BCD1,
  2564. BCD2 : tBCD ) z : Boolean; Inline;
  2565. begin
  2566. z := BCDCompare ( BCD1, BCD2 ) < 0;
  2567. end;
  2568. operator > ( const BCD1,
  2569. BCD2 : tBCD ) z : Boolean; Inline;
  2570. begin
  2571. z := BCDCompare ( BCD1, BCD2 ) > 0;
  2572. end;
  2573. operator <= ( const BCD1,
  2574. BCD2 : tBCD ) z : Boolean; Inline;
  2575. begin
  2576. z := BCDCompare ( BCD1, BCD2 ) <= 0;
  2577. end;
  2578. operator >= ( const BCD1,
  2579. BCD2 : tBCD ) z : Boolean; Inline;
  2580. begin
  2581. z := BCDCompare ( BCD1, BCD2 ) >= 0;
  2582. end;
  2583. (* ######################## not allowed: why?
  2584. operator + ( const BCD : tBCD ) z : tBCD; Inline;
  2585. begin
  2586. z := bcd;
  2587. end;
  2588. ##################################################### *)
  2589. operator - ( const BCD : tBCD ) z : tBCD; Inline;
  2590. begin
  2591. z := BCD;
  2592. BCDNegate ( z );
  2593. end;
  2594. operator + ( const BCD1,
  2595. BCD2 : tBCD ) z : tBCD; Inline;
  2596. begin
  2597. BCDAdd ( BCD1, BCD2, z );
  2598. end;
  2599. operator + ( const BCD : tBCD;
  2600. const i : myInttype ) z : tBCD; Inline;
  2601. begin
  2602. BCDAdd ( BCD, i, z );
  2603. end;
  2604. operator + ( const i : myInttype;
  2605. const BCD : tBCD ) z : tBCD; Inline;
  2606. begin
  2607. BCDAdd ( i, BCD, z );
  2608. end;
  2609. operator + ( const BCD : tBCD;
  2610. const r : myRealtype ) z : tBCD; Inline;
  2611. begin
  2612. BCDAdd ( BCD, DoubleToBCD ( r ), z );
  2613. end;
  2614. operator + ( const r : myRealtype;
  2615. const BCD : tBCD ) z : tBCD; Inline;
  2616. begin
  2617. BCDAdd ( DoubleToBCD ( r ), BCD, z );
  2618. end;
  2619. operator + ( const BCD : tBCD;
  2620. const c : currency ) z : tBCD; Inline;
  2621. begin
  2622. BCDAdd ( BCD, CurrToBCD ( c ), z );
  2623. end;
  2624. operator + ( const c : currency;
  2625. const BCD : tBCD ) z : tBCD; Inline;
  2626. begin
  2627. BCDAdd ( CurrToBCD ( c ), BCD, z );
  2628. end;
  2629. {$ifdef comproutines}
  2630. operator + ( const BCD : tBCD;
  2631. const c : Comp ) z : tBCD; Inline;
  2632. begin
  2633. BCDAdd ( BCD, CompToBCD ( c ), z );
  2634. end;
  2635. operator + ( const c : Comp;
  2636. const BCD : tBCD ) z : tBCD; Inline;
  2637. begin
  2638. BCDAdd ( CompToBCD ( c ), BCD, z );
  2639. end;
  2640. {$endif}
  2641. operator + ( const BCD : tBCD;
  2642. const s : FmtBCDStringtype ) z : tBCD; Inline;
  2643. begin
  2644. BCDAdd ( BCD, StrToBCD ( s ), z );
  2645. end;
  2646. operator + ( const s : FmtBCDStringtype;
  2647. const BCD : tBCD ) z : tBCD; Inline;
  2648. begin
  2649. BCDAdd ( StrToBCD ( s ), BCD, z );
  2650. end;
  2651. operator - ( const BCD1,
  2652. BCD2 : tBCD ) z : tBCD; Inline;
  2653. begin
  2654. BCDSubtract ( BCD1, BCD2, z );
  2655. end;
  2656. operator - ( const BCD : tBCD;
  2657. const i : myInttype ) z : tBCD; Inline;
  2658. begin
  2659. BCDSubtract ( BCD, i, z );
  2660. end;
  2661. operator - ( const i : myInttype;
  2662. const BCD : tBCD ) z : tBCD; Inline;
  2663. begin
  2664. BCDSubtract ( BCD, i, z );
  2665. BCDNegate ( z );
  2666. end;
  2667. operator - ( const BCD : tBCD;
  2668. const r : myRealtype ) z : tBCD; Inline;
  2669. begin
  2670. BCDSubtract ( BCD, DoubleToBCD ( r ), z );
  2671. end;
  2672. operator - ( const r : myRealtype;
  2673. const BCD : tBCD ) z : tBCD; Inline;
  2674. begin
  2675. BCDSubtract ( DoubleToBCD ( r ), BCD, z );
  2676. end;
  2677. operator - ( const BCD : tBCD;
  2678. const c : currency ) z : tBCD; Inline;
  2679. begin
  2680. BCDSubtract ( BCD, CurrToBCD ( c ), z );
  2681. end;
  2682. operator - ( const c : currency;
  2683. const BCD : tBCD ) z : tBCD; Inline;
  2684. begin
  2685. BCDSubtract ( CurrToBCD ( c ), BCD, z );
  2686. end;
  2687. {$ifdef comproutines}
  2688. operator - ( const BCD : tBCD;
  2689. const c : Comp ) z : tBCD; Inline;
  2690. begin
  2691. BCDSubtract ( BCD, CompToBCD ( c ), z );
  2692. end;
  2693. operator - ( const c : Comp;
  2694. const BCD : tBCD ) z : tBCD; Inline;
  2695. begin
  2696. BCDSubtract ( CompToBCD ( c ), BCD, z );
  2697. end;
  2698. {$endif}
  2699. operator - ( const BCD : tBCD;
  2700. const s : FmtBCDStringtype ) z : tBCD; Inline;
  2701. begin
  2702. BCDSubtract ( BCD, StrToBCD ( s ), z );
  2703. end;
  2704. operator - ( const s : FmtBCDStringtype;
  2705. const BCD : tBCD ) z : tBCD; Inline;
  2706. begin
  2707. BCDSubtract ( StrToBCD ( s ), BCD, z );
  2708. end;
  2709. operator * ( const BCD1,
  2710. BCD2 : tBCD ) z : tBCD; Inline;
  2711. begin
  2712. BCDMultiply ( BCD1, BCD2, z );
  2713. end;
  2714. operator * ( const BCD : tBCD;
  2715. const i : myInttype ) z : tBCD; Inline;
  2716. begin
  2717. BCDMultiply ( BCD, i, z );
  2718. end;
  2719. operator * ( const i : myInttype;
  2720. const BCD : tBCD ) z : tBCD; Inline;
  2721. begin
  2722. BCDMultiply ( BCD, i, z );
  2723. end;
  2724. operator * ( const BCD : tBCD;
  2725. const r : myRealtype ) z : tBCD; Inline;
  2726. begin
  2727. BCDMultiply ( BCD, DoubleToBCD ( r ), z );
  2728. end;
  2729. operator * ( const r : myRealtype;
  2730. const BCD : tBCD ) z : tBCD; Inline;
  2731. begin
  2732. BCDMultiply ( DoubleToBCD ( r ), BCD, z );
  2733. end;
  2734. operator * ( const BCD : tBCD;
  2735. const c : currency ) z : tBCD; Inline;
  2736. begin
  2737. BCDMultiply ( BCD, CurrToBCD ( c ), z );
  2738. end;
  2739. operator * ( const c : currency;
  2740. const BCD : tBCD ) z : tBCD; Inline;
  2741. begin
  2742. BCDMultiply ( CurrToBCD ( c ), BCD, z );
  2743. end;
  2744. {$ifdef comproutines}
  2745. operator * ( const BCD : tBCD;
  2746. const c : Comp ) z : tBCD; Inline;
  2747. begin
  2748. BCDMultiply ( BCD, CompToBCD ( c ), z );
  2749. end;
  2750. operator * ( const c : Comp;
  2751. const BCD : tBCD ) z : tBCD; Inline;
  2752. begin
  2753. BCDMultiply ( CompToBCD ( c ), BCD, z );
  2754. end;
  2755. {$endif}
  2756. operator * ( const BCD : tBCD;
  2757. const s : FmtBCDStringtype ) z : tBCD; Inline;
  2758. begin
  2759. BCDMultiply ( BCD, StrToBCD ( s ), z );
  2760. end;
  2761. operator * ( const s : FmtBCDStringtype;
  2762. const BCD : tBCD ) z : tBCD; Inline;
  2763. begin
  2764. BCDMultiply ( StrToBCD ( s ), BCD, z );
  2765. end;
  2766. operator / ( const BCD1,
  2767. BCD2 : tBCD ) z : tBCD; Inline;
  2768. begin
  2769. BCDDivide ( BCD1, BCD2, z );
  2770. end;
  2771. operator / ( const BCD : tBCD;
  2772. const i : myInttype ) z : tBCD; Inline;
  2773. begin
  2774. BCDDivide ( BCD, i, z );
  2775. end;
  2776. operator / ( const i : myInttype;
  2777. const BCD : tBCD ) z : tBCD; Inline;
  2778. begin
  2779. BCDDivide ( IntegerToBCD ( i ), BCD, z );
  2780. end;
  2781. operator / ( const BCD : tBCD;
  2782. const r : myRealtype ) z : tBCD; Inline;
  2783. begin
  2784. BCDDivide ( BCD, DoubleToBCD ( r ), z );
  2785. end;
  2786. operator / ( const r : myRealtype;
  2787. const BCD : tBCD ) z : tBCD; Inline;
  2788. begin
  2789. BCDDivide ( DoubleToBCD ( r ), BCD, z );
  2790. end;
  2791. operator / ( const BCD : tBCD;
  2792. const c : currency ) z : tBCD; Inline;
  2793. begin
  2794. BCDDivide ( BCD, CurrToBCD ( c ), z );
  2795. end;
  2796. operator / ( const c : currency;
  2797. const BCD : tBCD ) z : tBCD; Inline;
  2798. begin
  2799. BCDDivide ( CurrToBCD ( c ), BCD, z );
  2800. end;
  2801. {$ifdef comproutines}
  2802. operator / ( const BCD : tBCD;
  2803. const c : Comp ) z : tBCD; Inline;
  2804. begin
  2805. BCDDivide ( BCD, CompToBCD ( c ), z );
  2806. end;
  2807. operator / ( const c : Comp;
  2808. const BCD : tBCD ) z : tBCD; Inline;
  2809. begin
  2810. BCDDivide ( CompToBCD ( c ), BCD, z );
  2811. end;
  2812. {$endif}
  2813. operator / ( const BCD : tBCD;
  2814. const s : FmtBCDStringtype ) z : tBCD; Inline;
  2815. begin
  2816. BCDDivide ( BCD, StrToBCD ( s ), z );
  2817. end;
  2818. operator / ( const s : FmtBCDStringtype;
  2819. const BCD : tBCD ) z : tBCD; Inline;
  2820. begin
  2821. BCDDivide ( StrToBCD ( s ), BCD, z );
  2822. end;
  2823. operator := ( const i : Byte ) z : tBCD; Inline;
  2824. begin
  2825. z := IntegerToBCD ( myInttype ( i ) );
  2826. end;
  2827. operator := ( const BCD : tBCD ) z : Byte; Inline;
  2828. begin
  2829. z := BCDToInteger ( BCD );
  2830. end;
  2831. operator := ( const i : Word ) z : tBCD; Inline;
  2832. begin
  2833. z := IntegerToBCD ( myInttype ( i ) );
  2834. end;
  2835. operator := ( const BCD : tBCD ) z : Word; Inline;
  2836. begin
  2837. z := BCDToInteger ( BCD );
  2838. end;
  2839. operator := ( const i : longword ) z : tBCD; Inline;
  2840. begin
  2841. z := IntegerToBCD ( myInttype ( i ) );
  2842. end;
  2843. operator := ( const BCD : tBCD ) z : longword; Inline;
  2844. begin
  2845. z := BCDToInteger ( BCD );
  2846. end;
  2847. {$if declared ( qword ) }
  2848. operator := ( const i : qword ) z : tBCD; Inline;
  2849. begin
  2850. z := IntegerToBCD ( myInttype ( i ) );
  2851. end;
  2852. operator := ( const BCD : tBCD ) z : qword; Inline;
  2853. begin
  2854. z := BCDToInteger ( BCD );
  2855. end;
  2856. {$endif}
  2857. operator := ( const i : ShortInt ) z : tBCD; Inline;
  2858. begin
  2859. z := IntegerToBCD ( myInttype ( i ) );
  2860. end;
  2861. operator := ( const BCD : tBCD ) z : ShortInt; Inline;
  2862. begin
  2863. z := BCDToInteger ( BCD );
  2864. end;
  2865. operator := ( const i : smallint ) z : tBCD; Inline;
  2866. begin
  2867. z := IntegerToBCD ( myInttype ( i ) );
  2868. end;
  2869. operator := ( const BCD : tBCD ) z : smallint; Inline;
  2870. begin
  2871. z := BCDToInteger ( BCD );
  2872. end;
  2873. operator := ( const i : LongInt ) z : tBCD; Inline;
  2874. begin
  2875. z := IntegerToBCD ( myInttype ( i ) );
  2876. end;
  2877. operator := ( const BCD : tBCD ) z : LongInt; Inline;
  2878. begin
  2879. z := BCDToInteger ( BCD );
  2880. end;
  2881. {$if declared ( int64 ) }
  2882. operator := ( const i : int64 ) z : tBCD; Inline;
  2883. begin
  2884. z := IntegerToBCD ( myInttype ( i ) );
  2885. end;
  2886. operator := ( const BCD : tBCD ) z : int64; Inline;
  2887. begin
  2888. z := BCDToInteger ( BCD );
  2889. end;
  2890. {$endif}
  2891. operator := ( const r : Single ) z : tBCD; Inline;
  2892. begin
  2893. z := DoubleToBCD ( myRealtype ( r ) );
  2894. end;
  2895. operator := ( const BCD : tBCD ) z : Single; Inline;
  2896. begin
  2897. z := BCDToDouble ( BCD );
  2898. end;
  2899. operator := ( const r : Double ) z : tBCD; Inline;
  2900. begin
  2901. z := DoubleToBCD ( myRealtype ( r ) );
  2902. end;
  2903. operator := ( const BCD : tBCD ) z : Double; Inline;
  2904. begin
  2905. z := BCDToDouble ( BCD );
  2906. end;
  2907. {$if sizeof ( extended ) <> sizeof ( double )}
  2908. operator := ( const r : Extended ) z : tBCD; Inline;
  2909. begin
  2910. z := DoubleToBCD ( {myRealtype (} r {)} );
  2911. end;
  2912. operator := ( const BCD : tBCD ) z : Extended; Inline;
  2913. begin
  2914. z := BCDToDouble ( BCD );
  2915. end;
  2916. {$endif}
  2917. operator := ( const c : currency ) z : tBCD; Inline;
  2918. begin
  2919. CurrToBCD ( c, z );
  2920. end;
  2921. operator := ( const BCD : tBCD ) z : currency; Inline;
  2922. begin
  2923. BCDToCurr ( BCD, z );
  2924. end;
  2925. {$ifdef comproutines}
  2926. {$undef makedirect}
  2927. {$ifdef makedirect}
  2928. operator := ( const c : Comp ) z : tBCD; Inline;
  2929. var
  2930. cc : int64 absolute c;
  2931. begin
  2932. z := IntegerToBCD ( cc );
  2933. end;
  2934. { $define version1} { only one of these may be defined! }
  2935. { $define version2} { version 1 produces a compiler error (with INLINE only!)}
  2936. {$define version3} { I wasn't able to reduce the problem, sorry }
  2937. {$ifdef version1}
  2938. operator := ( const BCD : tBCD ) z : Comp; Inline;
  2939. var
  2940. zz : Comp absolute z;
  2941. begin
  2942. zz := BCDToInteger ( BCD );
  2943. end;
  2944. {$endif}
  2945. {$ifdef version2}
  2946. operator := ( const BCD : tBCD ) z : Comp; Inline;
  2947. var
  2948. zz : int64;
  2949. zzz : Comp absolute zz;
  2950. begin
  2951. zz := BCDToInteger ( BCD );
  2952. z := zzz;
  2953. end;
  2954. {$endif}
  2955. {$ifdef version3}
  2956. operator := ( const BCD : tBCD ) z : Comp; Inline;
  2957. var
  2958. zz : record
  2959. case Boolean of
  2960. False: ( i : int64 );
  2961. True: ( c : Comp );
  2962. end;
  2963. begin
  2964. zz.i := BCDToInteger ( BCD );
  2965. z := zz.c;
  2966. end;
  2967. {$endif}
  2968. {$else}
  2969. operator := ( const c : Comp ) z : tBCD; Inline;
  2970. begin
  2971. z := CompToBCD ( c );
  2972. end;
  2973. operator := ( const BCD : tBCD ) z : Comp; Inline;
  2974. begin
  2975. z := BCDToComp ( BCD );
  2976. end;
  2977. {$endif}
  2978. {$endif}
  2979. operator := ( const s : string ) z : tBCD; Inline;
  2980. begin
  2981. z := StrToBCD ( s );
  2982. end;
  2983. operator := ( const BCD : tBCD ) z : string; Inline;
  2984. begin
  2985. z := BCDToStr ( BCD );
  2986. end;
  2987. operator := ( const s : AnsiString ) z : tBCD; Inline;
  2988. begin
  2989. z := StrToBCD ( s );
  2990. end;
  2991. operator := ( const BCD : tBCD ) z : AnsiString; Inline;
  2992. begin
  2993. z := BCDToStr ( BCD );
  2994. end;
  2995. {$endif}
  2996. constructor TFMTBcdVarData.create;
  2997. begin
  2998. inherited create;
  2999. FBcd:=NullBCD;
  3000. end;
  3001. constructor TFMTBcdVarData.create(const BCD : tBCD);
  3002. begin
  3003. inherited create;
  3004. FBcd:=BCD;
  3005. end;
  3006. function TFMTBcdFactory.GetInstance(const v : TVarData): tObject;
  3007. begin
  3008. result:=tObject(v.VPointer);
  3009. end;
  3010. procedure TFMTBcdFactory.BinaryOp(var Left: TVarData; const Right: TVarData; const Operation: TVarOp);
  3011. begin
  3012. case Operation of
  3013. opAdd:
  3014. TFMTBcdVarData(Left.VPointer).BCD:=TFMTBcdVarData(Left.VPointer).BCD+TFMTBcdVarData(Right.VPointer).BCD;
  3015. opSubtract:
  3016. TFMTBcdVarData(Left.VPointer).BCD:=TFMTBcdVarData(Left.VPointer).BCD-TFMTBcdVarData(Right.VPointer).BCD;
  3017. opMultiply:
  3018. TFMTBcdVarData(Left.VPointer).BCD:=TFMTBcdVarData(Left.VPointer).BCD*TFMTBcdVarData(Right.VPointer).BCD;
  3019. opDivide:
  3020. TFMTBcdVarData(Left.VPointer).BCD:=TFMTBcdVarData(Left.VPointer).BCD/TFMTBcdVarData(Right.VPointer).BCD;
  3021. else
  3022. RaiseInvalidOp;
  3023. end;
  3024. end;
  3025. {$if declared ( myMinIntBCD ) }
  3026. (*
  3027. {$if sizeof ( integer ) = 2 }
  3028. {$ifdef BCDgr4 }
  3029. const
  3030. myMinIntBCDValue : packed array [ 1..3 ] of Char = #$32#$76#$80;
  3031. {$endif}
  3032. {$else}
  3033. {$if sizeof ( integer ) = 4 }
  3034. *)
  3035. {$ifdef BCDgr9 }
  3036. const
  3037. myMinIntBCDValue : packed array [ 1..10 ] of Char = #$21#$47#$48#$36#$48;
  3038. {$endif}
  3039. (*
  3040. {$else}
  3041. {$if sizeof ( integer ) = 8 }
  3042. {$ifdef BCDgr18 }
  3043. const
  3044. myMinIntBCDValue : packed array [ 1..19 ] of Char = #$92#$23#$37#$20#$36#$85#$47#$75#$80#$80;
  3045. {$endif}
  3046. {$else}
  3047. {$fatal You have an interesting integer type! Sorry, not supported}
  3048. {$endif}
  3049. {$endif}
  3050. {$endif}
  3051. *)
  3052. {$endif}
  3053. initialization
  3054. FillChar ( null_, SizeOf ( null_ ), #0 );
  3055. FillChar ( NullBCD_, SizeOf ( NullBCD_ ), #0 );
  3056. FillChar ( OneBCD_, SizeOf ( OneBCD_ ), #0 );
  3057. OneBCD_.Precision := 1;
  3058. OneBCD_.Fraction[low ( OneBCD_.Fraction )] := $10;
  3059. {$if declared ( myMinIntBCD ) }
  3060. FillChar ( myMinIntBCD, SizeOf ( myMinIntBCD ), #0 );
  3061. {$ifndef bigger_BCD}
  3062. myMinIntBCD.SignSpecialPlaces := NegBit;
  3063. {$else}
  3064. myMinIntBCD.Negativ := True;
  3065. {$endif}
  3066. {$if sizeof ( integer ) = 2 }
  3067. {$ifdef BCDgr4 }
  3068. myMinIntBCD.Precision := 5;
  3069. Move ( myMinIntBCDValue, myMinIntBCD.Fraction, SizeOf ( myMinIntBCDValue ) );
  3070. {$endif}
  3071. {$else}
  3072. {$if sizeof ( integer ) = 4 }
  3073. {$ifdef BCDgr9 }
  3074. myMinIntBCD.Precision := 10;
  3075. Move ( myMinIntBCDValue, myMinIntBCD.Fraction, SizeOf ( myMinIntBCDValue ) );
  3076. {$endif}
  3077. {$else}
  3078. {$if sizeof ( integer ) = 8 }
  3079. {$ifdef BCDgr18 }
  3080. myMinIntBCD.Precision := 19;
  3081. Move ( myMinIntBCDValue, myMinIntBCD.Fraction, SizeOf ( myMinIntBCDValue ) );
  3082. {$endif}
  3083. {$else}
  3084. {$fatal You have an interesting integer type! Sorry, not supported}
  3085. {$endif}
  3086. {$endif}
  3087. {$endif}
  3088. {$endif}
  3089. FMTBcdFactory:=TFMTBcdFactory.create;
  3090. finalization
  3091. FreeAndNil(FMTBcdFactory)
  3092. end.