fmtbcd.pp 115 KB

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