bitvect.c 117 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045
  1. #include "util.h"
  2. #include "coretype.h"
  3. /*****************************************************************************/
  4. /* MODULE NAME: BitVector.c MODULE TYPE: (adt) */
  5. /*****************************************************************************/
  6. /* MODULE IMPORTS: */
  7. /*****************************************************************************/
  8. #include <ctype.h> /* MODULE TYPE: (sys) */
  9. #include <limits.h> /* MODULE TYPE: (sys) */
  10. #include <string.h> /* MODULE TYPE: (sys) */
  11. /*****************************************************************************/
  12. /* MODULE INTERFACE: */
  13. /*****************************************************************************/
  14. #include "bitvect.h"
  15. /* ToolBox.h */
  16. #define and && /* logical (boolean) operators: lower case */
  17. #define or ||
  18. #define not !
  19. #define AND & /* binary (bitwise) operators: UPPER CASE */
  20. #define OR |
  21. #define XOR ^
  22. #define NOT ~
  23. #define SHL <<
  24. #define SHR >>
  25. #ifdef ENABLE_MODULO
  26. #define mod % /* arithmetic operators */
  27. #endif
  28. #define blockdef(name,size) unsigned char name[size]
  29. #define blocktypedef(name,size) typedef unsigned char name[size]
  30. /*****************************************************************************/
  31. /* MODULE RESOURCES: */
  32. /*****************************************************************************/
  33. #define bits_(BitVector) *(BitVector-3)
  34. #define size_(BitVector) *(BitVector-2)
  35. #define mask_(BitVector) *(BitVector-1)
  36. #define ERRCODE_TYPE "sizeof(word) > sizeof(size_t)"
  37. #define ERRCODE_BITS "bits(word) != sizeof(word)*8"
  38. #define ERRCODE_WORD "bits(word) < 16"
  39. #define ERRCODE_LONG "bits(word) > bits(long)"
  40. #define ERRCODE_POWR "bits(word) != 2^x"
  41. #define ERRCODE_LOGA "bits(word) != 2^ld(bits(word))"
  42. #define ERRCODE_NULL "unable to allocate memory"
  43. #define ERRCODE_INDX "index out of range"
  44. #define ERRCODE_ORDR "minimum > maximum index"
  45. #define ERRCODE_SIZE "bit vector size mismatch"
  46. #define ERRCODE_PARS "input string syntax error"
  47. #define ERRCODE_OVFL "numeric overflow error"
  48. #define ERRCODE_SAME "result vector(s) must be distinct"
  49. #define ERRCODE_EXPO "exponent must be positive"
  50. #define ERRCODE_ZERO "division by zero error"
  51. #define ERRCODE_OOPS "unexpected internal error - please contact author"
  52. const N_int BitVector_BYTENORM[256] =
  53. {
  54. 0x00, 0x01, 0x01, 0x02, 0x01, 0x02, 0x02, 0x03,
  55. 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, /* 0x00 */
  56. 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04,
  57. 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, /* 0x10 */
  58. 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04,
  59. 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, /* 0x20 */
  60. 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
  61. 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, /* 0x30 */
  62. 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04,
  63. 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, /* 0x40 */
  64. 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
  65. 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, /* 0x50 */
  66. 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
  67. 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, /* 0x60 */
  68. 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
  69. 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07, /* 0x70 */
  70. 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04,
  71. 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, /* 0x80 */
  72. 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
  73. 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, /* 0x90 */
  74. 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
  75. 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, /* 0xA0 */
  76. 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
  77. 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07, /* 0xB0 */
  78. 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
  79. 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, /* 0xC0 */
  80. 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
  81. 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07, /* 0xD0 */
  82. 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
  83. 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07, /* 0xE0 */
  84. 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07,
  85. 0x05, 0x06, 0x06, 0x07, 0x06, 0x07, 0x07, 0x08 /* 0xF0 */
  86. };
  87. /*****************************************************************************/
  88. /* MODULE IMPLEMENTATION: */
  89. /*****************************************************************************/
  90. /**********************************************/
  91. /* global implementation-intrinsic constants: */
  92. /**********************************************/
  93. #define BIT_VECTOR_HIDDEN_WORDS 3
  94. /*****************************************************************/
  95. /* global machine-dependent constants (set by "BitVector_Boot"): */
  96. /*****************************************************************/
  97. static N_word BITS; /* = # of bits in machine word (must be power of 2) */
  98. static N_word MODMASK; /* = BITS - 1 (mask for calculating modulo BITS) */
  99. static N_word LOGBITS; /* = ld(BITS) (logarithmus dualis) */
  100. static N_word FACTOR; /* = ld(BITS / 8) (ld of # of bytes) */
  101. static N_word LSB = 1; /* = mask for least significant bit */
  102. static N_word MSB; /* = mask for most significant bit */
  103. static N_word LONGBITS; /* = # of bits in unsigned long */
  104. static N_word LOG10; /* = logarithm to base 10 of BITS - 1 */
  105. static N_word EXP10; /* = largest possible power of 10 in signed int */
  106. /********************************************************************/
  107. /* global bit mask table for fast access (set by "BitVector_Boot"): */
  108. /********************************************************************/
  109. static wordptr BITMASKTAB;
  110. /*****************************/
  111. /* global macro definitions: */
  112. /*****************************/
  113. #define BIT_VECTOR_ZERO_WORDS(target,count) \
  114. while (count-- > 0) *target++ = 0;
  115. #define BIT_VECTOR_FILL_WORDS(target,fill,count) \
  116. while (count-- > 0) *target++ = fill;
  117. #define BIT_VECTOR_FLIP_WORDS(target,flip,count) \
  118. while (count-- > 0) *target++ ^= flip;
  119. #define BIT_VECTOR_COPY_WORDS(target,source,count) \
  120. while (count-- > 0) *target++ = *source++;
  121. #define BIT_VECTOR_BACK_WORDS(target,source,count) \
  122. { target += count; source += count; while (count-- > 0) *--target = *--source; }
  123. #define BIT_VECTOR_CLR_BIT(address,index) \
  124. *(address+(index>>LOGBITS)) &= NOT BITMASKTAB[index AND MODMASK];
  125. #define BIT_VECTOR_SET_BIT(address,index) \
  126. *(address+(index>>LOGBITS)) |= BITMASKTAB[index AND MODMASK];
  127. #define BIT_VECTOR_TST_BIT(address,index) \
  128. ((*(address+(index>>LOGBITS)) AND BITMASKTAB[index AND MODMASK]) != 0)
  129. #define BIT_VECTOR_FLP_BIT(address,index,mask) \
  130. (mask = BITMASKTAB[index AND MODMASK]), \
  131. (((*(addr+(index>>LOGBITS)) ^= mask) AND mask) != 0)
  132. #define BIT_VECTOR_DIGITIZE(type,value,digit) \
  133. value = (type) ((digit = value) / 10); \
  134. digit -= value * 10; \
  135. digit += (type) '0';
  136. /*********************************************************/
  137. /* private low-level functions (potentially dangerous!): */
  138. /*********************************************************/
  139. static N_word power10(N_word x)
  140. {
  141. N_word y = 1;
  142. while (x-- > 0) y *= 10;
  143. return(y);
  144. }
  145. static void BIT_VECTOR_zro_words(wordptr addr, N_word count)
  146. {
  147. BIT_VECTOR_ZERO_WORDS(addr,count)
  148. }
  149. static void BIT_VECTOR_cpy_words(wordptr target, wordptr source, N_word count)
  150. {
  151. BIT_VECTOR_COPY_WORDS(target,source,count)
  152. }
  153. static void BIT_VECTOR_mov_words(wordptr target, wordptr source, N_word count)
  154. {
  155. if (target != source)
  156. {
  157. if (target < source) BIT_VECTOR_COPY_WORDS(target,source,count)
  158. else BIT_VECTOR_BACK_WORDS(target,source,count)
  159. }
  160. }
  161. static void BIT_VECTOR_ins_words(wordptr addr, N_word total, N_word count,
  162. boolean clear)
  163. {
  164. N_word length;
  165. if ((total > 0) and (count > 0))
  166. {
  167. if (count > total) count = total;
  168. length = total - count;
  169. if (length > 0) BIT_VECTOR_mov_words(addr+count,addr,length);
  170. if (clear) BIT_VECTOR_zro_words(addr,count);
  171. }
  172. }
  173. static void BIT_VECTOR_del_words(wordptr addr, N_word total, N_word count,
  174. boolean clear)
  175. {
  176. N_word length;
  177. if ((total > 0) and (count > 0))
  178. {
  179. if (count > total) count = total;
  180. length = total - count;
  181. if (length > 0) BIT_VECTOR_mov_words(addr,addr+count,length);
  182. if (clear) BIT_VECTOR_zro_words(addr+length,count);
  183. }
  184. }
  185. static void BIT_VECTOR_reverse(charptr string, N_word length)
  186. {
  187. charptr last;
  188. N_char temp;
  189. if (length > 1)
  190. {
  191. last = string + length - 1;
  192. while (string < last)
  193. {
  194. temp = *string;
  195. *string = *last;
  196. *last = temp;
  197. string++;
  198. last--;
  199. }
  200. }
  201. }
  202. static N_word BIT_VECTOR_int2str(charptr string, N_word value)
  203. {
  204. N_word length;
  205. N_word digit;
  206. charptr work;
  207. work = string;
  208. if (value > 0)
  209. {
  210. length = 0;
  211. while (value > 0)
  212. {
  213. BIT_VECTOR_DIGITIZE(N_word,value,digit)
  214. *work++ = (N_char) digit;
  215. length++;
  216. }
  217. BIT_VECTOR_reverse(string,length);
  218. }
  219. else
  220. {
  221. length = 1;
  222. *work++ = (N_char) '0';
  223. }
  224. return(length);
  225. }
  226. static N_word BIT_VECTOR_str2int(charptr string, N_word *value)
  227. {
  228. N_word length;
  229. N_word digit;
  230. *value = 0;
  231. length = 0;
  232. digit = (N_word) *string++;
  233. /* separate because isdigit() is likely a macro! */
  234. while (isdigit((int)digit) != 0)
  235. {
  236. length++;
  237. digit -= (N_word) '0';
  238. if (*value) *value *= 10;
  239. *value += digit;
  240. digit = (N_word) *string++;
  241. }
  242. return(length);
  243. }
  244. /********************************************/
  245. /* routine to convert error code to string: */
  246. /********************************************/
  247. const char * BitVector_Error(ErrCode error)
  248. {
  249. switch (error)
  250. {
  251. case ErrCode_Ok: return( NULL ); break;
  252. case ErrCode_Type: return( ERRCODE_TYPE ); break;
  253. case ErrCode_Bits: return( ERRCODE_BITS ); break;
  254. case ErrCode_Word: return( ERRCODE_WORD ); break;
  255. case ErrCode_Long: return( ERRCODE_LONG ); break;
  256. case ErrCode_Powr: return( ERRCODE_POWR ); break;
  257. case ErrCode_Loga: return( ERRCODE_LOGA ); break;
  258. case ErrCode_Null: return( ERRCODE_NULL ); break;
  259. case ErrCode_Indx: return( ERRCODE_INDX ); break;
  260. case ErrCode_Ordr: return( ERRCODE_ORDR ); break;
  261. case ErrCode_Size: return( ERRCODE_SIZE ); break;
  262. case ErrCode_Pars: return( ERRCODE_PARS ); break;
  263. case ErrCode_Ovfl: return( ERRCODE_OVFL ); break;
  264. case ErrCode_Same: return( ERRCODE_SAME ); break;
  265. case ErrCode_Expo: return( ERRCODE_EXPO ); break;
  266. case ErrCode_Zero: return( ERRCODE_ZERO ); break;
  267. default: return( ERRCODE_OOPS ); break;
  268. }
  269. }
  270. /*****************************************/
  271. /* automatic self-configuration routine: */
  272. /*****************************************/
  273. /*******************************************************/
  274. /* */
  275. /* MUST be called once prior to any other function */
  276. /* to initialize the machine dependent constants */
  277. /* of this package! (But call only ONCE, or you */
  278. /* will suffer memory leaks!) */
  279. /* */
  280. /*******************************************************/
  281. ErrCode BitVector_Boot(void)
  282. {
  283. N_long longsample = 1L;
  284. N_word sample = LSB;
  285. N_word lsb;
  286. if (sizeof(N_word) > sizeof(size_t)) return(ErrCode_Type);
  287. BITS = 1;
  288. while (sample <<= 1) BITS++; /* determine # of bits in a machine word */
  289. if (BITS != (sizeof(N_word) << 3)) return(ErrCode_Bits);
  290. if (BITS < 16) return(ErrCode_Word);
  291. LONGBITS = 1;
  292. while (longsample <<= 1) LONGBITS++; /* = # of bits in an unsigned long */
  293. if (BITS > LONGBITS) return(ErrCode_Long);
  294. LOGBITS = 0;
  295. sample = BITS;
  296. lsb = (sample AND LSB);
  297. while ((sample >>= 1) and (not lsb))
  298. {
  299. LOGBITS++;
  300. lsb = (sample AND LSB);
  301. }
  302. if (sample) return(ErrCode_Powr); /* # of bits is not a power of 2! */
  303. if (BITS != (LSB << LOGBITS)) return(ErrCode_Loga);
  304. MODMASK = BITS - 1;
  305. FACTOR = LOGBITS - 3; /* ld(BITS / 8) = ld(BITS) - ld(8) = ld(BITS) - 3 */
  306. MSB = (LSB << MODMASK);
  307. BITMASKTAB = (wordptr) yasm_xmalloc((size_t) (BITS << FACTOR));
  308. if (BITMASKTAB == NULL) return(ErrCode_Null);
  309. for ( sample = 0; sample < BITS; sample++ )
  310. {
  311. BITMASKTAB[sample] = (LSB << sample);
  312. }
  313. LOG10 = (N_word) (MODMASK * 0.30103); /* = (BITS - 1) * ( ln 2 / ln 10 ) */
  314. EXP10 = power10(LOG10);
  315. return(ErrCode_Ok);
  316. }
  317. void BitVector_Shutdown(void)
  318. {
  319. if (BITMASKTAB) yasm_xfree(BITMASKTAB);
  320. }
  321. N_word BitVector_Size(N_int bits) /* bit vector size (# of words) */
  322. {
  323. N_word size;
  324. size = bits >> LOGBITS;
  325. if (bits AND MODMASK) size++;
  326. return(size);
  327. }
  328. N_word BitVector_Mask(N_int bits) /* bit vector mask (unused bits) */
  329. {
  330. N_word mask;
  331. mask = bits AND MODMASK;
  332. if (mask) mask = (N_word) ~(~0L << mask); else mask = (N_word) ~0L;
  333. return(mask);
  334. }
  335. const char * BitVector_Version(void)
  336. {
  337. return("6.4");
  338. }
  339. N_int BitVector_Word_Bits(void)
  340. {
  341. return(BITS);
  342. }
  343. N_int BitVector_Long_Bits(void)
  344. {
  345. return(LONGBITS);
  346. }
  347. /********************************************************************/
  348. /* */
  349. /* WARNING: Do not "free()" constant character strings, i.e., */
  350. /* don't call "BitVector_Dispose()" for strings returned */
  351. /* by "BitVector_Error()" or "BitVector_Version()"! */
  352. /* */
  353. /* ONLY call this function for strings allocated with "malloc()", */
  354. /* i.e., the strings returned by the functions "BitVector_to_*()" */
  355. /* and "BitVector_Block_Read()"! */
  356. /* */
  357. /********************************************************************/
  358. void BitVector_Dispose(charptr string) /* free string */
  359. {
  360. if (string != NULL) yasm_xfree((voidptr) string);
  361. }
  362. void BitVector_Destroy(wordptr addr) /* free bitvec */
  363. {
  364. if (addr != NULL)
  365. {
  366. addr -= BIT_VECTOR_HIDDEN_WORDS;
  367. yasm_xfree((voidptr) addr);
  368. }
  369. }
  370. void BitVector_Destroy_List(listptr list, N_int count) /* free list */
  371. {
  372. listptr slot;
  373. if (list != NULL)
  374. {
  375. slot = list;
  376. while (count-- > 0)
  377. {
  378. BitVector_Destroy(*slot++);
  379. }
  380. free((voidptr) list);
  381. }
  382. }
  383. wordptr BitVector_Create(N_int bits, boolean clear) /* malloc */
  384. {
  385. N_word size;
  386. N_word mask;
  387. N_word bytes;
  388. wordptr addr;
  389. wordptr zero;
  390. size = BitVector_Size(bits);
  391. mask = BitVector_Mask(bits);
  392. bytes = (size + BIT_VECTOR_HIDDEN_WORDS) << FACTOR;
  393. addr = (wordptr) yasm_xmalloc((size_t) bytes);
  394. if (addr != NULL)
  395. {
  396. *addr++ = bits;
  397. *addr++ = size;
  398. *addr++ = mask;
  399. if (clear)
  400. {
  401. zero = addr;
  402. BIT_VECTOR_ZERO_WORDS(zero,size)
  403. }
  404. }
  405. return(addr);
  406. }
  407. listptr BitVector_Create_List(N_int bits, boolean clear, N_int count)
  408. {
  409. listptr list = NULL;
  410. listptr slot;
  411. wordptr addr;
  412. N_int i;
  413. if (count > 0)
  414. {
  415. list = (listptr) malloc(sizeof(wordptr) * count);
  416. if (list != NULL)
  417. {
  418. slot = list;
  419. for ( i = 0; i < count; i++ )
  420. {
  421. addr = BitVector_Create(bits,clear);
  422. if (addr == NULL)
  423. {
  424. BitVector_Destroy_List(list,i);
  425. return(NULL);
  426. }
  427. *slot++ = addr;
  428. }
  429. }
  430. }
  431. return(list);
  432. }
  433. wordptr BitVector_Resize(wordptr oldaddr, N_int bits) /* realloc */
  434. {
  435. N_word bytes;
  436. N_word oldsize;
  437. N_word oldmask;
  438. N_word newsize;
  439. N_word newmask;
  440. wordptr newaddr;
  441. wordptr source;
  442. wordptr target;
  443. oldsize = size_(oldaddr);
  444. oldmask = mask_(oldaddr);
  445. newsize = BitVector_Size(bits);
  446. newmask = BitVector_Mask(bits);
  447. if (oldsize > 0) *(oldaddr+oldsize-1) &= oldmask;
  448. if (newsize <= oldsize)
  449. {
  450. newaddr = oldaddr;
  451. bits_(newaddr) = bits;
  452. size_(newaddr) = newsize;
  453. mask_(newaddr) = newmask;
  454. if (newsize > 0) *(newaddr+newsize-1) &= newmask;
  455. }
  456. else
  457. {
  458. bytes = (newsize + BIT_VECTOR_HIDDEN_WORDS) << FACTOR;
  459. newaddr = (wordptr) yasm_xmalloc((size_t) bytes);
  460. if (newaddr != NULL)
  461. {
  462. *newaddr++ = bits;
  463. *newaddr++ = newsize;
  464. *newaddr++ = newmask;
  465. target = newaddr;
  466. source = oldaddr;
  467. newsize -= oldsize;
  468. BIT_VECTOR_COPY_WORDS(target,source,oldsize)
  469. BIT_VECTOR_ZERO_WORDS(target,newsize)
  470. }
  471. BitVector_Destroy(oldaddr);
  472. }
  473. return(newaddr);
  474. }
  475. wordptr BitVector_Shadow(wordptr addr) /* makes new, same size but empty */
  476. {
  477. return( BitVector_Create(bits_(addr),true) );
  478. }
  479. wordptr BitVector_Clone(wordptr addr) /* makes exact duplicate */
  480. {
  481. N_word bits;
  482. wordptr twin;
  483. bits = bits_(addr);
  484. twin = BitVector_Create(bits,false);
  485. if ((twin != NULL) and (bits > 0))
  486. BIT_VECTOR_cpy_words(twin,addr,size_(addr));
  487. return(twin);
  488. }
  489. wordptr BitVector_Concat(wordptr X, wordptr Y) /* returns concatenation */
  490. {
  491. /* BEWARE that X = most significant part, Y = least significant part! */
  492. N_word bitsX;
  493. N_word bitsY;
  494. N_word bitsZ;
  495. wordptr Z;
  496. bitsX = bits_(X);
  497. bitsY = bits_(Y);
  498. bitsZ = bitsX + bitsY;
  499. Z = BitVector_Create(bitsZ,false);
  500. if ((Z != NULL) and (bitsZ > 0))
  501. {
  502. BIT_VECTOR_cpy_words(Z,Y,size_(Y));
  503. BitVector_Interval_Copy(Z,X,bitsY,0,bitsX);
  504. *(Z+size_(Z)-1) &= mask_(Z);
  505. }
  506. return(Z);
  507. }
  508. void BitVector_Copy(wordptr X, wordptr Y) /* X = Y */
  509. {
  510. N_word sizeX = size_(X);
  511. N_word sizeY = size_(Y);
  512. N_word maskX = mask_(X);
  513. N_word maskY = mask_(Y);
  514. N_word fill = 0;
  515. wordptr lastX;
  516. wordptr lastY;
  517. if ((X != Y) and (sizeX > 0))
  518. {
  519. lastX = X + sizeX - 1;
  520. if (sizeY > 0)
  521. {
  522. lastY = Y + sizeY - 1;
  523. if ( (*lastY AND (maskY AND NOT (maskY >> 1))) == 0 ) *lastY &= maskY;
  524. else
  525. {
  526. fill = (N_word) ~0L;
  527. *lastY |= NOT maskY;
  528. }
  529. while ((sizeX > 0) and (sizeY > 0))
  530. {
  531. *X++ = *Y++;
  532. sizeX--;
  533. sizeY--;
  534. }
  535. *lastY &= maskY;
  536. }
  537. while (sizeX-- > 0) *X++ = fill;
  538. *lastX &= maskX;
  539. }
  540. }
  541. void BitVector_Empty(wordptr addr) /* X = {} clr all */
  542. {
  543. N_word size = size_(addr);
  544. BIT_VECTOR_ZERO_WORDS(addr,size)
  545. }
  546. void BitVector_Fill(wordptr addr) /* X = ~{} set all */
  547. {
  548. N_word size = size_(addr);
  549. N_word mask = mask_(addr);
  550. N_word fill = (N_word) ~0L;
  551. if (size > 0)
  552. {
  553. BIT_VECTOR_FILL_WORDS(addr,fill,size)
  554. *(--addr) &= mask;
  555. }
  556. }
  557. void BitVector_Flip(wordptr addr) /* X = ~X flip all */
  558. {
  559. N_word size = size_(addr);
  560. N_word mask = mask_(addr);
  561. N_word flip = (N_word) ~0L;
  562. if (size > 0)
  563. {
  564. BIT_VECTOR_FLIP_WORDS(addr,flip,size)
  565. *(--addr) &= mask;
  566. }
  567. }
  568. void BitVector_Primes(wordptr addr)
  569. {
  570. N_word bits = bits_(addr);
  571. N_word size = size_(addr);
  572. wordptr work;
  573. N_word temp;
  574. N_word i,j;
  575. if (size > 0)
  576. {
  577. temp = 0xAAAA;
  578. i = BITS >> 4;
  579. while (--i > 0)
  580. {
  581. temp <<= 16;
  582. temp |= 0xAAAA;
  583. }
  584. i = size;
  585. work = addr;
  586. *work++ = temp XOR 0x0006;
  587. while (--i > 0) *work++ = temp;
  588. for ( i = 3; (j = i * i) < bits; i += 2 )
  589. {
  590. for ( ; j < bits; j += i ) BIT_VECTOR_CLR_BIT(addr,j)
  591. }
  592. *(addr+size-1) &= mask_(addr);
  593. }
  594. }
  595. void BitVector_Reverse(wordptr X, wordptr Y)
  596. {
  597. N_word bits = bits_(X);
  598. N_word mask;
  599. N_word bit;
  600. N_word value;
  601. if (bits > 0)
  602. {
  603. if (X == Y) BitVector_Interval_Reverse(X,0,bits-1);
  604. else if (bits == bits_(Y))
  605. {
  606. /* mask = mask_(Y); */
  607. /* mask &= NOT (mask >> 1); */
  608. mask = BITMASKTAB[(bits-1) AND MODMASK];
  609. Y += size_(Y) - 1;
  610. value = 0;
  611. bit = LSB;
  612. while (bits-- > 0)
  613. {
  614. if ((*Y AND mask) != 0)
  615. {
  616. value |= bit;
  617. }
  618. if (not (mask >>= 1))
  619. {
  620. Y--;
  621. mask = MSB;
  622. }
  623. if (not (bit <<= 1))
  624. {
  625. *X++ = value;
  626. value = 0;
  627. bit = LSB;
  628. }
  629. }
  630. if (bit > LSB) *X = value;
  631. }
  632. }
  633. }
  634. void BitVector_Interval_Empty(wordptr addr, N_int lower, N_int upper)
  635. { /* X = X \ [lower..upper] */
  636. N_word bits = bits_(addr);
  637. N_word size = size_(addr);
  638. wordptr loaddr;
  639. wordptr hiaddr;
  640. N_word lobase;
  641. N_word hibase;
  642. N_word lomask;
  643. N_word himask;
  644. N_word diff;
  645. if ((size > 0) and (lower < bits) and (upper < bits) and (lower <= upper))
  646. {
  647. lobase = lower >> LOGBITS;
  648. hibase = upper >> LOGBITS;
  649. diff = hibase - lobase;
  650. loaddr = addr + lobase;
  651. hiaddr = addr + hibase;
  652. lomask = (N_word) (~0L << (lower AND MODMASK));
  653. himask = (N_word) ~((~0L << (upper AND MODMASK)) << 1);
  654. if (diff == 0)
  655. {
  656. *loaddr &= NOT (lomask AND himask);
  657. }
  658. else
  659. {
  660. *loaddr++ &= NOT lomask;
  661. while (--diff > 0)
  662. {
  663. *loaddr++ = 0;
  664. }
  665. *hiaddr &= NOT himask;
  666. }
  667. }
  668. }
  669. void BitVector_Interval_Fill(wordptr addr, N_int lower, N_int upper)
  670. { /* X = X + [lower..upper] */
  671. N_word bits = bits_(addr);
  672. N_word size = size_(addr);
  673. N_word fill = (N_word) ~0L;
  674. wordptr loaddr;
  675. wordptr hiaddr;
  676. N_word lobase;
  677. N_word hibase;
  678. N_word lomask;
  679. N_word himask;
  680. N_word diff;
  681. if ((size > 0) and (lower < bits) and (upper < bits) and (lower <= upper))
  682. {
  683. lobase = lower >> LOGBITS;
  684. hibase = upper >> LOGBITS;
  685. diff = hibase - lobase;
  686. loaddr = addr + lobase;
  687. hiaddr = addr + hibase;
  688. lomask = (N_word) (~0L << (lower AND MODMASK));
  689. himask = (N_word) ~((~0L << (upper AND MODMASK)) << 1);
  690. if (diff == 0)
  691. {
  692. *loaddr |= (lomask AND himask);
  693. }
  694. else
  695. {
  696. *loaddr++ |= lomask;
  697. while (--diff > 0)
  698. {
  699. *loaddr++ = fill;
  700. }
  701. *hiaddr |= himask;
  702. }
  703. *(addr+size-1) &= mask_(addr);
  704. }
  705. }
  706. void BitVector_Interval_Flip(wordptr addr, N_int lower, N_int upper)
  707. { /* X = X ^ [lower..upper] */
  708. N_word bits = bits_(addr);
  709. N_word size = size_(addr);
  710. N_word flip = (N_word) ~0L;
  711. wordptr loaddr;
  712. wordptr hiaddr;
  713. N_word lobase;
  714. N_word hibase;
  715. N_word lomask;
  716. N_word himask;
  717. N_word diff;
  718. if ((size > 0) and (lower < bits) and (upper < bits) and (lower <= upper))
  719. {
  720. lobase = lower >> LOGBITS;
  721. hibase = upper >> LOGBITS;
  722. diff = hibase - lobase;
  723. loaddr = addr + lobase;
  724. hiaddr = addr + hibase;
  725. lomask = (N_word) (~0L << (lower AND MODMASK));
  726. himask = (N_word) ~((~0L << (upper AND MODMASK)) << 1);
  727. if (diff == 0)
  728. {
  729. *loaddr ^= (lomask AND himask);
  730. }
  731. else
  732. {
  733. *loaddr++ ^= lomask;
  734. while (--diff > 0)
  735. {
  736. *loaddr++ ^= flip;
  737. }
  738. *hiaddr ^= himask;
  739. }
  740. *(addr+size-1) &= mask_(addr);
  741. }
  742. }
  743. void BitVector_Interval_Reverse(wordptr addr, N_int lower, N_int upper)
  744. {
  745. N_word bits = bits_(addr);
  746. wordptr loaddr;
  747. wordptr hiaddr;
  748. N_word lomask;
  749. N_word himask;
  750. if ((bits > 0) and (lower < bits) and (upper < bits) and (lower < upper))
  751. {
  752. loaddr = addr + (lower >> LOGBITS);
  753. hiaddr = addr + (upper >> LOGBITS);
  754. lomask = BITMASKTAB[lower AND MODMASK];
  755. himask = BITMASKTAB[upper AND MODMASK];
  756. for ( bits = upper - lower + 1; bits > 1; bits -= 2 )
  757. {
  758. if (((*loaddr AND lomask) != 0) XOR ((*hiaddr AND himask) != 0))
  759. {
  760. *loaddr ^= lomask; /* swap bits only if they differ! */
  761. *hiaddr ^= himask;
  762. }
  763. if (not (lomask <<= 1))
  764. {
  765. lomask = LSB;
  766. loaddr++;
  767. }
  768. if (not (himask >>= 1))
  769. {
  770. himask = MSB;
  771. hiaddr--;
  772. }
  773. }
  774. }
  775. }
  776. boolean BitVector_interval_scan_inc(wordptr addr, N_int start,
  777. N_intptr min, N_intptr max)
  778. {
  779. N_word size = size_(addr);
  780. N_word mask = mask_(addr);
  781. N_word offset;
  782. N_word bitmask;
  783. N_word value;
  784. boolean empty;
  785. if ((size == 0) or (start >= bits_(addr))) return(FALSE);
  786. *min = start;
  787. *max = start;
  788. offset = start >> LOGBITS;
  789. *(addr+size-1) &= mask;
  790. addr += offset;
  791. size -= offset;
  792. bitmask = BITMASKTAB[start AND MODMASK];
  793. mask = NOT (bitmask OR (bitmask - 1));
  794. value = *addr++;
  795. if ((value AND bitmask) == 0)
  796. {
  797. value &= mask;
  798. if (value == 0)
  799. {
  800. offset++;
  801. empty = TRUE;
  802. while (empty and (--size > 0))
  803. {
  804. if ((value = *addr++)) empty = false; else offset++;
  805. }
  806. if (empty) return(FALSE);
  807. }
  808. start = offset << LOGBITS;
  809. bitmask = LSB;
  810. mask = value;
  811. while (not (mask AND LSB))
  812. {
  813. bitmask <<= 1;
  814. mask >>= 1;
  815. start++;
  816. }
  817. mask = NOT (bitmask OR (bitmask - 1));
  818. *min = start;
  819. *max = start;
  820. }
  821. value = NOT value;
  822. value &= mask;
  823. if (value == 0)
  824. {
  825. offset++;
  826. empty = TRUE;
  827. while (empty and (--size > 0))
  828. {
  829. if ((value = NOT *addr++)) empty = false; else offset++;
  830. }
  831. if (empty) value = LSB;
  832. }
  833. start = offset << LOGBITS;
  834. while (not (value AND LSB))
  835. {
  836. value >>= 1;
  837. start++;
  838. }
  839. *max = --start;
  840. return(TRUE);
  841. }
  842. boolean BitVector_interval_scan_dec(wordptr addr, N_int start,
  843. N_intptr min, N_intptr max)
  844. {
  845. N_word size = size_(addr);
  846. N_word mask = mask_(addr);
  847. N_word offset;
  848. N_word bitmask;
  849. N_word value;
  850. boolean empty;
  851. if ((size == 0) or (start >= bits_(addr))) return(FALSE);
  852. *min = start;
  853. *max = start;
  854. offset = start >> LOGBITS;
  855. if (offset >= size) return(FALSE);
  856. *(addr+size-1) &= mask;
  857. addr += offset;
  858. size = ++offset;
  859. bitmask = BITMASKTAB[start AND MODMASK];
  860. mask = (bitmask - 1);
  861. value = *addr--;
  862. if ((value AND bitmask) == 0)
  863. {
  864. value &= mask;
  865. if (value == 0)
  866. {
  867. offset--;
  868. empty = TRUE;
  869. while (empty and (--size > 0))
  870. {
  871. if ((value = *addr--)) empty = false; else offset--;
  872. }
  873. if (empty) return(FALSE);
  874. }
  875. start = offset << LOGBITS;
  876. bitmask = MSB;
  877. mask = value;
  878. while (not (mask AND MSB))
  879. {
  880. bitmask >>= 1;
  881. mask <<= 1;
  882. start--;
  883. }
  884. mask = (bitmask - 1);
  885. *max = --start;
  886. *min = start;
  887. }
  888. value = NOT value;
  889. value &= mask;
  890. if (value == 0)
  891. {
  892. offset--;
  893. empty = TRUE;
  894. while (empty and (--size > 0))
  895. {
  896. if ((value = NOT *addr--)) empty = false; else offset--;
  897. }
  898. if (empty) value = MSB;
  899. }
  900. start = offset << LOGBITS;
  901. while (not (value AND MSB))
  902. {
  903. value <<= 1;
  904. start--;
  905. }
  906. *min = start;
  907. return(TRUE);
  908. }
  909. void BitVector_Interval_Copy(wordptr X, wordptr Y, N_int Xoffset,
  910. N_int Yoffset, N_int length)
  911. {
  912. N_word bitsX = bits_(X);
  913. N_word bitsY = bits_(Y);
  914. N_word source = 0; /* silence compiler warning */
  915. N_word target = 0; /* silence compiler warning */
  916. N_word s_lo_base;
  917. N_word s_hi_base;
  918. N_word s_lo_bit;
  919. N_word s_hi_bit;
  920. N_word s_base;
  921. N_word s_lower = 0; /* silence compiler warning */
  922. N_word s_upper = 0; /* silence compiler warning */
  923. N_word s_bits;
  924. N_word s_min;
  925. N_word s_max;
  926. N_word t_lo_base;
  927. N_word t_hi_base;
  928. N_word t_lo_bit;
  929. N_word t_hi_bit;
  930. N_word t_base;
  931. N_word t_lower = 0; /* silence compiler warning */
  932. N_word t_upper = 0; /* silence compiler warning */
  933. N_word t_bits;
  934. N_word t_min;
  935. N_word mask;
  936. N_word bits;
  937. N_word sel;
  938. boolean ascending;
  939. boolean notfirst;
  940. wordptr Z = X;
  941. if ((length > 0) and (Xoffset < bitsX) and (Yoffset < bitsY))
  942. {
  943. if ((Xoffset + length) > bitsX) length = bitsX - Xoffset;
  944. if ((Yoffset + length) > bitsY) length = bitsY - Yoffset;
  945. ascending = (Xoffset <= Yoffset);
  946. s_lo_base = Yoffset >> LOGBITS;
  947. s_lo_bit = Yoffset AND MODMASK;
  948. Yoffset += --length;
  949. s_hi_base = Yoffset >> LOGBITS;
  950. s_hi_bit = Yoffset AND MODMASK;
  951. t_lo_base = Xoffset >> LOGBITS;
  952. t_lo_bit = Xoffset AND MODMASK;
  953. Xoffset += length;
  954. t_hi_base = Xoffset >> LOGBITS;
  955. t_hi_bit = Xoffset AND MODMASK;
  956. if (ascending)
  957. {
  958. s_base = s_lo_base;
  959. t_base = t_lo_base;
  960. }
  961. else
  962. {
  963. s_base = s_hi_base;
  964. t_base = t_hi_base;
  965. }
  966. s_bits = 0;
  967. t_bits = 0;
  968. Y += s_base;
  969. X += t_base;
  970. notfirst = FALSE;
  971. while (TRUE)
  972. {
  973. if (t_bits == 0)
  974. {
  975. if (notfirst)
  976. {
  977. *X = target;
  978. if (ascending)
  979. {
  980. if (t_base == t_hi_base) break;
  981. t_base++;
  982. X++;
  983. }
  984. else
  985. {
  986. if (t_base == t_lo_base) break;
  987. t_base--;
  988. X--;
  989. }
  990. }
  991. sel = ((t_base == t_hi_base) << 1) OR (t_base == t_lo_base);
  992. switch (sel)
  993. {
  994. case 0:
  995. t_lower = 0;
  996. t_upper = BITS - 1;
  997. t_bits = BITS;
  998. target = 0;
  999. break;
  1000. case 1:
  1001. t_lower = t_lo_bit;
  1002. t_upper = BITS - 1;
  1003. t_bits = BITS - t_lo_bit;
  1004. mask = (N_word) (~0L << t_lower);
  1005. target = *X AND NOT mask;
  1006. break;
  1007. case 2:
  1008. t_lower = 0;
  1009. t_upper = t_hi_bit;
  1010. t_bits = t_hi_bit + 1;
  1011. mask = (N_word) ((~0L << t_upper) << 1);
  1012. target = *X AND mask;
  1013. break;
  1014. case 3:
  1015. t_lower = t_lo_bit;
  1016. t_upper = t_hi_bit;
  1017. t_bits = t_hi_bit - t_lo_bit + 1;
  1018. mask = (N_word) (~0L << t_lower);
  1019. mask &= (N_word) ~((~0L << t_upper) << 1);
  1020. target = *X AND NOT mask;
  1021. break;
  1022. }
  1023. }
  1024. if (s_bits == 0)
  1025. {
  1026. if (notfirst)
  1027. {
  1028. if (ascending)
  1029. {
  1030. if (s_base == s_hi_base) break;
  1031. s_base++;
  1032. Y++;
  1033. }
  1034. else
  1035. {
  1036. if (s_base == s_lo_base) break;
  1037. s_base--;
  1038. Y--;
  1039. }
  1040. }
  1041. source = *Y;
  1042. sel = ((s_base == s_hi_base) << 1) OR (s_base == s_lo_base);
  1043. switch (sel)
  1044. {
  1045. case 0:
  1046. s_lower = 0;
  1047. s_upper = BITS - 1;
  1048. s_bits = BITS;
  1049. break;
  1050. case 1:
  1051. s_lower = s_lo_bit;
  1052. s_upper = BITS - 1;
  1053. s_bits = BITS - s_lo_bit;
  1054. break;
  1055. case 2:
  1056. s_lower = 0;
  1057. s_upper = s_hi_bit;
  1058. s_bits = s_hi_bit + 1;
  1059. break;
  1060. case 3:
  1061. s_lower = s_lo_bit;
  1062. s_upper = s_hi_bit;
  1063. s_bits = s_hi_bit - s_lo_bit + 1;
  1064. break;
  1065. }
  1066. }
  1067. notfirst = TRUE;
  1068. if (s_bits > t_bits)
  1069. {
  1070. bits = t_bits - 1;
  1071. if (ascending)
  1072. {
  1073. s_min = s_lower;
  1074. s_max = s_lower + bits;
  1075. }
  1076. else
  1077. {
  1078. s_max = s_upper;
  1079. s_min = s_upper - bits;
  1080. }
  1081. t_min = t_lower;
  1082. }
  1083. else
  1084. {
  1085. bits = s_bits - 1;
  1086. if (ascending) t_min = t_lower;
  1087. else t_min = t_upper - bits;
  1088. s_min = s_lower;
  1089. s_max = s_upper;
  1090. }
  1091. bits++;
  1092. mask = (N_word) (~0L << s_min);
  1093. mask &= (N_word) ~((~0L << s_max) << 1);
  1094. if (s_min == t_min) target |= (source AND mask);
  1095. else
  1096. {
  1097. if (s_min < t_min) target |= (source AND mask) << (t_min-s_min);
  1098. else target |= (source AND mask) >> (s_min-t_min);
  1099. }
  1100. if (ascending)
  1101. {
  1102. s_lower += bits;
  1103. t_lower += bits;
  1104. }
  1105. else
  1106. {
  1107. s_upper -= bits;
  1108. t_upper -= bits;
  1109. }
  1110. s_bits -= bits;
  1111. t_bits -= bits;
  1112. }
  1113. *(Z+size_(Z)-1) &= mask_(Z);
  1114. }
  1115. }
  1116. wordptr BitVector_Interval_Substitute(wordptr X, wordptr Y,
  1117. N_int Xoffset, N_int Xlength,
  1118. N_int Yoffset, N_int Ylength)
  1119. {
  1120. N_word Xbits = bits_(X);
  1121. N_word Ybits = bits_(Y);
  1122. N_word limit;
  1123. N_word diff;
  1124. if ((Xoffset <= Xbits) and (Yoffset <= Ybits))
  1125. {
  1126. limit = Xoffset + Xlength;
  1127. if (limit > Xbits)
  1128. {
  1129. limit = Xbits;
  1130. Xlength = Xbits - Xoffset;
  1131. }
  1132. if ((Yoffset + Ylength) > Ybits)
  1133. {
  1134. Ylength = Ybits - Yoffset;
  1135. }
  1136. if (Xlength == Ylength)
  1137. {
  1138. if ((Ylength > 0) and ((X != Y) or (Xoffset != Yoffset)))
  1139. {
  1140. BitVector_Interval_Copy(X,Y,Xoffset,Yoffset,Ylength);
  1141. }
  1142. }
  1143. else /* Xlength != Ylength */
  1144. {
  1145. if (Xlength > Ylength)
  1146. {
  1147. diff = Xlength - Ylength;
  1148. if (Ylength > 0) BitVector_Interval_Copy(X,Y,Xoffset,Yoffset,Ylength);
  1149. if (limit < Xbits) BitVector_Delete(X,Xoffset+Ylength,diff,FALSE);
  1150. if ((X = BitVector_Resize(X,Xbits-diff)) == NULL) return(NULL);
  1151. }
  1152. else /* Ylength > Xlength ==> Ylength > 0 */
  1153. {
  1154. diff = Ylength - Xlength;
  1155. if (X != Y)
  1156. {
  1157. if ((X = BitVector_Resize(X,Xbits+diff)) == NULL) return(NULL);
  1158. if (limit < Xbits) BitVector_Insert(X,limit,diff,FALSE);
  1159. BitVector_Interval_Copy(X,Y,Xoffset,Yoffset,Ylength);
  1160. }
  1161. else /* in-place */
  1162. {
  1163. if ((Y = X = BitVector_Resize(X,Xbits+diff)) == NULL) return(NULL);
  1164. if (limit >= Xbits)
  1165. {
  1166. BitVector_Interval_Copy(X,Y,Xoffset,Yoffset,Ylength);
  1167. }
  1168. else /* limit < Xbits */
  1169. {
  1170. BitVector_Insert(X,limit,diff,FALSE);
  1171. if ((Yoffset+Ylength) <= limit)
  1172. {
  1173. BitVector_Interval_Copy(X,Y,Xoffset,Yoffset,Ylength);
  1174. }
  1175. else /* overlaps or lies above critical area */
  1176. {
  1177. if (limit <= Yoffset)
  1178. {
  1179. Yoffset += diff;
  1180. BitVector_Interval_Copy(X,Y,Xoffset,Yoffset,Ylength);
  1181. }
  1182. else /* Yoffset < limit */
  1183. {
  1184. Xlength = limit - Yoffset;
  1185. BitVector_Interval_Copy(X,Y,Xoffset,Yoffset,Xlength);
  1186. Yoffset = Xoffset + Ylength; /* = limit + diff */
  1187. Xoffset += Xlength;
  1188. Ylength -= Xlength;
  1189. BitVector_Interval_Copy(X,Y,Xoffset,Yoffset,Ylength);
  1190. }
  1191. }
  1192. }
  1193. }
  1194. }
  1195. }
  1196. }
  1197. return(X);
  1198. }
  1199. boolean BitVector_is_empty(wordptr addr) /* X == {} ? */
  1200. {
  1201. N_word size = size_(addr);
  1202. boolean r = TRUE;
  1203. if (size > 0)
  1204. {
  1205. *(addr+size-1) &= mask_(addr);
  1206. while (r and (size-- > 0)) r = ( *addr++ == 0 );
  1207. }
  1208. return(r);
  1209. }
  1210. boolean BitVector_is_full(wordptr addr) /* X == ~{} ? */
  1211. {
  1212. N_word size = size_(addr);
  1213. N_word mask = mask_(addr);
  1214. boolean r = FALSE;
  1215. wordptr last;
  1216. if (size > 0)
  1217. {
  1218. r = TRUE;
  1219. last = addr + size - 1;
  1220. *last |= NOT mask;
  1221. while (r and (size-- > 0)) r = ( NOT *addr++ == 0 );
  1222. *last &= mask;
  1223. }
  1224. return(r);
  1225. }
  1226. boolean BitVector_equal(wordptr X, wordptr Y) /* X == Y ? */
  1227. {
  1228. N_word size = size_(X);
  1229. N_word mask = mask_(X);
  1230. boolean r = FALSE;
  1231. if (bits_(X) == bits_(Y))
  1232. {
  1233. r = TRUE;
  1234. if (size > 0)
  1235. {
  1236. *(X+size-1) &= mask;
  1237. *(Y+size-1) &= mask;
  1238. while (r and (size-- > 0)) r = (*X++ == *Y++);
  1239. }
  1240. }
  1241. return(r);
  1242. }
  1243. Z_int BitVector_Lexicompare(wordptr X, wordptr Y) /* X <,=,> Y ? */
  1244. { /* unsigned */
  1245. N_word bitsX = bits_(X);
  1246. N_word bitsY = bits_(Y);
  1247. N_word size = size_(X);
  1248. boolean r = TRUE;
  1249. if (bitsX == bitsY)
  1250. {
  1251. if (size > 0)
  1252. {
  1253. X += size;
  1254. Y += size;
  1255. while (r and (size-- > 0)) r = (*(--X) == *(--Y));
  1256. }
  1257. if (r) return((Z_int) 0);
  1258. else
  1259. {
  1260. if (*X < *Y) return((Z_int) -1); else return((Z_int) 1);
  1261. }
  1262. }
  1263. else
  1264. {
  1265. if (bitsX < bitsY) return((Z_int) -1); else return((Z_int) 1);
  1266. }
  1267. }
  1268. Z_int BitVector_Compare(wordptr X, wordptr Y) /* X <,=,> Y ? */
  1269. { /* signed */
  1270. N_word bitsX = bits_(X);
  1271. N_word bitsY = bits_(Y);
  1272. N_word size = size_(X);
  1273. N_word mask = mask_(X);
  1274. N_word sign;
  1275. boolean r = TRUE;
  1276. if (bitsX == bitsY)
  1277. {
  1278. if (size > 0)
  1279. {
  1280. X += size;
  1281. Y += size;
  1282. mask &= NOT (mask >> 1);
  1283. if ((sign = (*(X-1) AND mask)) != (*(Y-1) AND mask))
  1284. {
  1285. if (sign) return((Z_int) -1); else return((Z_int) 1);
  1286. }
  1287. while (r and (size-- > 0)) r = (*(--X) == *(--Y));
  1288. }
  1289. if (r) return((Z_int) 0);
  1290. else
  1291. {
  1292. if (*X < *Y) return((Z_int) -1); else return((Z_int) 1);
  1293. }
  1294. }
  1295. else
  1296. {
  1297. if (bitsX < bitsY) return((Z_int) -1); else return((Z_int) 1);
  1298. }
  1299. }
  1300. charptr BitVector_to_Hex(wordptr addr)
  1301. {
  1302. N_word bits = bits_(addr);
  1303. N_word size = size_(addr);
  1304. N_word value;
  1305. N_word count;
  1306. N_word digit;
  1307. N_word length;
  1308. charptr string;
  1309. length = bits >> 2;
  1310. if (bits AND 0x0003) length++;
  1311. string = (charptr) yasm_xmalloc((size_t) (length+1));
  1312. if (string == NULL) return(NULL);
  1313. string += length;
  1314. *string = (N_char) '\0';
  1315. if (size > 0)
  1316. {
  1317. *(addr+size-1) &= mask_(addr);
  1318. while ((size-- > 0) and (length > 0))
  1319. {
  1320. value = *addr++;
  1321. count = BITS >> 2;
  1322. while ((count-- > 0) and (length > 0))
  1323. {
  1324. digit = value AND 0x000F;
  1325. if (digit > 9) digit += (N_word) 'A' - 10;
  1326. else digit += (N_word) '0';
  1327. *(--string) = (N_char) digit; length--;
  1328. if ((count > 0) and (length > 0)) value >>= 4;
  1329. }
  1330. }
  1331. }
  1332. return(string);
  1333. }
  1334. ErrCode BitVector_from_Hex(wordptr addr, charptr string)
  1335. {
  1336. N_word size = size_(addr);
  1337. N_word mask = mask_(addr);
  1338. boolean ok = TRUE;
  1339. size_t length;
  1340. N_word value;
  1341. N_word count;
  1342. int digit;
  1343. if (size > 0)
  1344. {
  1345. length = strlen((char *) string);
  1346. string += length;
  1347. while (size-- > 0)
  1348. {
  1349. value = 0;
  1350. for ( count = 0; (ok and (length > 0) and (count < BITS)); count += 4 )
  1351. {
  1352. digit = (int) *(--string); length--;
  1353. /* separate because toupper() is likely a macro! */
  1354. digit = toupper(digit);
  1355. if (digit == '_')
  1356. count -= 4;
  1357. else if ((ok = (isxdigit(digit) != 0)))
  1358. {
  1359. if (digit >= (int) 'A') digit -= (int) 'A' - 10;
  1360. else digit -= (int) '0';
  1361. value |= (((N_word) digit) << count);
  1362. }
  1363. }
  1364. *addr++ = value;
  1365. }
  1366. *(--addr) &= mask;
  1367. }
  1368. if (ok) return(ErrCode_Ok);
  1369. else return(ErrCode_Pars);
  1370. }
  1371. ErrCode BitVector_from_Oct(wordptr addr, charptr string)
  1372. {
  1373. N_word size = size_(addr);
  1374. N_word mask = mask_(addr);
  1375. boolean ok = TRUE;
  1376. size_t length;
  1377. N_word value;
  1378. N_word value_fill = 0;
  1379. N_word count;
  1380. Z_word count_fill = 0;
  1381. int digit = 0;
  1382. if (size > 0)
  1383. {
  1384. length = strlen((char *) string);
  1385. string += length;
  1386. while (size-- > 0)
  1387. {
  1388. value = value_fill;
  1389. for ( count = count_fill; (ok and (length > 0) and (count < BITS)); count += 3 )
  1390. {
  1391. digit = (int) *(--string); length--;
  1392. if (digit == '_')
  1393. count -= 3;
  1394. else if ((ok = (isdigit(digit) && digit != '8' && digit != '9')) != 0)
  1395. {
  1396. digit -= (int) '0';
  1397. value |= (((N_word) digit) << count);
  1398. }
  1399. }
  1400. count_fill = (Z_word)count-(Z_word)BITS;
  1401. if (count_fill > 0)
  1402. value_fill = (((N_word) digit) >> (3-count_fill));
  1403. else
  1404. value_fill = 0;
  1405. *addr++ = value;
  1406. }
  1407. *(--addr) &= mask;
  1408. }
  1409. if (ok) return(ErrCode_Ok);
  1410. else return(ErrCode_Pars);
  1411. }
  1412. charptr BitVector_to_Bin(wordptr addr)
  1413. {
  1414. N_word size = size_(addr);
  1415. N_word value;
  1416. N_word count;
  1417. N_word digit;
  1418. N_word length;
  1419. charptr string;
  1420. length = bits_(addr);
  1421. string = (charptr) yasm_xmalloc((size_t) (length+1));
  1422. if (string == NULL) return(NULL);
  1423. string += length;
  1424. *string = (N_char) '\0';
  1425. if (size > 0)
  1426. {
  1427. *(addr+size-1) &= mask_(addr);
  1428. while (size-- > 0)
  1429. {
  1430. value = *addr++;
  1431. count = BITS;
  1432. if (count > length) count = length;
  1433. while (count-- > 0)
  1434. {
  1435. digit = value AND 0x0001;
  1436. digit += (N_word) '0';
  1437. *(--string) = (N_char) digit; length--;
  1438. if (count > 0) value >>= 1;
  1439. }
  1440. }
  1441. }
  1442. return(string);
  1443. }
  1444. ErrCode BitVector_from_Bin(wordptr addr, charptr string)
  1445. {
  1446. N_word size = size_(addr);
  1447. N_word mask = mask_(addr);
  1448. boolean ok = TRUE;
  1449. size_t length;
  1450. N_word value;
  1451. N_word count;
  1452. int digit;
  1453. if (size > 0)
  1454. {
  1455. length = strlen((char *) string);
  1456. string += length;
  1457. while (size-- > 0)
  1458. {
  1459. value = 0;
  1460. for ( count = 0; (ok and (length > 0) and (count < BITS)); count++ )
  1461. {
  1462. digit = (int) *(--string); length--;
  1463. switch (digit)
  1464. {
  1465. case (int) '0':
  1466. break;
  1467. case (int) '1':
  1468. value |= BITMASKTAB[count];
  1469. break;
  1470. case (int) '_':
  1471. count--;
  1472. break;
  1473. default:
  1474. ok = FALSE;
  1475. break;
  1476. }
  1477. }
  1478. *addr++ = value;
  1479. }
  1480. *(--addr) &= mask;
  1481. }
  1482. if (ok) return(ErrCode_Ok);
  1483. else return(ErrCode_Pars);
  1484. }
  1485. charptr BitVector_to_Dec(wordptr addr)
  1486. {
  1487. N_word bits = bits_(addr);
  1488. N_word length;
  1489. N_word digits;
  1490. N_word count;
  1491. N_word q;
  1492. N_word r;
  1493. boolean loop;
  1494. charptr result;
  1495. charptr string;
  1496. wordptr quot;
  1497. wordptr rest;
  1498. wordptr temp;
  1499. wordptr base;
  1500. Z_int sign;
  1501. length = (N_word) (bits / 3.3); /* digits = bits * ln(2) / ln(10) */
  1502. length += 2; /* compensate for truncating & provide space for minus sign */
  1503. result = (charptr) yasm_xmalloc((size_t) (length+1)); /* remember the '\0'! */
  1504. if (result == NULL) return(NULL);
  1505. string = result;
  1506. sign = BitVector_Sign(addr);
  1507. if ((bits < 4) or (sign == 0))
  1508. {
  1509. if (bits > 0) digits = *addr; else digits = (N_word) 0;
  1510. if (sign < 0) digits = ((N_word)(-((Z_word)digits))) AND mask_(addr);
  1511. *string++ = (N_char) digits + (N_char) '0';
  1512. digits = 1;
  1513. }
  1514. else
  1515. {
  1516. quot = BitVector_Create(bits,FALSE);
  1517. if (quot == NULL)
  1518. {
  1519. BitVector_Dispose(result);
  1520. return(NULL);
  1521. }
  1522. rest = BitVector_Create(bits,FALSE);
  1523. if (rest == NULL)
  1524. {
  1525. BitVector_Dispose(result);
  1526. BitVector_Destroy(quot);
  1527. return(NULL);
  1528. }
  1529. temp = BitVector_Create(bits,FALSE);
  1530. if (temp == NULL)
  1531. {
  1532. BitVector_Dispose(result);
  1533. BitVector_Destroy(quot);
  1534. BitVector_Destroy(rest);
  1535. return(NULL);
  1536. }
  1537. base = BitVector_Create(bits,TRUE);
  1538. if (base == NULL)
  1539. {
  1540. BitVector_Dispose(result);
  1541. BitVector_Destroy(quot);
  1542. BitVector_Destroy(rest);
  1543. BitVector_Destroy(temp);
  1544. return(NULL);
  1545. }
  1546. if (sign < 0) BitVector_Negate(quot,addr);
  1547. else BitVector_Copy(quot,addr);
  1548. digits = 0;
  1549. *base = EXP10;
  1550. loop = (bits >= BITS);
  1551. do
  1552. {
  1553. if (loop)
  1554. {
  1555. BitVector_Copy(temp,quot);
  1556. if (BitVector_Div_Pos(quot,temp,base,rest))
  1557. {
  1558. BitVector_Dispose(result); /* emergency exit */
  1559. BitVector_Destroy(quot);
  1560. BitVector_Destroy(rest); /* should never occur */
  1561. BitVector_Destroy(temp); /* under normal operation */
  1562. BitVector_Destroy(base);
  1563. return(NULL);
  1564. }
  1565. loop = not BitVector_is_empty(quot);
  1566. q = *rest;
  1567. }
  1568. else q = *quot;
  1569. count = LOG10;
  1570. while (((loop and (count-- > 0)) or ((not loop) and (q != 0))) and
  1571. (digits < length))
  1572. {
  1573. if (q != 0)
  1574. {
  1575. BIT_VECTOR_DIGITIZE(N_word,q,r)
  1576. }
  1577. else r = (N_word) '0';
  1578. *string++ = (N_char) r;
  1579. digits++;
  1580. }
  1581. }
  1582. while (loop and (digits < length));
  1583. BitVector_Destroy(quot);
  1584. BitVector_Destroy(rest);
  1585. BitVector_Destroy(temp);
  1586. BitVector_Destroy(base);
  1587. }
  1588. if ((sign < 0) and (digits < length))
  1589. {
  1590. *string++ = (N_char) '-';
  1591. digits++;
  1592. }
  1593. *string = (N_char) '\0';
  1594. BIT_VECTOR_reverse(result,digits);
  1595. return(result);
  1596. }
  1597. struct BitVector_from_Dec_static_data {
  1598. wordptr term;
  1599. wordptr base;
  1600. wordptr prod;
  1601. wordptr rank;
  1602. wordptr temp;
  1603. };
  1604. BitVector_from_Dec_static_data *BitVector_from_Dec_static_Boot(N_word bits)
  1605. {
  1606. BitVector_from_Dec_static_data *data;
  1607. data = yasm_xmalloc(sizeof(BitVector_from_Dec_static_data));
  1608. if (bits > 0)
  1609. {
  1610. data->term = BitVector_Create(BITS,FALSE);
  1611. data->base = BitVector_Create(BITS,FALSE);
  1612. data->prod = BitVector_Create(bits,FALSE);
  1613. data->rank = BitVector_Create(bits,FALSE);
  1614. data->temp = BitVector_Create(bits,FALSE);
  1615. } else {
  1616. data->term = NULL;
  1617. data->base = NULL;
  1618. data->prod = NULL;
  1619. data->rank = NULL;
  1620. data->temp = NULL;
  1621. }
  1622. return data;
  1623. }
  1624. void BitVector_from_Dec_static_Shutdown(BitVector_from_Dec_static_data *data)
  1625. {
  1626. if (data) {
  1627. BitVector_Destroy(data->term);
  1628. BitVector_Destroy(data->base);
  1629. BitVector_Destroy(data->prod);
  1630. BitVector_Destroy(data->rank);
  1631. BitVector_Destroy(data->temp);
  1632. }
  1633. yasm_xfree(data);
  1634. }
  1635. ErrCode BitVector_from_Dec_static(BitVector_from_Dec_static_data *data,
  1636. wordptr addr, charptr string)
  1637. {
  1638. ErrCode error = ErrCode_Ok;
  1639. N_word bits = bits_(addr);
  1640. N_word mask = mask_(addr);
  1641. boolean init = (bits > BITS);
  1642. boolean minus;
  1643. boolean shift;
  1644. boolean carry;
  1645. wordptr term;
  1646. wordptr base;
  1647. wordptr prod;
  1648. wordptr rank;
  1649. wordptr temp;
  1650. N_word accu;
  1651. N_word powr;
  1652. N_word count;
  1653. size_t length;
  1654. int digit;
  1655. if (bits > 0)
  1656. {
  1657. term = data->term;
  1658. base = data->base;
  1659. prod = data->prod;
  1660. rank = data->rank;
  1661. temp = data->temp;
  1662. length = strlen((char *) string);
  1663. if (length == 0) return(ErrCode_Pars);
  1664. digit = (int) *string;
  1665. if ((minus = (digit == (int) '-')) or
  1666. (digit == (int) '+'))
  1667. {
  1668. string++;
  1669. if (--length == 0) return(ErrCode_Pars);
  1670. }
  1671. string += length;
  1672. if (init)
  1673. {
  1674. BitVector_Empty(prod);
  1675. BitVector_Empty(rank);
  1676. }
  1677. BitVector_Empty(addr);
  1678. *base = EXP10;
  1679. shift = FALSE;
  1680. while ((not error) and (length > 0))
  1681. {
  1682. accu = 0;
  1683. powr = 1;
  1684. count = LOG10;
  1685. while ((not error) and (length > 0) and (count-- > 0))
  1686. {
  1687. digit = (int) *(--string); length--;
  1688. /* separate because isdigit() is likely a macro! */
  1689. if (isdigit(digit) != 0)
  1690. {
  1691. accu += ((N_word) digit - (N_word) '0') * powr;
  1692. powr *= 10;
  1693. }
  1694. else error = ErrCode_Pars;
  1695. }
  1696. if (not error)
  1697. {
  1698. if (shift)
  1699. {
  1700. *term = accu;
  1701. BitVector_Copy(temp,rank);
  1702. error = BitVector_Mul_Pos(prod,temp,term,FALSE);
  1703. }
  1704. else
  1705. {
  1706. *prod = accu;
  1707. if ((not init) and ((accu AND NOT mask) != 0)) error = ErrCode_Ovfl;
  1708. }
  1709. if (not error)
  1710. {
  1711. carry = FALSE;
  1712. BitVector_compute(addr,addr,prod,FALSE,&carry);
  1713. /* ignores sign change (= overflow) but not */
  1714. /* numbers too large (= carry) for resulting bit vector */
  1715. if (carry) error = ErrCode_Ovfl;
  1716. else
  1717. {
  1718. if (length > 0)
  1719. {
  1720. if (shift)
  1721. {
  1722. BitVector_Copy(temp,rank);
  1723. error = BitVector_Mul_Pos(rank,temp,base,FALSE);
  1724. }
  1725. else
  1726. {
  1727. *rank = *base;
  1728. shift = TRUE;
  1729. }
  1730. }
  1731. }
  1732. }
  1733. }
  1734. }
  1735. if (not error and minus)
  1736. {
  1737. BitVector_Negate(addr,addr);
  1738. if ((*(addr + size_(addr) - 1) AND mask AND NOT (mask >> 1)) == 0)
  1739. error = ErrCode_Ovfl;
  1740. }
  1741. }
  1742. return(error);
  1743. }
  1744. ErrCode BitVector_from_Dec(wordptr addr, charptr string)
  1745. {
  1746. ErrCode error = ErrCode_Ok;
  1747. N_word bits = bits_(addr);
  1748. N_word mask = mask_(addr);
  1749. boolean init = (bits > BITS);
  1750. boolean minus;
  1751. boolean shift;
  1752. boolean carry;
  1753. wordptr term;
  1754. wordptr base;
  1755. wordptr prod;
  1756. wordptr rank;
  1757. wordptr temp;
  1758. N_word accu;
  1759. N_word powr;
  1760. N_word count;
  1761. size_t length;
  1762. int digit;
  1763. if (bits > 0)
  1764. {
  1765. length = strlen((char *) string);
  1766. if (length == 0) return(ErrCode_Pars);
  1767. digit = (int) *string;
  1768. if ((minus = (digit == (int) '-')) or
  1769. (digit == (int) '+'))
  1770. {
  1771. string++;
  1772. if (--length == 0) return(ErrCode_Pars);
  1773. }
  1774. string += length;
  1775. term = BitVector_Create(BITS,FALSE);
  1776. if (term == NULL)
  1777. {
  1778. return(ErrCode_Null);
  1779. }
  1780. base = BitVector_Create(BITS,FALSE);
  1781. if (base == NULL)
  1782. {
  1783. BitVector_Destroy(term);
  1784. return(ErrCode_Null);
  1785. }
  1786. prod = BitVector_Create(bits,init);
  1787. if (prod == NULL)
  1788. {
  1789. BitVector_Destroy(term);
  1790. BitVector_Destroy(base);
  1791. return(ErrCode_Null);
  1792. }
  1793. rank = BitVector_Create(bits,init);
  1794. if (rank == NULL)
  1795. {
  1796. BitVector_Destroy(term);
  1797. BitVector_Destroy(base);
  1798. BitVector_Destroy(prod);
  1799. return(ErrCode_Null);
  1800. }
  1801. temp = BitVector_Create(bits,FALSE);
  1802. if (temp == NULL)
  1803. {
  1804. BitVector_Destroy(term);
  1805. BitVector_Destroy(base);
  1806. BitVector_Destroy(prod);
  1807. BitVector_Destroy(rank);
  1808. return(ErrCode_Null);
  1809. }
  1810. BitVector_Empty(addr);
  1811. *base = EXP10;
  1812. shift = FALSE;
  1813. while ((not error) and (length > 0))
  1814. {
  1815. accu = 0;
  1816. powr = 1;
  1817. count = LOG10;
  1818. while ((not error) and (length > 0) and (count-- > 0))
  1819. {
  1820. digit = (int) *(--string); length--;
  1821. /* separate because isdigit() is likely a macro! */
  1822. if (isdigit(digit) != 0)
  1823. {
  1824. accu += ((N_word) digit - (N_word) '0') * powr;
  1825. powr *= 10;
  1826. }
  1827. else error = ErrCode_Pars;
  1828. }
  1829. if (not error)
  1830. {
  1831. if (shift)
  1832. {
  1833. *term = accu;
  1834. BitVector_Copy(temp,rank);
  1835. error = BitVector_Mul_Pos(prod,temp,term,FALSE);
  1836. }
  1837. else
  1838. {
  1839. *prod = accu;
  1840. if ((not init) and ((accu AND NOT mask) != 0)) error = ErrCode_Ovfl;
  1841. }
  1842. if (not error)
  1843. {
  1844. carry = FALSE;
  1845. BitVector_compute(addr,addr,prod,FALSE,&carry);
  1846. /* ignores sign change (= overflow) but not */
  1847. /* numbers too large (= carry) for resulting bit vector */
  1848. if (carry) error = ErrCode_Ovfl;
  1849. else
  1850. {
  1851. if (length > 0)
  1852. {
  1853. if (shift)
  1854. {
  1855. BitVector_Copy(temp,rank);
  1856. error = BitVector_Mul_Pos(rank,temp,base,FALSE);
  1857. }
  1858. else
  1859. {
  1860. *rank = *base;
  1861. shift = TRUE;
  1862. }
  1863. }
  1864. }
  1865. }
  1866. }
  1867. }
  1868. BitVector_Destroy(term);
  1869. BitVector_Destroy(base);
  1870. BitVector_Destroy(prod);
  1871. BitVector_Destroy(rank);
  1872. BitVector_Destroy(temp);
  1873. if (not error and minus)
  1874. {
  1875. BitVector_Negate(addr,addr);
  1876. if ((*(addr + size_(addr) - 1) AND mask AND NOT (mask >> 1)) == 0)
  1877. error = ErrCode_Ovfl;
  1878. }
  1879. }
  1880. return(error);
  1881. }
  1882. charptr BitVector_to_Enum(wordptr addr)
  1883. {
  1884. N_word bits = bits_(addr);
  1885. N_word sample;
  1886. N_word length;
  1887. N_word digits;
  1888. N_word factor;
  1889. N_word power;
  1890. N_word start;
  1891. N_word min;
  1892. N_word max;
  1893. charptr string;
  1894. charptr target;
  1895. boolean comma;
  1896. if (bits > 0)
  1897. {
  1898. sample = bits - 1; /* greatest possible index */
  1899. length = 2; /* account for index 0 and terminating '\0' */
  1900. digits = 1; /* account for intervening dashes and commas */
  1901. factor = 1;
  1902. power = 10;
  1903. while (sample >= (power-1))
  1904. {
  1905. length += ++digits * factor * 6; /* 9,90,900,9000,... (9*2/3 = 6) */
  1906. factor = power;
  1907. power *= 10;
  1908. }
  1909. if (sample > --factor)
  1910. {
  1911. sample -= factor;
  1912. factor = (N_word) ( sample / 3 );
  1913. factor = (factor << 1) + (sample - (factor * 3));
  1914. length += ++digits * factor;
  1915. }
  1916. }
  1917. else length = 1;
  1918. string = (charptr) yasm_xmalloc((size_t) length);
  1919. if (string == NULL) return(NULL);
  1920. start = 0;
  1921. comma = FALSE;
  1922. target = string;
  1923. while ((start < bits) and BitVector_interval_scan_inc(addr,start,&min,&max))
  1924. {
  1925. start = max + 2;
  1926. if (comma) *target++ = (N_char) ',';
  1927. if (min == max)
  1928. {
  1929. target += BIT_VECTOR_int2str(target,min);
  1930. }
  1931. else
  1932. {
  1933. if (min+1 == max)
  1934. {
  1935. target += BIT_VECTOR_int2str(target,min);
  1936. *target++ = (N_char) ',';
  1937. target += BIT_VECTOR_int2str(target,max);
  1938. }
  1939. else
  1940. {
  1941. target += BIT_VECTOR_int2str(target,min);
  1942. *target++ = (N_char) '-';
  1943. target += BIT_VECTOR_int2str(target,max);
  1944. }
  1945. }
  1946. comma = TRUE;
  1947. }
  1948. *target = (N_char) '\0';
  1949. return(string);
  1950. }
  1951. ErrCode BitVector_from_Enum(wordptr addr, charptr string)
  1952. {
  1953. ErrCode error = ErrCode_Ok;
  1954. N_word bits = bits_(addr);
  1955. N_word state = 1;
  1956. N_word token;
  1957. N_word indx = 0; /* silence compiler warning */
  1958. N_word start = 0; /* silence compiler warning */
  1959. if (bits > 0)
  1960. {
  1961. BitVector_Empty(addr);
  1962. while ((not error) and (state != 0))
  1963. {
  1964. token = (N_word) *string;
  1965. /* separate because isdigit() is likely a macro! */
  1966. if (isdigit((int)token) != 0)
  1967. {
  1968. string += BIT_VECTOR_str2int(string,&indx);
  1969. if (indx < bits) token = (N_word) '0';
  1970. else error = ErrCode_Indx;
  1971. }
  1972. else string++;
  1973. if (not error)
  1974. switch (state)
  1975. {
  1976. case 1:
  1977. switch (token)
  1978. {
  1979. case (N_word) '0':
  1980. state = 2;
  1981. break;
  1982. case (N_word) '\0':
  1983. state = 0;
  1984. break;
  1985. default:
  1986. error = ErrCode_Pars;
  1987. break;
  1988. }
  1989. break;
  1990. case 2:
  1991. switch (token)
  1992. {
  1993. case (N_word) '-':
  1994. start = indx;
  1995. state = 3;
  1996. break;
  1997. case (N_word) ',':
  1998. BIT_VECTOR_SET_BIT(addr,indx)
  1999. state = 5;
  2000. break;
  2001. case (N_word) '\0':
  2002. BIT_VECTOR_SET_BIT(addr,indx)
  2003. state = 0;
  2004. break;
  2005. default:
  2006. error = ErrCode_Pars;
  2007. break;
  2008. }
  2009. break;
  2010. case 3:
  2011. switch (token)
  2012. {
  2013. case (N_word) '0':
  2014. if (start < indx)
  2015. BitVector_Interval_Fill(addr,start,indx);
  2016. else if (start == indx)
  2017. BIT_VECTOR_SET_BIT(addr,indx)
  2018. else error = ErrCode_Ordr;
  2019. state = 4;
  2020. break;
  2021. default:
  2022. error = ErrCode_Pars;
  2023. break;
  2024. }
  2025. break;
  2026. case 4:
  2027. switch (token)
  2028. {
  2029. case (N_word) ',':
  2030. state = 5;
  2031. break;
  2032. case (N_word) '\0':
  2033. state = 0;
  2034. break;
  2035. default:
  2036. error = ErrCode_Pars;
  2037. break;
  2038. }
  2039. break;
  2040. case 5:
  2041. switch (token)
  2042. {
  2043. case (N_word) '0':
  2044. state = 2;
  2045. break;
  2046. default:
  2047. error = ErrCode_Pars;
  2048. break;
  2049. }
  2050. break;
  2051. }
  2052. }
  2053. }
  2054. return(error);
  2055. }
  2056. void BitVector_Bit_Off(wordptr addr, N_int indx) /* X = X \ {x} */
  2057. {
  2058. if (indx < bits_(addr)) BIT_VECTOR_CLR_BIT(addr,indx)
  2059. }
  2060. void BitVector_Bit_On(wordptr addr, N_int indx) /* X = X + {x} */
  2061. {
  2062. if (indx < bits_(addr)) BIT_VECTOR_SET_BIT(addr,indx)
  2063. }
  2064. boolean BitVector_bit_flip(wordptr addr, N_int indx) /* X=(X+{x})\(X*{x}) */
  2065. {
  2066. N_word mask;
  2067. if (indx < bits_(addr)) return( BIT_VECTOR_FLP_BIT(addr,indx,mask) );
  2068. else return( FALSE );
  2069. }
  2070. boolean BitVector_bit_test(wordptr addr, N_int indx) /* {x} in X ? */
  2071. {
  2072. if (indx < bits_(addr)) return( BIT_VECTOR_TST_BIT(addr,indx) );
  2073. else return( FALSE );
  2074. }
  2075. void BitVector_Bit_Copy(wordptr addr, N_int indx, boolean bit)
  2076. {
  2077. if (indx < bits_(addr))
  2078. {
  2079. if (bit) BIT_VECTOR_SET_BIT(addr,indx)
  2080. else BIT_VECTOR_CLR_BIT(addr,indx)
  2081. }
  2082. }
  2083. void BitVector_LSB(wordptr addr, boolean bit)
  2084. {
  2085. if (bits_(addr) > 0)
  2086. {
  2087. if (bit) *addr |= LSB;
  2088. else *addr &= NOT LSB;
  2089. }
  2090. }
  2091. void BitVector_MSB(wordptr addr, boolean bit)
  2092. {
  2093. N_word size = size_(addr);
  2094. N_word mask = mask_(addr);
  2095. if (size-- > 0)
  2096. {
  2097. if (bit) *(addr+size) |= mask AND NOT (mask >> 1);
  2098. else *(addr+size) &= NOT mask OR (mask >> 1);
  2099. }
  2100. }
  2101. boolean BitVector_lsb_(wordptr addr)
  2102. {
  2103. if (size_(addr) > 0) return( (*addr AND LSB) != 0 );
  2104. else return( FALSE );
  2105. }
  2106. boolean BitVector_msb_(wordptr addr)
  2107. {
  2108. N_word size = size_(addr);
  2109. N_word mask = mask_(addr);
  2110. if (size-- > 0)
  2111. return( (*(addr+size) AND (mask AND NOT (mask >> 1))) != 0 );
  2112. else
  2113. return( FALSE );
  2114. }
  2115. boolean BitVector_rotate_left(wordptr addr)
  2116. {
  2117. N_word size = size_(addr);
  2118. N_word mask = mask_(addr);
  2119. N_word msb;
  2120. boolean carry_in;
  2121. boolean carry_out = FALSE;
  2122. if (size > 0)
  2123. {
  2124. msb = mask AND NOT (mask >> 1);
  2125. carry_in = ((*(addr+size-1) AND msb) != 0);
  2126. while (size-- > 1)
  2127. {
  2128. carry_out = ((*addr AND MSB) != 0);
  2129. *addr <<= 1;
  2130. if (carry_in) *addr |= LSB;
  2131. carry_in = carry_out;
  2132. addr++;
  2133. }
  2134. carry_out = ((*addr AND msb) != 0);
  2135. *addr <<= 1;
  2136. if (carry_in) *addr |= LSB;
  2137. *addr &= mask;
  2138. }
  2139. return(carry_out);
  2140. }
  2141. boolean BitVector_rotate_right(wordptr addr)
  2142. {
  2143. N_word size = size_(addr);
  2144. N_word mask = mask_(addr);
  2145. N_word msb;
  2146. boolean carry_in;
  2147. boolean carry_out = FALSE;
  2148. if (size > 0)
  2149. {
  2150. msb = mask AND NOT (mask >> 1);
  2151. carry_in = ((*addr AND LSB) != 0);
  2152. addr += size-1;
  2153. *addr &= mask;
  2154. carry_out = ((*addr AND LSB) != 0);
  2155. *addr >>= 1;
  2156. if (carry_in) *addr |= msb;
  2157. carry_in = carry_out;
  2158. addr--;
  2159. size--;
  2160. while (size-- > 0)
  2161. {
  2162. carry_out = ((*addr AND LSB) != 0);
  2163. *addr >>= 1;
  2164. if (carry_in) *addr |= MSB;
  2165. carry_in = carry_out;
  2166. addr--;
  2167. }
  2168. }
  2169. return(carry_out);
  2170. }
  2171. boolean BitVector_shift_left(wordptr addr, boolean carry_in)
  2172. {
  2173. N_word size = size_(addr);
  2174. N_word mask = mask_(addr);
  2175. N_word msb;
  2176. boolean carry_out = carry_in;
  2177. if (size > 0)
  2178. {
  2179. msb = mask AND NOT (mask >> 1);
  2180. while (size-- > 1)
  2181. {
  2182. carry_out = ((*addr AND MSB) != 0);
  2183. *addr <<= 1;
  2184. if (carry_in) *addr |= LSB;
  2185. carry_in = carry_out;
  2186. addr++;
  2187. }
  2188. carry_out = ((*addr AND msb) != 0);
  2189. *addr <<= 1;
  2190. if (carry_in) *addr |= LSB;
  2191. *addr &= mask;
  2192. }
  2193. return(carry_out);
  2194. }
  2195. boolean BitVector_shift_right(wordptr addr, boolean carry_in)
  2196. {
  2197. N_word size = size_(addr);
  2198. N_word mask = mask_(addr);
  2199. N_word msb;
  2200. boolean carry_out = carry_in;
  2201. if (size > 0)
  2202. {
  2203. msb = mask AND NOT (mask >> 1);
  2204. addr += size-1;
  2205. *addr &= mask;
  2206. carry_out = ((*addr AND LSB) != 0);
  2207. *addr >>= 1;
  2208. if (carry_in) *addr |= msb;
  2209. carry_in = carry_out;
  2210. addr--;
  2211. size--;
  2212. while (size-- > 0)
  2213. {
  2214. carry_out = ((*addr AND LSB) != 0);
  2215. *addr >>= 1;
  2216. if (carry_in) *addr |= MSB;
  2217. carry_in = carry_out;
  2218. addr--;
  2219. }
  2220. }
  2221. return(carry_out);
  2222. }
  2223. void BitVector_Move_Left(wordptr addr, N_int bits)
  2224. {
  2225. N_word count;
  2226. N_word words;
  2227. if (bits > 0)
  2228. {
  2229. count = bits AND MODMASK;
  2230. words = bits >> LOGBITS;
  2231. if (bits >= bits_(addr)) BitVector_Empty(addr);
  2232. else
  2233. {
  2234. while (count-- > 0) BitVector_shift_left(addr,0);
  2235. BitVector_Word_Insert(addr,0,words,TRUE);
  2236. }
  2237. }
  2238. }
  2239. void BitVector_Move_Right(wordptr addr, N_int bits)
  2240. {
  2241. N_word count;
  2242. N_word words;
  2243. if (bits > 0)
  2244. {
  2245. count = bits AND MODMASK;
  2246. words = bits >> LOGBITS;
  2247. if (bits >= bits_(addr)) BitVector_Empty(addr);
  2248. else
  2249. {
  2250. while (count-- > 0) BitVector_shift_right(addr,0);
  2251. BitVector_Word_Delete(addr,0,words,TRUE);
  2252. }
  2253. }
  2254. }
  2255. void BitVector_Insert(wordptr addr, N_int offset, N_int count, boolean clear)
  2256. {
  2257. N_word bits = bits_(addr);
  2258. N_word last;
  2259. if ((count > 0) and (offset < bits))
  2260. {
  2261. last = offset + count;
  2262. if (last < bits)
  2263. {
  2264. BitVector_Interval_Copy(addr,addr,last,offset,(bits-last));
  2265. }
  2266. else last = bits;
  2267. if (clear) BitVector_Interval_Empty(addr,offset,(last-1));
  2268. }
  2269. }
  2270. void BitVector_Delete(wordptr addr, N_int offset, N_int count, boolean clear)
  2271. {
  2272. N_word bits = bits_(addr);
  2273. N_word last;
  2274. if ((count > 0) and (offset < bits))
  2275. {
  2276. last = offset + count;
  2277. if (last < bits)
  2278. {
  2279. BitVector_Interval_Copy(addr,addr,offset,last,(bits-last));
  2280. }
  2281. else count = bits - offset;
  2282. if (clear) BitVector_Interval_Empty(addr,(bits-count),(bits-1));
  2283. }
  2284. }
  2285. boolean BitVector_increment(wordptr addr) /* X++ */
  2286. {
  2287. N_word size = size_(addr);
  2288. N_word mask = mask_(addr);
  2289. wordptr last = addr + size - 1;
  2290. boolean carry = TRUE;
  2291. if (size > 0)
  2292. {
  2293. *last |= NOT mask;
  2294. while (carry and (size-- > 0))
  2295. {
  2296. carry = (++(*addr++) == 0);
  2297. }
  2298. *last &= mask;
  2299. }
  2300. return(carry);
  2301. }
  2302. boolean BitVector_decrement(wordptr addr) /* X-- */
  2303. {
  2304. N_word size = size_(addr);
  2305. N_word mask = mask_(addr);
  2306. wordptr last = addr + size - 1;
  2307. boolean carry = TRUE;
  2308. if (size > 0)
  2309. {
  2310. *last &= mask;
  2311. while (carry and (size-- > 0))
  2312. {
  2313. carry = (*addr == 0);
  2314. --(*addr++);
  2315. }
  2316. *last &= mask;
  2317. }
  2318. return(carry);
  2319. }
  2320. boolean BitVector_compute(wordptr X, wordptr Y, wordptr Z, boolean minus, boolean *carry)
  2321. {
  2322. N_word size = size_(X);
  2323. N_word mask = mask_(X);
  2324. N_word vv = 0;
  2325. N_word cc;
  2326. N_word mm;
  2327. N_word yy;
  2328. N_word zz;
  2329. N_word lo;
  2330. N_word hi;
  2331. if (size > 0)
  2332. {
  2333. if (minus) cc = (*carry == 0);
  2334. else cc = (*carry != 0);
  2335. /* deal with (size-1) least significant full words first: */
  2336. while (--size > 0)
  2337. {
  2338. yy = *Y++;
  2339. if (minus) zz = (N_word) NOT ( Z ? *Z++ : 0 );
  2340. else zz = (N_word) ( Z ? *Z++ : 0 );
  2341. lo = (yy AND LSB) + (zz AND LSB) + cc;
  2342. hi = (yy >> 1) + (zz >> 1) + (lo >> 1);
  2343. cc = ((hi AND MSB) != 0);
  2344. *X++ = (hi << 1) OR (lo AND LSB);
  2345. }
  2346. /* deal with most significant word (may be used only partially): */
  2347. yy = *Y AND mask;
  2348. if (minus) zz = (N_word) NOT ( Z ? *Z : 0 );
  2349. else zz = (N_word) ( Z ? *Z : 0 );
  2350. zz &= mask;
  2351. if (mask == LSB) /* special case, only one bit used */
  2352. {
  2353. vv = cc;
  2354. lo = yy + zz + cc;
  2355. cc = (lo >> 1);
  2356. vv ^= cc;
  2357. *X = lo AND LSB;
  2358. }
  2359. else
  2360. {
  2361. if (NOT mask) /* not all bits are used, but more than one */
  2362. {
  2363. mm = (mask >> 1);
  2364. vv = (yy AND mm) + (zz AND mm) + cc;
  2365. mm = mask AND NOT mm;
  2366. lo = yy + zz + cc;
  2367. cc = (lo >> 1);
  2368. vv ^= cc;
  2369. vv &= mm;
  2370. cc &= mm;
  2371. *X = lo AND mask;
  2372. }
  2373. else /* other special case, all bits are used */
  2374. {
  2375. mm = NOT MSB;
  2376. lo = (yy AND mm) + (zz AND mm) + cc;
  2377. vv = lo AND MSB;
  2378. hi = ((yy AND MSB) >> 1) + ((zz AND MSB) >> 1) + (vv >> 1);
  2379. cc = hi AND MSB;
  2380. vv ^= cc;
  2381. *X = (hi << 1) OR (lo AND mm);
  2382. }
  2383. }
  2384. if (minus) *carry = (cc == 0);
  2385. else *carry = (cc != 0);
  2386. }
  2387. return(vv != 0);
  2388. }
  2389. boolean BitVector_add(wordptr X, wordptr Y, wordptr Z, boolean *carry)
  2390. {
  2391. return(BitVector_compute(X,Y,Z,FALSE,carry));
  2392. }
  2393. boolean BitVector_sub(wordptr X, wordptr Y, wordptr Z, boolean *carry)
  2394. {
  2395. return(BitVector_compute(X,Y,Z,TRUE,carry));
  2396. }
  2397. boolean BitVector_inc(wordptr X, wordptr Y)
  2398. {
  2399. boolean carry = TRUE;
  2400. return(BitVector_compute(X,Y,NULL,FALSE,&carry));
  2401. }
  2402. boolean BitVector_dec(wordptr X, wordptr Y)
  2403. {
  2404. boolean carry = TRUE;
  2405. return(BitVector_compute(X,Y,NULL,TRUE,&carry));
  2406. }
  2407. void BitVector_Negate(wordptr X, wordptr Y)
  2408. {
  2409. N_word size = size_(X);
  2410. N_word mask = mask_(X);
  2411. boolean carry = TRUE;
  2412. if (size > 0)
  2413. {
  2414. while (size-- > 0)
  2415. {
  2416. *X = NOT *Y++;
  2417. if (carry)
  2418. {
  2419. carry = (++(*X) == 0);
  2420. }
  2421. X++;
  2422. }
  2423. *(--X) &= mask;
  2424. }
  2425. }
  2426. void BitVector_Absolute(wordptr X, wordptr Y)
  2427. {
  2428. N_word size = size_(Y);
  2429. N_word mask = mask_(Y);
  2430. if (size > 0)
  2431. {
  2432. if (*(Y+size-1) AND (mask AND NOT (mask >> 1))) BitVector_Negate(X,Y);
  2433. else BitVector_Copy(X,Y);
  2434. }
  2435. }
  2436. Z_int BitVector_Sign(wordptr addr)
  2437. {
  2438. N_word size = size_(addr);
  2439. N_word mask = mask_(addr);
  2440. wordptr last = addr + size - 1;
  2441. boolean r = TRUE;
  2442. if (size > 0)
  2443. {
  2444. *last &= mask;
  2445. while (r and (size-- > 0)) r = ( *addr++ == 0 );
  2446. }
  2447. if (r) return((Z_int) 0);
  2448. else
  2449. {
  2450. if (*last AND (mask AND NOT (mask >> 1))) return((Z_int) -1);
  2451. else return((Z_int) 1);
  2452. }
  2453. }
  2454. ErrCode BitVector_Mul_Pos(wordptr X, wordptr Y, wordptr Z, boolean strict)
  2455. {
  2456. N_word mask;
  2457. N_word limit;
  2458. N_word count;
  2459. Z_long last;
  2460. wordptr sign;
  2461. boolean carry;
  2462. boolean overflow;
  2463. boolean ok = TRUE;
  2464. /*
  2465. Requirements:
  2466. - X, Y and Z must be distinct
  2467. - X and Y must have equal sizes (whereas Z may be any size!)
  2468. - Z should always contain the SMALLER of the two factors Y and Z
  2469. Constraints:
  2470. - The contents of Y (and of X, of course) are destroyed
  2471. (only Z is preserved!)
  2472. */
  2473. if ((X == Y) or (X == Z) or (Y == Z)) return(ErrCode_Same);
  2474. if (bits_(X) != bits_(Y)) return(ErrCode_Size);
  2475. BitVector_Empty(X);
  2476. if (BitVector_is_empty(Y)) return(ErrCode_Ok); /* exit also taken if bits_(Y)==0 */
  2477. if ((last = Set_Max(Z)) < 0L) return(ErrCode_Ok);
  2478. limit = (N_word) last;
  2479. sign = Y + size_(Y) - 1;
  2480. mask = mask_(Y);
  2481. *sign &= mask;
  2482. mask &= NOT (mask >> 1);
  2483. for ( count = 0; (ok and (count <= limit)); count++ )
  2484. {
  2485. if ( BIT_VECTOR_TST_BIT(Z,count) )
  2486. {
  2487. carry = false;
  2488. overflow = BitVector_compute(X,X,Y,false,&carry);
  2489. if (strict) ok = not (carry or overflow);
  2490. else ok = not carry;
  2491. }
  2492. if (ok and (count < limit))
  2493. {
  2494. carry = BitVector_shift_left(Y,0);
  2495. if (strict)
  2496. {
  2497. overflow = ((*sign AND mask) != 0);
  2498. ok = not (carry or overflow);
  2499. }
  2500. else ok = not carry;
  2501. }
  2502. }
  2503. if (ok) return(ErrCode_Ok); else return(ErrCode_Ovfl);
  2504. }
  2505. ErrCode BitVector_Multiply(wordptr X, wordptr Y, wordptr Z)
  2506. {
  2507. ErrCode error = ErrCode_Ok;
  2508. N_word bit_x = bits_(X);
  2509. N_word bit_y = bits_(Y);
  2510. N_word bit_z = bits_(Z);
  2511. N_word size;
  2512. N_word mask;
  2513. N_word msb;
  2514. wordptr ptr_y;
  2515. wordptr ptr_z;
  2516. boolean sgn_x;
  2517. boolean sgn_y;
  2518. boolean sgn_z;
  2519. boolean zero;
  2520. wordptr A;
  2521. wordptr B;
  2522. /*
  2523. Requirements:
  2524. - Y and Z must have equal sizes
  2525. - X must have at least the same size as Y and Z but may be larger (!)
  2526. Features:
  2527. - The contents of Y and Z are preserved
  2528. - X may be identical with Y or Z (or both!)
  2529. (in-place multiplication is possible!)
  2530. */
  2531. if ((bit_y != bit_z) or (bit_x < bit_y)) return(ErrCode_Size);
  2532. if (BitVector_is_empty(Y) or BitVector_is_empty(Z))
  2533. {
  2534. BitVector_Empty(X);
  2535. }
  2536. else
  2537. {
  2538. A = BitVector_Create(bit_y,FALSE);
  2539. if (A == NULL) return(ErrCode_Null);
  2540. B = BitVector_Create(bit_z,FALSE);
  2541. if (B == NULL) { BitVector_Destroy(A); return(ErrCode_Null); }
  2542. size = size_(Y);
  2543. mask = mask_(Y);
  2544. msb = (mask AND NOT (mask >> 1));
  2545. sgn_y = (((*(Y+size-1) &= mask) AND msb) != 0);
  2546. sgn_z = (((*(Z+size-1) &= mask) AND msb) != 0);
  2547. sgn_x = sgn_y XOR sgn_z;
  2548. if (sgn_y) BitVector_Negate(A,Y); else BitVector_Copy(A,Y);
  2549. if (sgn_z) BitVector_Negate(B,Z); else BitVector_Copy(B,Z);
  2550. ptr_y = A + size;
  2551. ptr_z = B + size;
  2552. zero = TRUE;
  2553. while (zero and (size-- > 0))
  2554. {
  2555. zero &= (*(--ptr_y) == 0);
  2556. zero &= (*(--ptr_z) == 0);
  2557. }
  2558. if (*ptr_y > *ptr_z)
  2559. {
  2560. if (bit_x > bit_y)
  2561. {
  2562. A = BitVector_Resize(A,bit_x);
  2563. if (A == NULL) { BitVector_Destroy(B); return(ErrCode_Null); }
  2564. }
  2565. error = BitVector_Mul_Pos(X,A,B,TRUE);
  2566. }
  2567. else
  2568. {
  2569. if (bit_x > bit_z)
  2570. {
  2571. B = BitVector_Resize(B,bit_x);
  2572. if (B == NULL) { BitVector_Destroy(A); return(ErrCode_Null); }
  2573. }
  2574. error = BitVector_Mul_Pos(X,B,A,TRUE);
  2575. }
  2576. if ((not error) and sgn_x) BitVector_Negate(X,X);
  2577. BitVector_Destroy(A);
  2578. BitVector_Destroy(B);
  2579. }
  2580. return(error);
  2581. }
  2582. ErrCode BitVector_Div_Pos(wordptr Q, wordptr X, wordptr Y, wordptr R)
  2583. {
  2584. N_word bits = bits_(Q);
  2585. N_word mask;
  2586. wordptr addr;
  2587. Z_long last;
  2588. boolean flag;
  2589. boolean copy = FALSE; /* flags whether valid rest is in R (0) or X (1) */
  2590. /*
  2591. Requirements:
  2592. - All bit vectors must have equal sizes
  2593. - Q, X, Y and R must all be distinct bit vectors
  2594. - Y must be non-zero (of course!)
  2595. Constraints:
  2596. - The contents of X (and Q and R, of course) are destroyed
  2597. (only Y is preserved!)
  2598. */
  2599. if ((bits != bits_(X)) or (bits != bits_(Y)) or (bits != bits_(R)))
  2600. return(ErrCode_Size);
  2601. if ((Q == X) or (Q == Y) or (Q == R) or (X == Y) or (X == R) or (Y == R))
  2602. return(ErrCode_Same);
  2603. if (BitVector_is_empty(Y))
  2604. return(ErrCode_Zero);
  2605. BitVector_Empty(R);
  2606. BitVector_Copy(Q,X);
  2607. if ((last = Set_Max(Q)) < 0L) return(ErrCode_Ok);
  2608. bits = (N_word) ++last;
  2609. while (bits-- > 0)
  2610. {
  2611. addr = Q + (bits >> LOGBITS);
  2612. mask = BITMASKTAB[bits AND MODMASK];
  2613. flag = ((*addr AND mask) != 0);
  2614. if (copy)
  2615. {
  2616. BitVector_shift_left(X,flag);
  2617. flag = FALSE;
  2618. BitVector_compute(R,X,Y,TRUE,&flag);
  2619. }
  2620. else
  2621. {
  2622. BitVector_shift_left(R,flag);
  2623. flag = FALSE;
  2624. BitVector_compute(X,R,Y,TRUE,&flag);
  2625. }
  2626. if (flag) *addr &= NOT mask;
  2627. else
  2628. {
  2629. *addr |= mask;
  2630. copy = not copy;
  2631. }
  2632. }
  2633. if (copy) BitVector_Copy(R,X);
  2634. return(ErrCode_Ok);
  2635. }
  2636. ErrCode BitVector_Divide(wordptr Q, wordptr X, wordptr Y, wordptr R)
  2637. {
  2638. ErrCode error = ErrCode_Ok;
  2639. N_word bits = bits_(Q);
  2640. N_word size = size_(Q);
  2641. N_word mask = mask_(Q);
  2642. N_word msb = (mask AND NOT (mask >> 1));
  2643. boolean sgn_q;
  2644. boolean sgn_x;
  2645. boolean sgn_y;
  2646. wordptr A;
  2647. wordptr B;
  2648. /*
  2649. Requirements:
  2650. - All bit vectors must have equal sizes
  2651. - Q and R must be two distinct bit vectors
  2652. - Y must be non-zero (of course!)
  2653. Features:
  2654. - The contents of X and Y are preserved
  2655. - Q may be identical with X or Y (or both)
  2656. (in-place division is possible!)
  2657. - R may be identical with X or Y (or both)
  2658. (but not identical with Q!)
  2659. */
  2660. if ((bits != bits_(X)) or (bits != bits_(Y)) or (bits != bits_(R)))
  2661. return(ErrCode_Size);
  2662. if (Q == R)
  2663. return(ErrCode_Same);
  2664. if (BitVector_is_empty(Y))
  2665. return(ErrCode_Zero);
  2666. if (BitVector_is_empty(X))
  2667. {
  2668. BitVector_Empty(Q);
  2669. BitVector_Empty(R);
  2670. }
  2671. else
  2672. {
  2673. A = BitVector_Create(bits,FALSE);
  2674. if (A == NULL) return(ErrCode_Null);
  2675. B = BitVector_Create(bits,FALSE);
  2676. if (B == NULL) { BitVector_Destroy(A); return(ErrCode_Null); }
  2677. size--;
  2678. sgn_x = (((*(X+size) &= mask) AND msb) != 0);
  2679. sgn_y = (((*(Y+size) &= mask) AND msb) != 0);
  2680. sgn_q = sgn_x XOR sgn_y;
  2681. if (sgn_x) BitVector_Negate(A,X); else BitVector_Copy(A,X);
  2682. if (sgn_y) BitVector_Negate(B,Y); else BitVector_Copy(B,Y);
  2683. if (not (error = BitVector_Div_Pos(Q,A,B,R)))
  2684. {
  2685. if (sgn_q) BitVector_Negate(Q,Q);
  2686. if (sgn_x) BitVector_Negate(R,R);
  2687. }
  2688. BitVector_Destroy(A);
  2689. BitVector_Destroy(B);
  2690. }
  2691. return(error);
  2692. }
  2693. ErrCode BitVector_GCD(wordptr X, wordptr Y, wordptr Z)
  2694. {
  2695. ErrCode error = ErrCode_Ok;
  2696. N_word bits = bits_(X);
  2697. N_word size = size_(X);
  2698. N_word mask = mask_(X);
  2699. N_word msb = (mask AND NOT (mask >> 1));
  2700. boolean sgn_a;
  2701. boolean sgn_b;
  2702. boolean sgn_r;
  2703. wordptr Q;
  2704. wordptr R;
  2705. wordptr A;
  2706. wordptr B;
  2707. wordptr T;
  2708. /*
  2709. Requirements:
  2710. - All bit vectors must have equal sizes
  2711. Features:
  2712. - The contents of Y and Z are preserved
  2713. - X may be identical with Y or Z (or both)
  2714. (in-place is possible!)
  2715. - GCD(0,z) == GCD(z,0) == z
  2716. - negative values are handled correctly
  2717. */
  2718. if ((bits != bits_(Y)) or (bits != bits_(Z))) return(ErrCode_Size);
  2719. if (BitVector_is_empty(Y))
  2720. {
  2721. if (X != Z) BitVector_Copy(X,Z);
  2722. return(ErrCode_Ok);
  2723. }
  2724. if (BitVector_is_empty(Z))
  2725. {
  2726. if (X != Y) BitVector_Copy(X,Y);
  2727. return(ErrCode_Ok);
  2728. }
  2729. Q = BitVector_Create(bits,false);
  2730. if (Q == NULL)
  2731. {
  2732. return(ErrCode_Null);
  2733. }
  2734. R = BitVector_Create(bits,FALSE);
  2735. if (R == NULL)
  2736. {
  2737. BitVector_Destroy(Q);
  2738. return(ErrCode_Null);
  2739. }
  2740. A = BitVector_Create(bits,FALSE);
  2741. if (A == NULL)
  2742. {
  2743. BitVector_Destroy(Q);
  2744. BitVector_Destroy(R);
  2745. return(ErrCode_Null);
  2746. }
  2747. B = BitVector_Create(bits,FALSE);
  2748. if (B == NULL)
  2749. {
  2750. BitVector_Destroy(Q);
  2751. BitVector_Destroy(R);
  2752. BitVector_Destroy(A);
  2753. return(ErrCode_Null);
  2754. }
  2755. size--;
  2756. sgn_a = (((*(Y+size) &= mask) AND msb) != 0);
  2757. sgn_b = (((*(Z+size) &= mask) AND msb) != 0);
  2758. if (sgn_a) BitVector_Negate(A,Y); else BitVector_Copy(A,Y);
  2759. if (sgn_b) BitVector_Negate(B,Z); else BitVector_Copy(B,Z);
  2760. while (not error)
  2761. {
  2762. if (not (error = BitVector_Div_Pos(Q,A,B,R)))
  2763. {
  2764. if (BitVector_is_empty(R)) break;
  2765. T = A; sgn_r = sgn_a;
  2766. A = B; sgn_a = sgn_b;
  2767. B = R; sgn_b = sgn_r;
  2768. R = T;
  2769. }
  2770. }
  2771. if (not error)
  2772. {
  2773. if (sgn_b) BitVector_Negate(X,B); else BitVector_Copy(X,B);
  2774. }
  2775. BitVector_Destroy(Q);
  2776. BitVector_Destroy(R);
  2777. BitVector_Destroy(A);
  2778. BitVector_Destroy(B);
  2779. return(error);
  2780. }
  2781. ErrCode BitVector_GCD2(wordptr U, wordptr V, wordptr W, wordptr X, wordptr Y)
  2782. {
  2783. ErrCode error = ErrCode_Ok;
  2784. N_word bits = bits_(U);
  2785. N_word size = size_(U);
  2786. N_word mask = mask_(U);
  2787. N_word msb = (mask AND NOT (mask >> 1));
  2788. boolean minus;
  2789. boolean carry;
  2790. boolean sgn_q;
  2791. boolean sgn_r;
  2792. boolean sgn_a;
  2793. boolean sgn_b;
  2794. boolean sgn_x;
  2795. boolean sgn_y;
  2796. listptr L;
  2797. wordptr Q;
  2798. wordptr R;
  2799. wordptr A;
  2800. wordptr B;
  2801. wordptr T;
  2802. wordptr X1;
  2803. wordptr X2;
  2804. wordptr X3;
  2805. wordptr Y1;
  2806. wordptr Y2;
  2807. wordptr Y3;
  2808. wordptr Z;
  2809. /*
  2810. Requirements:
  2811. - All bit vectors must have equal sizes
  2812. - U, V, and W must all be distinct bit vectors
  2813. Features:
  2814. - The contents of X and Y are preserved
  2815. - U, V and W may be identical with X or Y (or both,
  2816. provided that U, V and W are mutually distinct)
  2817. (i.e., in-place is possible!)
  2818. - GCD(0,z) == GCD(z,0) == z
  2819. - negative values are handled correctly
  2820. */
  2821. if ((bits != bits_(V)) or
  2822. (bits != bits_(W)) or
  2823. (bits != bits_(X)) or
  2824. (bits != bits_(Y)))
  2825. {
  2826. return(ErrCode_Size);
  2827. }
  2828. if ((U == V) or (U == W) or (V == W))
  2829. {
  2830. return(ErrCode_Same);
  2831. }
  2832. if (BitVector_is_empty(X))
  2833. {
  2834. if (U != Y) BitVector_Copy(U,Y);
  2835. BitVector_Empty(V);
  2836. BitVector_Empty(W);
  2837. *W = 1;
  2838. return(ErrCode_Ok);
  2839. }
  2840. if (BitVector_is_empty(Y))
  2841. {
  2842. if (U != X) BitVector_Copy(U,X);
  2843. BitVector_Empty(V);
  2844. BitVector_Empty(W);
  2845. *V = 1;
  2846. return(ErrCode_Ok);
  2847. }
  2848. if ((L = BitVector_Create_List(bits,false,11)) == NULL)
  2849. {
  2850. return(ErrCode_Null);
  2851. }
  2852. Q = L[0];
  2853. R = L[1];
  2854. A = L[2];
  2855. B = L[3];
  2856. X1 = L[4];
  2857. X2 = L[5];
  2858. X3 = L[6];
  2859. Y1 = L[7];
  2860. Y2 = L[8];
  2861. Y3 = L[9];
  2862. Z = L[10];
  2863. size--;
  2864. sgn_a = (((*(X+size) &= mask) AND msb) != 0);
  2865. sgn_b = (((*(Y+size) &= mask) AND msb) != 0);
  2866. if (sgn_a) BitVector_Negate(A,X); else BitVector_Copy(A,X);
  2867. if (sgn_b) BitVector_Negate(B,Y); else BitVector_Copy(B,Y);
  2868. BitVector_Empty(X1);
  2869. BitVector_Empty(X2);
  2870. *X1 = 1;
  2871. BitVector_Empty(Y1);
  2872. BitVector_Empty(Y2);
  2873. *Y2 = 1;
  2874. sgn_x = false;
  2875. sgn_y = false;
  2876. while (not error)
  2877. {
  2878. if ((error = BitVector_Div_Pos(Q,A,B,R)))
  2879. {
  2880. break;
  2881. }
  2882. if (BitVector_is_empty(R))
  2883. {
  2884. break;
  2885. }
  2886. sgn_q = sgn_a XOR sgn_b;
  2887. if (sgn_x) BitVector_Negate(Z,X2); else BitVector_Copy(Z,X2);
  2888. if ((error = BitVector_Mul_Pos(X3,Z,Q,true)))
  2889. {
  2890. break;
  2891. }
  2892. minus = not (sgn_x XOR sgn_q);
  2893. carry = 0;
  2894. if (BitVector_compute(X3,X1,X3,minus,&carry))
  2895. {
  2896. error = ErrCode_Ovfl;
  2897. break;
  2898. }
  2899. sgn_x = (((*(X3+size) &= mask) AND msb) != 0);
  2900. if (sgn_y) BitVector_Negate(Z,Y2); else BitVector_Copy(Z,Y2);
  2901. if ((error = BitVector_Mul_Pos(Y3,Z,Q,true)))
  2902. {
  2903. break;
  2904. }
  2905. minus = not (sgn_y XOR sgn_q);
  2906. carry = 0;
  2907. if (BitVector_compute(Y3,Y1,Y3,minus,&carry))
  2908. {
  2909. error = ErrCode_Ovfl;
  2910. break;
  2911. }
  2912. sgn_y = (((*(Y3+size) &= mask) AND msb) != 0);
  2913. T = A; sgn_r = sgn_a;
  2914. A = B; sgn_a = sgn_b;
  2915. B = R; sgn_b = sgn_r;
  2916. R = T;
  2917. T = X1;
  2918. X1 = X2;
  2919. X2 = X3;
  2920. X3 = T;
  2921. T = Y1;
  2922. Y1 = Y2;
  2923. Y2 = Y3;
  2924. Y3 = T;
  2925. }
  2926. if (not error)
  2927. {
  2928. if (sgn_b) BitVector_Negate(U,B); else BitVector_Copy(U,B);
  2929. BitVector_Copy(V,X2);
  2930. BitVector_Copy(W,Y2);
  2931. }
  2932. BitVector_Destroy_List(L,11);
  2933. return(error);
  2934. }
  2935. ErrCode BitVector_Power(wordptr X, wordptr Y, wordptr Z)
  2936. {
  2937. ErrCode error = ErrCode_Ok;
  2938. N_word bits = bits_(X);
  2939. boolean first = TRUE;
  2940. Z_long last;
  2941. N_word limit;
  2942. N_word count;
  2943. wordptr T;
  2944. /*
  2945. Requirements:
  2946. - X must have at least the same size as Y but may be larger (!)
  2947. - X may not be identical with Z
  2948. - Z must be positive
  2949. Features:
  2950. - The contents of Y and Z are preserved
  2951. */
  2952. if (X == Z) return(ErrCode_Same);
  2953. if (bits < bits_(Y)) return(ErrCode_Size);
  2954. if (BitVector_msb_(Z)) return(ErrCode_Expo);
  2955. if ((last = Set_Max(Z)) < 0L)
  2956. {
  2957. if (bits < 2) return(ErrCode_Ovfl);
  2958. BitVector_Empty(X);
  2959. *X |= LSB;
  2960. return(ErrCode_Ok); /* anything ^ 0 == 1 */
  2961. }
  2962. if (BitVector_is_empty(Y))
  2963. {
  2964. if (X != Y) BitVector_Empty(X);
  2965. return(ErrCode_Ok); /* 0 ^ anything not zero == 0 */
  2966. }
  2967. T = BitVector_Create(bits,FALSE);
  2968. if (T == NULL) return(ErrCode_Null);
  2969. limit = (N_word) last;
  2970. for ( count = 0; ((!error) and (count <= limit)); count++ )
  2971. {
  2972. if ( BIT_VECTOR_TST_BIT(Z,count) )
  2973. {
  2974. if (first)
  2975. {
  2976. first = FALSE;
  2977. if (count) { BitVector_Copy(X,T); }
  2978. else { if (X != Y) BitVector_Copy(X,Y); }
  2979. }
  2980. else error = BitVector_Multiply(X,T,X); /* order important because T > X */
  2981. }
  2982. if ((!error) and (count < limit))
  2983. {
  2984. if (count) error = BitVector_Multiply(T,T,T);
  2985. else error = BitVector_Multiply(T,Y,Y);
  2986. }
  2987. }
  2988. BitVector_Destroy(T);
  2989. return(error);
  2990. }
  2991. void BitVector_Block_Store(wordptr addr, charptr buffer, N_int length)
  2992. {
  2993. N_word size = size_(addr);
  2994. N_word mask = mask_(addr);
  2995. N_word value;
  2996. N_word count;
  2997. /* provide translation for independence of endian-ness: */
  2998. if (size > 0)
  2999. {
  3000. while (size-- > 0)
  3001. {
  3002. value = 0;
  3003. for ( count = 0; (length > 0) and (count < BITS); count += 8 )
  3004. {
  3005. value |= (((N_word) *buffer++) << count); length--;
  3006. }
  3007. *addr++ = value;
  3008. }
  3009. *(--addr) &= mask;
  3010. }
  3011. }
  3012. charptr BitVector_Block_Read(wordptr addr, N_intptr length)
  3013. {
  3014. N_word size = size_(addr);
  3015. N_word value;
  3016. N_word count;
  3017. charptr buffer;
  3018. charptr target;
  3019. /* provide translation for independence of endian-ness: */
  3020. *length = size << FACTOR;
  3021. buffer = (charptr) yasm_xmalloc((size_t) ((*length)+1));
  3022. if (buffer == NULL) return(NULL);
  3023. target = buffer;
  3024. if (size > 0)
  3025. {
  3026. *(addr+size-1) &= mask_(addr);
  3027. while (size-- > 0)
  3028. {
  3029. value = *addr++;
  3030. count = BITS >> 3;
  3031. while (count-- > 0)
  3032. {
  3033. *target++ = (N_char) (value AND 0x00FF);
  3034. if (count > 0) value >>= 8;
  3035. }
  3036. }
  3037. }
  3038. *target = (N_char) '\0';
  3039. return(buffer);
  3040. }
  3041. void BitVector_Word_Store(wordptr addr, N_int offset, N_int value)
  3042. {
  3043. N_word size = size_(addr);
  3044. if (size > 0)
  3045. {
  3046. if (offset < size) *(addr+offset) = value;
  3047. *(addr+size-1) &= mask_(addr);
  3048. }
  3049. }
  3050. N_int BitVector_Word_Read(wordptr addr, N_int offset)
  3051. {
  3052. N_word size = size_(addr);
  3053. if (size > 0)
  3054. {
  3055. *(addr+size-1) &= mask_(addr);
  3056. if (offset < size) return( *(addr+offset) );
  3057. }
  3058. return( (N_int) 0 );
  3059. }
  3060. void BitVector_Word_Insert(wordptr addr, N_int offset, N_int count,
  3061. boolean clear)
  3062. {
  3063. N_word size = size_(addr);
  3064. N_word mask = mask_(addr);
  3065. wordptr last = addr+size-1;
  3066. if (size > 0)
  3067. {
  3068. *last &= mask;
  3069. if (offset > size) offset = size;
  3070. BIT_VECTOR_ins_words(addr+offset,size-offset,count,clear);
  3071. *last &= mask;
  3072. }
  3073. }
  3074. void BitVector_Word_Delete(wordptr addr, N_int offset, N_int count,
  3075. boolean clear)
  3076. {
  3077. N_word size = size_(addr);
  3078. N_word mask = mask_(addr);
  3079. wordptr last = addr+size-1;
  3080. if (size > 0)
  3081. {
  3082. *last &= mask;
  3083. if (offset > size) offset = size;
  3084. BIT_VECTOR_del_words(addr+offset,size-offset,count,clear);
  3085. *last &= mask;
  3086. }
  3087. }
  3088. void BitVector_Chunk_Store(wordptr addr, N_int chunksize, N_int offset,
  3089. N_long value)
  3090. {
  3091. N_word bits = bits_(addr);
  3092. N_word mask;
  3093. N_word temp;
  3094. if ((chunksize > 0) and (offset < bits))
  3095. {
  3096. if (chunksize > LONGBITS) chunksize = LONGBITS;
  3097. if ((offset + chunksize) > bits) chunksize = bits - offset;
  3098. addr += offset >> LOGBITS;
  3099. offset &= MODMASK;
  3100. while (chunksize > 0)
  3101. {
  3102. mask = (N_word) (~0L << offset);
  3103. bits = offset + chunksize;
  3104. if (bits < BITS)
  3105. {
  3106. mask &= (N_word) ~(~0L << bits);
  3107. bits = chunksize;
  3108. }
  3109. else bits = BITS - offset;
  3110. temp = (N_word) (value << offset);
  3111. temp &= mask;
  3112. *addr &= NOT mask;
  3113. *addr++ |= temp;
  3114. value >>= bits;
  3115. chunksize -= bits;
  3116. offset = 0;
  3117. }
  3118. }
  3119. }
  3120. N_long BitVector_Chunk_Read(wordptr addr, N_int chunksize, N_int offset)
  3121. {
  3122. N_word bits = bits_(addr);
  3123. N_word chunkbits = 0;
  3124. N_long value = 0L;
  3125. N_long temp;
  3126. N_word mask;
  3127. if ((chunksize > 0) and (offset < bits))
  3128. {
  3129. if (chunksize > LONGBITS) chunksize = LONGBITS;
  3130. if ((offset + chunksize) > bits) chunksize = bits - offset;
  3131. addr += offset >> LOGBITS;
  3132. offset &= MODMASK;
  3133. while (chunksize > 0)
  3134. {
  3135. bits = offset + chunksize;
  3136. if (bits < BITS)
  3137. {
  3138. mask = (N_word) ~(~0L << bits);
  3139. bits = chunksize;
  3140. }
  3141. else
  3142. {
  3143. mask = (N_word) ~0L;
  3144. bits = BITS - offset;
  3145. }
  3146. temp = (N_long) ((*addr++ AND mask) >> offset);
  3147. value |= temp << chunkbits;
  3148. chunkbits += bits;
  3149. chunksize -= bits;
  3150. offset = 0;
  3151. }
  3152. }
  3153. return(value);
  3154. }
  3155. /*******************/
  3156. /* set operations: */
  3157. /*******************/
  3158. void Set_Union(wordptr X, wordptr Y, wordptr Z) /* X = Y + Z */
  3159. {
  3160. N_word bits = bits_(X);
  3161. N_word size = size_(X);
  3162. N_word mask = mask_(X);
  3163. if ((size > 0) and (bits == bits_(Y)) and (bits == bits_(Z)))
  3164. {
  3165. while (size-- > 0) *X++ = *Y++ OR *Z++;
  3166. *(--X) &= mask;
  3167. }
  3168. }
  3169. void Set_Intersection(wordptr X, wordptr Y, wordptr Z) /* X = Y * Z */
  3170. {
  3171. N_word bits = bits_(X);
  3172. N_word size = size_(X);
  3173. N_word mask = mask_(X);
  3174. if ((size > 0) and (bits == bits_(Y)) and (bits == bits_(Z)))
  3175. {
  3176. while (size-- > 0) *X++ = *Y++ AND *Z++;
  3177. *(--X) &= mask;
  3178. }
  3179. }
  3180. void Set_Difference(wordptr X, wordptr Y, wordptr Z) /* X = Y \ Z */
  3181. {
  3182. N_word bits = bits_(X);
  3183. N_word size = size_(X);
  3184. N_word mask = mask_(X);
  3185. if ((size > 0) and (bits == bits_(Y)) and (bits == bits_(Z)))
  3186. {
  3187. while (size-- > 0) *X++ = *Y++ AND NOT *Z++;
  3188. *(--X) &= mask;
  3189. }
  3190. }
  3191. void Set_ExclusiveOr(wordptr X, wordptr Y, wordptr Z) /* X=(Y+Z)\(Y*Z) */
  3192. {
  3193. N_word bits = bits_(X);
  3194. N_word size = size_(X);
  3195. N_word mask = mask_(X);
  3196. if ((size > 0) and (bits == bits_(Y)) and (bits == bits_(Z)))
  3197. {
  3198. while (size-- > 0) *X++ = *Y++ XOR *Z++;
  3199. *(--X) &= mask;
  3200. }
  3201. }
  3202. void Set_Complement(wordptr X, wordptr Y) /* X = ~Y */
  3203. {
  3204. N_word size = size_(X);
  3205. N_word mask = mask_(X);
  3206. if ((size > 0) and (bits_(X) == bits_(Y)))
  3207. {
  3208. while (size-- > 0) *X++ = NOT *Y++;
  3209. *(--X) &= mask;
  3210. }
  3211. }
  3212. /******************/
  3213. /* set functions: */
  3214. /******************/
  3215. boolean Set_subset(wordptr X, wordptr Y) /* X subset Y ? */
  3216. {
  3217. N_word size = size_(X);
  3218. boolean r = FALSE;
  3219. if ((size > 0) and (bits_(X) == bits_(Y)))
  3220. {
  3221. r = TRUE;
  3222. while (r and (size-- > 0)) r = ((*X++ AND NOT *Y++) == 0);
  3223. }
  3224. return(r);
  3225. }
  3226. N_int Set_Norm(wordptr addr) /* = | X | */
  3227. {
  3228. byteptr byte;
  3229. N_word bytes;
  3230. N_int n;
  3231. byte = (byteptr) addr;
  3232. bytes = size_(addr) << FACTOR;
  3233. n = 0;
  3234. while (bytes-- > 0)
  3235. {
  3236. n += BitVector_BYTENORM[*byte++];
  3237. }
  3238. return(n);
  3239. }
  3240. N_int Set_Norm2(wordptr addr) /* = | X | */
  3241. {
  3242. N_word size = size_(addr);
  3243. N_word w0,w1;
  3244. N_int n,k;
  3245. n = 0;
  3246. while (size-- > 0)
  3247. {
  3248. k = 0;
  3249. w1 = NOT (w0 = *addr++);
  3250. while (w0 and w1)
  3251. {
  3252. w0 &= w0 - 1;
  3253. w1 &= w1 - 1;
  3254. k++;
  3255. }
  3256. if (w0 == 0) n += k;
  3257. else n += BITS - k;
  3258. }
  3259. return(n);
  3260. }
  3261. N_int Set_Norm3(wordptr addr) /* = | X | */
  3262. {
  3263. N_word size = size_(addr);
  3264. N_int count = 0;
  3265. N_word c;
  3266. while (size-- > 0)
  3267. {
  3268. c = *addr++;
  3269. while (c)
  3270. {
  3271. c &= c - 1;
  3272. count++;
  3273. }
  3274. }
  3275. return(count);
  3276. }
  3277. Z_long Set_Min(wordptr addr) /* = min(X) */
  3278. {
  3279. boolean empty = TRUE;
  3280. N_word size = size_(addr);
  3281. N_word i = 0;
  3282. N_word c = 0; /* silence compiler warning */
  3283. while (empty and (size-- > 0))
  3284. {
  3285. if ((c = *addr++)) empty = false; else i++;
  3286. }
  3287. if (empty) return((Z_long) LONG_MAX); /* plus infinity */
  3288. i <<= LOGBITS;
  3289. while (not (c AND LSB))
  3290. {
  3291. c >>= 1;
  3292. i++;
  3293. }
  3294. return((Z_long) i);
  3295. }
  3296. Z_long Set_Max(wordptr addr) /* = max(X) */
  3297. {
  3298. boolean empty = TRUE;
  3299. N_word size = size_(addr);
  3300. N_word i = size;
  3301. N_word c = 0; /* silence compiler warning */
  3302. addr += size-1;
  3303. while (empty and (size-- > 0))
  3304. {
  3305. if ((c = *addr--)) empty = false; else i--;
  3306. }
  3307. if (empty) return((Z_long) LONG_MIN); /* minus infinity */
  3308. i <<= LOGBITS;
  3309. while (not (c AND MSB))
  3310. {
  3311. c <<= 1;
  3312. i--;
  3313. }
  3314. return((Z_long) --i);
  3315. }
  3316. /**********************************/
  3317. /* matrix-of-booleans operations: */
  3318. /**********************************/
  3319. void Matrix_Multiplication(wordptr X, N_int rowsX, N_int colsX,
  3320. wordptr Y, N_int rowsY, N_int colsY,
  3321. wordptr Z, N_int rowsZ, N_int colsZ)
  3322. {
  3323. N_word i;
  3324. N_word j;
  3325. N_word k;
  3326. N_word indxX;
  3327. N_word indxY;
  3328. N_word indxZ;
  3329. N_word termX;
  3330. N_word termY;
  3331. N_word sum;
  3332. if ((colsY == rowsZ) and (rowsX == rowsY) and (colsX == colsZ) and
  3333. (bits_(X) == rowsX*colsX) and
  3334. (bits_(Y) == rowsY*colsY) and
  3335. (bits_(Z) == rowsZ*colsZ))
  3336. {
  3337. for ( i = 0; i < rowsY; i++ )
  3338. {
  3339. termX = i * colsX;
  3340. termY = i * colsY;
  3341. for ( j = 0; j < colsZ; j++ )
  3342. {
  3343. indxX = termX + j;
  3344. sum = 0;
  3345. for ( k = 0; k < colsY; k++ )
  3346. {
  3347. indxY = termY + k;
  3348. indxZ = k * colsZ + j;
  3349. if ( BIT_VECTOR_TST_BIT(Y,indxY) &&
  3350. BIT_VECTOR_TST_BIT(Z,indxZ) ) sum ^= 1;
  3351. }
  3352. if (sum) BIT_VECTOR_SET_BIT(X,indxX)
  3353. else BIT_VECTOR_CLR_BIT(X,indxX)
  3354. }
  3355. }
  3356. }
  3357. }
  3358. void Matrix_Product(wordptr X, N_int rowsX, N_int colsX,
  3359. wordptr Y, N_int rowsY, N_int colsY,
  3360. wordptr Z, N_int rowsZ, N_int colsZ)
  3361. {
  3362. N_word i;
  3363. N_word j;
  3364. N_word k;
  3365. N_word indxX;
  3366. N_word indxY;
  3367. N_word indxZ;
  3368. N_word termX;
  3369. N_word termY;
  3370. N_word sum;
  3371. if ((colsY == rowsZ) and (rowsX == rowsY) and (colsX == colsZ) and
  3372. (bits_(X) == rowsX*colsX) and
  3373. (bits_(Y) == rowsY*colsY) and
  3374. (bits_(Z) == rowsZ*colsZ))
  3375. {
  3376. for ( i = 0; i < rowsY; i++ )
  3377. {
  3378. termX = i * colsX;
  3379. termY = i * colsY;
  3380. for ( j = 0; j < colsZ; j++ )
  3381. {
  3382. indxX = termX + j;
  3383. sum = 0;
  3384. for ( k = 0; k < colsY; k++ )
  3385. {
  3386. indxY = termY + k;
  3387. indxZ = k * colsZ + j;
  3388. if ( BIT_VECTOR_TST_BIT(Y,indxY) &&
  3389. BIT_VECTOR_TST_BIT(Z,indxZ) ) sum |= 1;
  3390. }
  3391. if (sum) BIT_VECTOR_SET_BIT(X,indxX)
  3392. else BIT_VECTOR_CLR_BIT(X,indxX)
  3393. }
  3394. }
  3395. }
  3396. }
  3397. void Matrix_Closure(wordptr addr, N_int rows, N_int cols)
  3398. {
  3399. N_word i;
  3400. N_word j;
  3401. N_word k;
  3402. N_word ii;
  3403. N_word ij;
  3404. N_word ik;
  3405. N_word kj;
  3406. N_word termi;
  3407. N_word termk;
  3408. if ((rows == cols) and (bits_(addr) == rows*cols))
  3409. {
  3410. for ( i = 0; i < rows; i++ )
  3411. {
  3412. ii = i * cols + i;
  3413. BIT_VECTOR_SET_BIT(addr,ii)
  3414. }
  3415. for ( k = 0; k < rows; k++ )
  3416. {
  3417. termk = k * cols;
  3418. for ( i = 0; i < rows; i++ )
  3419. {
  3420. termi = i * cols;
  3421. ik = termi + k;
  3422. for ( j = 0; j < rows; j++ )
  3423. {
  3424. ij = termi + j;
  3425. kj = termk + j;
  3426. if ( BIT_VECTOR_TST_BIT(addr,ik) &&
  3427. BIT_VECTOR_TST_BIT(addr,kj) )
  3428. BIT_VECTOR_SET_BIT(addr,ij)
  3429. }
  3430. }
  3431. }
  3432. }
  3433. }
  3434. void Matrix_Transpose(wordptr X, N_int rowsX, N_int colsX,
  3435. wordptr Y, N_int rowsY, N_int colsY)
  3436. {
  3437. N_word i;
  3438. N_word j;
  3439. N_word ii;
  3440. N_word ij;
  3441. N_word ji;
  3442. N_word addii;
  3443. N_word addij;
  3444. N_word addji;
  3445. N_word bitii;
  3446. N_word bitij;
  3447. N_word bitji;
  3448. N_word termi;
  3449. N_word termj;
  3450. boolean swap;
  3451. /* BEWARE that "in-place" is ONLY possible if the matrix is quadratic!! */
  3452. if ((rowsX == colsY) and (colsX == rowsY) and
  3453. (bits_(X) == rowsX*colsX) and
  3454. (bits_(Y) == rowsY*colsY))
  3455. {
  3456. if (rowsY == colsY) /* in-place is possible! */
  3457. {
  3458. for ( i = 0; i < rowsY; i++ )
  3459. {
  3460. termi = i * colsY;
  3461. for ( j = 0; j < i; j++ )
  3462. {
  3463. termj = j * colsX;
  3464. ij = termi + j;
  3465. ji = termj + i;
  3466. addij = ij >> LOGBITS;
  3467. addji = ji >> LOGBITS;
  3468. bitij = BITMASKTAB[ij AND MODMASK];
  3469. bitji = BITMASKTAB[ji AND MODMASK];
  3470. swap = ((*(Y+addij) AND bitij) != 0);
  3471. if ((*(Y+addji) AND bitji) != 0)
  3472. *(X+addij) |= bitij;
  3473. else
  3474. *(X+addij) &= NOT bitij;
  3475. if (swap)
  3476. *(X+addji) |= bitji;
  3477. else
  3478. *(X+addji) &= NOT bitji;
  3479. }
  3480. ii = termi + i;
  3481. addii = ii >> LOGBITS;
  3482. bitii = BITMASKTAB[ii AND MODMASK];
  3483. if ((*(Y+addii) AND bitii) != 0)
  3484. *(X+addii) |= bitii;
  3485. else
  3486. *(X+addii) &= NOT bitii;
  3487. }
  3488. }
  3489. else /* rowsX != colsX, in-place is NOT possible! */
  3490. {
  3491. for ( i = 0; i < rowsY; i++ )
  3492. {
  3493. termi = i * colsY;
  3494. for ( j = 0; j < colsY; j++ )
  3495. {
  3496. termj = j * colsX;
  3497. ij = termi + j;
  3498. ji = termj + i;
  3499. addij = ij >> LOGBITS;
  3500. addji = ji >> LOGBITS;
  3501. bitij = BITMASKTAB[ij AND MODMASK];
  3502. bitji = BITMASKTAB[ji AND MODMASK];
  3503. if ((*(Y+addij) AND bitij) != 0)
  3504. *(X+addji) |= bitji;
  3505. else
  3506. *(X+addji) &= NOT bitji;
  3507. }
  3508. }
  3509. }
  3510. }
  3511. }
  3512. /*****************************************************************************/
  3513. /* VERSION: 6.4 */
  3514. /*****************************************************************************/
  3515. /* VERSION HISTORY: */
  3516. /*****************************************************************************/
  3517. /* */
  3518. /* Version 6.4 03.10.04 Added C++ comp. directives. Improved "Norm()". */
  3519. /* Version 6.3 28.09.02 Added "Create_List()" and "GCD2()". */
  3520. /* Version 6.2 15.09.02 Overhauled error handling. Fixed "GCD()". */
  3521. /* Version 6.1 08.10.01 Make VMS linker happy: _lsb,_msb => _lsb_,_msb_ */
  3522. /* Version 6.0 08.10.00 Corrected overflow handling. */
  3523. /* Version 5.8 14.07.00 Added "Power()". Changed "Copy()". */
  3524. /* Version 5.7 19.05.99 Quickened "Div_Pos()". Added "Product()". */
  3525. /* Version 5.6 02.11.98 Leading zeros eliminated in "to_Hex()". */
  3526. /* Version 5.5 21.09.98 Fixed bug of uninitialized "error" in Multiply. */
  3527. /* Version 5.4 07.09.98 Fixed bug of uninitialized "error" in Divide. */
  3528. /* Version 5.3 12.05.98 Improved Norm. Completed history. */
  3529. /* Version 5.2 31.03.98 Improved Norm. */
  3530. /* Version 5.1 09.03.98 No changes. */
  3531. /* Version 5.0 01.03.98 Major additions and rewrite. */
  3532. /* Version 4.2 16.07.97 Added is_empty, is_full. */
  3533. /* Version 4.1 30.06.97 Added word-ins/del, move-left/right, inc/dec. */
  3534. /* Version 4.0 23.04.97 Rewrite. Added bit shift and bool. matrix ops. */
  3535. /* Version 3.2 04.02.97 Added interval methods. */
  3536. /* Version 3.1 21.01.97 Fixed bug on 64 bit machines. */
  3537. /* Version 3.0 12.01.97 Added flip. */
  3538. /* Version 2.0 14.12.96 Efficiency and consistency improvements. */
  3539. /* Version 1.1 08.01.96 Added Resize and ExclusiveOr. */
  3540. /* Version 1.0 14.12.95 First version under UNIX (with Perl module). */
  3541. /* Version 0.9 01.11.93 First version of C library under MS-DOS. */
  3542. /* Version 0.1 ??.??.89 First version in Turbo Pascal under CP/M. */
  3543. /* */
  3544. /*****************************************************************************/
  3545. /* AUTHOR: */
  3546. /*****************************************************************************/
  3547. /* */
  3548. /* Steffen Beyer */
  3549. /* mailto:sb@engelschall.com */
  3550. /* http://www.engelschall.com/u/sb/download/ */
  3551. /* */
  3552. /*****************************************************************************/
  3553. /* COPYRIGHT: */
  3554. /*****************************************************************************/
  3555. /* */
  3556. /* Copyright (c) 1995 - 2004 by Steffen Beyer. */
  3557. /* All rights reserved. */
  3558. /* */
  3559. /*****************************************************************************/
  3560. /* LICENSE: */
  3561. /*****************************************************************************/
  3562. /* This package is free software; you can use, modify and redistribute */
  3563. /* it under the same terms as Perl itself, i.e., under the terms of */
  3564. /* the "Artistic License" or the "GNU General Public License". */
  3565. /* */
  3566. /* The C library at the core of this Perl module can additionally */
  3567. /* be used, modified and redistributed under the terms of the */
  3568. /* "GNU Library General Public License". */
  3569. /* */
  3570. /*****************************************************************************/
  3571. /* ARTISTIC LICENSE: */
  3572. /*****************************************************************************/
  3573. /*
  3574. The "Artistic License"
  3575. Preamble
  3576. The intent of this document is to state the conditions under which a
  3577. Package may be copied, such that the Copyright Holder maintains some
  3578. semblance of artistic control over the development of the package,
  3579. while giving the users of the package the right to use and distribute
  3580. the Package in a more-or-less customary fashion, plus the right to make
  3581. reasonable modifications.
  3582. Definitions:
  3583. "Package" refers to the collection of files distributed by the
  3584. Copyright Holder, and derivatives of that collection of files
  3585. created through textual modification.
  3586. "Standard Version" refers to such a Package if it has not been
  3587. modified, or has been modified in accordance with the wishes
  3588. of the Copyright Holder as specified below.
  3589. "Copyright Holder" is whoever is named in the copyright or
  3590. copyrights for the package.
  3591. "You" is you, if you're thinking about copying or distributing
  3592. this Package.
  3593. "Reasonable copying fee" is whatever you can justify on the
  3594. basis of media cost, duplication charges, time of people involved,
  3595. and so on. (You will not be required to justify it to the
  3596. Copyright Holder, but only to the computing community at large
  3597. as a market that must bear the fee.)
  3598. "Freely Available" means that no fee is charged for the item
  3599. itself, though there may be fees involved in handling the item.
  3600. It also means that recipients of the item may redistribute it
  3601. under the same conditions they received it.
  3602. 1. You may make and give away verbatim copies of the source form of the
  3603. Standard Version of this Package without restriction, provided that you
  3604. duplicate all of the original copyright notices and associated disclaimers.
  3605. 2. You may apply bug fixes, portability fixes and other modifications
  3606. derived from the Public Domain or from the Copyright Holder. A Package
  3607. modified in such a way shall still be considered the Standard Version.
  3608. 3. You may otherwise modify your copy of this Package in any way, provided
  3609. that you insert a prominent notice in each changed file stating how and
  3610. when you changed that file, and provided that you do at least ONE of the
  3611. following:
  3612. a) place your modifications in the Public Domain or otherwise make them
  3613. Freely Available, such as by posting said modifications to Usenet or
  3614. an equivalent medium, or placing the modifications on a major archive
  3615. site such as uunet.uu.net, or by allowing the Copyright Holder to include
  3616. your modifications in the Standard Version of the Package.
  3617. b) use the modified Package only within your corporation or organization.
  3618. c) rename any non-standard executables so the names do not conflict
  3619. with standard executables, which must also be provided, and provide
  3620. a separate manual page for each non-standard executable that clearly
  3621. documents how it differs from the Standard Version.
  3622. d) make other distribution arrangements with the Copyright Holder.
  3623. 4. You may distribute the programs of this Package in object code or
  3624. executable form, provided that you do at least ONE of the following:
  3625. a) distribute a Standard Version of the executables and library files,
  3626. together with instructions (in the manual page or equivalent) on where
  3627. to get the Standard Version.
  3628. b) accompany the distribution with the machine-readable source of
  3629. the Package with your modifications.
  3630. c) give non-standard executables non-standard names, and clearly
  3631. document the differences in manual pages (or equivalent), together
  3632. with instructions on where to get the Standard Version.
  3633. d) make other distribution arrangements with the Copyright Holder.
  3634. 5. You may charge a reasonable copying fee for any distribution of this
  3635. Package. You may charge any fee you choose for support of this
  3636. Package. You may not charge a fee for this Package itself. However,
  3637. you may distribute this Package in aggregate with other (possibly
  3638. commercial) programs as part of a larger (possibly commercial) software
  3639. distribution provided that you do not advertise this Package as a
  3640. product of your own. You may embed this Package's interpreter within
  3641. an executable of yours (by linking); this shall be construed as a mere
  3642. form of aggregation, provided that the complete Standard Version of the
  3643. interpreter is so embedded.
  3644. 6. The scripts and library files supplied as input to or produced as
  3645. output from the programs of this Package do not automatically fall
  3646. under the copyright of this Package, but belong to whoever generated
  3647. them, and may be sold commercially, and may be aggregated with this
  3648. Package. If such scripts or library files are aggregated with this
  3649. Package via the so-called "undump" or "unexec" methods of producing a
  3650. binary executable image, then distribution of such an image shall
  3651. neither be construed as a distribution of this Package nor shall it
  3652. fall under the restrictions of Paragraphs 3 and 4, provided that you do
  3653. not represent such an executable image as a Standard Version of this
  3654. Package.
  3655. 7. C subroutines (or comparably compiled subroutines in other
  3656. languages) supplied by you and linked into this Package in order to
  3657. emulate subroutines and variables of the language defined by this
  3658. Package shall not be considered part of this Package, but are the
  3659. equivalent of input as in Paragraph 6, provided these subroutines do
  3660. not change the language in any way that would cause it to fail the
  3661. regression tests for the language.
  3662. 8. Aggregation of this Package with a commercial distribution is always
  3663. permitted provided that the use of this Package is embedded; that is,
  3664. when no overt attempt is made to make this Package's interfaces visible
  3665. to the end user of the commercial distribution. Such use shall not be
  3666. construed as a distribution of this Package.
  3667. 9. The name of the Copyright Holder may not be used to endorse or promote
  3668. products derived from this software without specific prior written permission.
  3669. 10. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
  3670. IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  3671. WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  3672. The End
  3673. */
  3674. /*****************************************************************************/
  3675. /* GNU GENERAL PUBLIC LICENSE: */
  3676. /*****************************************************************************/
  3677. /* This program is free software; you can redistribute it and/or */
  3678. /* modify it under the terms of the GNU General Public License */
  3679. /* as published by the Free Software Foundation; either version 2 */
  3680. /* of the License, or (at your option) any later version. */
  3681. /* */
  3682. /* This program is distributed in the hope that it will be useful, */
  3683. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  3684. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  3685. /* GNU General Public License for more details. */
  3686. /* */
  3687. /* You should have received a copy of the GNU General Public License */
  3688. /* along with this program; if not, write to the */
  3689. /* Free Software Foundation, Inc., */
  3690. /* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  3691. /* */
  3692. /*****************************************************************************/
  3693. /* GNU LIBRARY GENERAL PUBLIC LICENSE: */
  3694. /*****************************************************************************/
  3695. /* This library is free software; you can redistribute it and/or */
  3696. /* modify it under the terms of the GNU Library General Public */
  3697. /* License as published by the Free Software Foundation; either */
  3698. /* version 2 of the License, or (at your option) any later version. */
  3699. /* */
  3700. /* This library is distributed in the hope that it will be useful, */
  3701. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  3702. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
  3703. /* Library General Public License for more details. */
  3704. /* */
  3705. /* You should have received a copy of the GNU Library General Public */
  3706. /* License along with this library; if not, write to the */
  3707. /* Free Software Foundation, Inc., */
  3708. /* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  3709. /* */
  3710. /* or download a copy from ftp://ftp.gnu.org/pub/gnu/COPYING.LIB-2.0 */
  3711. /* */
  3712. /*****************************************************************************/