fmtbcd.pp 115 KB

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