2
0

fmtbcd.pp 115 KB

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