ncx.c 194 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385
  1. /* Do not edit this file. It is produced from the corresponding .m4 source */
  2. /*
  3. * Copyright 1996, University Corporation for Atmospheric Research
  4. * See netcdf/COPYRIGHT file for copying and redistribution conditions.
  5. *
  6. * This file contains some routines derived from code
  7. * which is copyrighted by Sun Microsystems, Inc.
  8. * The "#ifdef vax" versions of
  9. * ncx_put_float_float()
  10. * ncx_get_float_float()
  11. * ncx_put_double_double()
  12. * ncx_get_double_double()
  13. * ncx_putn_float_float()
  14. * ncx_getn_float_float()
  15. * ncx_putn_double_double()
  16. * ncx_getn_double_double()
  17. * are derived from xdr_float() and xdr_double() routines
  18. * in the freely available, copyrighted Sun RPCSRC 3.9
  19. * distribution, xdr_float.c.
  20. * Our "value added" is that these are always memory to memory,
  21. * they handle IEEE subnormals properly, and their "n" versions
  22. * operate speedily on arrays.
  23. */
  24. /* $Id: ncx.m4,v 2.58 2010/05/26 18:11:08 dmh Exp $ */
  25. /*
  26. * An external data representation interface.
  27. */
  28. #include "ncx.h"
  29. #include "nc3dispatch.h"
  30. #include <string.h>
  31. #include <limits.h>
  32. /* alias poorly named limits.h macros */
  33. #define SHORT_MAX SHRT_MAX
  34. #define SHORT_MIN SHRT_MIN
  35. #define USHORT_MAX USHRT_MAX
  36. #ifndef LLONG_MAX
  37. # define LLONG_MAX 9223372036854775807LL
  38. # define LLONG_MIN (-LLONG_MAX - 1LL)
  39. # define ULLONG_MAX 18446744073709551615ULL
  40. #endif
  41. #define LONG_LONG_MAX LLONG_MAX
  42. #define LONG_LONG_MIN LLONG_MIN
  43. #define ULONG_LONG_MAX ULLONG_MAX
  44. #include <float.h>
  45. #ifndef FLT_MAX /* This POSIX macro missing on some systems */
  46. # ifndef NO_IEEE_FLOAT
  47. # define FLT_MAX 3.40282347e+38f
  48. # else
  49. # error "You will need to define FLT_MAX"
  50. # endif
  51. #endif
  52. /* alias poorly named float.h macros */
  53. #define FLOAT_MAX FLT_MAX
  54. #define FLOAT_MIN (-FLT_MAX)
  55. #define DOUBLE_MAX DBL_MAX
  56. #define DOUBLE_MIN (-DBL_MAX)
  57. #define FLOAT_MAX_EXP FLT_MAX_EXP
  58. #define DOUBLE_MAX_EXP DBL_MAX_EXP
  59. #include <assert.h>
  60. #define UCHAR_MIN 0
  61. #define Min(a,b) ((a) < (b) ? (a) : (b))
  62. #define Max(a,b) ((a) > (b) ? (a) : (b))
  63. /*
  64. * If the machine's float domain is "smaller" than the external one
  65. * use the machine domain
  66. */
  67. #if defined(FLT_MAX_EXP) && FLT_MAX_EXP < 128 /* 128 is X_FLT_MAX_EXP */
  68. #undef X_FLOAT_MAX
  69. # define X_FLOAT_MAX FLT_MAX
  70. #undef X_FLOAT_MIN
  71. # define X_FLOAT_MIN (-X_FLOAT_MAX)
  72. #endif
  73. #if _SX /* NEC SUPER UX */
  74. #define LOOPCNT 256 /* must be no longer than hardware vector length */
  75. #if _INT64
  76. #undef INT_MAX /* workaround cpp bug */
  77. #define INT_MAX X_INT_MAX
  78. #undef INT_MIN /* workaround cpp bug */
  79. #define INT_MIN X_INT_MIN
  80. #undef LONG_MAX /* workaround cpp bug */
  81. #define LONG_MAX X_INT_MAX
  82. #undef LONG_MIN /* workaround cpp bug */
  83. #define LONG_MIN X_INT_MIN
  84. #elif _LONG64
  85. #undef LONG_MAX /* workaround cpp bug */
  86. #define LONG_MAX 4294967295L
  87. #undef LONG_MIN /* workaround cpp bug */
  88. #define LONG_MIN -4294967295L
  89. #endif
  90. #if !_FLOAT0
  91. #error "FLOAT1 and FLOAT2 not supported"
  92. #endif
  93. #endif /* _SX */
  94. static const char nada[X_ALIGN] = {0, 0, 0, 0};
  95. #ifndef WORDS_BIGENDIAN
  96. /* LITTLE_ENDIAN: DEC and intel */
  97. /*
  98. * Routines to convert to BIGENDIAN.
  99. * Optimize the swapn?b() and swap?b() routines aggressivly.
  100. */
  101. #define SWAP2(a) ( (((a) & 0xff) << 8) | \
  102. (((a) >> 8) & 0xff) )
  103. #define SWAP4(a) ( ((a) << 24) | \
  104. (((a) << 8) & 0x00ff0000) | \
  105. (((a) >> 8) & 0x0000ff00) | \
  106. (((a) >> 24) & 0x000000ff) )
  107. static void
  108. swapn2b(void *dst, const void *src, size_t nn)
  109. {
  110. char *op = dst;
  111. const char *ip = src;
  112. /* unroll the following to reduce loop overhead
  113. *
  114. * while(nn-- != 0)
  115. * {
  116. * *op++ = *(++ip);
  117. * *op++ = *(ip++ -1);
  118. * }
  119. */
  120. while(nn > 3)
  121. {
  122. *op++ = *(++ip);
  123. *op++ = *(ip++ -1);
  124. *op++ = *(++ip);
  125. *op++ = *(ip++ -1);
  126. *op++ = *(++ip);
  127. *op++ = *(ip++ -1);
  128. *op++ = *(++ip);
  129. *op++ = *(ip++ -1);
  130. nn -= 4;
  131. }
  132. while(nn-- != 0)
  133. {
  134. *op++ = *(++ip);
  135. *op++ = *(ip++ -1);
  136. }
  137. }
  138. # ifndef vax
  139. static void
  140. swap4b(void *dst, const void *src)
  141. {
  142. char *op = dst;
  143. const char *ip = src;
  144. op[0] = ip[3];
  145. op[1] = ip[2];
  146. op[2] = ip[1];
  147. op[3] = ip[0];
  148. }
  149. # endif /* !vax */
  150. static void
  151. swapn4b(void *dst, const void *src, size_t nn)
  152. {
  153. char *op = dst;
  154. const char *ip = src;
  155. /* unroll the following to reduce loop overhead
  156. * while(nn-- != 0)
  157. * {
  158. * op[0] = ip[3];
  159. * op[1] = ip[2];
  160. * op[2] = ip[1];
  161. * op[3] = ip[0];
  162. * op += 4;
  163. * ip += 4;
  164. * }
  165. */
  166. while(nn > 3)
  167. {
  168. op[0] = ip[3];
  169. op[1] = ip[2];
  170. op[2] = ip[1];
  171. op[3] = ip[0];
  172. op[4] = ip[7];
  173. op[5] = ip[6];
  174. op[6] = ip[5];
  175. op[7] = ip[4];
  176. op[8] = ip[11];
  177. op[9] = ip[10];
  178. op[10] = ip[9];
  179. op[11] = ip[8];
  180. op[12] = ip[15];
  181. op[13] = ip[14];
  182. op[14] = ip[13];
  183. op[15] = ip[12];
  184. op += 16;
  185. ip += 16;
  186. nn -= 4;
  187. }
  188. while(nn-- != 0)
  189. {
  190. op[0] = ip[3];
  191. op[1] = ip[2];
  192. op[2] = ip[1];
  193. op[3] = ip[0];
  194. op += 4;
  195. ip += 4;
  196. }
  197. }
  198. # ifndef vax
  199. static void
  200. swap8b(void *dst, const void *src)
  201. {
  202. char *op = dst;
  203. const char *ip = src;
  204. # ifndef FLOAT_WORDS_BIGENDIAN
  205. op[0] = ip[7];
  206. op[1] = ip[6];
  207. op[2] = ip[5];
  208. op[3] = ip[4];
  209. op[4] = ip[3];
  210. op[5] = ip[2];
  211. op[6] = ip[1];
  212. op[7] = ip[0];
  213. # else
  214. op[0] = ip[3];
  215. op[1] = ip[2];
  216. op[2] = ip[1];
  217. op[3] = ip[0];
  218. op[4] = ip[7];
  219. op[5] = ip[6];
  220. op[6] = ip[5];
  221. op[7] = ip[4];
  222. # endif
  223. }
  224. # endif /* !vax */
  225. # ifndef vax
  226. static void
  227. swapn8b(void *dst, const void *src, size_t nn)
  228. {
  229. char *op = dst;
  230. const char *ip = src;
  231. /* unroll the following to reduce loop overhead
  232. * while(nn-- != 0)
  233. * {
  234. * op[0] = ip[7];
  235. * op[1] = ip[6];
  236. * op[2] = ip[5];
  237. * op[3] = ip[4];
  238. * op[4] = ip[3];
  239. * op[5] = ip[2];
  240. * op[6] = ip[1];
  241. * op[7] = ip[0];
  242. * op += 8;
  243. * ip += 8;
  244. * }
  245. */
  246. # ifndef FLOAT_WORDS_BIGENDIAN
  247. while(nn > 1)
  248. {
  249. op[0] = ip[7];
  250. op[1] = ip[6];
  251. op[2] = ip[5];
  252. op[3] = ip[4];
  253. op[4] = ip[3];
  254. op[5] = ip[2];
  255. op[6] = ip[1];
  256. op[7] = ip[0];
  257. op[8] = ip[15];
  258. op[9] = ip[14];
  259. op[10] = ip[13];
  260. op[11] = ip[12];
  261. op[12] = ip[11];
  262. op[13] = ip[10];
  263. op[14] = ip[9];
  264. op[15] = ip[8];
  265. op += 16;
  266. ip += 16;
  267. nn -= 2;
  268. }
  269. while(nn-- != 0)
  270. {
  271. op[0] = ip[7];
  272. op[1] = ip[6];
  273. op[2] = ip[5];
  274. op[3] = ip[4];
  275. op[4] = ip[3];
  276. op[5] = ip[2];
  277. op[6] = ip[1];
  278. op[7] = ip[0];
  279. op += 8;
  280. ip += 8;
  281. }
  282. # else
  283. while(nn-- != 0)
  284. {
  285. op[0] = ip[3];
  286. op[1] = ip[2];
  287. op[2] = ip[1];
  288. op[3] = ip[0];
  289. op[4] = ip[7];
  290. op[5] = ip[6];
  291. op[6] = ip[5];
  292. op[7] = ip[4];
  293. op += 8;
  294. ip += 8;
  295. }
  296. # endif
  297. }
  298. # endif /* !vax */
  299. #endif /* LITTLE_ENDIAN */
  300. /*
  301. * Primitive numeric conversion functions.
  302. */
  303. /* x_schar */
  304. /* We don't implement any x_schar primitives. */
  305. /* x_short */
  306. #if SHORT_MAX == X_SHORT_MAX
  307. typedef short ix_short;
  308. #define SIZEOF_IX_SHORT SIZEOF_SHORT
  309. #define IX_SHORT_MAX SHORT_MAX
  310. #elif INT_MAX >= X_SHORT_MAX
  311. typedef int ix_short;
  312. #define SIZEOF_IX_SHORT SIZEOF_INT
  313. #define IX_SHORT_MAX INT_MAX
  314. #elif LONG_MAX >= X_SHORT_MAX
  315. typedef long ix_short;
  316. #define SIZEOF_IX_SHORT SIZEOF_LONG
  317. #define IX_SHORT_MAX LONG_MAX
  318. #elif LLONG_MAX >= X_SHORT_MAX
  319. typedef long long ix_short;
  320. #define SIZEOF_IX_SHORT SIZEOF_LONG_LONG
  321. #define IX_SHORT_MAX LLONG_MAX
  322. #else
  323. #error "ix_short implementation"
  324. #endif
  325. static void
  326. get_ix_short(const void *xp, ix_short *ip)
  327. {
  328. const uchar *cp = (const uchar *) xp;
  329. *ip = *cp++ << 8;
  330. #if SIZEOF_IX_SHORT > X_SIZEOF_SHORT
  331. if(*ip & 0x8000)
  332. {
  333. /* extern is negative */
  334. *ip |= (~(0xffff)); /* N.B. Assumes "twos complement" */
  335. }
  336. #endif
  337. *ip |= *cp;
  338. }
  339. static void
  340. put_ix_short(void *xp, const ix_short *ip)
  341. {
  342. uchar *cp = (uchar *) xp;
  343. *cp++ = (*ip) >> 8;
  344. *cp = (*ip) & 0xff;
  345. }
  346. int
  347. ncx_get_short_schar(const void *xp, schar *ip)
  348. {
  349. ix_short xx;
  350. get_ix_short(xp, &xx);
  351. *ip = xx;
  352. if(xx > SCHAR_MAX || xx < SCHAR_MIN)
  353. return NC_ERANGE;
  354. return ENOERR;
  355. }
  356. int
  357. ncx_get_short_uchar(const void *xp, uchar *ip)
  358. {
  359. ix_short xx;
  360. get_ix_short(xp, &xx);
  361. *ip = xx;
  362. if(xx > UCHAR_MAX || xx < 0)
  363. return NC_ERANGE;
  364. return ENOERR;
  365. }
  366. int
  367. ncx_get_short_short(const void *xp, short *ip)
  368. {
  369. #if SIZEOF_IX_SHORT == SIZEOF_SHORT && IX_SHORT_MAX == SHORT_MAX
  370. get_ix_short(xp, (ix_short *)ip);
  371. return ENOERR;
  372. #else
  373. ix_short xx;
  374. get_ix_short(xp, &xx);
  375. *ip = xx;
  376. # if IX_SHORT_MAX > SHORT_MAX
  377. if(xx > SHORT_MAX || xx < SHORT_MIN)
  378. return NC_ERANGE;
  379. # endif
  380. return ENOERR;
  381. #endif
  382. }
  383. int
  384. ncx_get_short_int(const void *xp, int *ip)
  385. {
  386. #if SIZEOF_IX_SHORT == SIZEOF_INT && IX_SHORT_MAX == INT_MAX
  387. get_ix_short(xp, (ix_short *)ip);
  388. return ENOERR;
  389. #else
  390. ix_short xx;
  391. get_ix_short(xp, &xx);
  392. *ip = xx;
  393. # if IX_SHORT_MAX > INT_MAX
  394. if(xx > INT_MAX || xx < INT_MIN)
  395. return NC_ERANGE;
  396. # endif
  397. return ENOERR;
  398. #endif
  399. }
  400. int
  401. ncx_get_short_uint(const void *xp, unsigned int *ip)
  402. {
  403. #if SIZEOF_IX_SHORT == SIZEOF_INT && IX_SHORT_MAX == INT_MAX
  404. get_ix_short(xp, (ix_short *)ip);
  405. return ENOERR;
  406. #else
  407. ix_short xx;
  408. get_ix_short(xp, &xx);
  409. *ip = xx;
  410. # if IX_SHORT_MAX > INT_MAX
  411. if(xx > UINT_MAX || xx < 0)
  412. return NC_ERANGE;
  413. # endif
  414. return ENOERR;
  415. #endif
  416. }
  417. int
  418. ncx_get_short_longlong(const void *xp, long long *ip)
  419. {
  420. #if SIZEOF_IX_SHORT == SIZEOF_LONG_LONG && IX_SHORT_MAX == LONG_LONG_MAX
  421. get_ix_short(xp, (ix_short *)ip);
  422. return ENOERR;
  423. #else
  424. /* assert(LONG_LONG_MAX >= X_SHORT_MAX); */
  425. ix_short xx;
  426. get_ix_short(xp, &xx);
  427. *ip = xx;
  428. return ENOERR;
  429. #endif
  430. }
  431. int
  432. ncx_get_short_ulonglong(const void *xp, unsigned long long *ip)
  433. {
  434. #if SIZEOF_IX_SHORT == SIZEOF_LONG && IX_SHORT_MAX == LONG_MAX
  435. get_ix_short(xp, (ix_short *)ip);
  436. return ENOERR;
  437. #else
  438. /* assert(LONG_LONG_MAX >= X_SHORT_MAX); */
  439. ix_short xx;
  440. get_ix_short(xp, &xx);
  441. *ip = xx;
  442. if(xx < 0)
  443. return NC_ERANGE;
  444. return ENOERR;
  445. #endif
  446. }
  447. int
  448. ncx_get_short_float(const void *xp, float *ip)
  449. {
  450. ix_short xx;
  451. get_ix_short(xp, &xx);
  452. *ip = xx;
  453. #if 0 /* TODO: determine when necessary */
  454. if(xx > FLT_MAX || xx < (-FLT_MAX))
  455. return NC_ERANGE;
  456. #endif
  457. return ENOERR;
  458. }
  459. int
  460. ncx_get_short_double(const void *xp, double *ip)
  461. {
  462. /* assert(DBL_MAX >= X_SHORT_MAX); */
  463. ix_short xx;
  464. get_ix_short(xp, &xx);
  465. *ip = xx;
  466. return ENOERR;
  467. }
  468. int
  469. ncx_put_short_schar(void *xp, const schar *ip)
  470. {
  471. uchar *cp = (uchar *) xp;
  472. if(*ip & 0x80)
  473. *cp++ = 0xff;
  474. else
  475. *cp++ = 0;
  476. *cp = (uchar)*ip;
  477. return ENOERR;
  478. }
  479. int
  480. ncx_put_short_uchar(void *xp, const uchar *ip)
  481. {
  482. uchar *cp = (uchar *) xp;
  483. *cp++ = 0;
  484. *cp = *ip;
  485. return ENOERR;
  486. }
  487. int
  488. ncx_put_short_short(void *xp, const short *ip)
  489. {
  490. #if SIZEOF_IX_SHORT == SIZEOF_SHORT && X_SHORT_MAX == SHORT_MAX
  491. put_ix_short(xp, (const ix_short *)ip);
  492. return ENOERR;
  493. #else
  494. ix_short xx = (ix_short)*ip;
  495. put_ix_short(xp, &xx);
  496. # if X_SHORT_MAX < SHORT_MAX
  497. if(*ip > X_SHORT_MAX || *ip < X_SHORT_MIN)
  498. return NC_ERANGE;
  499. # endif
  500. return ENOERR;
  501. #endif
  502. }
  503. int
  504. ncx_put_short_int(void *xp, const int *ip)
  505. {
  506. #if SIZEOF_IX_SHORT == SIZEOF_INT && X_SHORT_MAX == INT_MAX
  507. put_ix_short(xp, (const ix_short *)ip);
  508. return ENOERR;
  509. #else
  510. ix_short xx = (ix_short)*ip;
  511. put_ix_short(xp, &xx);
  512. # if X_SHORT_MAX < INT_MAX
  513. if(*ip > X_SHORT_MAX || *ip < X_SHORT_MIN)
  514. return NC_ERANGE;
  515. # endif
  516. return ENOERR;
  517. #endif
  518. }
  519. int
  520. ncx_put_short_uint(void *xp, const unsigned int *ip)
  521. {
  522. #if SIZEOF_IX_SHORT == SIZEOF_INT && X_SHORT_MAX == INT_MAX
  523. put_ix_short(xp, (const ix_short *)ip);
  524. return ENOERR;
  525. #else
  526. ix_short xx = (ix_short)*ip;
  527. put_ix_short(xp, &xx);
  528. # if X_SHORT_MAX < INT_MAX
  529. if(*ip > X_SHORT_MAX)
  530. return NC_ERANGE;
  531. # endif
  532. return ENOERR;
  533. #endif
  534. }
  535. int
  536. ncx_put_short_longlong(void *xp, const long long *ip)
  537. {
  538. #if SIZEOF_IX_SHORT == SIZEOF_LONG_LONG && X_SHORT_MAX == LONG_LONG_MAX
  539. put_ix_short(xp, (const ix_short *)ip);
  540. return ENOERR;
  541. #else
  542. ix_short xx = (ix_short)*ip;
  543. put_ix_short(xp, &xx);
  544. # if X_SHORT_MAX < LONG_LONG_MAX
  545. if(*ip > X_SHORT_MAX || *ip < X_SHORT_MIN)
  546. return NC_ERANGE;
  547. # endif
  548. return ENOERR;
  549. #endif
  550. }
  551. int
  552. ncx_put_short_ulonglong(void *xp, const unsigned long long *ip)
  553. {
  554. #if SIZEOF_IX_SHORT == SIZEOF_LONG_LONG && X_SHORT_MAX == LONG_LONG_MAX
  555. put_ix_short(xp, (const ix_short *)ip);
  556. return ENOERR;
  557. #else
  558. ix_short xx = (ix_short)*ip;
  559. put_ix_short(xp, &xx);
  560. # if X_SHORT_MAX < LONG_LONG_MAX
  561. if(*ip > X_SHORT_MAX)
  562. return NC_ERANGE;
  563. # endif
  564. return ENOERR;
  565. #endif
  566. }
  567. int
  568. ncx_put_short_float(void *xp, const float *ip)
  569. {
  570. ix_short xx = *ip;
  571. put_ix_short(xp, &xx);
  572. if(*ip > X_SHORT_MAX || *ip < X_SHORT_MIN)
  573. return NC_ERANGE;
  574. return ENOERR;
  575. }
  576. int
  577. ncx_put_short_double(void *xp, const double *ip)
  578. {
  579. ix_short xx = *ip;
  580. put_ix_short(xp, &xx);
  581. if(*ip > X_SHORT_MAX || *ip < X_SHORT_MIN)
  582. return NC_ERANGE;
  583. return ENOERR;
  584. }
  585. /* x_int */
  586. #if SHORT_MAX == X_INT_MAX
  587. typedef short ix_int;
  588. #define SIZEOF_IX_INT SIZEOF_SHORT
  589. #define IX_INT_MAX SHORT_MAX
  590. #elif INT_MAX >= X_INT_MAX
  591. typedef int ix_int;
  592. #define SIZEOF_IX_INT SIZEOF_INT
  593. #define IX_INT_MAX INT_MAX
  594. #elif LONG_MAX >= X_INT_MAX
  595. typedef long ix_int;
  596. #define SIZEOF_IX_INT SIZEOF_LONG
  597. #define IX_INT_MAX LONG_MAX
  598. #else
  599. #error "ix_int implementation"
  600. #endif
  601. static void
  602. get_ix_int(const void *xp, ix_int *ip)
  603. {
  604. const uchar *cp = (const uchar *) xp;
  605. *ip = *cp++ << 24;
  606. #if SIZEOF_IX_INT > X_SIZEOF_INT
  607. if(*ip & 0x80000000)
  608. {
  609. /* extern is negative */
  610. *ip |= (~(0xffffffff)); /* N.B. Assumes "twos complement" */
  611. }
  612. #endif
  613. *ip |= (*cp++ << 16);
  614. *ip |= (*cp++ << 8);
  615. *ip |= *cp;
  616. }
  617. static void
  618. put_ix_int(void *xp, const ix_int *ip)
  619. {
  620. uchar *cp = (uchar *) xp;
  621. *cp++ = (*ip) >> 24;
  622. *cp++ = ((*ip) & 0x00ff0000) >> 16;
  623. *cp++ = ((*ip) & 0x0000ff00) >> 8;
  624. *cp = ((*ip) & 0x000000ff);
  625. }
  626. int
  627. ncx_get_int_schar(const void *xp, schar *ip)
  628. {
  629. ix_int xx;
  630. get_ix_int(xp, &xx);
  631. *ip = xx;
  632. if(xx > SCHAR_MAX || xx < SCHAR_MIN)
  633. return NC_ERANGE;
  634. return ENOERR;
  635. }
  636. int
  637. ncx_get_int_uchar(const void *xp, uchar *ip)
  638. {
  639. ix_int xx;
  640. get_ix_int(xp, &xx);
  641. *ip = xx;
  642. if(xx > UCHAR_MAX || xx < 0)
  643. return NC_ERANGE;
  644. return ENOERR;
  645. }
  646. int
  647. ncx_get_int_short(const void *xp, short *ip)
  648. {
  649. #if SIZEOF_IX_INT == SIZEOF_SHORT && IX_INT_MAX == SHORT_MAX
  650. get_ix_int(xp, (ix_int *)ip);
  651. return ENOERR;
  652. #else
  653. ix_int xx;
  654. get_ix_int(xp, &xx);
  655. *ip = xx;
  656. # if IX_INT_MAX > SHORT_MAX
  657. if(xx > SHORT_MAX || xx < SHORT_MIN)
  658. return NC_ERANGE;
  659. # endif
  660. return ENOERR;
  661. #endif
  662. }
  663. int
  664. ncx_get_int_int(const void *xp, int *ip)
  665. {
  666. #if SIZEOF_IX_INT == SIZEOF_INT && IX_INT_MAX == INT_MAX
  667. get_ix_int(xp, (ix_int *)ip);
  668. return ENOERR;
  669. #else
  670. ix_int xx;
  671. get_ix_int(xp, &xx);
  672. *ip = xx;
  673. # if IX_INT_MAX > INT_MAX
  674. if(xx > INT_MAX || xx < INT_MIN)
  675. return NC_ERANGE;
  676. # endif
  677. return ENOERR;
  678. #endif
  679. }
  680. int
  681. ncx_get_int_uint(const void *xp, unsigned int *ip)
  682. {
  683. ix_int xx;
  684. get_ix_int(xp, &xx);
  685. *ip = xx;
  686. if(xx > UINT_MAX || xx < 0)
  687. return NC_ERANGE;
  688. return ENOERR;
  689. }
  690. int
  691. ncx_get_int_longlong(const void *xp, long long *ip)
  692. {
  693. ix_int xx;
  694. get_ix_int(xp, &xx);
  695. *ip = xx;
  696. return ENOERR;
  697. }
  698. int
  699. ncx_get_int_ulonglong(const void *xp, unsigned long long *ip)
  700. {
  701. ix_int xx;
  702. get_ix_int(xp, &xx);
  703. *ip = xx;
  704. if(xx < 0)
  705. return NC_ERANGE;
  706. return ENOERR;
  707. }
  708. int
  709. ncx_get_int_float(const void *xp, float *ip)
  710. {
  711. ix_int xx;
  712. get_ix_int(xp, &xx);
  713. *ip = xx;
  714. #if 0 /* TODO: determine when necessary */
  715. if(xx > FLT_MAX || xx < (-FLT_MAX))
  716. return NC_ERANGE;
  717. #endif
  718. return ENOERR;
  719. }
  720. int
  721. ncx_get_int_double(const void *xp, double *ip)
  722. {
  723. /* assert((DBL_MAX >= X_INT_MAX); */
  724. ix_int xx;
  725. get_ix_int(xp, &xx);
  726. *ip = xx;
  727. return ENOERR;
  728. }
  729. int
  730. ncx_put_int_schar(void *xp, const schar *ip)
  731. {
  732. uchar *cp = (uchar *) xp;
  733. if(*ip & 0x80)
  734. {
  735. *cp++ = 0xff;
  736. *cp++ = 0xff;
  737. *cp++ = 0xff;
  738. }
  739. else
  740. {
  741. *cp++ = 0x00;
  742. *cp++ = 0x00;
  743. *cp++ = 0x00;
  744. }
  745. *cp = (uchar)*ip;
  746. return ENOERR;
  747. }
  748. int
  749. ncx_put_int_uchar(void *xp, const uchar *ip)
  750. {
  751. uchar *cp = (uchar *) xp;
  752. *cp++ = 0x00;
  753. *cp++ = 0x00;
  754. *cp++ = 0x00;
  755. *cp = *ip;
  756. return ENOERR;
  757. }
  758. int
  759. ncx_put_int_short(void *xp, const short *ip)
  760. {
  761. #if SIZEOF_IX_INT == SIZEOF_SHORT && IX_INT_MAX == SHORT_MAX
  762. put_ix_int(xp, (ix_int *)ip);
  763. return ENOERR;
  764. #else
  765. ix_int xx = (ix_int)(*ip);
  766. put_ix_int(xp, &xx);
  767. # if IX_INT_MAX < SHORT_MAX
  768. if(*ip > X_INT_MAX || *ip < X_INT_MIN)
  769. return NC_ERANGE;
  770. # endif
  771. return ENOERR;
  772. #endif
  773. }
  774. int
  775. ncx_put_int_int(void *xp, const int *ip)
  776. {
  777. #if SIZEOF_IX_INT == SIZEOF_INT && IX_INT_MAX == INT_MAX
  778. put_ix_int(xp, (ix_int *)ip);
  779. return ENOERR;
  780. #else
  781. ix_int xx = (ix_int)(*ip);
  782. put_ix_int(xp, &xx);
  783. # if IX_INT_MAX < INT_MAX
  784. if(*ip > X_INT_MAX || *ip < X_INT_MIN)
  785. return NC_ERANGE;
  786. # endif
  787. return ENOERR;
  788. #endif
  789. }
  790. int
  791. ncx_put_int_uint(void *xp, const unsigned int *ip)
  792. {
  793. #if SIZEOF_IX_INT == SIZEOF_INT && IX_INT_MAX == INT_MAX
  794. put_ix_int(xp, (ix_int *)ip);
  795. return ENOERR;
  796. #else
  797. ix_int xx = (ix_int)(*ip);
  798. put_ix_int(xp, &xx);
  799. if(*ip > X_UINT_MAX)
  800. return NC_ERANGE;
  801. return ENOERR;
  802. #endif
  803. }
  804. int
  805. ncx_put_int_longlong(void *xp, const longlong *ip)
  806. {
  807. #if SIZEOF_IX_INT == SIZEOF_LONG && IX_INT_MAX == LONG_MAX
  808. put_ix_int(xp, (ix_int *)ip);
  809. return ENOERR;
  810. #else
  811. ix_int xx = (ix_int)(*ip);
  812. put_ix_int(xp, &xx);
  813. # if IX_INT_MAX < LONG_LONG_MAX
  814. if(*ip > X_INT_MAX || *ip < X_INT_MIN)
  815. return NC_ERANGE;
  816. # endif
  817. return ENOERR;
  818. #endif
  819. }
  820. int
  821. ncx_put_int_ulonglong(void *xp, const unsigned long long *ip)
  822. {
  823. #if SIZEOF_IX_INT == SIZEOF_LONG && IX_INT_MAX == LONG_MAX
  824. put_ix_int(xp, (ix_int *)ip);
  825. return ENOERR;
  826. #else
  827. ix_int xx = (ix_int)(*ip);
  828. put_ix_int(xp, &xx);
  829. # if IX_INT_MAX < LONG_MAX
  830. if(*ip > X_INT_MAX)
  831. return NC_ERANGE;
  832. # endif
  833. return ENOERR;
  834. #endif
  835. }
  836. int
  837. ncx_put_int_float(void *xp, const float *ip)
  838. {
  839. ix_int xx = (ix_int)(*ip);
  840. put_ix_int(xp, &xx);
  841. if(*ip > (double)X_INT_MAX || *ip < (double)X_INT_MIN)
  842. return NC_ERANGE;
  843. return ENOERR;
  844. }
  845. int
  846. ncx_put_int_double(void *xp, const double *ip)
  847. {
  848. ix_int xx = (ix_int)(*ip);
  849. put_ix_int(xp, &xx);
  850. if(*ip > X_INT_MAX || *ip < X_INT_MIN)
  851. return NC_ERANGE;
  852. return ENOERR;
  853. }
  854. /* x_float */
  855. #if X_SIZEOF_FLOAT == SIZEOF_FLOAT && !defined(NO_IEEE_FLOAT)
  856. static void
  857. get_ix_float(const void *xp, float *ip)
  858. {
  859. #ifdef WORDS_BIGENDIAN
  860. (void) memcpy(ip, xp, sizeof(float));
  861. #else
  862. swap4b(ip, xp);
  863. #endif
  864. }
  865. static void
  866. put_ix_float(void *xp, const float *ip)
  867. {
  868. #ifdef WORDS_BIGENDIAN
  869. (void) memcpy(xp, ip, X_SIZEOF_FLOAT);
  870. #else
  871. swap4b(xp, ip);
  872. #endif
  873. }
  874. #elif vax
  875. /* What IEEE single precision floating point looks like on a Vax */
  876. struct ieee_single {
  877. unsigned int exp_hi : 7;
  878. unsigned int sign : 1;
  879. unsigned int mant_hi : 7;
  880. unsigned int exp_lo : 1;
  881. unsigned int mant_lo_hi : 8;
  882. unsigned int mant_lo_lo : 8;
  883. };
  884. /* Vax single precision floating point */
  885. struct vax_single {
  886. unsigned int mantissa1 : 7;
  887. unsigned int exp : 8;
  888. unsigned int sign : 1;
  889. unsigned int mantissa2 : 16;
  890. };
  891. #define VAX_SNG_BIAS 0x81
  892. #define IEEE_SNG_BIAS 0x7f
  893. static struct sgl_limits {
  894. struct vax_single s;
  895. struct ieee_single ieee;
  896. } max = {
  897. { 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */
  898. { 0x7f, 0x0, 0x0, 0x1, 0x0, 0x0 } /* Max IEEE */
  899. };
  900. static struct sgl_limits min = {
  901. { 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */
  902. { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 } /* Min IEEE */
  903. };
  904. static void
  905. get_ix_float(const void *xp, float *ip)
  906. {
  907. struct vax_single *const vsp = (struct vax_single *) ip;
  908. const struct ieee_single *const isp =
  909. (const struct ieee_single *) xp;
  910. unsigned exp = isp->exp_hi << 1 | isp->exp_lo;
  911. switch(exp) {
  912. case 0 :
  913. /* ieee subnormal */
  914. if(isp->mant_hi == min.ieee.mant_hi
  915. && isp->mant_lo_hi == min.ieee.mant_lo_hi
  916. && isp->mant_lo_lo == min.ieee.mant_lo_lo)
  917. {
  918. *vsp = min.s;
  919. }
  920. else
  921. {
  922. unsigned mantissa = (isp->mant_hi << 16)
  923. | isp->mant_lo_hi << 8
  924. | isp->mant_lo_lo;
  925. unsigned tmp = mantissa >> 20;
  926. if(tmp >= 4) {
  927. vsp->exp = 2;
  928. } else if (tmp >= 2) {
  929. vsp->exp = 1;
  930. } else {
  931. *vsp = min.s;
  932. break;
  933. } /* else */
  934. tmp = mantissa - (1 << (20 + vsp->exp ));
  935. tmp <<= 3 - vsp->exp;
  936. vsp->mantissa2 = tmp;
  937. vsp->mantissa1 = (tmp >> 16);
  938. }
  939. break;
  940. case 0xfe :
  941. case 0xff :
  942. *vsp = max.s;
  943. break;
  944. default :
  945. vsp->exp = exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
  946. vsp->mantissa2 = isp->mant_lo_hi << 8 | isp->mant_lo_lo;
  947. vsp->mantissa1 = isp->mant_hi;
  948. }
  949. vsp->sign = isp->sign;
  950. }
  951. static void
  952. put_ix_float(void *xp, const float *ip)
  953. {
  954. const struct vax_single *const vsp =
  955. (const struct vax_single *)ip;
  956. struct ieee_single *const isp = (struct ieee_single *) xp;
  957. switch(vsp->exp){
  958. case 0 :
  959. /* all vax float with zero exponent map to zero */
  960. *isp = min.ieee;
  961. break;
  962. case 2 :
  963. case 1 :
  964. {
  965. /* These will map to subnormals */
  966. unsigned mantissa = (vsp->mantissa1 << 16)
  967. | vsp->mantissa2;
  968. mantissa >>= 3 - vsp->exp;
  969. mantissa += (1 << (20 + vsp->exp));
  970. isp->mant_lo_lo = mantissa;
  971. isp->mant_lo_hi = mantissa >> 8;
  972. isp->mant_hi = mantissa >> 16;
  973. isp->exp_lo = 0;
  974. isp->exp_hi = 0;
  975. }
  976. break;
  977. case 0xff : /* max.s.exp */
  978. if( vsp->mantissa2 == max.s.mantissa2
  979. && vsp->mantissa1 == max.s.mantissa1)
  980. {
  981. /* map largest vax float to ieee infinity */
  982. *isp = max.ieee;
  983. break;
  984. } /* else, fall thru */
  985. default :
  986. {
  987. unsigned exp = vsp->exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
  988. isp->exp_hi = exp >> 1;
  989. isp->exp_lo = exp;
  990. isp->mant_lo_lo = vsp->mantissa2;
  991. isp->mant_lo_hi = vsp->mantissa2 >> 8;
  992. isp->mant_hi = vsp->mantissa1;
  993. }
  994. }
  995. isp->sign = vsp->sign;
  996. }
  997. /* vax */
  998. #elif defined(_CRAY) && !defined(__crayx1)
  999. /*
  1000. * Return the number of bytes until the next "word" boundary
  1001. * N.B. This is based on the very wierd YMP address structure,
  1002. * which puts the address within a word in the leftmost 3 bits
  1003. * of the address.
  1004. */
  1005. static size_t
  1006. word_align(const void *vp)
  1007. {
  1008. const size_t rem = ((size_t)vp >> (64 - 3)) & 0x7;
  1009. return (rem != 0);
  1010. }
  1011. struct ieee_single_hi {
  1012. unsigned int sign : 1;
  1013. unsigned int exp : 8;
  1014. unsigned int mant :23;
  1015. unsigned int pad :32;
  1016. };
  1017. typedef struct ieee_single_hi ieee_single_hi;
  1018. struct ieee_single_lo {
  1019. unsigned int pad :32;
  1020. unsigned int sign : 1;
  1021. unsigned int exp : 8;
  1022. unsigned int mant :23;
  1023. };
  1024. typedef struct ieee_single_lo ieee_single_lo;
  1025. static const int ieee_single_bias = 0x7f;
  1026. struct ieee_double {
  1027. unsigned int sign : 1;
  1028. unsigned int exp :11;
  1029. unsigned int mant :52;
  1030. };
  1031. typedef struct ieee_double ieee_double;
  1032. static const int ieee_double_bias = 0x3ff;
  1033. #if defined(NO_IEEE_FLOAT)
  1034. struct cray_single {
  1035. unsigned int sign : 1;
  1036. unsigned int exp :15;
  1037. unsigned int mant :48;
  1038. };
  1039. typedef struct cray_single cray_single;
  1040. static const int cs_ieis_bias = 0x4000 - 0x7f;
  1041. static const int cs_id_bias = 0x4000 - 0x3ff;
  1042. static void
  1043. get_ix_float(const void *xp, float *ip)
  1044. {
  1045. if(word_align(xp) == 0)
  1046. {
  1047. const ieee_single_hi *isp = (const ieee_single_hi *) xp;
  1048. cray_single *csp = (cray_single *) ip;
  1049. if(isp->exp == 0)
  1050. {
  1051. /* ieee subnormal */
  1052. *ip = (double)isp->mant;
  1053. if(isp->mant != 0)
  1054. {
  1055. csp->exp -= (ieee_single_bias + 22);
  1056. }
  1057. }
  1058. else
  1059. {
  1060. csp->exp = isp->exp + cs_ieis_bias + 1;
  1061. csp->mant = isp->mant << (48 - 1 - 23);
  1062. csp->mant |= (1 << (48 - 1));
  1063. }
  1064. csp->sign = isp->sign;
  1065. }
  1066. else
  1067. {
  1068. const ieee_single_lo *isp = (const ieee_single_lo *) xp;
  1069. cray_single *csp = (cray_single *) ip;
  1070. if(isp->exp == 0)
  1071. {
  1072. /* ieee subnormal */
  1073. *ip = (double)isp->mant;
  1074. if(isp->mant != 0)
  1075. {
  1076. csp->exp -= (ieee_single_bias + 22);
  1077. }
  1078. }
  1079. else
  1080. {
  1081. csp->exp = isp->exp + cs_ieis_bias + 1;
  1082. csp->mant = isp->mant << (48 - 1 - 23);
  1083. csp->mant |= (1 << (48 - 1));
  1084. }
  1085. csp->sign = isp->sign;
  1086. }
  1087. }
  1088. static void
  1089. put_ix_float(void *xp, const float *ip)
  1090. {
  1091. if(word_align(xp) == 0)
  1092. {
  1093. ieee_single_hi *isp = (ieee_single_hi*)xp;
  1094. const cray_single *csp = (const cray_single *) ip;
  1095. int ieee_exp = csp->exp - cs_ieis_bias -1;
  1096. isp->sign = csp->sign;
  1097. if(ieee_exp >= 0xff)
  1098. {
  1099. /* NC_ERANGE => ieee Inf */
  1100. isp->exp = 0xff;
  1101. isp->mant = 0x0;
  1102. }
  1103. else if(ieee_exp > 0)
  1104. {
  1105. /* normal ieee representation */
  1106. isp->exp = ieee_exp;
  1107. /* assumes cray rep is in normal form */
  1108. assert(csp->mant & 0x800000000000);
  1109. isp->mant = (((csp->mant << 1) &
  1110. 0xffffffffffff) >> (48 - 23));
  1111. }
  1112. else if(ieee_exp > -23)
  1113. {
  1114. /* ieee subnormal, right shift */
  1115. const int rshift = (48 - 23 - ieee_exp);
  1116. isp->mant = csp->mant >> rshift;
  1117. #if 0
  1118. if(csp->mant & (1 << (rshift -1)))
  1119. {
  1120. /* round up */
  1121. isp->mant++;
  1122. }
  1123. #endif
  1124. isp->exp = 0;
  1125. }
  1126. else
  1127. {
  1128. /* smaller than ieee can represent */
  1129. isp->exp = 0;
  1130. isp->mant = 0;
  1131. }
  1132. }
  1133. else
  1134. {
  1135. ieee_single_lo *isp = (ieee_single_lo*)xp;
  1136. const cray_single *csp = (const cray_single *) ip;
  1137. int ieee_exp = csp->exp - cs_ieis_bias -1;
  1138. isp->sign = csp->sign;
  1139. if(ieee_exp >= 0xff)
  1140. {
  1141. /* NC_ERANGE => ieee Inf */
  1142. isp->exp = 0xff;
  1143. isp->mant = 0x0;
  1144. }
  1145. else if(ieee_exp > 0)
  1146. {
  1147. /* normal ieee representation */
  1148. isp->exp = ieee_exp;
  1149. /* assumes cray rep is in normal form */
  1150. assert(csp->mant & 0x800000000000);
  1151. isp->mant = (((csp->mant << 1) &
  1152. 0xffffffffffff) >> (48 - 23));
  1153. }
  1154. else if(ieee_exp > -23)
  1155. {
  1156. /* ieee subnormal, right shift */
  1157. const int rshift = (48 - 23 - ieee_exp);
  1158. isp->mant = csp->mant >> rshift;
  1159. #if 0
  1160. if(csp->mant & (1 << (rshift -1)))
  1161. {
  1162. /* round up */
  1163. isp->mant++;
  1164. }
  1165. #endif
  1166. isp->exp = 0;
  1167. }
  1168. else
  1169. {
  1170. /* smaller than ieee can represent */
  1171. isp->exp = 0;
  1172. isp->mant = 0;
  1173. }
  1174. }
  1175. }
  1176. #else
  1177. /* IEEE Cray with only doubles */
  1178. static void
  1179. get_ix_float(const void *xp, float *ip)
  1180. {
  1181. ieee_double *idp = (ieee_double *) ip;
  1182. if(word_align(xp) == 0)
  1183. {
  1184. const ieee_single_hi *isp = (const ieee_single_hi *) xp;
  1185. if(isp->exp == 0 && isp->mant == 0)
  1186. {
  1187. idp->exp = 0;
  1188. idp->mant = 0;
  1189. }
  1190. else
  1191. {
  1192. idp->exp = isp->exp + (ieee_double_bias - ieee_single_bias);
  1193. idp->mant = isp->mant << (52 - 23);
  1194. }
  1195. idp->sign = isp->sign;
  1196. }
  1197. else
  1198. {
  1199. const ieee_single_lo *isp = (const ieee_single_lo *) xp;
  1200. if(isp->exp == 0 && isp->mant == 0)
  1201. {
  1202. idp->exp = 0;
  1203. idp->mant = 0;
  1204. }
  1205. else
  1206. {
  1207. idp->exp = isp->exp + (ieee_double_bias - ieee_single_bias);
  1208. idp->mant = isp->mant << (52 - 23);
  1209. }
  1210. idp->sign = isp->sign;
  1211. }
  1212. }
  1213. static void
  1214. put_ix_float(void *xp, const float *ip)
  1215. {
  1216. const ieee_double *idp = (const ieee_double *) ip;
  1217. if(word_align(xp) == 0)
  1218. {
  1219. ieee_single_hi *isp = (ieee_single_hi*)xp;
  1220. if(idp->exp > (ieee_double_bias - ieee_single_bias))
  1221. isp->exp = idp->exp - (ieee_double_bias - ieee_single_bias);
  1222. else
  1223. isp->exp = 0;
  1224. isp->mant = idp->mant >> (52 - 23);
  1225. isp->sign = idp->sign;
  1226. }
  1227. else
  1228. {
  1229. ieee_single_lo *isp = (ieee_single_lo*)xp;
  1230. if(idp->exp > (ieee_double_bias - ieee_single_bias))
  1231. isp->exp = idp->exp - (ieee_double_bias - ieee_single_bias);
  1232. else
  1233. isp->exp = 0;
  1234. isp->mant = idp->mant >> (52 - 23);
  1235. isp->sign = idp->sign;
  1236. }
  1237. }
  1238. #endif
  1239. #else
  1240. #error "ix_float implementation"
  1241. #endif
  1242. int
  1243. ncx_get_float_schar(const void *xp, schar *ip)
  1244. {
  1245. float xx;
  1246. get_ix_float(xp, &xx);
  1247. *ip = (schar) xx;
  1248. if(xx > SCHAR_MAX || xx < SCHAR_MIN)
  1249. return NC_ERANGE;
  1250. return ENOERR;
  1251. }
  1252. int
  1253. ncx_get_float_uchar(const void *xp, uchar *ip)
  1254. {
  1255. float xx;
  1256. get_ix_float(xp, &xx);
  1257. *ip = (uchar) xx;
  1258. if(xx > UCHAR_MAX || xx < 0)
  1259. return NC_ERANGE;
  1260. return ENOERR;
  1261. }
  1262. int
  1263. ncx_get_float_short(const void *xp, short *ip)
  1264. {
  1265. float xx;
  1266. get_ix_float(xp, &xx);
  1267. *ip = (short) xx;
  1268. if(xx > SHORT_MAX || xx < SHORT_MIN)
  1269. return NC_ERANGE;
  1270. return ENOERR;
  1271. }
  1272. int
  1273. ncx_get_float_int(const void *xp, int *ip)
  1274. {
  1275. float xx;
  1276. get_ix_float(xp, &xx);
  1277. *ip = (int) xx;
  1278. if(xx > (double)INT_MAX || xx < (double)INT_MIN)
  1279. return NC_ERANGE;
  1280. return ENOERR;
  1281. }
  1282. int
  1283. ncx_get_float_uint(const void *xp, unsigned int *ip)
  1284. {
  1285. float xx;
  1286. get_ix_float(xp, &xx);
  1287. *ip = (unsigned int) xx;
  1288. if(xx > (double)UINT_MAX || xx < 0)
  1289. return NC_ERANGE;
  1290. return ENOERR;
  1291. }
  1292. int
  1293. ncx_get_float_longlong(const void *xp, longlong *ip)
  1294. {
  1295. float xx;
  1296. get_ix_float(xp, &xx);
  1297. *ip = (longlong) xx;
  1298. if(xx > (double)LONG_LONG_MAX || xx < (double)LONG_LONG_MIN)
  1299. return NC_ERANGE;
  1300. return ENOERR;
  1301. }
  1302. int
  1303. ncx_get_float_ulonglong(const void *xp, unsigned long long *ip)
  1304. {
  1305. float xx;
  1306. get_ix_float(xp, &xx);
  1307. *ip = (longlong) xx;
  1308. if(xx > (double)ULONG_LONG_MAX || xx < 0)
  1309. return NC_ERANGE;
  1310. return ENOERR;
  1311. }
  1312. int
  1313. ncx_get_float_float(const void *xp, float *ip)
  1314. {
  1315. /* TODO */
  1316. get_ix_float(xp, ip);
  1317. return ENOERR;
  1318. }
  1319. int
  1320. ncx_get_float_double(const void *xp, double *ip)
  1321. {
  1322. /* TODO */
  1323. float xx;
  1324. get_ix_float(xp, &xx);
  1325. *ip = xx;
  1326. return ENOERR;
  1327. }
  1328. int
  1329. ncx_put_float_schar(void *xp, const schar *ip)
  1330. {
  1331. float xx = (float) *ip;
  1332. put_ix_float(xp, &xx);
  1333. return ENOERR;
  1334. }
  1335. int
  1336. ncx_put_float_uchar(void *xp, const uchar *ip)
  1337. {
  1338. float xx = (float) *ip;
  1339. put_ix_float(xp, &xx);
  1340. return ENOERR;
  1341. }
  1342. int
  1343. ncx_put_float_short(void *xp, const short *ip)
  1344. {
  1345. float xx = (float) *ip;
  1346. put_ix_float(xp, &xx);
  1347. #if 0 /* TODO: figure this out */
  1348. if((float)(*ip) > X_FLOAT_MAX || (float)(*ip) < X_FLOAT_MIN)
  1349. return NC_ERANGE;
  1350. #endif
  1351. return ENOERR;
  1352. }
  1353. int
  1354. ncx_put_float_int(void *xp, const int *ip)
  1355. {
  1356. float xx = (float) *ip;
  1357. put_ix_float(xp, &xx);
  1358. #if 1 /* TODO: figure this out */
  1359. if((float)(*ip) > X_FLOAT_MAX || (float)(*ip) < X_FLOAT_MIN)
  1360. return NC_ERANGE;
  1361. #endif
  1362. return ENOERR;
  1363. }
  1364. int
  1365. ncx_put_float_uint(void *xp, const unsigned int *ip)
  1366. {
  1367. float xx = (float) *ip;
  1368. put_ix_float(xp, &xx);
  1369. #if 1 /* TODO: figure this out */
  1370. if((float)(*ip) > X_FLOAT_MAX)
  1371. return NC_ERANGE;
  1372. #endif
  1373. return ENOERR;
  1374. }
  1375. int
  1376. ncx_put_float_longlong(void *xp, const longlong *ip)
  1377. {
  1378. float xx = (float) *ip;
  1379. put_ix_float(xp, &xx);
  1380. #if 1 /* TODO: figure this out */
  1381. if((float)(*ip) > X_FLOAT_MAX || (float)(*ip) < X_FLOAT_MIN)
  1382. return NC_ERANGE;
  1383. #endif
  1384. return ENOERR;
  1385. }
  1386. int
  1387. ncx_put_float_ulonglong(void *xp, const unsigned long long *ip)
  1388. {
  1389. float xx = (float) *ip;
  1390. put_ix_float(xp, &xx);
  1391. #if 1 /* TODO: figure this out */
  1392. if((float)(*ip) > X_FLOAT_MAX)
  1393. return NC_ERANGE;
  1394. #endif
  1395. return ENOERR;
  1396. }
  1397. int
  1398. ncx_put_float_float(void *xp, const float *ip)
  1399. {
  1400. put_ix_float(xp, ip);
  1401. #ifdef NO_IEEE_FLOAT
  1402. if(*ip > X_FLOAT_MAX || *ip < X_FLOAT_MIN)
  1403. return NC_ERANGE;
  1404. #endif
  1405. return ENOERR;
  1406. }
  1407. int
  1408. ncx_put_float_double(void *xp, const double *ip)
  1409. {
  1410. float xx = (float) *ip;
  1411. put_ix_float(xp, &xx);
  1412. if(*ip > X_FLOAT_MAX || *ip < X_FLOAT_MIN)
  1413. return NC_ERANGE;
  1414. return ENOERR;
  1415. }
  1416. /* x_double */
  1417. #if X_SIZEOF_DOUBLE == SIZEOF_DOUBLE && !defined(NO_IEEE_FLOAT)
  1418. static void
  1419. get_ix_double(const void *xp, double *ip)
  1420. {
  1421. #ifdef WORDS_BIGENDIAN
  1422. (void) memcpy(ip, xp, sizeof(double));
  1423. #else
  1424. swap8b(ip, xp);
  1425. #endif
  1426. }
  1427. static void
  1428. put_ix_double(void *xp, const double *ip)
  1429. {
  1430. #ifdef WORDS_BIGENDIAN
  1431. (void) memcpy(xp, ip, X_SIZEOF_DOUBLE);
  1432. #else
  1433. swap8b(xp, ip);
  1434. #endif
  1435. }
  1436. #elif vax
  1437. /* What IEEE double precision floating point looks like on a Vax */
  1438. struct ieee_double {
  1439. unsigned int exp_hi : 7;
  1440. unsigned int sign : 1;
  1441. unsigned int mant_6 : 4;
  1442. unsigned int exp_lo : 4;
  1443. unsigned int mant_5 : 8;
  1444. unsigned int mant_4 : 8;
  1445. unsigned int mant_lo : 32;
  1446. };
  1447. /* Vax double precision floating point */
  1448. struct vax_double {
  1449. unsigned int mantissa1 : 7;
  1450. unsigned int exp : 8;
  1451. unsigned int sign : 1;
  1452. unsigned int mantissa2 : 16;
  1453. unsigned int mantissa3 : 16;
  1454. unsigned int mantissa4 : 16;
  1455. };
  1456. #define VAX_DBL_BIAS 0x81
  1457. #define IEEE_DBL_BIAS 0x3ff
  1458. #define MASK(nbits) ((1 << nbits) - 1)
  1459. static const struct dbl_limits {
  1460. struct vax_double d;
  1461. struct ieee_double ieee;
  1462. } dbl_limits[2] = {
  1463. {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */
  1464. { 0x7f, 0x0, 0x0, 0xf, 0x0, 0x0, 0x0}}, /* Max IEEE */
  1465. {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
  1466. { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, /* Min IEEE */
  1467. };
  1468. static void
  1469. get_ix_double(const void *xp, double *ip)
  1470. {
  1471. struct vax_double *const vdp =
  1472. (struct vax_double *)ip;
  1473. const struct ieee_double *const idp =
  1474. (const struct ieee_double *) xp;
  1475. {
  1476. const struct dbl_limits *lim;
  1477. int ii;
  1478. for (ii = 0, lim = dbl_limits;
  1479. ii < sizeof(dbl_limits)/sizeof(struct dbl_limits);
  1480. ii++, lim++)
  1481. {
  1482. if ((idp->mant_lo == lim->ieee.mant_lo)
  1483. && (idp->mant_4 == lim->ieee.mant_4)
  1484. && (idp->mant_5 == lim->ieee.mant_5)
  1485. && (idp->mant_6 == lim->ieee.mant_6)
  1486. && (idp->exp_lo == lim->ieee.exp_lo)
  1487. && (idp->exp_hi == lim->ieee.exp_hi)
  1488. )
  1489. {
  1490. *vdp = lim->d;
  1491. goto doneit;
  1492. }
  1493. }
  1494. }
  1495. {
  1496. unsigned exp = idp->exp_hi << 4 | idp->exp_lo;
  1497. vdp->exp = exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
  1498. }
  1499. {
  1500. unsigned mant_hi = ((idp->mant_6 << 16)
  1501. | (idp->mant_5 << 8)
  1502. | idp->mant_4);
  1503. unsigned mant_lo = SWAP4(idp->mant_lo);
  1504. vdp->mantissa1 = (mant_hi >> 13);
  1505. vdp->mantissa2 = ((mant_hi & MASK(13)) << 3)
  1506. | (mant_lo >> 29);
  1507. vdp->mantissa3 = (mant_lo >> 13);
  1508. vdp->mantissa4 = (mant_lo << 3);
  1509. }
  1510. doneit:
  1511. vdp->sign = idp->sign;
  1512. }
  1513. static void
  1514. put_ix_double(void *xp, const double *ip)
  1515. {
  1516. const struct vax_double *const vdp =
  1517. (const struct vax_double *)ip;
  1518. struct ieee_double *const idp =
  1519. (struct ieee_double *) xp;
  1520. if ((vdp->mantissa4 > (dbl_limits[0].d.mantissa4 - 3)) &&
  1521. (vdp->mantissa3 == dbl_limits[0].d.mantissa3) &&
  1522. (vdp->mantissa2 == dbl_limits[0].d.mantissa2) &&
  1523. (vdp->mantissa1 == dbl_limits[0].d.mantissa1) &&
  1524. (vdp->exp == dbl_limits[0].d.exp))
  1525. {
  1526. *idp = dbl_limits[0].ieee;
  1527. goto shipit;
  1528. }
  1529. if ((vdp->mantissa4 == dbl_limits[1].d.mantissa4) &&
  1530. (vdp->mantissa3 == dbl_limits[1].d.mantissa3) &&
  1531. (vdp->mantissa2 == dbl_limits[1].d.mantissa2) &&
  1532. (vdp->mantissa1 == dbl_limits[1].d.mantissa1) &&
  1533. (vdp->exp == dbl_limits[1].d.exp))
  1534. {
  1535. *idp = dbl_limits[1].ieee;
  1536. goto shipit;
  1537. }
  1538. {
  1539. unsigned exp = vdp->exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
  1540. unsigned mant_lo = ((vdp->mantissa2 & MASK(3)) << 29) |
  1541. (vdp->mantissa3 << 13) |
  1542. ((vdp->mantissa4 >> 3) & MASK(13));
  1543. unsigned mant_hi = (vdp->mantissa1 << 13)
  1544. | (vdp->mantissa2 >> 3);
  1545. if((vdp->mantissa4 & 7) > 4)
  1546. {
  1547. /* round up */
  1548. mant_lo++;
  1549. if(mant_lo == 0)
  1550. {
  1551. mant_hi++;
  1552. if(mant_hi > 0xffffff)
  1553. {
  1554. mant_hi = 0;
  1555. exp++;
  1556. }
  1557. }
  1558. }
  1559. idp->mant_lo = SWAP4(mant_lo);
  1560. idp->mant_6 = mant_hi >> 16;
  1561. idp->mant_5 = (mant_hi & 0xff00) >> 8;
  1562. idp->mant_4 = mant_hi;
  1563. idp->exp_hi = exp >> 4;
  1564. idp->exp_lo = exp;
  1565. }
  1566. shipit:
  1567. idp->sign = vdp->sign;
  1568. }
  1569. /* vax */
  1570. #elif defined(_CRAY) && !defined(__crayx1)
  1571. static void
  1572. get_ix_double(const void *xp, double *ip)
  1573. {
  1574. const ieee_double *idp = (const ieee_double *) xp;
  1575. cray_single *csp = (cray_single *) ip;
  1576. if(idp->exp == 0)
  1577. {
  1578. /* ieee subnormal */
  1579. *ip = (double)idp->mant;
  1580. if(idp->mant != 0)
  1581. {
  1582. csp->exp -= (ieee_double_bias + 51);
  1583. }
  1584. }
  1585. else
  1586. {
  1587. csp->exp = idp->exp + cs_id_bias + 1;
  1588. csp->mant = idp->mant >> (52 - 48 + 1);
  1589. csp->mant |= (1 << (48 - 1));
  1590. }
  1591. csp->sign = idp->sign;
  1592. }
  1593. static void
  1594. put_ix_double(void *xp, const double *ip)
  1595. {
  1596. ieee_double *idp = (ieee_double *) xp;
  1597. const cray_single *csp = (const cray_single *) ip;
  1598. int ieee_exp = csp->exp - cs_id_bias -1;
  1599. idp->sign = csp->sign;
  1600. if(ieee_exp >= 0x7ff)
  1601. {
  1602. /* NC_ERANGE => ieee Inf */
  1603. idp->exp = 0x7ff;
  1604. idp->mant = 0x0;
  1605. }
  1606. else if(ieee_exp > 0)
  1607. {
  1608. /* normal ieee representation */
  1609. idp->exp = ieee_exp;
  1610. /* assumes cray rep is in normal form */
  1611. assert(csp->mant & 0x800000000000);
  1612. idp->mant = (((csp->mant << 1) &
  1613. 0xffffffffffff) << (52 - 48));
  1614. }
  1615. else if(ieee_exp >= (-(52 -48)))
  1616. {
  1617. /* ieee subnormal, left shift */
  1618. const int lshift = (52 - 48) + ieee_exp;
  1619. idp->mant = csp->mant << lshift;
  1620. idp->exp = 0;
  1621. }
  1622. else if(ieee_exp >= -52)
  1623. {
  1624. /* ieee subnormal, right shift */
  1625. const int rshift = (- (52 - 48) - ieee_exp);
  1626. idp->mant = csp->mant >> rshift;
  1627. #if 0
  1628. if(csp->mant & (1 << (rshift -1)))
  1629. {
  1630. /* round up */
  1631. idp->mant++;
  1632. }
  1633. #endif
  1634. idp->exp = 0;
  1635. }
  1636. else
  1637. {
  1638. /* smaller than ieee can represent */
  1639. idp->exp = 0;
  1640. idp->mant = 0;
  1641. }
  1642. }
  1643. #else
  1644. #error "ix_double implementation"
  1645. #endif
  1646. int
  1647. ncx_get_double_schar(const void *xp, schar *ip)
  1648. {
  1649. double xx;
  1650. get_ix_double(xp, &xx);
  1651. *ip = (schar) xx;
  1652. if(xx > SCHAR_MAX || xx < SCHAR_MIN)
  1653. return NC_ERANGE;
  1654. return ENOERR;
  1655. }
  1656. int
  1657. ncx_get_double_uchar(const void *xp, uchar *ip)
  1658. {
  1659. double xx;
  1660. get_ix_double(xp, &xx);
  1661. *ip = (uchar) xx;
  1662. if(xx > UCHAR_MAX || xx < 0)
  1663. return NC_ERANGE;
  1664. return ENOERR;
  1665. }
  1666. int
  1667. ncx_get_double_short(const void *xp, short *ip)
  1668. {
  1669. double xx;
  1670. get_ix_double(xp, &xx);
  1671. *ip = (short) xx;
  1672. if(xx > SHORT_MAX || xx < SHORT_MIN)
  1673. return NC_ERANGE;
  1674. return ENOERR;
  1675. }
  1676. int
  1677. ncx_get_double_int(const void *xp, int *ip)
  1678. {
  1679. double xx;
  1680. get_ix_double(xp, &xx);
  1681. *ip = (int) xx;
  1682. if(xx > INT_MAX || xx < INT_MIN)
  1683. return NC_ERANGE;
  1684. return ENOERR;
  1685. }
  1686. int
  1687. ncx_get_double_uint(const void *xp, unsigned int *ip)
  1688. {
  1689. double xx;
  1690. get_ix_double(xp, &xx);
  1691. *ip = (unsigned int) xx;
  1692. if(xx > UINT_MAX || xx < 0)
  1693. return NC_ERANGE;
  1694. return ENOERR;
  1695. }
  1696. int
  1697. ncx_get_double_longlong(const void *xp, longlong *ip)
  1698. {
  1699. double xx;
  1700. get_ix_double(xp, &xx);
  1701. *ip = (longlong) xx;
  1702. if(xx > LONG_LONG_MAX || xx < LONG_LONG_MIN)
  1703. return NC_ERANGE;
  1704. return ENOERR;
  1705. }
  1706. int
  1707. ncx_get_double_ulonglong(const void *xp, unsigned long long *ip)
  1708. {
  1709. double xx;
  1710. get_ix_double(xp, &xx);
  1711. *ip = (unsigned longlong) xx;
  1712. if(xx > ULONG_LONG_MAX || xx < 0)
  1713. return NC_ERANGE;
  1714. return ENOERR;
  1715. }
  1716. int
  1717. ncx_get_double_float(const void *xp, float *ip)
  1718. {
  1719. double xx;
  1720. get_ix_double(xp, &xx);
  1721. if(xx > FLT_MAX)
  1722. {
  1723. *ip = FLT_MAX;
  1724. return NC_ERANGE;
  1725. }
  1726. if(xx < (-FLT_MAX))
  1727. {
  1728. *ip = (-FLT_MAX);
  1729. return NC_ERANGE;
  1730. }
  1731. *ip = (float) xx;
  1732. return ENOERR;
  1733. }
  1734. int
  1735. ncx_get_double_double(const void *xp, double *ip)
  1736. {
  1737. /* TODO */
  1738. get_ix_double(xp, ip);
  1739. return ENOERR;
  1740. }
  1741. int
  1742. ncx_put_double_schar(void *xp, const schar *ip)
  1743. {
  1744. double xx = (double) *ip;
  1745. put_ix_double(xp, &xx);
  1746. return ENOERR;
  1747. }
  1748. int
  1749. ncx_put_double_uchar(void *xp, const uchar *ip)
  1750. {
  1751. double xx = (double) *ip;
  1752. put_ix_double(xp, &xx);
  1753. return ENOERR;
  1754. }
  1755. int
  1756. ncx_put_double_short(void *xp, const short *ip)
  1757. {
  1758. double xx = (double) *ip;
  1759. put_ix_double(xp, &xx);
  1760. #if 0 /* TODO: figure this out */
  1761. if((double)(*ip) > X_DOUBLE_MAX || (double)(*ip) < X_DOUBLE_MIN)
  1762. return NC_ERANGE;
  1763. #endif
  1764. return ENOERR;
  1765. }
  1766. int
  1767. ncx_put_double_int(void *xp, const int *ip)
  1768. {
  1769. double xx = (double) *ip;
  1770. put_ix_double(xp, &xx);
  1771. #if 0 /* TODO: figure this out */
  1772. if((double)(*ip) > X_DOUBLE_MAX || (double)(*ip) < X_DOUBLE_MIN)
  1773. return NC_ERANGE;
  1774. #endif
  1775. return ENOERR;
  1776. }
  1777. int
  1778. ncx_put_double_uint(void *xp, const unsigned int *ip)
  1779. {
  1780. double xx = (double) *ip;
  1781. put_ix_double(xp, &xx);
  1782. #if 0 /* TODO: figure this out */
  1783. if((double)(*ip) > X_DOUBLE_MAX)
  1784. return NC_ERANGE;
  1785. #endif
  1786. return ENOERR;
  1787. }
  1788. int
  1789. ncx_put_double_longlong(void *xp, const longlong *ip)
  1790. {
  1791. double xx = (double) *ip;
  1792. put_ix_double(xp, &xx);
  1793. #if 1 /* TODO: figure this out */
  1794. if((double)(*ip) > X_DOUBLE_MAX || (double)(*ip) < X_DOUBLE_MIN)
  1795. return NC_ERANGE;
  1796. #endif
  1797. return ENOERR;
  1798. }
  1799. int
  1800. ncx_put_double_ulonglong(void *xp, const unsigned long long *ip)
  1801. {
  1802. double xx = (double) *ip;
  1803. put_ix_double(xp, &xx);
  1804. #if 1 /* TODO: figure this out */
  1805. if((double)(*ip) > X_DOUBLE_MAX)
  1806. return NC_ERANGE;
  1807. #endif
  1808. return ENOERR;
  1809. }
  1810. int
  1811. ncx_put_double_float(void *xp, const float *ip)
  1812. {
  1813. double xx = (double) *ip;
  1814. put_ix_double(xp, &xx);
  1815. #if 1 /* TODO: figure this out */
  1816. if((double)(*ip) > X_DOUBLE_MAX || (double)(*ip) < X_DOUBLE_MIN)
  1817. return NC_ERANGE;
  1818. #endif
  1819. return ENOERR;
  1820. }
  1821. int
  1822. ncx_put_double_double(void *xp, const double *ip)
  1823. {
  1824. put_ix_double(xp, ip);
  1825. #ifdef NO_IEEE_FLOAT
  1826. if(*ip > X_DOUBLE_MAX || *ip < X_DOUBLE_MIN)
  1827. return NC_ERANGE;
  1828. #endif
  1829. return ENOERR;
  1830. }
  1831. /* x_size_t */
  1832. #if SIZEOF_SIZE_T < X_SIZEOF_SIZE_T
  1833. #error "x_size_t implementation"
  1834. /* netcdf requires size_t which can hold a values from 0 to 2^32 -1 */
  1835. #endif
  1836. int
  1837. ncx_put_size_t(void **xpp, const size_t *ulp)
  1838. {
  1839. /* similar to put_ix_int() */
  1840. uchar *cp = (uchar *) *xpp;
  1841. assert(*ulp <= X_SIZE_MAX);
  1842. *cp++ = (uchar)((*ulp) >> 24);
  1843. *cp++ = (uchar)(((*ulp) & 0x00ff0000) >> 16);
  1844. *cp++ = (uchar)(((*ulp) & 0x0000ff00) >> 8);
  1845. *cp = (uchar)((*ulp) & 0x000000ff);
  1846. *xpp = (void *)((char *)(*xpp) + X_SIZEOF_SIZE_T);
  1847. return ENOERR;
  1848. }
  1849. int
  1850. ncx_get_size_t(const void **xpp, size_t *ulp)
  1851. {
  1852. /* similar to get_ix_int */
  1853. const uchar *cp = (const uchar *) *xpp;
  1854. *ulp = (unsigned)(*cp++ << 24);
  1855. *ulp |= (*cp++ << 16);
  1856. *ulp |= (*cp++ << 8);
  1857. *ulp |= *cp;
  1858. *xpp = (const void *)((const char *)(*xpp) + X_SIZEOF_SIZE_T);
  1859. return ENOERR;
  1860. }
  1861. /* x_off_t */
  1862. int
  1863. ncx_put_off_t(void **xpp, const off_t *lp, size_t sizeof_off_t)
  1864. {
  1865. /* similar to put_ix_int() */
  1866. uchar *cp = (uchar *) *xpp;
  1867. /* No negative offsets stored in netcdf */
  1868. if (*lp < 0) {
  1869. /* Assume this is an overflow of a 32-bit int... */
  1870. return ERANGE;
  1871. }
  1872. assert(sizeof_off_t == 4 || sizeof_off_t == 8);
  1873. if (sizeof_off_t == 4) {
  1874. *cp++ = (uchar) ((*lp) >> 24);
  1875. *cp++ = (uchar)(((*lp) & 0x00ff0000) >> 16);
  1876. *cp++ = (uchar)(((*lp) & 0x0000ff00) >> 8);
  1877. *cp = (uchar)( (*lp) & 0x000000ff);
  1878. } else {
  1879. #if SIZEOF_OFF_T == 4
  1880. /* Write a 64-bit offset on a system with only a 32-bit offset */
  1881. *cp++ = (uchar)0;
  1882. *cp++ = (uchar)0;
  1883. *cp++ = (uchar)0;
  1884. *cp++ = (uchar)0;
  1885. *cp++ = (uchar)(((*lp) & 0xff000000) >> 24);
  1886. *cp++ = (uchar)(((*lp) & 0x00ff0000) >> 16);
  1887. *cp++ = (uchar)(((*lp) & 0x0000ff00) >> 8);
  1888. *cp = (uchar)( (*lp) & 0x000000ff);
  1889. #else
  1890. *cp++ = (uchar) ((*lp) >> 56);
  1891. *cp++ = (uchar)(((*lp) & 0x00ff000000000000ULL) >> 48);
  1892. *cp++ = (uchar)(((*lp) & 0x0000ff0000000000ULL) >> 40);
  1893. *cp++ = (uchar)(((*lp) & 0x000000ff00000000ULL) >> 32);
  1894. *cp++ = (uchar)(((*lp) & 0x00000000ff000000ULL) >> 24);
  1895. *cp++ = (uchar)(((*lp) & 0x0000000000ff0000ULL) >> 16);
  1896. *cp++ = (uchar)(((*lp) & 0x000000000000ff00ULL) >> 8);
  1897. *cp = (uchar)( (*lp) & 0x00000000000000ffULL);
  1898. #endif
  1899. }
  1900. *xpp = (void *)((char *)(*xpp) + sizeof_off_t);
  1901. return ENOERR;
  1902. }
  1903. int
  1904. ncx_get_off_t(const void **xpp, off_t *lp, size_t sizeof_off_t)
  1905. {
  1906. /* similar to get_ix_int() */
  1907. const uchar *cp = (const uchar *) *xpp;
  1908. assert(sizeof_off_t == 4 || sizeof_off_t == 8);
  1909. if (sizeof_off_t == 4) {
  1910. *lp = *cp++ << 24;
  1911. *lp |= (*cp++ << 16);
  1912. *lp |= (*cp++ << 8);
  1913. *lp |= *cp;
  1914. } else {
  1915. #if SIZEOF_OFF_T == 4
  1916. /* Read a 64-bit offset on a system with only a 32-bit offset */
  1917. /* If the offset overflows, set an error code and return */
  1918. *lp = ((off_t)(*cp++) << 24);
  1919. *lp |= ((off_t)(*cp++) << 16);
  1920. *lp |= ((off_t)(*cp++) << 8);
  1921. *lp |= ((off_t)(*cp++));
  1922. /*
  1923. * lp now contains the upper 32-bits of the 64-bit offset. if lp is
  1924. * not zero, then the dataset is larger than can be represented
  1925. * on this system. Set an error code and return.
  1926. */
  1927. if (*lp != 0) {
  1928. return ERANGE;
  1929. }
  1930. *lp = ((off_t)(*cp++) << 24);
  1931. *lp |= ((off_t)(*cp++) << 16);
  1932. *lp |= ((off_t)(*cp++) << 8);
  1933. *lp |= (off_t)*cp;
  1934. if (*lp < 0) {
  1935. /*
  1936. * If this fails, then the offset is >2^31, but less
  1937. * than 2^32 which is not allowed, but is not caught
  1938. * by the previous check
  1939. */
  1940. return ERANGE;
  1941. }
  1942. #else
  1943. *lp = ((off_t)(*cp++) << 56);
  1944. *lp |= ((off_t)(*cp++) << 48);
  1945. *lp |= ((off_t)(*cp++) << 40);
  1946. *lp |= ((off_t)(*cp++) << 32);
  1947. *lp |= ((off_t)(*cp++) << 24);
  1948. *lp |= ((off_t)(*cp++) << 16);
  1949. *lp |= ((off_t)(*cp++) << 8);
  1950. *lp |= (off_t)*cp;
  1951. #endif
  1952. }
  1953. *xpp = (const void *)((const char *)(*xpp) + sizeof_off_t);
  1954. return ENOERR;
  1955. }
  1956. /*
  1957. * Aggregate numeric conversion functions.
  1958. */
  1959. /* schar */
  1960. int
  1961. ncx_getn_schar_schar(const void **xpp, size_t nelems, schar *tp)
  1962. {
  1963. (void) memcpy(tp, *xpp, nelems);
  1964. *xpp = (void *)((char *)(*xpp) + nelems);
  1965. return ENOERR;
  1966. }
  1967. int
  1968. ncx_getn_schar_uchar(const void **xpp, size_t nelems, uchar *tp)
  1969. {
  1970. (void) memcpy(tp, *xpp, nelems);
  1971. *xpp = (void *)((char *)(*xpp) + nelems);
  1972. return ENOERR;
  1973. }
  1974. int
  1975. ncx_getn_schar_short(const void **xpp, size_t nelems, short *tp)
  1976. {
  1977. schar *xp = (schar *)(*xpp);
  1978. while(nelems-- != 0)
  1979. {
  1980. *tp++ = *xp++;
  1981. }
  1982. *xpp = (const void *)xp;
  1983. return ENOERR;
  1984. }
  1985. int
  1986. ncx_getn_schar_int(const void **xpp, size_t nelems, int *tp)
  1987. {
  1988. schar *xp = (schar *)(*xpp);
  1989. while(nelems-- != 0)
  1990. {
  1991. *tp++ = *xp++;
  1992. }
  1993. *xpp = (const void *)xp;
  1994. return ENOERR;
  1995. }
  1996. int
  1997. ncx_getn_schar_float(const void **xpp, size_t nelems, float *tp)
  1998. {
  1999. schar *xp = (schar *)(*xpp);
  2000. while(nelems-- != 0)
  2001. {
  2002. *tp++ = *xp++;
  2003. }
  2004. *xpp = (const void *)xp;
  2005. return ENOERR;
  2006. }
  2007. int
  2008. ncx_getn_schar_double(const void **xpp, size_t nelems, double *tp)
  2009. {
  2010. schar *xp = (schar *)(*xpp);
  2011. while(nelems-- != 0)
  2012. {
  2013. *tp++ = *xp++;
  2014. }
  2015. *xpp = (const void *)xp;
  2016. return ENOERR;
  2017. }
  2018. int
  2019. ncx_getn_schar_uint(const void **xpp, size_t nelems, uint *tp)
  2020. {
  2021. schar *xp = (schar *)(*xpp);
  2022. while(nelems-- != 0)
  2023. {
  2024. *tp++ = *xp++;
  2025. }
  2026. *xpp = (const void *)xp;
  2027. return ENOERR;
  2028. }
  2029. int
  2030. ncx_getn_schar_longlong(const void **xpp, size_t nelems, longlong *tp)
  2031. {
  2032. schar *xp = (schar *)(*xpp);
  2033. while(nelems-- != 0)
  2034. {
  2035. *tp++ = *xp++;
  2036. }
  2037. *xpp = (const void *)xp;
  2038. return ENOERR;
  2039. }
  2040. int
  2041. ncx_getn_schar_ulonglong(const void **xpp, size_t nelems, ulonglong *tp)
  2042. {
  2043. schar *xp = (schar *)(*xpp);
  2044. while(nelems-- != 0)
  2045. {
  2046. *tp++ = *xp++;
  2047. }
  2048. *xpp = (const void *)xp;
  2049. return ENOERR;
  2050. }
  2051. int
  2052. ncx_pad_getn_schar_schar(const void **xpp, size_t nelems, schar *tp)
  2053. {
  2054. size_t rndup = nelems % X_ALIGN;
  2055. if(rndup)
  2056. rndup = X_ALIGN - rndup;
  2057. (void) memcpy(tp, *xpp, nelems);
  2058. *xpp = (void *)((char *)(*xpp) + nelems + rndup);
  2059. return ENOERR;
  2060. }
  2061. int
  2062. ncx_pad_getn_schar_uchar(const void **xpp, size_t nelems, uchar *tp)
  2063. {
  2064. size_t rndup = nelems % X_ALIGN;
  2065. if(rndup)
  2066. rndup = X_ALIGN - rndup;
  2067. (void) memcpy(tp, *xpp, nelems);
  2068. *xpp = (void *)((char *)(*xpp) + nelems + rndup);
  2069. return ENOERR;
  2070. }
  2071. int
  2072. ncx_pad_getn_schar_short(const void **xpp, size_t nelems, short *tp)
  2073. {
  2074. size_t rndup = nelems % X_ALIGN;
  2075. schar *xp = (schar *) *xpp;
  2076. if(rndup)
  2077. rndup = X_ALIGN - rndup;
  2078. while(nelems-- != 0)
  2079. {
  2080. *tp++ = *xp++;
  2081. }
  2082. *xpp = (void *)(xp + rndup);
  2083. return ENOERR;
  2084. }
  2085. int
  2086. ncx_pad_getn_schar_int(const void **xpp, size_t nelems, int *tp)
  2087. {
  2088. size_t rndup = nelems % X_ALIGN;
  2089. schar *xp = (schar *) *xpp;
  2090. if(rndup)
  2091. rndup = X_ALIGN - rndup;
  2092. while(nelems-- != 0)
  2093. {
  2094. *tp++ = *xp++;
  2095. }
  2096. *xpp = (void *)(xp + rndup);
  2097. return ENOERR;
  2098. }
  2099. int
  2100. ncx_pad_getn_schar_float(const void **xpp, size_t nelems, float *tp)
  2101. {
  2102. size_t rndup = nelems % X_ALIGN;
  2103. schar *xp = (schar *) *xpp;
  2104. if(rndup)
  2105. rndup = X_ALIGN - rndup;
  2106. while(nelems-- != 0)
  2107. {
  2108. *tp++ = *xp++;
  2109. }
  2110. *xpp = (void *)(xp + rndup);
  2111. return ENOERR;
  2112. }
  2113. int
  2114. ncx_pad_getn_schar_double(const void **xpp, size_t nelems, double *tp)
  2115. {
  2116. size_t rndup = nelems % X_ALIGN;
  2117. schar *xp = (schar *) *xpp;
  2118. if(rndup)
  2119. rndup = X_ALIGN - rndup;
  2120. while(nelems-- != 0)
  2121. {
  2122. *tp++ = *xp++;
  2123. }
  2124. *xpp = (void *)(xp + rndup);
  2125. return ENOERR;
  2126. }
  2127. int
  2128. ncx_pad_getn_schar_uint(const void **xpp, size_t nelems, uint *tp)
  2129. {
  2130. size_t rndup = nelems % X_ALIGN;
  2131. schar *xp = (schar *) *xpp;
  2132. if(rndup)
  2133. rndup = X_ALIGN - rndup;
  2134. while(nelems-- != 0)
  2135. {
  2136. *tp++ = *xp++;
  2137. }
  2138. *xpp = (void *)(xp + rndup);
  2139. return ENOERR;
  2140. }
  2141. int
  2142. ncx_pad_getn_schar_longlong(const void **xpp, size_t nelems, longlong *tp)
  2143. {
  2144. size_t rndup = nelems % X_ALIGN;
  2145. schar *xp = (schar *) *xpp;
  2146. if(rndup)
  2147. rndup = X_ALIGN - rndup;
  2148. while(nelems-- != 0)
  2149. {
  2150. *tp++ = *xp++;
  2151. }
  2152. *xpp = (void *)(xp + rndup);
  2153. return ENOERR;
  2154. }
  2155. int
  2156. ncx_pad_getn_schar_ulonglong(const void **xpp, size_t nelems, ulonglong *tp)
  2157. {
  2158. size_t rndup = nelems % X_ALIGN;
  2159. schar *xp = (schar *) *xpp;
  2160. if(rndup)
  2161. rndup = X_ALIGN - rndup;
  2162. while(nelems-- != 0)
  2163. {
  2164. *tp++ = *xp++;
  2165. }
  2166. *xpp = (void *)(xp + rndup);
  2167. return ENOERR;
  2168. }
  2169. int
  2170. ncx_putn_schar_schar(void **xpp, size_t nelems, const schar *tp)
  2171. {
  2172. (void) memcpy(*xpp, tp, nelems);
  2173. *xpp = (void *)((char *)(*xpp) + nelems);
  2174. return ENOERR;
  2175. }
  2176. int
  2177. ncx_putn_schar_uchar(void **xpp, size_t nelems, const uchar *tp)
  2178. {
  2179. (void) memcpy(*xpp, tp, nelems);
  2180. *xpp = (void *)((char *)(*xpp) + nelems);
  2181. return ENOERR;
  2182. }
  2183. int
  2184. ncx_putn_schar_short(void **xpp, size_t nelems, const short *tp)
  2185. {
  2186. int status = ENOERR;
  2187. schar *xp = (schar *) *xpp;
  2188. while(nelems-- != 0)
  2189. {
  2190. if(*tp > X_SCHAR_MAX || *tp < X_SCHAR_MIN)
  2191. status = NC_ERANGE;
  2192. *xp++ = (schar) *tp++;
  2193. }
  2194. *xpp = (void *)xp;
  2195. return status;
  2196. }
  2197. int
  2198. ncx_putn_schar_int(void **xpp, size_t nelems, const int *tp)
  2199. {
  2200. int status = ENOERR;
  2201. schar *xp = (schar *) *xpp;
  2202. while(nelems-- != 0)
  2203. {
  2204. if(*tp > X_SCHAR_MAX || *tp < X_SCHAR_MIN)
  2205. status = NC_ERANGE;
  2206. *xp++ = (schar) *tp++;
  2207. }
  2208. *xpp = (void *)xp;
  2209. return status;
  2210. }
  2211. int
  2212. ncx_putn_schar_float(void **xpp, size_t nelems, const float *tp)
  2213. {
  2214. int status = ENOERR;
  2215. schar *xp = (schar *) *xpp;
  2216. while(nelems-- != 0)
  2217. {
  2218. if(*tp > X_SCHAR_MAX || *tp < X_SCHAR_MIN)
  2219. status = NC_ERANGE;
  2220. *xp++ = (schar) *tp++;
  2221. }
  2222. *xpp = (void *)xp;
  2223. return status;
  2224. }
  2225. int
  2226. ncx_putn_schar_double(void **xpp, size_t nelems, const double *tp)
  2227. {
  2228. int status = ENOERR;
  2229. schar *xp = (schar *) *xpp;
  2230. while(nelems-- != 0)
  2231. {
  2232. if(*tp > X_SCHAR_MAX || *tp < X_SCHAR_MIN)
  2233. status = NC_ERANGE;
  2234. *xp++ = (schar) *tp++;
  2235. }
  2236. *xpp = (void *)xp;
  2237. return status;
  2238. }
  2239. int
  2240. ncx_putn_schar_uint(void **xpp, size_t nelems, const uint *tp)
  2241. {
  2242. int status = ENOERR;
  2243. schar *xp = (schar *) *xpp;
  2244. while(nelems-- != 0)
  2245. {
  2246. if(*tp > X_SCHAR_MAX || *tp < X_SCHAR_MIN)
  2247. status = NC_ERANGE;
  2248. *xp++ = (schar) *tp++;
  2249. }
  2250. *xpp = (void *)xp;
  2251. return status;
  2252. }
  2253. int
  2254. ncx_putn_schar_longlong(void **xpp, size_t nelems, const longlong *tp)
  2255. {
  2256. int status = ENOERR;
  2257. schar *xp = (schar *) *xpp;
  2258. while(nelems-- != 0)
  2259. {
  2260. if(*tp > X_SCHAR_MAX || *tp < X_SCHAR_MIN)
  2261. status = NC_ERANGE;
  2262. *xp++ = (schar) *tp++;
  2263. }
  2264. *xpp = (void *)xp;
  2265. return status;
  2266. }
  2267. int
  2268. ncx_putn_schar_ulonglong(void **xpp, size_t nelems, const ulonglong *tp)
  2269. {
  2270. int status = ENOERR;
  2271. schar *xp = (schar *) *xpp;
  2272. while(nelems-- != 0)
  2273. {
  2274. if(*tp > X_SCHAR_MAX || *tp < X_SCHAR_MIN)
  2275. status = NC_ERANGE;
  2276. *xp++ = (schar) *tp++;
  2277. }
  2278. *xpp = (void *)xp;
  2279. return status;
  2280. }
  2281. int
  2282. ncx_pad_putn_schar_schar(void **xpp, size_t nelems, const schar *tp)
  2283. {
  2284. size_t rndup = nelems % X_ALIGN;
  2285. if(rndup)
  2286. rndup = X_ALIGN - rndup;
  2287. (void) memcpy(*xpp, tp, nelems);
  2288. *xpp = (void *)((char *)(*xpp) + nelems);
  2289. if(rndup)
  2290. {
  2291. (void) memcpy(*xpp, nada, rndup);
  2292. *xpp = (void *)((char *)(*xpp) + rndup);
  2293. }
  2294. return ENOERR;
  2295. }
  2296. int
  2297. ncx_pad_putn_schar_uchar(void **xpp, size_t nelems, const uchar *tp)
  2298. {
  2299. size_t rndup = nelems % X_ALIGN;
  2300. if(rndup)
  2301. rndup = X_ALIGN - rndup;
  2302. (void) memcpy(*xpp, tp, nelems);
  2303. *xpp = (void *)((char *)(*xpp) + nelems);
  2304. if(rndup)
  2305. {
  2306. (void) memcpy(*xpp, nada, rndup);
  2307. *xpp = (void *)((char *)(*xpp) + rndup);
  2308. }
  2309. return ENOERR;
  2310. }
  2311. int
  2312. ncx_pad_putn_schar_short(void **xpp, size_t nelems, const short *tp)
  2313. {
  2314. int status = ENOERR;
  2315. size_t rndup = nelems % X_ALIGN;
  2316. schar *xp = (schar *) *xpp;
  2317. if(rndup)
  2318. rndup = X_ALIGN - rndup;
  2319. while(nelems-- != 0)
  2320. {
  2321. /* N.B. schar as signed */
  2322. if(*tp > X_SCHAR_MAX || *tp < X_SCHAR_MIN)
  2323. status = NC_ERANGE;
  2324. *xp++ = (schar) *tp++;
  2325. }
  2326. if(rndup)
  2327. {
  2328. (void) memcpy(xp, nada, rndup);
  2329. xp += rndup;
  2330. }
  2331. *xpp = (void *)xp;
  2332. return status;
  2333. }
  2334. int
  2335. ncx_pad_putn_schar_int(void **xpp, size_t nelems, const int *tp)
  2336. {
  2337. int status = ENOERR;
  2338. size_t rndup = nelems % X_ALIGN;
  2339. schar *xp = (schar *) *xpp;
  2340. if(rndup)
  2341. rndup = X_ALIGN - rndup;
  2342. while(nelems-- != 0)
  2343. {
  2344. /* N.B. schar as signed */
  2345. if(*tp > X_SCHAR_MAX || *tp < X_SCHAR_MIN)
  2346. status = NC_ERANGE;
  2347. *xp++ = (schar) *tp++;
  2348. }
  2349. if(rndup)
  2350. {
  2351. (void) memcpy(xp, nada, rndup);
  2352. xp += rndup;
  2353. }
  2354. *xpp = (void *)xp;
  2355. return status;
  2356. }
  2357. int
  2358. ncx_pad_putn_schar_float(void **xpp, size_t nelems, const float *tp)
  2359. {
  2360. int status = ENOERR;
  2361. size_t rndup = nelems % X_ALIGN;
  2362. schar *xp = (schar *) *xpp;
  2363. if(rndup)
  2364. rndup = X_ALIGN - rndup;
  2365. while(nelems-- != 0)
  2366. {
  2367. /* N.B. schar as signed */
  2368. if(*tp > X_SCHAR_MAX || *tp < X_SCHAR_MIN)
  2369. status = NC_ERANGE;
  2370. *xp++ = (schar) *tp++;
  2371. }
  2372. if(rndup)
  2373. {
  2374. (void) memcpy(xp, nada, rndup);
  2375. xp += rndup;
  2376. }
  2377. *xpp = (void *)xp;
  2378. return status;
  2379. }
  2380. int
  2381. ncx_pad_putn_schar_double(void **xpp, size_t nelems, const double *tp)
  2382. {
  2383. int status = ENOERR;
  2384. size_t rndup = nelems % X_ALIGN;
  2385. schar *xp = (schar *) *xpp;
  2386. if(rndup)
  2387. rndup = X_ALIGN - rndup;
  2388. while(nelems-- != 0)
  2389. {
  2390. /* N.B. schar as signed */
  2391. if(*tp > X_SCHAR_MAX || *tp < X_SCHAR_MIN)
  2392. status = NC_ERANGE;
  2393. *xp++ = (schar) *tp++;
  2394. }
  2395. if(rndup)
  2396. {
  2397. (void) memcpy(xp, nada, rndup);
  2398. xp += rndup;
  2399. }
  2400. *xpp = (void *)xp;
  2401. return status;
  2402. }
  2403. int
  2404. ncx_pad_putn_schar_uint(void **xpp, size_t nelems, const uint *tp)
  2405. {
  2406. int status = ENOERR;
  2407. size_t rndup = nelems % X_ALIGN;
  2408. schar *xp = (schar *) *xpp;
  2409. if(rndup)
  2410. rndup = X_ALIGN - rndup;
  2411. while(nelems-- != 0)
  2412. {
  2413. /* N.B. schar as signed */
  2414. if(*tp > X_SCHAR_MAX || *tp < X_SCHAR_MIN)
  2415. status = NC_ERANGE;
  2416. *xp++ = (schar) *tp++;
  2417. }
  2418. if(rndup)
  2419. {
  2420. (void) memcpy(xp, nada, rndup);
  2421. xp += rndup;
  2422. }
  2423. *xpp = (void *)xp;
  2424. return status;
  2425. }
  2426. int
  2427. ncx_pad_putn_schar_longlong(void **xpp, size_t nelems, const longlong *tp)
  2428. {
  2429. int status = ENOERR;
  2430. size_t rndup = nelems % X_ALIGN;
  2431. schar *xp = (schar *) *xpp;
  2432. if(rndup)
  2433. rndup = X_ALIGN - rndup;
  2434. while(nelems-- != 0)
  2435. {
  2436. /* N.B. schar as signed */
  2437. if(*tp > X_SCHAR_MAX || *tp < X_SCHAR_MIN)
  2438. status = NC_ERANGE;
  2439. *xp++ = (schar) *tp++;
  2440. }
  2441. if(rndup)
  2442. {
  2443. (void) memcpy(xp, nada, rndup);
  2444. xp += rndup;
  2445. }
  2446. *xpp = (void *)xp;
  2447. return status;
  2448. }
  2449. int
  2450. ncx_pad_putn_schar_ulonglong(void **xpp, size_t nelems, const ulonglong *tp)
  2451. {
  2452. int status = ENOERR;
  2453. size_t rndup = nelems % X_ALIGN;
  2454. schar *xp = (schar *) *xpp;
  2455. if(rndup)
  2456. rndup = X_ALIGN - rndup;
  2457. while(nelems-- != 0)
  2458. {
  2459. /* N.B. schar as signed */
  2460. if(*tp > X_SCHAR_MAX || *tp < X_SCHAR_MIN)
  2461. status = NC_ERANGE;
  2462. *xp++ = (schar) *tp++;
  2463. }
  2464. if(rndup)
  2465. {
  2466. (void) memcpy(xp, nada, rndup);
  2467. xp += rndup;
  2468. }
  2469. *xpp = (void *)xp;
  2470. return status;
  2471. }
  2472. /* short */
  2473. int
  2474. ncx_getn_short_schar(const void **xpp, size_t nelems, schar *tp)
  2475. {
  2476. #if _SX && \
  2477. X_SIZEOF_SHORT == SIZEOF_SHORT
  2478. /* basic algorithm is:
  2479. * - ensure sane alignment of input data
  2480. * - copy (conversion happens automatically) input data
  2481. * to output
  2482. * - update xpp to point at next unconverted input, and tp to point
  2483. * at next location for converted output
  2484. */
  2485. long i, j, ni;
  2486. short tmp[LOOPCNT]; /* in case input is misaligned */
  2487. short *xp;
  2488. int nrange = 0; /* number of range errors */
  2489. int realign = 0; /* "do we need to fix input data alignment?" */
  2490. long cxp = (long) *((char**)xpp);
  2491. realign = (cxp & 7) % SIZEOF_SHORT;
  2492. /* sjl: manually stripmine so we can limit amount of
  2493. * vector work space reserved to LOOPCNT elements. Also
  2494. * makes vectorisation easy */
  2495. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  2496. ni=Min(nelems-j,LOOPCNT);
  2497. if (realign) {
  2498. memcpy(tmp, *xpp, ni*SIZEOF_SHORT);
  2499. xp = tmp;
  2500. } else {
  2501. xp = (short *) *xpp;
  2502. }
  2503. /* copy the next block */
  2504. #pragma cdir loopcnt=LOOPCNT
  2505. #pragma cdir shortloop
  2506. for (i=0; i<ni; i++) {
  2507. tp[i] = (schar) Max( SCHAR_MIN, Min(SCHAR_MAX, (schar) xp[i]));
  2508. /* test for range errors (not always needed but do it anyway) */
  2509. nrange += xp[i] < SCHAR_MIN || xp[i] > SCHAR_MAX;
  2510. }
  2511. /* update xpp and tp */
  2512. if (realign) xp = (short *) *xpp;
  2513. xp += ni;
  2514. tp += ni;
  2515. *xpp = (void*)xp;
  2516. }
  2517. return nrange == 0 ? ENOERR : NC_ERANGE;
  2518. #else /* not SX */
  2519. const char *xp = (const char *) *xpp;
  2520. int status = ENOERR;
  2521. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2522. {
  2523. const int lstatus = ncx_get_short_schar(xp, tp);
  2524. if(lstatus != ENOERR)
  2525. status = lstatus;
  2526. }
  2527. *xpp = (const void *)xp;
  2528. return status;
  2529. # endif
  2530. }
  2531. int
  2532. ncx_getn_short_uchar(const void **xpp, size_t nelems, uchar *tp)
  2533. {
  2534. #if _SX && \
  2535. X_SIZEOF_SHORT == SIZEOF_SHORT
  2536. /* basic algorithm is:
  2537. * - ensure sane alignment of input data
  2538. * - copy (conversion happens automatically) input data
  2539. * to output
  2540. * - update xpp to point at next unconverted input, and tp to point
  2541. * at next location for converted output
  2542. */
  2543. long i, j, ni;
  2544. short tmp[LOOPCNT]; /* in case input is misaligned */
  2545. short *xp;
  2546. int nrange = 0; /* number of range errors */
  2547. int realign = 0; /* "do we need to fix input data alignment?" */
  2548. long cxp = (long) *((char**)xpp);
  2549. realign = (cxp & 7) % SIZEOF_SHORT;
  2550. /* sjl: manually stripmine so we can limit amount of
  2551. * vector work space reserved to LOOPCNT elements. Also
  2552. * makes vectorisation easy */
  2553. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  2554. ni=Min(nelems-j,LOOPCNT);
  2555. if (realign) {
  2556. memcpy(tmp, *xpp, ni*SIZEOF_SHORT);
  2557. xp = tmp;
  2558. } else {
  2559. xp = (short *) *xpp;
  2560. }
  2561. /* copy the next block */
  2562. #pragma cdir loopcnt=LOOPCNT
  2563. #pragma cdir shortloop
  2564. for (i=0; i<ni; i++) {
  2565. tp[i] = (uchar) Max( UCHAR_MIN, Min(UCHAR_MAX, (uchar) xp[i]));
  2566. /* test for range errors (not always needed but do it anyway) */
  2567. nrange += xp[i] < UCHAR_MIN || xp[i] > UCHAR_MAX;
  2568. }
  2569. /* update xpp and tp */
  2570. if (realign) xp = (short *) *xpp;
  2571. xp += ni;
  2572. tp += ni;
  2573. *xpp = (void*)xp;
  2574. }
  2575. return nrange == 0 ? ENOERR : NC_ERANGE;
  2576. #else /* not SX */
  2577. const char *xp = (const char *) *xpp;
  2578. int status = ENOERR;
  2579. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2580. {
  2581. const int lstatus = ncx_get_short_uchar(xp, tp);
  2582. if(lstatus != ENOERR)
  2583. status = lstatus;
  2584. }
  2585. *xpp = (const void *)xp;
  2586. return status;
  2587. # endif
  2588. }
  2589. #if X_SIZEOF_SHORT == SIZEOF_SHORT
  2590. /* optimized version */
  2591. int
  2592. ncx_getn_short_short(const void **xpp, size_t nelems, short *tp)
  2593. {
  2594. #ifdef WORDS_BIGENDIAN
  2595. (void) memcpy(tp, *xpp, nelems * sizeof(short));
  2596. # else
  2597. swapn2b(tp, *xpp, nelems);
  2598. # endif
  2599. *xpp = (const void *)((const char *)(*xpp) + nelems * X_SIZEOF_SHORT);
  2600. return ENOERR;
  2601. }
  2602. #else
  2603. int
  2604. ncx_getn_short_short(const void **xpp, size_t nelems, short *tp)
  2605. {
  2606. #if _SX && \
  2607. X_SIZEOF_SHORT == SIZEOF_SHORT
  2608. /* basic algorithm is:
  2609. * - ensure sane alignment of input data
  2610. * - copy (conversion happens automatically) input data
  2611. * to output
  2612. * - update xpp to point at next unconverted input, and tp to point
  2613. * at next location for converted output
  2614. */
  2615. long i, j, ni;
  2616. short tmp[LOOPCNT]; /* in case input is misaligned */
  2617. short *xp;
  2618. int nrange = 0; /* number of range errors */
  2619. int realign = 0; /* "do we need to fix input data alignment?" */
  2620. long cxp = (long) *((char**)xpp);
  2621. realign = (cxp & 7) % SIZEOF_SHORT;
  2622. /* sjl: manually stripmine so we can limit amount of
  2623. * vector work space reserved to LOOPCNT elements. Also
  2624. * makes vectorisation easy */
  2625. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  2626. ni=Min(nelems-j,LOOPCNT);
  2627. if (realign) {
  2628. memcpy(tmp, *xpp, ni*SIZEOF_SHORT);
  2629. xp = tmp;
  2630. } else {
  2631. xp = (short *) *xpp;
  2632. }
  2633. /* copy the next block */
  2634. #pragma cdir loopcnt=LOOPCNT
  2635. #pragma cdir shortloop
  2636. for (i=0; i<ni; i++) {
  2637. tp[i] = (short) Max( SHORT_MIN, Min(SHORT_MAX, (short) xp[i]));
  2638. /* test for range errors (not always needed but do it anyway) */
  2639. nrange += xp[i] < SHORT_MIN || xp[i] > SHORT_MAX;
  2640. }
  2641. /* update xpp and tp */
  2642. if (realign) xp = (short *) *xpp;
  2643. xp += ni;
  2644. tp += ni;
  2645. *xpp = (void*)xp;
  2646. }
  2647. return nrange == 0 ? ENOERR : NC_ERANGE;
  2648. #else /* not SX */
  2649. const char *xp = (const char *) *xpp;
  2650. int status = ENOERR;
  2651. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2652. {
  2653. const int lstatus = ncx_get_short_short(xp, tp);
  2654. if(lstatus != ENOERR)
  2655. status = lstatus;
  2656. }
  2657. *xpp = (const void *)xp;
  2658. return status;
  2659. # endif
  2660. }
  2661. #endif
  2662. int
  2663. ncx_getn_short_int(const void **xpp, size_t nelems, int *tp)
  2664. {
  2665. #if _SX && \
  2666. X_SIZEOF_SHORT == SIZEOF_SHORT
  2667. /* basic algorithm is:
  2668. * - ensure sane alignment of input data
  2669. * - copy (conversion happens automatically) input data
  2670. * to output
  2671. * - update xpp to point at next unconverted input, and tp to point
  2672. * at next location for converted output
  2673. */
  2674. long i, j, ni;
  2675. short tmp[LOOPCNT]; /* in case input is misaligned */
  2676. short *xp;
  2677. int nrange = 0; /* number of range errors */
  2678. int realign = 0; /* "do we need to fix input data alignment?" */
  2679. long cxp = (long) *((char**)xpp);
  2680. realign = (cxp & 7) % SIZEOF_SHORT;
  2681. /* sjl: manually stripmine so we can limit amount of
  2682. * vector work space reserved to LOOPCNT elements. Also
  2683. * makes vectorisation easy */
  2684. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  2685. ni=Min(nelems-j,LOOPCNT);
  2686. if (realign) {
  2687. memcpy(tmp, *xpp, ni*SIZEOF_SHORT);
  2688. xp = tmp;
  2689. } else {
  2690. xp = (short *) *xpp;
  2691. }
  2692. /* copy the next block */
  2693. #pragma cdir loopcnt=LOOPCNT
  2694. #pragma cdir shortloop
  2695. for (i=0; i<ni; i++) {
  2696. tp[i] = (int) Max( INT_MIN, Min(INT_MAX, (int) xp[i]));
  2697. /* test for range errors (not always needed but do it anyway) */
  2698. nrange += xp[i] < INT_MIN || xp[i] > INT_MAX;
  2699. }
  2700. /* update xpp and tp */
  2701. if (realign) xp = (short *) *xpp;
  2702. xp += ni;
  2703. tp += ni;
  2704. *xpp = (void*)xp;
  2705. }
  2706. return nrange == 0 ? ENOERR : NC_ERANGE;
  2707. #else /* not SX */
  2708. const char *xp = (const char *) *xpp;
  2709. int status = ENOERR;
  2710. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2711. {
  2712. const int lstatus = ncx_get_short_int(xp, tp);
  2713. if(lstatus != ENOERR)
  2714. status = lstatus;
  2715. }
  2716. *xpp = (const void *)xp;
  2717. return status;
  2718. # endif
  2719. }
  2720. int
  2721. ncx_getn_short_float(const void **xpp, size_t nelems, float *tp)
  2722. {
  2723. #if _SX && \
  2724. X_SIZEOF_SHORT == SIZEOF_SHORT
  2725. /* basic algorithm is:
  2726. * - ensure sane alignment of input data
  2727. * - copy (conversion happens automatically) input data
  2728. * to output
  2729. * - update xpp to point at next unconverted input, and tp to point
  2730. * at next location for converted output
  2731. */
  2732. long i, j, ni;
  2733. short tmp[LOOPCNT]; /* in case input is misaligned */
  2734. short *xp;
  2735. int nrange = 0; /* number of range errors */
  2736. int realign = 0; /* "do we need to fix input data alignment?" */
  2737. long cxp = (long) *((char**)xpp);
  2738. realign = (cxp & 7) % SIZEOF_SHORT;
  2739. /* sjl: manually stripmine so we can limit amount of
  2740. * vector work space reserved to LOOPCNT elements. Also
  2741. * makes vectorisation easy */
  2742. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  2743. ni=Min(nelems-j,LOOPCNT);
  2744. if (realign) {
  2745. memcpy(tmp, *xpp, ni*SIZEOF_SHORT);
  2746. xp = tmp;
  2747. } else {
  2748. xp = (short *) *xpp;
  2749. }
  2750. /* copy the next block */
  2751. #pragma cdir loopcnt=LOOPCNT
  2752. #pragma cdir shortloop
  2753. for (i=0; i<ni; i++) {
  2754. tp[i] = (float) Max( FLOAT_MIN, Min(FLOAT_MAX, (float) xp[i]));
  2755. /* test for range errors (not always needed but do it anyway) */
  2756. nrange += xp[i] < FLOAT_MIN || xp[i] > FLOAT_MAX;
  2757. }
  2758. /* update xpp and tp */
  2759. if (realign) xp = (short *) *xpp;
  2760. xp += ni;
  2761. tp += ni;
  2762. *xpp = (void*)xp;
  2763. }
  2764. return nrange == 0 ? ENOERR : NC_ERANGE;
  2765. #else /* not SX */
  2766. const char *xp = (const char *) *xpp;
  2767. int status = ENOERR;
  2768. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2769. {
  2770. const int lstatus = ncx_get_short_float(xp, tp);
  2771. if(lstatus != ENOERR)
  2772. status = lstatus;
  2773. }
  2774. *xpp = (const void *)xp;
  2775. return status;
  2776. # endif
  2777. }
  2778. int
  2779. ncx_getn_short_double(const void **xpp, size_t nelems, double *tp)
  2780. {
  2781. #if _SX && \
  2782. X_SIZEOF_SHORT == SIZEOF_SHORT
  2783. /* basic algorithm is:
  2784. * - ensure sane alignment of input data
  2785. * - copy (conversion happens automatically) input data
  2786. * to output
  2787. * - update xpp to point at next unconverted input, and tp to point
  2788. * at next location for converted output
  2789. */
  2790. long i, j, ni;
  2791. short tmp[LOOPCNT]; /* in case input is misaligned */
  2792. short *xp;
  2793. int nrange = 0; /* number of range errors */
  2794. int realign = 0; /* "do we need to fix input data alignment?" */
  2795. long cxp = (long) *((char**)xpp);
  2796. realign = (cxp & 7) % SIZEOF_SHORT;
  2797. /* sjl: manually stripmine so we can limit amount of
  2798. * vector work space reserved to LOOPCNT elements. Also
  2799. * makes vectorisation easy */
  2800. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  2801. ni=Min(nelems-j,LOOPCNT);
  2802. if (realign) {
  2803. memcpy(tmp, *xpp, ni*SIZEOF_SHORT);
  2804. xp = tmp;
  2805. } else {
  2806. xp = (short *) *xpp;
  2807. }
  2808. /* copy the next block */
  2809. #pragma cdir loopcnt=LOOPCNT
  2810. #pragma cdir shortloop
  2811. for (i=0; i<ni; i++) {
  2812. tp[i] = (double) Max( DOUBLE_MIN, Min(DOUBLE_MAX, (double) xp[i]));
  2813. /* test for range errors (not always needed but do it anyway) */
  2814. nrange += xp[i] < DOUBLE_MIN || xp[i] > DOUBLE_MAX;
  2815. }
  2816. /* update xpp and tp */
  2817. if (realign) xp = (short *) *xpp;
  2818. xp += ni;
  2819. tp += ni;
  2820. *xpp = (void*)xp;
  2821. }
  2822. return nrange == 0 ? ENOERR : NC_ERANGE;
  2823. #else /* not SX */
  2824. const char *xp = (const char *) *xpp;
  2825. int status = ENOERR;
  2826. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2827. {
  2828. const int lstatus = ncx_get_short_double(xp, tp);
  2829. if(lstatus != ENOERR)
  2830. status = lstatus;
  2831. }
  2832. *xpp = (const void *)xp;
  2833. return status;
  2834. # endif
  2835. }
  2836. int
  2837. ncx_getn_short_uint(const void **xpp, size_t nelems, uint *tp)
  2838. {
  2839. #if _SX && \
  2840. X_SIZEOF_SHORT == SIZEOF_SHORT
  2841. /* basic algorithm is:
  2842. * - ensure sane alignment of input data
  2843. * - copy (conversion happens automatically) input data
  2844. * to output
  2845. * - update xpp to point at next unconverted input, and tp to point
  2846. * at next location for converted output
  2847. */
  2848. long i, j, ni;
  2849. short tmp[LOOPCNT]; /* in case input is misaligned */
  2850. short *xp;
  2851. int nrange = 0; /* number of range errors */
  2852. int realign = 0; /* "do we need to fix input data alignment?" */
  2853. long cxp = (long) *((char**)xpp);
  2854. realign = (cxp & 7) % SIZEOF_SHORT;
  2855. /* sjl: manually stripmine so we can limit amount of
  2856. * vector work space reserved to LOOPCNT elements. Also
  2857. * makes vectorisation easy */
  2858. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  2859. ni=Min(nelems-j,LOOPCNT);
  2860. if (realign) {
  2861. memcpy(tmp, *xpp, ni*SIZEOF_SHORT);
  2862. xp = tmp;
  2863. } else {
  2864. xp = (short *) *xpp;
  2865. }
  2866. /* copy the next block */
  2867. #pragma cdir loopcnt=LOOPCNT
  2868. #pragma cdir shortloop
  2869. for (i=0; i<ni; i++) {
  2870. tp[i] = (uint) Max( UINT_MIN, Min(UINT_MAX, (uint) xp[i]));
  2871. /* test for range errors (not always needed but do it anyway) */
  2872. nrange += xp[i] < UINT_MIN || xp[i] > UINT_MAX;
  2873. }
  2874. /* update xpp and tp */
  2875. if (realign) xp = (short *) *xpp;
  2876. xp += ni;
  2877. tp += ni;
  2878. *xpp = (void*)xp;
  2879. }
  2880. return nrange == 0 ? ENOERR : NC_ERANGE;
  2881. #else /* not SX */
  2882. const char *xp = (const char *) *xpp;
  2883. int status = ENOERR;
  2884. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2885. {
  2886. const int lstatus = ncx_get_short_uint(xp, tp);
  2887. if(lstatus != ENOERR)
  2888. status = lstatus;
  2889. }
  2890. *xpp = (const void *)xp;
  2891. return status;
  2892. # endif
  2893. }
  2894. int
  2895. ncx_getn_short_longlong(const void **xpp, size_t nelems, longlong *tp)
  2896. {
  2897. #if _SX && \
  2898. X_SIZEOF_SHORT == SIZEOF_SHORT
  2899. /* basic algorithm is:
  2900. * - ensure sane alignment of input data
  2901. * - copy (conversion happens automatically) input data
  2902. * to output
  2903. * - update xpp to point at next unconverted input, and tp to point
  2904. * at next location for converted output
  2905. */
  2906. long i, j, ni;
  2907. short tmp[LOOPCNT]; /* in case input is misaligned */
  2908. short *xp;
  2909. int nrange = 0; /* number of range errors */
  2910. int realign = 0; /* "do we need to fix input data alignment?" */
  2911. long cxp = (long) *((char**)xpp);
  2912. realign = (cxp & 7) % SIZEOF_SHORT;
  2913. /* sjl: manually stripmine so we can limit amount of
  2914. * vector work space reserved to LOOPCNT elements. Also
  2915. * makes vectorisation easy */
  2916. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  2917. ni=Min(nelems-j,LOOPCNT);
  2918. if (realign) {
  2919. memcpy(tmp, *xpp, ni*SIZEOF_SHORT);
  2920. xp = tmp;
  2921. } else {
  2922. xp = (short *) *xpp;
  2923. }
  2924. /* copy the next block */
  2925. #pragma cdir loopcnt=LOOPCNT
  2926. #pragma cdir shortloop
  2927. for (i=0; i<ni; i++) {
  2928. tp[i] = (longlong) Max( LONGLONG_MIN, Min(LONGLONG_MAX, (longlong) xp[i]));
  2929. /* test for range errors (not always needed but do it anyway) */
  2930. nrange += xp[i] < LONGLONG_MIN || xp[i] > LONGLONG_MAX;
  2931. }
  2932. /* update xpp and tp */
  2933. if (realign) xp = (short *) *xpp;
  2934. xp += ni;
  2935. tp += ni;
  2936. *xpp = (void*)xp;
  2937. }
  2938. return nrange == 0 ? ENOERR : NC_ERANGE;
  2939. #else /* not SX */
  2940. const char *xp = (const char *) *xpp;
  2941. int status = ENOERR;
  2942. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2943. {
  2944. const int lstatus = ncx_get_short_longlong(xp, tp);
  2945. if(lstatus != ENOERR)
  2946. status = lstatus;
  2947. }
  2948. *xpp = (const void *)xp;
  2949. return status;
  2950. # endif
  2951. }
  2952. int
  2953. ncx_getn_short_ulonglong(const void **xpp, size_t nelems, ulonglong *tp)
  2954. {
  2955. #if _SX && \
  2956. X_SIZEOF_SHORT == SIZEOF_SHORT
  2957. /* basic algorithm is:
  2958. * - ensure sane alignment of input data
  2959. * - copy (conversion happens automatically) input data
  2960. * to output
  2961. * - update xpp to point at next unconverted input, and tp to point
  2962. * at next location for converted output
  2963. */
  2964. long i, j, ni;
  2965. short tmp[LOOPCNT]; /* in case input is misaligned */
  2966. short *xp;
  2967. int nrange = 0; /* number of range errors */
  2968. int realign = 0; /* "do we need to fix input data alignment?" */
  2969. long cxp = (long) *((char**)xpp);
  2970. realign = (cxp & 7) % SIZEOF_SHORT;
  2971. /* sjl: manually stripmine so we can limit amount of
  2972. * vector work space reserved to LOOPCNT elements. Also
  2973. * makes vectorisation easy */
  2974. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  2975. ni=Min(nelems-j,LOOPCNT);
  2976. if (realign) {
  2977. memcpy(tmp, *xpp, ni*SIZEOF_SHORT);
  2978. xp = tmp;
  2979. } else {
  2980. xp = (short *) *xpp;
  2981. }
  2982. /* copy the next block */
  2983. #pragma cdir loopcnt=LOOPCNT
  2984. #pragma cdir shortloop
  2985. for (i=0; i<ni; i++) {
  2986. tp[i] = (ulonglong) Max( ULONGLONG_MIN, Min(ULONGLONG_MAX, (ulonglong) xp[i]));
  2987. /* test for range errors (not always needed but do it anyway) */
  2988. nrange += xp[i] < ULONGLONG_MIN || xp[i] > ULONGLONG_MAX;
  2989. }
  2990. /* update xpp and tp */
  2991. if (realign) xp = (short *) *xpp;
  2992. xp += ni;
  2993. tp += ni;
  2994. *xpp = (void*)xp;
  2995. }
  2996. return nrange == 0 ? ENOERR : NC_ERANGE;
  2997. #else /* not SX */
  2998. const char *xp = (const char *) *xpp;
  2999. int status = ENOERR;
  3000. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  3001. {
  3002. const int lstatus = ncx_get_short_ulonglong(xp, tp);
  3003. if(lstatus != ENOERR)
  3004. status = lstatus;
  3005. }
  3006. *xpp = (const void *)xp;
  3007. return status;
  3008. # endif
  3009. }
  3010. int
  3011. ncx_pad_getn_short_schar(const void **xpp, size_t nelems, schar *tp)
  3012. {
  3013. const size_t rndup = nelems % 2;
  3014. const char *xp = (const char *) *xpp;
  3015. int status = ENOERR;
  3016. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  3017. {
  3018. const int lstatus = ncx_get_short_schar(xp, tp);
  3019. if(lstatus != ENOERR)
  3020. status = lstatus;
  3021. }
  3022. if(rndup != 0)
  3023. xp += X_SIZEOF_SHORT;
  3024. *xpp = (void *)xp;
  3025. return status;
  3026. }
  3027. int
  3028. ncx_pad_getn_short_uchar(const void **xpp, size_t nelems, uchar *tp)
  3029. {
  3030. const size_t rndup = nelems % 2;
  3031. const char *xp = (const char *) *xpp;
  3032. int status = ENOERR;
  3033. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  3034. {
  3035. const int lstatus = ncx_get_short_uchar(xp, tp);
  3036. if(lstatus != ENOERR)
  3037. status = lstatus;
  3038. }
  3039. if(rndup != 0)
  3040. xp += X_SIZEOF_SHORT;
  3041. *xpp = (void *)xp;
  3042. return status;
  3043. }
  3044. int
  3045. ncx_pad_getn_short_short(const void **xpp, size_t nelems, short *tp)
  3046. {
  3047. const size_t rndup = nelems % 2;
  3048. const char *xp = (const char *) *xpp;
  3049. int status = ENOERR;
  3050. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  3051. {
  3052. const int lstatus = ncx_get_short_short(xp, tp);
  3053. if(lstatus != ENOERR)
  3054. status = lstatus;
  3055. }
  3056. if(rndup != 0)
  3057. xp += X_SIZEOF_SHORT;
  3058. *xpp = (void *)xp;
  3059. return status;
  3060. }
  3061. int
  3062. ncx_pad_getn_short_int(const void **xpp, size_t nelems, int *tp)
  3063. {
  3064. const size_t rndup = nelems % 2;
  3065. const char *xp = (const char *) *xpp;
  3066. int status = ENOERR;
  3067. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  3068. {
  3069. const int lstatus = ncx_get_short_int(xp, tp);
  3070. if(lstatus != ENOERR)
  3071. status = lstatus;
  3072. }
  3073. if(rndup != 0)
  3074. xp += X_SIZEOF_SHORT;
  3075. *xpp = (void *)xp;
  3076. return status;
  3077. }
  3078. int
  3079. ncx_pad_getn_short_float(const void **xpp, size_t nelems, float *tp)
  3080. {
  3081. const size_t rndup = nelems % 2;
  3082. const char *xp = (const char *) *xpp;
  3083. int status = ENOERR;
  3084. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  3085. {
  3086. const int lstatus = ncx_get_short_float(xp, tp);
  3087. if(lstatus != ENOERR)
  3088. status = lstatus;
  3089. }
  3090. if(rndup != 0)
  3091. xp += X_SIZEOF_SHORT;
  3092. *xpp = (void *)xp;
  3093. return status;
  3094. }
  3095. int
  3096. ncx_pad_getn_short_double(const void **xpp, size_t nelems, double *tp)
  3097. {
  3098. const size_t rndup = nelems % 2;
  3099. const char *xp = (const char *) *xpp;
  3100. int status = ENOERR;
  3101. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  3102. {
  3103. const int lstatus = ncx_get_short_double(xp, tp);
  3104. if(lstatus != ENOERR)
  3105. status = lstatus;
  3106. }
  3107. if(rndup != 0)
  3108. xp += X_SIZEOF_SHORT;
  3109. *xpp = (void *)xp;
  3110. return status;
  3111. }
  3112. int
  3113. ncx_pad_getn_short_uint(const void **xpp, size_t nelems, uint *tp)
  3114. {
  3115. const size_t rndup = nelems % 2;
  3116. const char *xp = (const char *) *xpp;
  3117. int status = ENOERR;
  3118. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  3119. {
  3120. const int lstatus = ncx_get_short_uint(xp, tp);
  3121. if(lstatus != ENOERR)
  3122. status = lstatus;
  3123. }
  3124. if(rndup != 0)
  3125. xp += X_SIZEOF_SHORT;
  3126. *xpp = (void *)xp;
  3127. return status;
  3128. }
  3129. int
  3130. ncx_pad_getn_short_longlong(const void **xpp, size_t nelems, longlong *tp)
  3131. {
  3132. const size_t rndup = nelems % 2;
  3133. const char *xp = (const char *) *xpp;
  3134. int status = ENOERR;
  3135. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  3136. {
  3137. const int lstatus = ncx_get_short_longlong(xp, tp);
  3138. if(lstatus != ENOERR)
  3139. status = lstatus;
  3140. }
  3141. if(rndup != 0)
  3142. xp += X_SIZEOF_SHORT;
  3143. *xpp = (void *)xp;
  3144. return status;
  3145. }
  3146. int
  3147. ncx_pad_getn_short_ulonglong(const void **xpp, size_t nelems, ulonglong *tp)
  3148. {
  3149. const size_t rndup = nelems % 2;
  3150. const char *xp = (const char *) *xpp;
  3151. int status = ENOERR;
  3152. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  3153. {
  3154. const int lstatus = ncx_get_short_ulonglong(xp, tp);
  3155. if(lstatus != ENOERR)
  3156. status = lstatus;
  3157. }
  3158. if(rndup != 0)
  3159. xp += X_SIZEOF_SHORT;
  3160. *xpp = (void *)xp;
  3161. return status;
  3162. }
  3163. int
  3164. ncx_putn_short_schar(void **xpp, size_t nelems, const schar *tp)
  3165. {
  3166. #if _SX && \
  3167. X_SIZEOF_SHORT == SIZEOF_SHORT
  3168. /* basic algorithm is:
  3169. * - ensure sane alignment of output data
  3170. * - copy (conversion happens automatically) input data
  3171. * to output
  3172. * - update tp to point at next unconverted input, and xpp to point
  3173. * at next location for converted output
  3174. */
  3175. long i, j, ni;
  3176. short tmp[LOOPCNT]; /* in case input is misaligned */
  3177. short *xp;
  3178. int nrange = 0; /* number of range errors */
  3179. int realign = 0; /* "do we need to fix input data alignment?" */
  3180. long cxp = (long) *((char**)xpp);
  3181. realign = (cxp & 7) % SIZEOF_SHORT;
  3182. /* sjl: manually stripmine so we can limit amount of
  3183. * vector work space reserved to LOOPCNT elements. Also
  3184. * makes vectorisation easy */
  3185. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  3186. ni=Min(nelems-j,LOOPCNT);
  3187. if (realign) {
  3188. xp = tmp;
  3189. } else {
  3190. xp = (short *) *xpp;
  3191. }
  3192. /* copy the next block */
  3193. #pragma cdir loopcnt=LOOPCNT
  3194. #pragma cdir shortloop
  3195. for (i=0; i<ni; i++) {
  3196. /* the normal case: */
  3197. xp[i] = (short) Max( X_SHORT_MIN, Min(X_SHORT_MAX, (short) tp[i]));
  3198. /* test for range errors (not always needed but do it anyway) */
  3199. nrange += tp[i] < X_SHORT_MIN || tp[i] > X_SHORT_MAX;
  3200. }
  3201. /* copy workspace back if necessary */
  3202. if (realign) {
  3203. memcpy(*xpp, tmp, ni*X_SIZEOF_SHORT);
  3204. xp = (short *) *xpp;
  3205. }
  3206. /* update xpp and tp */
  3207. xp += ni;
  3208. tp += ni;
  3209. *xpp = (void*)xp;
  3210. }
  3211. return nrange == 0 ? ENOERR : NC_ERANGE;
  3212. #else /* not SX */
  3213. char *xp = (char *) *xpp;
  3214. int status = ENOERR;
  3215. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  3216. {
  3217. int lstatus = ncx_put_short_schar(xp, tp);
  3218. if(lstatus != ENOERR)
  3219. status = lstatus;
  3220. }
  3221. *xpp = (void *)xp;
  3222. return status;
  3223. #endif
  3224. }
  3225. int
  3226. ncx_putn_short_uchar(void **xpp, size_t nelems, const uchar *tp)
  3227. {
  3228. #if _SX && \
  3229. X_SIZEOF_SHORT == SIZEOF_SHORT
  3230. /* basic algorithm is:
  3231. * - ensure sane alignment of output data
  3232. * - copy (conversion happens automatically) input data
  3233. * to output
  3234. * - update tp to point at next unconverted input, and xpp to point
  3235. * at next location for converted output
  3236. */
  3237. long i, j, ni;
  3238. short tmp[LOOPCNT]; /* in case input is misaligned */
  3239. short *xp;
  3240. int nrange = 0; /* number of range errors */
  3241. int realign = 0; /* "do we need to fix input data alignment?" */
  3242. long cxp = (long) *((char**)xpp);
  3243. realign = (cxp & 7) % SIZEOF_SHORT;
  3244. /* sjl: manually stripmine so we can limit amount of
  3245. * vector work space reserved to LOOPCNT elements. Also
  3246. * makes vectorisation easy */
  3247. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  3248. ni=Min(nelems-j,LOOPCNT);
  3249. if (realign) {
  3250. xp = tmp;
  3251. } else {
  3252. xp = (short *) *xpp;
  3253. }
  3254. /* copy the next block */
  3255. #pragma cdir loopcnt=LOOPCNT
  3256. #pragma cdir shortloop
  3257. for (i=0; i<ni; i++) {
  3258. /* the normal case: */
  3259. xp[i] = (short) Max( X_SHORT_MIN, Min(X_SHORT_MAX, (short) tp[i]));
  3260. /* test for range errors (not always needed but do it anyway) */
  3261. nrange += tp[i] < X_SHORT_MIN || tp[i] > X_SHORT_MAX;
  3262. }
  3263. /* copy workspace back if necessary */
  3264. if (realign) {
  3265. memcpy(*xpp, tmp, ni*X_SIZEOF_SHORT);
  3266. xp = (short *) *xpp;
  3267. }
  3268. /* update xpp and tp */
  3269. xp += ni;
  3270. tp += ni;
  3271. *xpp = (void*)xp;
  3272. }
  3273. return nrange == 0 ? ENOERR : NC_ERANGE;
  3274. #else /* not SX */
  3275. char *xp = (char *) *xpp;
  3276. int status = ENOERR;
  3277. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  3278. {
  3279. int lstatus = ncx_put_short_uchar(xp, tp);
  3280. if(lstatus != ENOERR)
  3281. status = lstatus;
  3282. }
  3283. *xpp = (void *)xp;
  3284. return status;
  3285. #endif
  3286. }
  3287. #if X_SIZEOF_SHORT == SIZEOF_SHORT
  3288. /* optimized version */
  3289. int
  3290. ncx_putn_short_short(void **xpp, size_t nelems, const short *tp)
  3291. {
  3292. #ifdef WORDS_BIGENDIAN
  3293. (void) memcpy(*xpp, tp, nelems * X_SIZEOF_SHORT);
  3294. # else
  3295. swapn2b(*xpp, tp, nelems);
  3296. # endif
  3297. *xpp = (void *)((char *)(*xpp) + nelems * X_SIZEOF_SHORT);
  3298. return ENOERR;
  3299. }
  3300. #else
  3301. int
  3302. ncx_putn_short_short(void **xpp, size_t nelems, const short *tp)
  3303. {
  3304. #if _SX && \
  3305. X_SIZEOF_SHORT == SIZEOF_SHORT
  3306. /* basic algorithm is:
  3307. * - ensure sane alignment of output data
  3308. * - copy (conversion happens automatically) input data
  3309. * to output
  3310. * - update tp to point at next unconverted input, and xpp to point
  3311. * at next location for converted output
  3312. */
  3313. long i, j, ni;
  3314. short tmp[LOOPCNT]; /* in case input is misaligned */
  3315. short *xp;
  3316. int nrange = 0; /* number of range errors */
  3317. int realign = 0; /* "do we need to fix input data alignment?" */
  3318. long cxp = (long) *((char**)xpp);
  3319. realign = (cxp & 7) % SIZEOF_SHORT;
  3320. /* sjl: manually stripmine so we can limit amount of
  3321. * vector work space reserved to LOOPCNT elements. Also
  3322. * makes vectorisation easy */
  3323. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  3324. ni=Min(nelems-j,LOOPCNT);
  3325. if (realign) {
  3326. xp = tmp;
  3327. } else {
  3328. xp = (short *) *xpp;
  3329. }
  3330. /* copy the next block */
  3331. #pragma cdir loopcnt=LOOPCNT
  3332. #pragma cdir shortloop
  3333. for (i=0; i<ni; i++) {
  3334. /* the normal case: */
  3335. xp[i] = (short) Max( X_SHORT_MIN, Min(X_SHORT_MAX, (short) tp[i]));
  3336. /* test for range errors (not always needed but do it anyway) */
  3337. nrange += tp[i] < X_SHORT_MIN || tp[i] > X_SHORT_MAX;
  3338. }
  3339. /* copy workspace back if necessary */
  3340. if (realign) {
  3341. memcpy(*xpp, tmp, ni*X_SIZEOF_SHORT);
  3342. xp = (short *) *xpp;
  3343. }
  3344. /* update xpp and tp */
  3345. xp += ni;
  3346. tp += ni;
  3347. *xpp = (void*)xp;
  3348. }
  3349. return nrange == 0 ? ENOERR : NC_ERANGE;
  3350. #else /* not SX */
  3351. char *xp = (char *) *xpp;
  3352. int status = ENOERR;
  3353. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  3354. {
  3355. int lstatus = ncx_put_short_short(xp, tp);
  3356. if(lstatus != ENOERR)
  3357. status = lstatus;
  3358. }
  3359. *xpp = (void *)xp;
  3360. return status;
  3361. #endif
  3362. }
  3363. #endif
  3364. int
  3365. ncx_putn_short_int(void **xpp, size_t nelems, const int *tp)
  3366. {
  3367. #if _SX && \
  3368. X_SIZEOF_SHORT == SIZEOF_SHORT
  3369. /* basic algorithm is:
  3370. * - ensure sane alignment of output data
  3371. * - copy (conversion happens automatically) input data
  3372. * to output
  3373. * - update tp to point at next unconverted input, and xpp to point
  3374. * at next location for converted output
  3375. */
  3376. long i, j, ni;
  3377. short tmp[LOOPCNT]; /* in case input is misaligned */
  3378. short *xp;
  3379. int nrange = 0; /* number of range errors */
  3380. int realign = 0; /* "do we need to fix input data alignment?" */
  3381. long cxp = (long) *((char**)xpp);
  3382. realign = (cxp & 7) % SIZEOF_SHORT;
  3383. /* sjl: manually stripmine so we can limit amount of
  3384. * vector work space reserved to LOOPCNT elements. Also
  3385. * makes vectorisation easy */
  3386. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  3387. ni=Min(nelems-j,LOOPCNT);
  3388. if (realign) {
  3389. xp = tmp;
  3390. } else {
  3391. xp = (short *) *xpp;
  3392. }
  3393. /* copy the next block */
  3394. #pragma cdir loopcnt=LOOPCNT
  3395. #pragma cdir shortloop
  3396. for (i=0; i<ni; i++) {
  3397. /* the normal case: */
  3398. xp[i] = (short) Max( X_SHORT_MIN, Min(X_SHORT_MAX, (short) tp[i]));
  3399. /* test for range errors (not always needed but do it anyway) */
  3400. nrange += tp[i] < X_SHORT_MIN || tp[i] > X_SHORT_MAX;
  3401. }
  3402. /* copy workspace back if necessary */
  3403. if (realign) {
  3404. memcpy(*xpp, tmp, ni*X_SIZEOF_SHORT);
  3405. xp = (short *) *xpp;
  3406. }
  3407. /* update xpp and tp */
  3408. xp += ni;
  3409. tp += ni;
  3410. *xpp = (void*)xp;
  3411. }
  3412. return nrange == 0 ? ENOERR : NC_ERANGE;
  3413. #else /* not SX */
  3414. char *xp = (char *) *xpp;
  3415. int status = ENOERR;
  3416. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  3417. {
  3418. int lstatus = ncx_put_short_int(xp, tp);
  3419. if(lstatus != ENOERR)
  3420. status = lstatus;
  3421. }
  3422. *xpp = (void *)xp;
  3423. return status;
  3424. #endif
  3425. }
  3426. int
  3427. ncx_putn_short_float(void **xpp, size_t nelems, const float *tp)
  3428. {
  3429. #if _SX && \
  3430. X_SIZEOF_SHORT == SIZEOF_SHORT
  3431. /* basic algorithm is:
  3432. * - ensure sane alignment of output data
  3433. * - copy (conversion happens automatically) input data
  3434. * to output
  3435. * - update tp to point at next unconverted input, and xpp to point
  3436. * at next location for converted output
  3437. */
  3438. long i, j, ni;
  3439. short tmp[LOOPCNT]; /* in case input is misaligned */
  3440. short *xp;
  3441. int nrange = 0; /* number of range errors */
  3442. int realign = 0; /* "do we need to fix input data alignment?" */
  3443. long cxp = (long) *((char**)xpp);
  3444. realign = (cxp & 7) % SIZEOF_SHORT;
  3445. /* sjl: manually stripmine so we can limit amount of
  3446. * vector work space reserved to LOOPCNT elements. Also
  3447. * makes vectorisation easy */
  3448. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  3449. ni=Min(nelems-j,LOOPCNT);
  3450. if (realign) {
  3451. xp = tmp;
  3452. } else {
  3453. xp = (short *) *xpp;
  3454. }
  3455. /* copy the next block */
  3456. #pragma cdir loopcnt=LOOPCNT
  3457. #pragma cdir shortloop
  3458. for (i=0; i<ni; i++) {
  3459. /* the normal case: */
  3460. xp[i] = (short) Max( X_SHORT_MIN, Min(X_SHORT_MAX, (short) tp[i]));
  3461. /* test for range errors (not always needed but do it anyway) */
  3462. nrange += tp[i] < X_SHORT_MIN || tp[i] > X_SHORT_MAX;
  3463. }
  3464. /* copy workspace back if necessary */
  3465. if (realign) {
  3466. memcpy(*xpp, tmp, ni*X_SIZEOF_SHORT);
  3467. xp = (short *) *xpp;
  3468. }
  3469. /* update xpp and tp */
  3470. xp += ni;
  3471. tp += ni;
  3472. *xpp = (void*)xp;
  3473. }
  3474. return nrange == 0 ? ENOERR : NC_ERANGE;
  3475. #else /* not SX */
  3476. char *xp = (char *) *xpp;
  3477. int status = ENOERR;
  3478. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  3479. {
  3480. int lstatus = ncx_put_short_float(xp, tp);
  3481. if(lstatus != ENOERR)
  3482. status = lstatus;
  3483. }
  3484. *xpp = (void *)xp;
  3485. return status;
  3486. #endif
  3487. }
  3488. int
  3489. ncx_putn_short_double(void **xpp, size_t nelems, const double *tp)
  3490. {
  3491. #if _SX && \
  3492. X_SIZEOF_SHORT == SIZEOF_SHORT
  3493. /* basic algorithm is:
  3494. * - ensure sane alignment of output data
  3495. * - copy (conversion happens automatically) input data
  3496. * to output
  3497. * - update tp to point at next unconverted input, and xpp to point
  3498. * at next location for converted output
  3499. */
  3500. long i, j, ni;
  3501. short tmp[LOOPCNT]; /* in case input is misaligned */
  3502. short *xp;
  3503. int nrange = 0; /* number of range errors */
  3504. int realign = 0; /* "do we need to fix input data alignment?" */
  3505. long cxp = (long) *((char**)xpp);
  3506. realign = (cxp & 7) % SIZEOF_SHORT;
  3507. /* sjl: manually stripmine so we can limit amount of
  3508. * vector work space reserved to LOOPCNT elements. Also
  3509. * makes vectorisation easy */
  3510. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  3511. ni=Min(nelems-j,LOOPCNT);
  3512. if (realign) {
  3513. xp = tmp;
  3514. } else {
  3515. xp = (short *) *xpp;
  3516. }
  3517. /* copy the next block */
  3518. #pragma cdir loopcnt=LOOPCNT
  3519. #pragma cdir shortloop
  3520. for (i=0; i<ni; i++) {
  3521. /* the normal case: */
  3522. xp[i] = (short) Max( X_SHORT_MIN, Min(X_SHORT_MAX, (short) tp[i]));
  3523. /* test for range errors (not always needed but do it anyway) */
  3524. nrange += tp[i] < X_SHORT_MIN || tp[i] > X_SHORT_MAX;
  3525. }
  3526. /* copy workspace back if necessary */
  3527. if (realign) {
  3528. memcpy(*xpp, tmp, ni*X_SIZEOF_SHORT);
  3529. xp = (short *) *xpp;
  3530. }
  3531. /* update xpp and tp */
  3532. xp += ni;
  3533. tp += ni;
  3534. *xpp = (void*)xp;
  3535. }
  3536. return nrange == 0 ? ENOERR : NC_ERANGE;
  3537. #else /* not SX */
  3538. char *xp = (char *) *xpp;
  3539. int status = ENOERR;
  3540. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  3541. {
  3542. int lstatus = ncx_put_short_double(xp, tp);
  3543. if(lstatus != ENOERR)
  3544. status = lstatus;
  3545. }
  3546. *xpp = (void *)xp;
  3547. return status;
  3548. #endif
  3549. }
  3550. int
  3551. ncx_putn_short_uint(void **xpp, size_t nelems, const uint *tp)
  3552. {
  3553. #if _SX && \
  3554. X_SIZEOF_SHORT == SIZEOF_SHORT
  3555. /* basic algorithm is:
  3556. * - ensure sane alignment of output data
  3557. * - copy (conversion happens automatically) input data
  3558. * to output
  3559. * - update tp to point at next unconverted input, and xpp to point
  3560. * at next location for converted output
  3561. */
  3562. long i, j, ni;
  3563. short tmp[LOOPCNT]; /* in case input is misaligned */
  3564. short *xp;
  3565. int nrange = 0; /* number of range errors */
  3566. int realign = 0; /* "do we need to fix input data alignment?" */
  3567. long cxp = (long) *((char**)xpp);
  3568. realign = (cxp & 7) % SIZEOF_SHORT;
  3569. /* sjl: manually stripmine so we can limit amount of
  3570. * vector work space reserved to LOOPCNT elements. Also
  3571. * makes vectorisation easy */
  3572. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  3573. ni=Min(nelems-j,LOOPCNT);
  3574. if (realign) {
  3575. xp = tmp;
  3576. } else {
  3577. xp = (short *) *xpp;
  3578. }
  3579. /* copy the next block */
  3580. #pragma cdir loopcnt=LOOPCNT
  3581. #pragma cdir shortloop
  3582. for (i=0; i<ni; i++) {
  3583. /* the normal case: */
  3584. xp[i] = (short) Max( X_SHORT_MIN, Min(X_SHORT_MAX, (short) tp[i]));
  3585. /* test for range errors (not always needed but do it anyway) */
  3586. nrange += tp[i] < X_SHORT_MIN || tp[i] > X_SHORT_MAX;
  3587. }
  3588. /* copy workspace back if necessary */
  3589. if (realign) {
  3590. memcpy(*xpp, tmp, ni*X_SIZEOF_SHORT);
  3591. xp = (short *) *xpp;
  3592. }
  3593. /* update xpp and tp */
  3594. xp += ni;
  3595. tp += ni;
  3596. *xpp = (void*)xp;
  3597. }
  3598. return nrange == 0 ? ENOERR : NC_ERANGE;
  3599. #else /* not SX */
  3600. char *xp = (char *) *xpp;
  3601. int status = ENOERR;
  3602. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  3603. {
  3604. int lstatus = ncx_put_short_uint(xp, tp);
  3605. if(lstatus != ENOERR)
  3606. status = lstatus;
  3607. }
  3608. *xpp = (void *)xp;
  3609. return status;
  3610. #endif
  3611. }
  3612. int
  3613. ncx_putn_short_longlong(void **xpp, size_t nelems, const longlong *tp)
  3614. {
  3615. #if _SX && \
  3616. X_SIZEOF_SHORT == SIZEOF_SHORT
  3617. /* basic algorithm is:
  3618. * - ensure sane alignment of output data
  3619. * - copy (conversion happens automatically) input data
  3620. * to output
  3621. * - update tp to point at next unconverted input, and xpp to point
  3622. * at next location for converted output
  3623. */
  3624. long i, j, ni;
  3625. short tmp[LOOPCNT]; /* in case input is misaligned */
  3626. short *xp;
  3627. int nrange = 0; /* number of range errors */
  3628. int realign = 0; /* "do we need to fix input data alignment?" */
  3629. long cxp = (long) *((char**)xpp);
  3630. realign = (cxp & 7) % SIZEOF_SHORT;
  3631. /* sjl: manually stripmine so we can limit amount of
  3632. * vector work space reserved to LOOPCNT elements. Also
  3633. * makes vectorisation easy */
  3634. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  3635. ni=Min(nelems-j,LOOPCNT);
  3636. if (realign) {
  3637. xp = tmp;
  3638. } else {
  3639. xp = (short *) *xpp;
  3640. }
  3641. /* copy the next block */
  3642. #pragma cdir loopcnt=LOOPCNT
  3643. #pragma cdir shortloop
  3644. for (i=0; i<ni; i++) {
  3645. /* the normal case: */
  3646. xp[i] = (short) Max( X_SHORT_MIN, Min(X_SHORT_MAX, (short) tp[i]));
  3647. /* test for range errors (not always needed but do it anyway) */
  3648. nrange += tp[i] < X_SHORT_MIN || tp[i] > X_SHORT_MAX;
  3649. }
  3650. /* copy workspace back if necessary */
  3651. if (realign) {
  3652. memcpy(*xpp, tmp, ni*X_SIZEOF_SHORT);
  3653. xp = (short *) *xpp;
  3654. }
  3655. /* update xpp and tp */
  3656. xp += ni;
  3657. tp += ni;
  3658. *xpp = (void*)xp;
  3659. }
  3660. return nrange == 0 ? ENOERR : NC_ERANGE;
  3661. #else /* not SX */
  3662. char *xp = (char *) *xpp;
  3663. int status = ENOERR;
  3664. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  3665. {
  3666. int lstatus = ncx_put_short_longlong(xp, tp);
  3667. if(lstatus != ENOERR)
  3668. status = lstatus;
  3669. }
  3670. *xpp = (void *)xp;
  3671. return status;
  3672. #endif
  3673. }
  3674. int
  3675. ncx_putn_short_ulonglong(void **xpp, size_t nelems, const ulonglong *tp)
  3676. {
  3677. #if _SX && \
  3678. X_SIZEOF_SHORT == SIZEOF_SHORT
  3679. /* basic algorithm is:
  3680. * - ensure sane alignment of output data
  3681. * - copy (conversion happens automatically) input data
  3682. * to output
  3683. * - update tp to point at next unconverted input, and xpp to point
  3684. * at next location for converted output
  3685. */
  3686. long i, j, ni;
  3687. short tmp[LOOPCNT]; /* in case input is misaligned */
  3688. short *xp;
  3689. int nrange = 0; /* number of range errors */
  3690. int realign = 0; /* "do we need to fix input data alignment?" */
  3691. long cxp = (long) *((char**)xpp);
  3692. realign = (cxp & 7) % SIZEOF_SHORT;
  3693. /* sjl: manually stripmine so we can limit amount of
  3694. * vector work space reserved to LOOPCNT elements. Also
  3695. * makes vectorisation easy */
  3696. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  3697. ni=Min(nelems-j,LOOPCNT);
  3698. if (realign) {
  3699. xp = tmp;
  3700. } else {
  3701. xp = (short *) *xpp;
  3702. }
  3703. /* copy the next block */
  3704. #pragma cdir loopcnt=LOOPCNT
  3705. #pragma cdir shortloop
  3706. for (i=0; i<ni; i++) {
  3707. /* the normal case: */
  3708. xp[i] = (short) Max( X_SHORT_MIN, Min(X_SHORT_MAX, (short) tp[i]));
  3709. /* test for range errors (not always needed but do it anyway) */
  3710. nrange += tp[i] < X_SHORT_MIN || tp[i] > X_SHORT_MAX;
  3711. }
  3712. /* copy workspace back if necessary */
  3713. if (realign) {
  3714. memcpy(*xpp, tmp, ni*X_SIZEOF_SHORT);
  3715. xp = (short *) *xpp;
  3716. }
  3717. /* update xpp and tp */
  3718. xp += ni;
  3719. tp += ni;
  3720. *xpp = (void*)xp;
  3721. }
  3722. return nrange == 0 ? ENOERR : NC_ERANGE;
  3723. #else /* not SX */
  3724. char *xp = (char *) *xpp;
  3725. int status = ENOERR;
  3726. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  3727. {
  3728. int lstatus = ncx_put_short_ulonglong(xp, tp);
  3729. if(lstatus != ENOERR)
  3730. status = lstatus;
  3731. }
  3732. *xpp = (void *)xp;
  3733. return status;
  3734. #endif
  3735. }
  3736. int
  3737. ncx_pad_putn_short_schar(void **xpp, size_t nelems, const schar *tp)
  3738. {
  3739. const size_t rndup = nelems % 2;
  3740. char *xp = (char *) *xpp;
  3741. int status = ENOERR;
  3742. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  3743. {
  3744. int lstatus = ncx_put_short_schar(xp, tp);
  3745. if(lstatus != ENOERR)
  3746. status = lstatus;
  3747. }
  3748. if(rndup != 0)
  3749. {
  3750. (void) memcpy(xp, nada, X_SIZEOF_SHORT);
  3751. xp += X_SIZEOF_SHORT;
  3752. }
  3753. *xpp = (void *)xp;
  3754. return status;
  3755. }
  3756. int
  3757. ncx_pad_putn_short_uchar(void **xpp, size_t nelems, const uchar *tp)
  3758. {
  3759. const size_t rndup = nelems % 2;
  3760. char *xp = (char *) *xpp;
  3761. int status = ENOERR;
  3762. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  3763. {
  3764. int lstatus = ncx_put_short_uchar(xp, tp);
  3765. if(lstatus != ENOERR)
  3766. status = lstatus;
  3767. }
  3768. if(rndup != 0)
  3769. {
  3770. (void) memcpy(xp, nada, X_SIZEOF_SHORT);
  3771. xp += X_SIZEOF_SHORT;
  3772. }
  3773. *xpp = (void *)xp;
  3774. return status;
  3775. }
  3776. int
  3777. ncx_pad_putn_short_short(void **xpp, size_t nelems, const short *tp)
  3778. {
  3779. const size_t rndup = nelems % 2;
  3780. char *xp = (char *) *xpp;
  3781. int status = ENOERR;
  3782. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  3783. {
  3784. int lstatus = ncx_put_short_short(xp, tp);
  3785. if(lstatus != ENOERR)
  3786. status = lstatus;
  3787. }
  3788. if(rndup != 0)
  3789. {
  3790. (void) memcpy(xp, nada, X_SIZEOF_SHORT);
  3791. xp += X_SIZEOF_SHORT;
  3792. }
  3793. *xpp = (void *)xp;
  3794. return status;
  3795. }
  3796. int
  3797. ncx_pad_putn_short_int(void **xpp, size_t nelems, const int *tp)
  3798. {
  3799. const size_t rndup = nelems % 2;
  3800. char *xp = (char *) *xpp;
  3801. int status = ENOERR;
  3802. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  3803. {
  3804. int lstatus = ncx_put_short_int(xp, tp);
  3805. if(lstatus != ENOERR)
  3806. status = lstatus;
  3807. }
  3808. if(rndup != 0)
  3809. {
  3810. (void) memcpy(xp, nada, X_SIZEOF_SHORT);
  3811. xp += X_SIZEOF_SHORT;
  3812. }
  3813. *xpp = (void *)xp;
  3814. return status;
  3815. }
  3816. int
  3817. ncx_pad_putn_short_float(void **xpp, size_t nelems, const float *tp)
  3818. {
  3819. const size_t rndup = nelems % 2;
  3820. char *xp = (char *) *xpp;
  3821. int status = ENOERR;
  3822. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  3823. {
  3824. int lstatus = ncx_put_short_float(xp, tp);
  3825. if(lstatus != ENOERR)
  3826. status = lstatus;
  3827. }
  3828. if(rndup != 0)
  3829. {
  3830. (void) memcpy(xp, nada, X_SIZEOF_SHORT);
  3831. xp += X_SIZEOF_SHORT;
  3832. }
  3833. *xpp = (void *)xp;
  3834. return status;
  3835. }
  3836. int
  3837. ncx_pad_putn_short_double(void **xpp, size_t nelems, const double *tp)
  3838. {
  3839. const size_t rndup = nelems % 2;
  3840. char *xp = (char *) *xpp;
  3841. int status = ENOERR;
  3842. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  3843. {
  3844. int lstatus = ncx_put_short_double(xp, tp);
  3845. if(lstatus != ENOERR)
  3846. status = lstatus;
  3847. }
  3848. if(rndup != 0)
  3849. {
  3850. (void) memcpy(xp, nada, X_SIZEOF_SHORT);
  3851. xp += X_SIZEOF_SHORT;
  3852. }
  3853. *xpp = (void *)xp;
  3854. return status;
  3855. }
  3856. int
  3857. ncx_pad_putn_short_uint(void **xpp, size_t nelems, const uint *tp)
  3858. {
  3859. const size_t rndup = nelems % 2;
  3860. char *xp = (char *) *xpp;
  3861. int status = ENOERR;
  3862. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  3863. {
  3864. int lstatus = ncx_put_short_uint(xp, tp);
  3865. if(lstatus != ENOERR)
  3866. status = lstatus;
  3867. }
  3868. if(rndup != 0)
  3869. {
  3870. (void) memcpy(xp, nada, X_SIZEOF_SHORT);
  3871. xp += X_SIZEOF_SHORT;
  3872. }
  3873. *xpp = (void *)xp;
  3874. return status;
  3875. }
  3876. int
  3877. ncx_pad_putn_short_longlong(void **xpp, size_t nelems, const longlong *tp)
  3878. {
  3879. const size_t rndup = nelems % 2;
  3880. char *xp = (char *) *xpp;
  3881. int status = ENOERR;
  3882. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  3883. {
  3884. int lstatus = ncx_put_short_longlong(xp, tp);
  3885. if(lstatus != ENOERR)
  3886. status = lstatus;
  3887. }
  3888. if(rndup != 0)
  3889. {
  3890. (void) memcpy(xp, nada, X_SIZEOF_SHORT);
  3891. xp += X_SIZEOF_SHORT;
  3892. }
  3893. *xpp = (void *)xp;
  3894. return status;
  3895. }
  3896. int
  3897. ncx_pad_putn_short_ulonglong(void **xpp, size_t nelems, const ulonglong *tp)
  3898. {
  3899. const size_t rndup = nelems % 2;
  3900. char *xp = (char *) *xpp;
  3901. int status = ENOERR;
  3902. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  3903. {
  3904. int lstatus = ncx_put_short_ulonglong(xp, tp);
  3905. if(lstatus != ENOERR)
  3906. status = lstatus;
  3907. }
  3908. if(rndup != 0)
  3909. {
  3910. (void) memcpy(xp, nada, X_SIZEOF_SHORT);
  3911. xp += X_SIZEOF_SHORT;
  3912. }
  3913. *xpp = (void *)xp;
  3914. return status;
  3915. }
  3916. /* int */
  3917. int
  3918. ncx_getn_int_schar(const void **xpp, size_t nelems, schar *tp)
  3919. {
  3920. #if _SX && \
  3921. X_SIZEOF_INT == SIZEOF_INT
  3922. /* basic algorithm is:
  3923. * - ensure sane alignment of input data
  3924. * - copy (conversion happens automatically) input data
  3925. * to output
  3926. * - update xpp to point at next unconverted input, and tp to point
  3927. * at next location for converted output
  3928. */
  3929. long i, j, ni;
  3930. int tmp[LOOPCNT]; /* in case input is misaligned */
  3931. int *xp;
  3932. int nrange = 0; /* number of range errors */
  3933. int realign = 0; /* "do we need to fix input data alignment?" */
  3934. long cxp = (long) *((char**)xpp);
  3935. realign = (cxp & 7) % SIZEOF_INT;
  3936. /* sjl: manually stripmine so we can limit amount of
  3937. * vector work space reserved to LOOPCNT elements. Also
  3938. * makes vectorisation easy */
  3939. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  3940. ni=Min(nelems-j,LOOPCNT);
  3941. if (realign) {
  3942. memcpy(tmp, *xpp, ni*SIZEOF_INT);
  3943. xp = tmp;
  3944. } else {
  3945. xp = (int *) *xpp;
  3946. }
  3947. /* copy the next block */
  3948. #pragma cdir loopcnt=LOOPCNT
  3949. #pragma cdir shortloop
  3950. for (i=0; i<ni; i++) {
  3951. tp[i] = (schar) Max( SCHAR_MIN, Min(SCHAR_MAX, (schar) xp[i]));
  3952. /* test for range errors (not always needed but do it anyway) */
  3953. nrange += xp[i] < SCHAR_MIN || xp[i] > SCHAR_MAX;
  3954. }
  3955. /* update xpp and tp */
  3956. if (realign) xp = (int *) *xpp;
  3957. xp += ni;
  3958. tp += ni;
  3959. *xpp = (void*)xp;
  3960. }
  3961. return nrange == 0 ? ENOERR : NC_ERANGE;
  3962. #else /* not SX */
  3963. const char *xp = (const char *) *xpp;
  3964. int status = ENOERR;
  3965. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  3966. {
  3967. const int lstatus = ncx_get_int_schar(xp, tp);
  3968. if(lstatus != ENOERR)
  3969. status = lstatus;
  3970. }
  3971. *xpp = (const void *)xp;
  3972. return status;
  3973. # endif
  3974. }
  3975. int
  3976. ncx_getn_int_uchar(const void **xpp, size_t nelems, uchar *tp)
  3977. {
  3978. #if _SX && \
  3979. X_SIZEOF_INT == SIZEOF_INT
  3980. /* basic algorithm is:
  3981. * - ensure sane alignment of input data
  3982. * - copy (conversion happens automatically) input data
  3983. * to output
  3984. * - update xpp to point at next unconverted input, and tp to point
  3985. * at next location for converted output
  3986. */
  3987. long i, j, ni;
  3988. int tmp[LOOPCNT]; /* in case input is misaligned */
  3989. int *xp;
  3990. int nrange = 0; /* number of range errors */
  3991. int realign = 0; /* "do we need to fix input data alignment?" */
  3992. long cxp = (long) *((char**)xpp);
  3993. realign = (cxp & 7) % SIZEOF_INT;
  3994. /* sjl: manually stripmine so we can limit amount of
  3995. * vector work space reserved to LOOPCNT elements. Also
  3996. * makes vectorisation easy */
  3997. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  3998. ni=Min(nelems-j,LOOPCNT);
  3999. if (realign) {
  4000. memcpy(tmp, *xpp, ni*SIZEOF_INT);
  4001. xp = tmp;
  4002. } else {
  4003. xp = (int *) *xpp;
  4004. }
  4005. /* copy the next block */
  4006. #pragma cdir loopcnt=LOOPCNT
  4007. #pragma cdir shortloop
  4008. for (i=0; i<ni; i++) {
  4009. tp[i] = (uchar) Max( UCHAR_MIN, Min(UCHAR_MAX, (uchar) xp[i]));
  4010. /* test for range errors (not always needed but do it anyway) */
  4011. nrange += xp[i] < UCHAR_MIN || xp[i] > UCHAR_MAX;
  4012. }
  4013. /* update xpp and tp */
  4014. if (realign) xp = (int *) *xpp;
  4015. xp += ni;
  4016. tp += ni;
  4017. *xpp = (void*)xp;
  4018. }
  4019. return nrange == 0 ? ENOERR : NC_ERANGE;
  4020. #else /* not SX */
  4021. const char *xp = (const char *) *xpp;
  4022. int status = ENOERR;
  4023. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  4024. {
  4025. const int lstatus = ncx_get_int_uchar(xp, tp);
  4026. if(lstatus != ENOERR)
  4027. status = lstatus;
  4028. }
  4029. *xpp = (const void *)xp;
  4030. return status;
  4031. # endif
  4032. }
  4033. int
  4034. ncx_getn_int_short(const void **xpp, size_t nelems, short *tp)
  4035. {
  4036. #if _SX && \
  4037. X_SIZEOF_INT == SIZEOF_INT
  4038. /* basic algorithm is:
  4039. * - ensure sane alignment of input data
  4040. * - copy (conversion happens automatically) input data
  4041. * to output
  4042. * - update xpp to point at next unconverted input, and tp to point
  4043. * at next location for converted output
  4044. */
  4045. long i, j, ni;
  4046. int tmp[LOOPCNT]; /* in case input is misaligned */
  4047. int *xp;
  4048. int nrange = 0; /* number of range errors */
  4049. int realign = 0; /* "do we need to fix input data alignment?" */
  4050. long cxp = (long) *((char**)xpp);
  4051. realign = (cxp & 7) % SIZEOF_INT;
  4052. /* sjl: manually stripmine so we can limit amount of
  4053. * vector work space reserved to LOOPCNT elements. Also
  4054. * makes vectorisation easy */
  4055. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  4056. ni=Min(nelems-j,LOOPCNT);
  4057. if (realign) {
  4058. memcpy(tmp, *xpp, ni*SIZEOF_INT);
  4059. xp = tmp;
  4060. } else {
  4061. xp = (int *) *xpp;
  4062. }
  4063. /* copy the next block */
  4064. #pragma cdir loopcnt=LOOPCNT
  4065. #pragma cdir shortloop
  4066. for (i=0; i<ni; i++) {
  4067. tp[i] = (short) Max( SHORT_MIN, Min(SHORT_MAX, (short) xp[i]));
  4068. /* test for range errors (not always needed but do it anyway) */
  4069. nrange += xp[i] < SHORT_MIN || xp[i] > SHORT_MAX;
  4070. }
  4071. /* update xpp and tp */
  4072. if (realign) xp = (int *) *xpp;
  4073. xp += ni;
  4074. tp += ni;
  4075. *xpp = (void*)xp;
  4076. }
  4077. return nrange == 0 ? ENOERR : NC_ERANGE;
  4078. #else /* not SX */
  4079. const char *xp = (const char *) *xpp;
  4080. int status = ENOERR;
  4081. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  4082. {
  4083. const int lstatus = ncx_get_int_short(xp, tp);
  4084. if(lstatus != ENOERR)
  4085. status = lstatus;
  4086. }
  4087. *xpp = (const void *)xp;
  4088. return status;
  4089. # endif
  4090. }
  4091. #if X_SIZEOF_INT == SIZEOF_INT
  4092. /* optimized version */
  4093. int
  4094. ncx_getn_int_int(const void **xpp, size_t nelems, int *tp)
  4095. {
  4096. #ifdef WORDS_BIGENDIAN
  4097. (void) memcpy(tp, *xpp, nelems * sizeof(int));
  4098. # else
  4099. swapn4b(tp, *xpp, nelems);
  4100. # endif
  4101. *xpp = (const void *)((const char *)(*xpp) + nelems * X_SIZEOF_INT);
  4102. return ENOERR;
  4103. }
  4104. int
  4105. ncx_getn_int_uint(const void **xpp, size_t nelems, unsigned int *tp)
  4106. {
  4107. #ifdef WORDS_BIGENDIAN
  4108. (void) memcpy(tp, *xpp, nelems * sizeof(int));
  4109. # else
  4110. swapn4b(tp, *xpp, nelems);
  4111. # endif
  4112. *xpp = (const void *)((const char *)(*xpp) + nelems * X_SIZEOF_INT);
  4113. return ENOERR;
  4114. }
  4115. #else
  4116. int
  4117. ncx_getn_int_int(const void **xpp, size_t nelems, int *tp)
  4118. {
  4119. #if _SX && \
  4120. X_SIZEOF_INT == SIZEOF_INT
  4121. /* basic algorithm is:
  4122. * - ensure sane alignment of input data
  4123. * - copy (conversion happens automatically) input data
  4124. * to output
  4125. * - update xpp to point at next unconverted input, and tp to point
  4126. * at next location for converted output
  4127. */
  4128. long i, j, ni;
  4129. int tmp[LOOPCNT]; /* in case input is misaligned */
  4130. int *xp;
  4131. int nrange = 0; /* number of range errors */
  4132. int realign = 0; /* "do we need to fix input data alignment?" */
  4133. long cxp = (long) *((char**)xpp);
  4134. realign = (cxp & 7) % SIZEOF_INT;
  4135. /* sjl: manually stripmine so we can limit amount of
  4136. * vector work space reserved to LOOPCNT elements. Also
  4137. * makes vectorisation easy */
  4138. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  4139. ni=Min(nelems-j,LOOPCNT);
  4140. if (realign) {
  4141. memcpy(tmp, *xpp, ni*SIZEOF_INT);
  4142. xp = tmp;
  4143. } else {
  4144. xp = (int *) *xpp;
  4145. }
  4146. /* copy the next block */
  4147. #pragma cdir loopcnt=LOOPCNT
  4148. #pragma cdir shortloop
  4149. for (i=0; i<ni; i++) {
  4150. tp[i] = (int) Max( INT_MIN, Min(INT_MAX, (int) xp[i]));
  4151. /* test for range errors (not always needed but do it anyway) */
  4152. nrange += xp[i] < INT_MIN || xp[i] > INT_MAX;
  4153. }
  4154. /* update xpp and tp */
  4155. if (realign) xp = (int *) *xpp;
  4156. xp += ni;
  4157. tp += ni;
  4158. *xpp = (void*)xp;
  4159. }
  4160. return nrange == 0 ? ENOERR : NC_ERANGE;
  4161. #else /* not SX */
  4162. const char *xp = (const char *) *xpp;
  4163. int status = ENOERR;
  4164. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  4165. {
  4166. const int lstatus = ncx_get_int_int(xp, tp);
  4167. if(lstatus != ENOERR)
  4168. status = lstatus;
  4169. }
  4170. *xpp = (const void *)xp;
  4171. return status;
  4172. # endif
  4173. }
  4174. int
  4175. ncx_getn_int_uint(const void **xpp, size_t nelems, uint *tp)
  4176. {
  4177. #if _SX && \
  4178. X_SIZEOF_INT == SIZEOF_INT
  4179. /* basic algorithm is:
  4180. * - ensure sane alignment of input data
  4181. * - copy (conversion happens automatically) input data
  4182. * to output
  4183. * - update xpp to point at next unconverted input, and tp to point
  4184. * at next location for converted output
  4185. */
  4186. long i, j, ni;
  4187. int tmp[LOOPCNT]; /* in case input is misaligned */
  4188. int *xp;
  4189. int nrange = 0; /* number of range errors */
  4190. int realign = 0; /* "do we need to fix input data alignment?" */
  4191. long cxp = (long) *((char**)xpp);
  4192. realign = (cxp & 7) % SIZEOF_INT;
  4193. /* sjl: manually stripmine so we can limit amount of
  4194. * vector work space reserved to LOOPCNT elements. Also
  4195. * makes vectorisation easy */
  4196. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  4197. ni=Min(nelems-j,LOOPCNT);
  4198. if (realign) {
  4199. memcpy(tmp, *xpp, ni*SIZEOF_INT);
  4200. xp = tmp;
  4201. } else {
  4202. xp = (int *) *xpp;
  4203. }
  4204. /* copy the next block */
  4205. #pragma cdir loopcnt=LOOPCNT
  4206. #pragma cdir shortloop
  4207. for (i=0; i<ni; i++) {
  4208. tp[i] = (uint) Max( UINT_MIN, Min(UINT_MAX, (uint) xp[i]));
  4209. /* test for range errors (not always needed but do it anyway) */
  4210. nrange += xp[i] < UINT_MIN || xp[i] > UINT_MAX;
  4211. }
  4212. /* update xpp and tp */
  4213. if (realign) xp = (int *) *xpp;
  4214. xp += ni;
  4215. tp += ni;
  4216. *xpp = (void*)xp;
  4217. }
  4218. return nrange == 0 ? ENOERR : NC_ERANGE;
  4219. #else /* not SX */
  4220. const char *xp = (const char *) *xpp;
  4221. int status = ENOERR;
  4222. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  4223. {
  4224. const int lstatus = ncx_get_int_uint(xp, tp);
  4225. if(lstatus != ENOERR)
  4226. status = lstatus;
  4227. }
  4228. *xpp = (const void *)xp;
  4229. return status;
  4230. # endif
  4231. }
  4232. #endif
  4233. int
  4234. ncx_getn_int_longlong(const void **xpp, size_t nelems, longlong *tp)
  4235. {
  4236. #if _SX && \
  4237. X_SIZEOF_INT == SIZEOF_INT
  4238. /* basic algorithm is:
  4239. * - ensure sane alignment of input data
  4240. * - copy (conversion happens automatically) input data
  4241. * to output
  4242. * - update xpp to point at next unconverted input, and tp to point
  4243. * at next location for converted output
  4244. */
  4245. long i, j, ni;
  4246. int tmp[LOOPCNT]; /* in case input is misaligned */
  4247. int *xp;
  4248. int nrange = 0; /* number of range errors */
  4249. int realign = 0; /* "do we need to fix input data alignment?" */
  4250. long cxp = (long) *((char**)xpp);
  4251. realign = (cxp & 7) % SIZEOF_INT;
  4252. /* sjl: manually stripmine so we can limit amount of
  4253. * vector work space reserved to LOOPCNT elements. Also
  4254. * makes vectorisation easy */
  4255. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  4256. ni=Min(nelems-j,LOOPCNT);
  4257. if (realign) {
  4258. memcpy(tmp, *xpp, ni*SIZEOF_INT);
  4259. xp = tmp;
  4260. } else {
  4261. xp = (int *) *xpp;
  4262. }
  4263. /* copy the next block */
  4264. #pragma cdir loopcnt=LOOPCNT
  4265. #pragma cdir shortloop
  4266. for (i=0; i<ni; i++) {
  4267. tp[i] = (longlong) Max( LONGLONG_MIN, Min(LONGLONG_MAX, (longlong) xp[i]));
  4268. /* test for range errors (not always needed but do it anyway) */
  4269. nrange += xp[i] < LONGLONG_MIN || xp[i] > LONGLONG_MAX;
  4270. }
  4271. /* update xpp and tp */
  4272. if (realign) xp = (int *) *xpp;
  4273. xp += ni;
  4274. tp += ni;
  4275. *xpp = (void*)xp;
  4276. }
  4277. return nrange == 0 ? ENOERR : NC_ERANGE;
  4278. #else /* not SX */
  4279. const char *xp = (const char *) *xpp;
  4280. int status = ENOERR;
  4281. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  4282. {
  4283. const int lstatus = ncx_get_int_longlong(xp, tp);
  4284. if(lstatus != ENOERR)
  4285. status = lstatus;
  4286. }
  4287. *xpp = (const void *)xp;
  4288. return status;
  4289. # endif
  4290. }
  4291. int
  4292. ncx_getn_int_ulonglong(const void **xpp, size_t nelems, ulonglong *tp)
  4293. {
  4294. #if _SX && \
  4295. X_SIZEOF_INT == SIZEOF_INT
  4296. /* basic algorithm is:
  4297. * - ensure sane alignment of input data
  4298. * - copy (conversion happens automatically) input data
  4299. * to output
  4300. * - update xpp to point at next unconverted input, and tp to point
  4301. * at next location for converted output
  4302. */
  4303. long i, j, ni;
  4304. int tmp[LOOPCNT]; /* in case input is misaligned */
  4305. int *xp;
  4306. int nrange = 0; /* number of range errors */
  4307. int realign = 0; /* "do we need to fix input data alignment?" */
  4308. long cxp = (long) *((char**)xpp);
  4309. realign = (cxp & 7) % SIZEOF_INT;
  4310. /* sjl: manually stripmine so we can limit amount of
  4311. * vector work space reserved to LOOPCNT elements. Also
  4312. * makes vectorisation easy */
  4313. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  4314. ni=Min(nelems-j,LOOPCNT);
  4315. if (realign) {
  4316. memcpy(tmp, *xpp, ni*SIZEOF_INT);
  4317. xp = tmp;
  4318. } else {
  4319. xp = (int *) *xpp;
  4320. }
  4321. /* copy the next block */
  4322. #pragma cdir loopcnt=LOOPCNT
  4323. #pragma cdir shortloop
  4324. for (i=0; i<ni; i++) {
  4325. tp[i] = (ulonglong) Max( ULONGLONG_MIN, Min(ULONGLONG_MAX, (ulonglong) xp[i]));
  4326. /* test for range errors (not always needed but do it anyway) */
  4327. nrange += xp[i] < ULONGLONG_MIN || xp[i] > ULONGLONG_MAX;
  4328. }
  4329. /* update xpp and tp */
  4330. if (realign) xp = (int *) *xpp;
  4331. xp += ni;
  4332. tp += ni;
  4333. *xpp = (void*)xp;
  4334. }
  4335. return nrange == 0 ? ENOERR : NC_ERANGE;
  4336. #else /* not SX */
  4337. const char *xp = (const char *) *xpp;
  4338. int status = ENOERR;
  4339. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  4340. {
  4341. const int lstatus = ncx_get_int_ulonglong(xp, tp);
  4342. if(lstatus != ENOERR)
  4343. status = lstatus;
  4344. }
  4345. *xpp = (const void *)xp;
  4346. return status;
  4347. # endif
  4348. }
  4349. int
  4350. ncx_getn_int_float(const void **xpp, size_t nelems, float *tp)
  4351. {
  4352. #if _SX && \
  4353. X_SIZEOF_INT == SIZEOF_INT
  4354. /* basic algorithm is:
  4355. * - ensure sane alignment of input data
  4356. * - copy (conversion happens automatically) input data
  4357. * to output
  4358. * - update xpp to point at next unconverted input, and tp to point
  4359. * at next location for converted output
  4360. */
  4361. long i, j, ni;
  4362. int tmp[LOOPCNT]; /* in case input is misaligned */
  4363. int *xp;
  4364. int nrange = 0; /* number of range errors */
  4365. int realign = 0; /* "do we need to fix input data alignment?" */
  4366. long cxp = (long) *((char**)xpp);
  4367. realign = (cxp & 7) % SIZEOF_INT;
  4368. /* sjl: manually stripmine so we can limit amount of
  4369. * vector work space reserved to LOOPCNT elements. Also
  4370. * makes vectorisation easy */
  4371. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  4372. ni=Min(nelems-j,LOOPCNT);
  4373. if (realign) {
  4374. memcpy(tmp, *xpp, ni*SIZEOF_INT);
  4375. xp = tmp;
  4376. } else {
  4377. xp = (int *) *xpp;
  4378. }
  4379. /* copy the next block */
  4380. #pragma cdir loopcnt=LOOPCNT
  4381. #pragma cdir shortloop
  4382. for (i=0; i<ni; i++) {
  4383. tp[i] = (float) Max( FLOAT_MIN, Min(FLOAT_MAX, (float) xp[i]));
  4384. /* test for range errors (not always needed but do it anyway) */
  4385. nrange += xp[i] < FLOAT_MIN || xp[i] > FLOAT_MAX;
  4386. }
  4387. /* update xpp and tp */
  4388. if (realign) xp = (int *) *xpp;
  4389. xp += ni;
  4390. tp += ni;
  4391. *xpp = (void*)xp;
  4392. }
  4393. return nrange == 0 ? ENOERR : NC_ERANGE;
  4394. #else /* not SX */
  4395. const char *xp = (const char *) *xpp;
  4396. int status = ENOERR;
  4397. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  4398. {
  4399. const int lstatus = ncx_get_int_float(xp, tp);
  4400. if(lstatus != ENOERR)
  4401. status = lstatus;
  4402. }
  4403. *xpp = (const void *)xp;
  4404. return status;
  4405. # endif
  4406. }
  4407. int
  4408. ncx_getn_int_double(const void **xpp, size_t nelems, double *tp)
  4409. {
  4410. #if _SX && \
  4411. X_SIZEOF_INT == SIZEOF_INT
  4412. /* basic algorithm is:
  4413. * - ensure sane alignment of input data
  4414. * - copy (conversion happens automatically) input data
  4415. * to output
  4416. * - update xpp to point at next unconverted input, and tp to point
  4417. * at next location for converted output
  4418. */
  4419. long i, j, ni;
  4420. int tmp[LOOPCNT]; /* in case input is misaligned */
  4421. int *xp;
  4422. int nrange = 0; /* number of range errors */
  4423. int realign = 0; /* "do we need to fix input data alignment?" */
  4424. long cxp = (long) *((char**)xpp);
  4425. realign = (cxp & 7) % SIZEOF_INT;
  4426. /* sjl: manually stripmine so we can limit amount of
  4427. * vector work space reserved to LOOPCNT elements. Also
  4428. * makes vectorisation easy */
  4429. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  4430. ni=Min(nelems-j,LOOPCNT);
  4431. if (realign) {
  4432. memcpy(tmp, *xpp, ni*SIZEOF_INT);
  4433. xp = tmp;
  4434. } else {
  4435. xp = (int *) *xpp;
  4436. }
  4437. /* copy the next block */
  4438. #pragma cdir loopcnt=LOOPCNT
  4439. #pragma cdir shortloop
  4440. for (i=0; i<ni; i++) {
  4441. tp[i] = (double) Max( DOUBLE_MIN, Min(DOUBLE_MAX, (double) xp[i]));
  4442. /* test for range errors (not always needed but do it anyway) */
  4443. nrange += xp[i] < DOUBLE_MIN || xp[i] > DOUBLE_MAX;
  4444. }
  4445. /* update xpp and tp */
  4446. if (realign) xp = (int *) *xpp;
  4447. xp += ni;
  4448. tp += ni;
  4449. *xpp = (void*)xp;
  4450. }
  4451. return nrange == 0 ? ENOERR : NC_ERANGE;
  4452. #else /* not SX */
  4453. const char *xp = (const char *) *xpp;
  4454. int status = ENOERR;
  4455. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  4456. {
  4457. const int lstatus = ncx_get_int_double(xp, tp);
  4458. if(lstatus != ENOERR)
  4459. status = lstatus;
  4460. }
  4461. *xpp = (const void *)xp;
  4462. return status;
  4463. # endif
  4464. }
  4465. int
  4466. ncx_putn_int_schar(void **xpp, size_t nelems, const schar *tp)
  4467. {
  4468. #if _SX && \
  4469. X_SIZEOF_INT == SIZEOF_INT
  4470. /* basic algorithm is:
  4471. * - ensure sane alignment of output data
  4472. * - copy (conversion happens automatically) input data
  4473. * to output
  4474. * - update tp to point at next unconverted input, and xpp to point
  4475. * at next location for converted output
  4476. */
  4477. long i, j, ni;
  4478. int tmp[LOOPCNT]; /* in case input is misaligned */
  4479. int *xp;
  4480. int nrange = 0; /* number of range errors */
  4481. int realign = 0; /* "do we need to fix input data alignment?" */
  4482. long cxp = (long) *((char**)xpp);
  4483. realign = (cxp & 7) % SIZEOF_INT;
  4484. /* sjl: manually stripmine so we can limit amount of
  4485. * vector work space reserved to LOOPCNT elements. Also
  4486. * makes vectorisation easy */
  4487. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  4488. ni=Min(nelems-j,LOOPCNT);
  4489. if (realign) {
  4490. xp = tmp;
  4491. } else {
  4492. xp = (int *) *xpp;
  4493. }
  4494. /* copy the next block */
  4495. #pragma cdir loopcnt=LOOPCNT
  4496. #pragma cdir shortloop
  4497. for (i=0; i<ni; i++) {
  4498. /* the normal case: */
  4499. xp[i] = (int) Max( X_INT_MIN, Min(X_INT_MAX, (int) tp[i]));
  4500. /* test for range errors (not always needed but do it anyway) */
  4501. nrange += tp[i] < X_INT_MIN || tp[i] > X_INT_MAX;
  4502. }
  4503. /* copy workspace back if necessary */
  4504. if (realign) {
  4505. memcpy(*xpp, tmp, ni*X_SIZEOF_INT);
  4506. xp = (int *) *xpp;
  4507. }
  4508. /* update xpp and tp */
  4509. xp += ni;
  4510. tp += ni;
  4511. *xpp = (void*)xp;
  4512. }
  4513. return nrange == 0 ? ENOERR : NC_ERANGE;
  4514. #else /* not SX */
  4515. char *xp = (char *) *xpp;
  4516. int status = ENOERR;
  4517. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  4518. {
  4519. int lstatus = ncx_put_int_schar(xp, tp);
  4520. if(lstatus != ENOERR)
  4521. status = lstatus;
  4522. }
  4523. *xpp = (void *)xp;
  4524. return status;
  4525. #endif
  4526. }
  4527. int
  4528. ncx_putn_int_uchar(void **xpp, size_t nelems, const uchar *tp)
  4529. {
  4530. #if _SX && \
  4531. X_SIZEOF_INT == SIZEOF_INT
  4532. /* basic algorithm is:
  4533. * - ensure sane alignment of output data
  4534. * - copy (conversion happens automatically) input data
  4535. * to output
  4536. * - update tp to point at next unconverted input, and xpp to point
  4537. * at next location for converted output
  4538. */
  4539. long i, j, ni;
  4540. int tmp[LOOPCNT]; /* in case input is misaligned */
  4541. int *xp;
  4542. int nrange = 0; /* number of range errors */
  4543. int realign = 0; /* "do we need to fix input data alignment?" */
  4544. long cxp = (long) *((char**)xpp);
  4545. realign = (cxp & 7) % SIZEOF_INT;
  4546. /* sjl: manually stripmine so we can limit amount of
  4547. * vector work space reserved to LOOPCNT elements. Also
  4548. * makes vectorisation easy */
  4549. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  4550. ni=Min(nelems-j,LOOPCNT);
  4551. if (realign) {
  4552. xp = tmp;
  4553. } else {
  4554. xp = (int *) *xpp;
  4555. }
  4556. /* copy the next block */
  4557. #pragma cdir loopcnt=LOOPCNT
  4558. #pragma cdir shortloop
  4559. for (i=0; i<ni; i++) {
  4560. /* the normal case: */
  4561. xp[i] = (int) Max( X_INT_MIN, Min(X_INT_MAX, (int) tp[i]));
  4562. /* test for range errors (not always needed but do it anyway) */
  4563. nrange += tp[i] < X_INT_MIN || tp[i] > X_INT_MAX;
  4564. }
  4565. /* copy workspace back if necessary */
  4566. if (realign) {
  4567. memcpy(*xpp, tmp, ni*X_SIZEOF_INT);
  4568. xp = (int *) *xpp;
  4569. }
  4570. /* update xpp and tp */
  4571. xp += ni;
  4572. tp += ni;
  4573. *xpp = (void*)xp;
  4574. }
  4575. return nrange == 0 ? ENOERR : NC_ERANGE;
  4576. #else /* not SX */
  4577. char *xp = (char *) *xpp;
  4578. int status = ENOERR;
  4579. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  4580. {
  4581. int lstatus = ncx_put_int_uchar(xp, tp);
  4582. if(lstatus != ENOERR)
  4583. status = lstatus;
  4584. }
  4585. *xpp = (void *)xp;
  4586. return status;
  4587. #endif
  4588. }
  4589. int
  4590. ncx_putn_int_short(void **xpp, size_t nelems, const short *tp)
  4591. {
  4592. #if _SX && \
  4593. X_SIZEOF_INT == SIZEOF_INT
  4594. /* basic algorithm is:
  4595. * - ensure sane alignment of output data
  4596. * - copy (conversion happens automatically) input data
  4597. * to output
  4598. * - update tp to point at next unconverted input, and xpp to point
  4599. * at next location for converted output
  4600. */
  4601. long i, j, ni;
  4602. int tmp[LOOPCNT]; /* in case input is misaligned */
  4603. int *xp;
  4604. int nrange = 0; /* number of range errors */
  4605. int realign = 0; /* "do we need to fix input data alignment?" */
  4606. long cxp = (long) *((char**)xpp);
  4607. realign = (cxp & 7) % SIZEOF_INT;
  4608. /* sjl: manually stripmine so we can limit amount of
  4609. * vector work space reserved to LOOPCNT elements. Also
  4610. * makes vectorisation easy */
  4611. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  4612. ni=Min(nelems-j,LOOPCNT);
  4613. if (realign) {
  4614. xp = tmp;
  4615. } else {
  4616. xp = (int *) *xpp;
  4617. }
  4618. /* copy the next block */
  4619. #pragma cdir loopcnt=LOOPCNT
  4620. #pragma cdir shortloop
  4621. for (i=0; i<ni; i++) {
  4622. /* the normal case: */
  4623. xp[i] = (int) Max( X_INT_MIN, Min(X_INT_MAX, (int) tp[i]));
  4624. /* test for range errors (not always needed but do it anyway) */
  4625. nrange += tp[i] < X_INT_MIN || tp[i] > X_INT_MAX;
  4626. }
  4627. /* copy workspace back if necessary */
  4628. if (realign) {
  4629. memcpy(*xpp, tmp, ni*X_SIZEOF_INT);
  4630. xp = (int *) *xpp;
  4631. }
  4632. /* update xpp and tp */
  4633. xp += ni;
  4634. tp += ni;
  4635. *xpp = (void*)xp;
  4636. }
  4637. return nrange == 0 ? ENOERR : NC_ERANGE;
  4638. #else /* not SX */
  4639. char *xp = (char *) *xpp;
  4640. int status = ENOERR;
  4641. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  4642. {
  4643. int lstatus = ncx_put_int_short(xp, tp);
  4644. if(lstatus != ENOERR)
  4645. status = lstatus;
  4646. }
  4647. *xpp = (void *)xp;
  4648. return status;
  4649. #endif
  4650. }
  4651. #if X_SIZEOF_INT == SIZEOF_INT
  4652. /* optimized version */
  4653. int
  4654. ncx_putn_int_int(void **xpp, size_t nelems, const int *tp)
  4655. {
  4656. #ifdef WORDS_BIGENDIAN
  4657. (void) memcpy(*xpp, tp, nelems * X_SIZEOF_INT);
  4658. # else
  4659. swapn4b(*xpp, tp, nelems);
  4660. # endif
  4661. *xpp = (void *)((char *)(*xpp) + nelems * X_SIZEOF_INT);
  4662. return ENOERR;
  4663. }
  4664. int
  4665. ncx_putn_int_uint(void **xpp, size_t nelems, const unsigned int *tp)
  4666. {
  4667. #ifdef WORDS_BIGENDIAN
  4668. (void) memcpy(*xpp, tp, nelems * X_SIZEOF_INT);
  4669. # else
  4670. swapn4b(*xpp, tp, nelems);
  4671. # endif
  4672. *xpp = (void *)((char *)(*xpp) + nelems * X_SIZEOF_INT);
  4673. return ENOERR;
  4674. }
  4675. #else
  4676. int
  4677. ncx_putn_int_int(void **xpp, size_t nelems, const int *tp)
  4678. {
  4679. #if _SX && \
  4680. X_SIZEOF_INT == SIZEOF_INT
  4681. /* basic algorithm is:
  4682. * - ensure sane alignment of output data
  4683. * - copy (conversion happens automatically) input data
  4684. * to output
  4685. * - update tp to point at next unconverted input, and xpp to point
  4686. * at next location for converted output
  4687. */
  4688. long i, j, ni;
  4689. int tmp[LOOPCNT]; /* in case input is misaligned */
  4690. int *xp;
  4691. int nrange = 0; /* number of range errors */
  4692. int realign = 0; /* "do we need to fix input data alignment?" */
  4693. long cxp = (long) *((char**)xpp);
  4694. realign = (cxp & 7) % SIZEOF_INT;
  4695. /* sjl: manually stripmine so we can limit amount of
  4696. * vector work space reserved to LOOPCNT elements. Also
  4697. * makes vectorisation easy */
  4698. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  4699. ni=Min(nelems-j,LOOPCNT);
  4700. if (realign) {
  4701. xp = tmp;
  4702. } else {
  4703. xp = (int *) *xpp;
  4704. }
  4705. /* copy the next block */
  4706. #pragma cdir loopcnt=LOOPCNT
  4707. #pragma cdir shortloop
  4708. for (i=0; i<ni; i++) {
  4709. /* the normal case: */
  4710. xp[i] = (int) Max( X_INT_MIN, Min(X_INT_MAX, (int) tp[i]));
  4711. /* test for range errors (not always needed but do it anyway) */
  4712. nrange += tp[i] < X_INT_MIN || tp[i] > X_INT_MAX;
  4713. }
  4714. /* copy workspace back if necessary */
  4715. if (realign) {
  4716. memcpy(*xpp, tmp, ni*X_SIZEOF_INT);
  4717. xp = (int *) *xpp;
  4718. }
  4719. /* update xpp and tp */
  4720. xp += ni;
  4721. tp += ni;
  4722. *xpp = (void*)xp;
  4723. }
  4724. return nrange == 0 ? ENOERR : NC_ERANGE;
  4725. #else /* not SX */
  4726. char *xp = (char *) *xpp;
  4727. int status = ENOERR;
  4728. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  4729. {
  4730. int lstatus = ncx_put_int_int(xp, tp);
  4731. if(lstatus != ENOERR)
  4732. status = lstatus;
  4733. }
  4734. *xpp = (void *)xp;
  4735. return status;
  4736. #endif
  4737. }
  4738. int
  4739. ncx_putn_int_uint(void **xpp, size_t nelems, const uint *tp)
  4740. {
  4741. #if _SX && \
  4742. X_SIZEOF_INT == SIZEOF_INT
  4743. /* basic algorithm is:
  4744. * - ensure sane alignment of output data
  4745. * - copy (conversion happens automatically) input data
  4746. * to output
  4747. * - update tp to point at next unconverted input, and xpp to point
  4748. * at next location for converted output
  4749. */
  4750. long i, j, ni;
  4751. int tmp[LOOPCNT]; /* in case input is misaligned */
  4752. int *xp;
  4753. int nrange = 0; /* number of range errors */
  4754. int realign = 0; /* "do we need to fix input data alignment?" */
  4755. long cxp = (long) *((char**)xpp);
  4756. realign = (cxp & 7) % SIZEOF_INT;
  4757. /* sjl: manually stripmine so we can limit amount of
  4758. * vector work space reserved to LOOPCNT elements. Also
  4759. * makes vectorisation easy */
  4760. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  4761. ni=Min(nelems-j,LOOPCNT);
  4762. if (realign) {
  4763. xp = tmp;
  4764. } else {
  4765. xp = (int *) *xpp;
  4766. }
  4767. /* copy the next block */
  4768. #pragma cdir loopcnt=LOOPCNT
  4769. #pragma cdir shortloop
  4770. for (i=0; i<ni; i++) {
  4771. /* the normal case: */
  4772. xp[i] = (int) Max( X_INT_MIN, Min(X_INT_MAX, (int) tp[i]));
  4773. /* test for range errors (not always needed but do it anyway) */
  4774. nrange += tp[i] < X_INT_MIN || tp[i] > X_INT_MAX;
  4775. }
  4776. /* copy workspace back if necessary */
  4777. if (realign) {
  4778. memcpy(*xpp, tmp, ni*X_SIZEOF_INT);
  4779. xp = (int *) *xpp;
  4780. }
  4781. /* update xpp and tp */
  4782. xp += ni;
  4783. tp += ni;
  4784. *xpp = (void*)xp;
  4785. }
  4786. return nrange == 0 ? ENOERR : NC_ERANGE;
  4787. #else /* not SX */
  4788. char *xp = (char *) *xpp;
  4789. int status = ENOERR;
  4790. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  4791. {
  4792. int lstatus = ncx_put_int_uint(xp, tp);
  4793. if(lstatus != ENOERR)
  4794. status = lstatus;
  4795. }
  4796. *xpp = (void *)xp;
  4797. return status;
  4798. #endif
  4799. }
  4800. #endif
  4801. int
  4802. ncx_putn_int_longlong(void **xpp, size_t nelems, const longlong *tp)
  4803. {
  4804. #if _SX && \
  4805. X_SIZEOF_INT == SIZEOF_INT
  4806. /* basic algorithm is:
  4807. * - ensure sane alignment of output data
  4808. * - copy (conversion happens automatically) input data
  4809. * to output
  4810. * - update tp to point at next unconverted input, and xpp to point
  4811. * at next location for converted output
  4812. */
  4813. long i, j, ni;
  4814. int tmp[LOOPCNT]; /* in case input is misaligned */
  4815. int *xp;
  4816. int nrange = 0; /* number of range errors */
  4817. int realign = 0; /* "do we need to fix input data alignment?" */
  4818. long cxp = (long) *((char**)xpp);
  4819. realign = (cxp & 7) % SIZEOF_INT;
  4820. /* sjl: manually stripmine so we can limit amount of
  4821. * vector work space reserved to LOOPCNT elements. Also
  4822. * makes vectorisation easy */
  4823. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  4824. ni=Min(nelems-j,LOOPCNT);
  4825. if (realign) {
  4826. xp = tmp;
  4827. } else {
  4828. xp = (int *) *xpp;
  4829. }
  4830. /* copy the next block */
  4831. #pragma cdir loopcnt=LOOPCNT
  4832. #pragma cdir shortloop
  4833. for (i=0; i<ni; i++) {
  4834. /* the normal case: */
  4835. xp[i] = (int) Max( X_INT_MIN, Min(X_INT_MAX, (int) tp[i]));
  4836. /* test for range errors (not always needed but do it anyway) */
  4837. nrange += tp[i] < X_INT_MIN || tp[i] > X_INT_MAX;
  4838. }
  4839. /* copy workspace back if necessary */
  4840. if (realign) {
  4841. memcpy(*xpp, tmp, ni*X_SIZEOF_INT);
  4842. xp = (int *) *xpp;
  4843. }
  4844. /* update xpp and tp */
  4845. xp += ni;
  4846. tp += ni;
  4847. *xpp = (void*)xp;
  4848. }
  4849. return nrange == 0 ? ENOERR : NC_ERANGE;
  4850. #else /* not SX */
  4851. char *xp = (char *) *xpp;
  4852. int status = ENOERR;
  4853. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  4854. {
  4855. int lstatus = ncx_put_int_longlong(xp, tp);
  4856. if(lstatus != ENOERR)
  4857. status = lstatus;
  4858. }
  4859. *xpp = (void *)xp;
  4860. return status;
  4861. #endif
  4862. }
  4863. int
  4864. ncx_putn_int_ulonglong(void **xpp, size_t nelems, const ulonglong *tp)
  4865. {
  4866. #if _SX && \
  4867. X_SIZEOF_INT == SIZEOF_INT
  4868. /* basic algorithm is:
  4869. * - ensure sane alignment of output data
  4870. * - copy (conversion happens automatically) input data
  4871. * to output
  4872. * - update tp to point at next unconverted input, and xpp to point
  4873. * at next location for converted output
  4874. */
  4875. long i, j, ni;
  4876. int tmp[LOOPCNT]; /* in case input is misaligned */
  4877. int *xp;
  4878. int nrange = 0; /* number of range errors */
  4879. int realign = 0; /* "do we need to fix input data alignment?" */
  4880. long cxp = (long) *((char**)xpp);
  4881. realign = (cxp & 7) % SIZEOF_INT;
  4882. /* sjl: manually stripmine so we can limit amount of
  4883. * vector work space reserved to LOOPCNT elements. Also
  4884. * makes vectorisation easy */
  4885. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  4886. ni=Min(nelems-j,LOOPCNT);
  4887. if (realign) {
  4888. xp = tmp;
  4889. } else {
  4890. xp = (int *) *xpp;
  4891. }
  4892. /* copy the next block */
  4893. #pragma cdir loopcnt=LOOPCNT
  4894. #pragma cdir shortloop
  4895. for (i=0; i<ni; i++) {
  4896. /* the normal case: */
  4897. xp[i] = (int) Max( X_INT_MIN, Min(X_INT_MAX, (int) tp[i]));
  4898. /* test for range errors (not always needed but do it anyway) */
  4899. nrange += tp[i] < X_INT_MIN || tp[i] > X_INT_MAX;
  4900. }
  4901. /* copy workspace back if necessary */
  4902. if (realign) {
  4903. memcpy(*xpp, tmp, ni*X_SIZEOF_INT);
  4904. xp = (int *) *xpp;
  4905. }
  4906. /* update xpp and tp */
  4907. xp += ni;
  4908. tp += ni;
  4909. *xpp = (void*)xp;
  4910. }
  4911. return nrange == 0 ? ENOERR : NC_ERANGE;
  4912. #else /* not SX */
  4913. char *xp = (char *) *xpp;
  4914. int status = ENOERR;
  4915. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  4916. {
  4917. int lstatus = ncx_put_int_ulonglong(xp, tp);
  4918. if(lstatus != ENOERR)
  4919. status = lstatus;
  4920. }
  4921. *xpp = (void *)xp;
  4922. return status;
  4923. #endif
  4924. }
  4925. int
  4926. ncx_putn_int_float(void **xpp, size_t nelems, const float *tp)
  4927. {
  4928. #if _SX && \
  4929. X_SIZEOF_INT == SIZEOF_INT
  4930. /* basic algorithm is:
  4931. * - ensure sane alignment of output data
  4932. * - copy (conversion happens automatically) input data
  4933. * to output
  4934. * - update tp to point at next unconverted input, and xpp to point
  4935. * at next location for converted output
  4936. */
  4937. long i, j, ni;
  4938. int tmp[LOOPCNT]; /* in case input is misaligned */
  4939. int *xp;
  4940. double d; /* special case for ncx_putn_int_float */
  4941. int nrange = 0; /* number of range errors */
  4942. int realign = 0; /* "do we need to fix input data alignment?" */
  4943. long cxp = (long) *((char**)xpp);
  4944. realign = (cxp & 7) % SIZEOF_INT;
  4945. /* sjl: manually stripmine so we can limit amount of
  4946. * vector work space reserved to LOOPCNT elements. Also
  4947. * makes vectorisation easy */
  4948. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  4949. ni=Min(nelems-j,LOOPCNT);
  4950. if (realign) {
  4951. xp = tmp;
  4952. } else {
  4953. xp = (int *) *xpp;
  4954. }
  4955. /* copy the next block */
  4956. #pragma cdir loopcnt=LOOPCNT
  4957. #pragma cdir shortloop
  4958. for (i=0; i<ni; i++) {
  4959. /* for some reason int to float, for putn, requires a special case */
  4960. d = tp[i];
  4961. xp[i] = (int) Max( X_INT_MIN, Min(X_INT_MAX, (int) d));
  4962. nrange += d < X_INT_MIN || d > X_INT_MAX;
  4963. }
  4964. /* copy workspace back if necessary */
  4965. if (realign) {
  4966. memcpy(*xpp, tmp, ni*X_SIZEOF_INT);
  4967. xp = (int *) *xpp;
  4968. }
  4969. /* update xpp and tp */
  4970. xp += ni;
  4971. tp += ni;
  4972. *xpp = (void*)xp;
  4973. }
  4974. return nrange == 0 ? ENOERR : NC_ERANGE;
  4975. #else /* not SX */
  4976. char *xp = (char *) *xpp;
  4977. int status = ENOERR;
  4978. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  4979. {
  4980. int lstatus = ncx_put_int_float(xp, tp);
  4981. if(lstatus != ENOERR)
  4982. status = lstatus;
  4983. }
  4984. *xpp = (void *)xp;
  4985. return status;
  4986. #endif
  4987. }
  4988. int
  4989. ncx_putn_int_double(void **xpp, size_t nelems, const double *tp)
  4990. {
  4991. #if _SX && \
  4992. X_SIZEOF_INT == SIZEOF_INT
  4993. /* basic algorithm is:
  4994. * - ensure sane alignment of output data
  4995. * - copy (conversion happens automatically) input data
  4996. * to output
  4997. * - update tp to point at next unconverted input, and xpp to point
  4998. * at next location for converted output
  4999. */
  5000. long i, j, ni;
  5001. int tmp[LOOPCNT]; /* in case input is misaligned */
  5002. int *xp;
  5003. int nrange = 0; /* number of range errors */
  5004. int realign = 0; /* "do we need to fix input data alignment?" */
  5005. long cxp = (long) *((char**)xpp);
  5006. realign = (cxp & 7) % SIZEOF_INT;
  5007. /* sjl: manually stripmine so we can limit amount of
  5008. * vector work space reserved to LOOPCNT elements. Also
  5009. * makes vectorisation easy */
  5010. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  5011. ni=Min(nelems-j,LOOPCNT);
  5012. if (realign) {
  5013. xp = tmp;
  5014. } else {
  5015. xp = (int *) *xpp;
  5016. }
  5017. /* copy the next block */
  5018. #pragma cdir loopcnt=LOOPCNT
  5019. #pragma cdir shortloop
  5020. for (i=0; i<ni; i++) {
  5021. /* the normal case: */
  5022. xp[i] = (int) Max( X_INT_MIN, Min(X_INT_MAX, (int) tp[i]));
  5023. /* test for range errors (not always needed but do it anyway) */
  5024. nrange += tp[i] < X_INT_MIN || tp[i] > X_INT_MAX;
  5025. }
  5026. /* copy workspace back if necessary */
  5027. if (realign) {
  5028. memcpy(*xpp, tmp, ni*X_SIZEOF_INT);
  5029. xp = (int *) *xpp;
  5030. }
  5031. /* update xpp and tp */
  5032. xp += ni;
  5033. tp += ni;
  5034. *xpp = (void*)xp;
  5035. }
  5036. return nrange == 0 ? ENOERR : NC_ERANGE;
  5037. #else /* not SX */
  5038. char *xp = (char *) *xpp;
  5039. int status = ENOERR;
  5040. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  5041. {
  5042. int lstatus = ncx_put_int_double(xp, tp);
  5043. if(lstatus != ENOERR)
  5044. status = lstatus;
  5045. }
  5046. *xpp = (void *)xp;
  5047. return status;
  5048. #endif
  5049. }
  5050. /* float */
  5051. int
  5052. ncx_getn_float_schar(const void **xpp, size_t nelems, schar *tp)
  5053. {
  5054. #if _SX && \
  5055. X_SIZEOF_FLOAT == SIZEOF_FLOAT
  5056. /* basic algorithm is:
  5057. * - ensure sane alignment of input data
  5058. * - copy (conversion happens automatically) input data
  5059. * to output
  5060. * - update xpp to point at next unconverted input, and tp to point
  5061. * at next location for converted output
  5062. */
  5063. long i, j, ni;
  5064. float tmp[LOOPCNT]; /* in case input is misaligned */
  5065. float *xp;
  5066. int nrange = 0; /* number of range errors */
  5067. int realign = 0; /* "do we need to fix input data alignment?" */
  5068. long cxp = (long) *((char**)xpp);
  5069. realign = (cxp & 7) % SIZEOF_FLOAT;
  5070. /* sjl: manually stripmine so we can limit amount of
  5071. * vector work space reserved to LOOPCNT elements. Also
  5072. * makes vectorisation easy */
  5073. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  5074. ni=Min(nelems-j,LOOPCNT);
  5075. if (realign) {
  5076. memcpy(tmp, *xpp, ni*SIZEOF_FLOAT);
  5077. xp = tmp;
  5078. } else {
  5079. xp = (float *) *xpp;
  5080. }
  5081. /* copy the next block */
  5082. #pragma cdir loopcnt=LOOPCNT
  5083. #pragma cdir shortloop
  5084. for (i=0; i<ni; i++) {
  5085. tp[i] = (schar) Max( SCHAR_MIN, Min(SCHAR_MAX, (schar) xp[i]));
  5086. /* test for range errors (not always needed but do it anyway) */
  5087. nrange += xp[i] < SCHAR_MIN || xp[i] > SCHAR_MAX;
  5088. }
  5089. /* update xpp and tp */
  5090. if (realign) xp = (float *) *xpp;
  5091. xp += ni;
  5092. tp += ni;
  5093. *xpp = (void*)xp;
  5094. }
  5095. return nrange == 0 ? ENOERR : NC_ERANGE;
  5096. #else /* not SX */
  5097. const char *xp = (const char *) *xpp;
  5098. int status = ENOERR;
  5099. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  5100. {
  5101. const int lstatus = ncx_get_float_schar(xp, tp);
  5102. if(lstatus != ENOERR)
  5103. status = lstatus;
  5104. }
  5105. *xpp = (const void *)xp;
  5106. return status;
  5107. # endif
  5108. }
  5109. int
  5110. ncx_getn_float_uchar(const void **xpp, size_t nelems, uchar *tp)
  5111. {
  5112. #if _SX && \
  5113. X_SIZEOF_FLOAT == SIZEOF_FLOAT
  5114. /* basic algorithm is:
  5115. * - ensure sane alignment of input data
  5116. * - copy (conversion happens automatically) input data
  5117. * to output
  5118. * - update xpp to point at next unconverted input, and tp to point
  5119. * at next location for converted output
  5120. */
  5121. long i, j, ni;
  5122. float tmp[LOOPCNT]; /* in case input is misaligned */
  5123. float *xp;
  5124. int nrange = 0; /* number of range errors */
  5125. int realign = 0; /* "do we need to fix input data alignment?" */
  5126. long cxp = (long) *((char**)xpp);
  5127. realign = (cxp & 7) % SIZEOF_FLOAT;
  5128. /* sjl: manually stripmine so we can limit amount of
  5129. * vector work space reserved to LOOPCNT elements. Also
  5130. * makes vectorisation easy */
  5131. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  5132. ni=Min(nelems-j,LOOPCNT);
  5133. if (realign) {
  5134. memcpy(tmp, *xpp, ni*SIZEOF_FLOAT);
  5135. xp = tmp;
  5136. } else {
  5137. xp = (float *) *xpp;
  5138. }
  5139. /* copy the next block */
  5140. #pragma cdir loopcnt=LOOPCNT
  5141. #pragma cdir shortloop
  5142. for (i=0; i<ni; i++) {
  5143. tp[i] = (uchar) Max( UCHAR_MIN, Min(UCHAR_MAX, (uchar) xp[i]));
  5144. /* test for range errors (not always needed but do it anyway) */
  5145. nrange += xp[i] < UCHAR_MIN || xp[i] > UCHAR_MAX;
  5146. }
  5147. /* update xpp and tp */
  5148. if (realign) xp = (float *) *xpp;
  5149. xp += ni;
  5150. tp += ni;
  5151. *xpp = (void*)xp;
  5152. }
  5153. return nrange == 0 ? ENOERR : NC_ERANGE;
  5154. #else /* not SX */
  5155. const char *xp = (const char *) *xpp;
  5156. int status = ENOERR;
  5157. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  5158. {
  5159. const int lstatus = ncx_get_float_uchar(xp, tp);
  5160. if(lstatus != ENOERR)
  5161. status = lstatus;
  5162. }
  5163. *xpp = (const void *)xp;
  5164. return status;
  5165. # endif
  5166. }
  5167. int
  5168. ncx_getn_float_short(const void **xpp, size_t nelems, short *tp)
  5169. {
  5170. #if _SX && \
  5171. X_SIZEOF_FLOAT == SIZEOF_FLOAT
  5172. /* basic algorithm is:
  5173. * - ensure sane alignment of input data
  5174. * - copy (conversion happens automatically) input data
  5175. * to output
  5176. * - update xpp to point at next unconverted input, and tp to point
  5177. * at next location for converted output
  5178. */
  5179. long i, j, ni;
  5180. float tmp[LOOPCNT]; /* in case input is misaligned */
  5181. float *xp;
  5182. int nrange = 0; /* number of range errors */
  5183. int realign = 0; /* "do we need to fix input data alignment?" */
  5184. long cxp = (long) *((char**)xpp);
  5185. realign = (cxp & 7) % SIZEOF_FLOAT;
  5186. /* sjl: manually stripmine so we can limit amount of
  5187. * vector work space reserved to LOOPCNT elements. Also
  5188. * makes vectorisation easy */
  5189. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  5190. ni=Min(nelems-j,LOOPCNT);
  5191. if (realign) {
  5192. memcpy(tmp, *xpp, ni*SIZEOF_FLOAT);
  5193. xp = tmp;
  5194. } else {
  5195. xp = (float *) *xpp;
  5196. }
  5197. /* copy the next block */
  5198. #pragma cdir loopcnt=LOOPCNT
  5199. #pragma cdir shortloop
  5200. for (i=0; i<ni; i++) {
  5201. tp[i] = (short) Max( SHORT_MIN, Min(SHORT_MAX, (short) xp[i]));
  5202. /* test for range errors (not always needed but do it anyway) */
  5203. nrange += xp[i] < SHORT_MIN || xp[i] > SHORT_MAX;
  5204. }
  5205. /* update xpp and tp */
  5206. if (realign) xp = (float *) *xpp;
  5207. xp += ni;
  5208. tp += ni;
  5209. *xpp = (void*)xp;
  5210. }
  5211. return nrange == 0 ? ENOERR : NC_ERANGE;
  5212. #else /* not SX */
  5213. const char *xp = (const char *) *xpp;
  5214. int status = ENOERR;
  5215. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  5216. {
  5217. const int lstatus = ncx_get_float_short(xp, tp);
  5218. if(lstatus != ENOERR)
  5219. status = lstatus;
  5220. }
  5221. *xpp = (const void *)xp;
  5222. return status;
  5223. # endif
  5224. }
  5225. int
  5226. ncx_getn_float_int(const void **xpp, size_t nelems, int *tp)
  5227. {
  5228. #if _SX && \
  5229. X_SIZEOF_FLOAT == SIZEOF_FLOAT
  5230. /* basic algorithm is:
  5231. * - ensure sane alignment of input data
  5232. * - copy (conversion happens automatically) input data
  5233. * to output
  5234. * - update xpp to point at next unconverted input, and tp to point
  5235. * at next location for converted output
  5236. */
  5237. long i, j, ni;
  5238. float tmp[LOOPCNT]; /* in case input is misaligned */
  5239. float *xp;
  5240. int nrange = 0; /* number of range errors */
  5241. int realign = 0; /* "do we need to fix input data alignment?" */
  5242. long cxp = (long) *((char**)xpp);
  5243. realign = (cxp & 7) % SIZEOF_FLOAT;
  5244. /* sjl: manually stripmine so we can limit amount of
  5245. * vector work space reserved to LOOPCNT elements. Also
  5246. * makes vectorisation easy */
  5247. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  5248. ni=Min(nelems-j,LOOPCNT);
  5249. if (realign) {
  5250. memcpy(tmp, *xpp, ni*SIZEOF_FLOAT);
  5251. xp = tmp;
  5252. } else {
  5253. xp = (float *) *xpp;
  5254. }
  5255. /* copy the next block */
  5256. #pragma cdir loopcnt=LOOPCNT
  5257. #pragma cdir shortloop
  5258. for (i=0; i<ni; i++) {
  5259. tp[i] = (int) Max( INT_MIN, Min(INT_MAX, (int) xp[i]));
  5260. /* test for range errors (not always needed but do it anyway) */
  5261. nrange += xp[i] < INT_MIN || xp[i] > INT_MAX;
  5262. }
  5263. /* update xpp and tp */
  5264. if (realign) xp = (float *) *xpp;
  5265. xp += ni;
  5266. tp += ni;
  5267. *xpp = (void*)xp;
  5268. }
  5269. return nrange == 0 ? ENOERR : NC_ERANGE;
  5270. #else /* not SX */
  5271. const char *xp = (const char *) *xpp;
  5272. int status = ENOERR;
  5273. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  5274. {
  5275. const int lstatus = ncx_get_float_int(xp, tp);
  5276. if(lstatus != ENOERR)
  5277. status = lstatus;
  5278. }
  5279. *xpp = (const void *)xp;
  5280. return status;
  5281. # endif
  5282. }
  5283. #if X_SIZEOF_FLOAT == SIZEOF_FLOAT && !defined(NO_IEEE_FLOAT)
  5284. /* optimized version */
  5285. int
  5286. ncx_getn_float_float(const void **xpp, size_t nelems, float *tp)
  5287. {
  5288. #ifdef WORDS_BIGENDIAN
  5289. (void) memcpy(tp, *xpp, nelems * sizeof(float));
  5290. # else
  5291. swapn4b(tp, *xpp, nelems);
  5292. # endif
  5293. *xpp = (const void *)((const char *)(*xpp) + nelems * X_SIZEOF_FLOAT);
  5294. return ENOERR;
  5295. }
  5296. #elif vax
  5297. int
  5298. ncx_getn_float_float(const void **xpp, size_t nfloats, float *ip)
  5299. {
  5300. float *const end = ip + nfloats;
  5301. while(ip < end)
  5302. {
  5303. struct vax_single *const vsp = (struct vax_single *) ip;
  5304. const struct ieee_single *const isp =
  5305. (const struct ieee_single *) (*xpp);
  5306. unsigned exp = isp->exp_hi << 1 | isp->exp_lo;
  5307. switch(exp) {
  5308. case 0 :
  5309. /* ieee subnormal */
  5310. if(isp->mant_hi == min.ieee.mant_hi
  5311. && isp->mant_lo_hi == min.ieee.mant_lo_hi
  5312. && isp->mant_lo_lo == min.ieee.mant_lo_lo)
  5313. {
  5314. *vsp = min.s;
  5315. }
  5316. else
  5317. {
  5318. unsigned mantissa = (isp->mant_hi << 16)
  5319. | isp->mant_lo_hi << 8
  5320. | isp->mant_lo_lo;
  5321. unsigned tmp = mantissa >> 20;
  5322. if(tmp >= 4) {
  5323. vsp->exp = 2;
  5324. } else if (tmp >= 2) {
  5325. vsp->exp = 1;
  5326. } else {
  5327. *vsp = min.s;
  5328. break;
  5329. } /* else */
  5330. tmp = mantissa - (1 << (20 + vsp->exp ));
  5331. tmp <<= 3 - vsp->exp;
  5332. vsp->mantissa2 = tmp;
  5333. vsp->mantissa1 = (tmp >> 16);
  5334. }
  5335. break;
  5336. case 0xfe :
  5337. case 0xff :
  5338. *vsp = max.s;
  5339. break;
  5340. default :
  5341. vsp->exp = exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
  5342. vsp->mantissa2 = isp->mant_lo_hi << 8 | isp->mant_lo_lo;
  5343. vsp->mantissa1 = isp->mant_hi;
  5344. }
  5345. vsp->sign = isp->sign;
  5346. ip++;
  5347. *xpp = (char *)(*xpp) + X_SIZEOF_FLOAT;
  5348. }
  5349. return ENOERR;
  5350. }
  5351. #else
  5352. int
  5353. ncx_getn_float_float(const void **xpp, size_t nelems, float *tp)
  5354. {
  5355. const char *xp = *xpp;
  5356. int status = ENOERR;
  5357. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  5358. {
  5359. const int lstatus = ncx_get_float_float(xp, tp);
  5360. if(lstatus != ENOERR)
  5361. status = lstatus;
  5362. }
  5363. *xpp = (const void *)xp;
  5364. return status;
  5365. }
  5366. #endif
  5367. int
  5368. ncx_getn_float_double(const void **xpp, size_t nelems, double *tp)
  5369. {
  5370. #if _SX && \
  5371. X_SIZEOF_FLOAT == SIZEOF_FLOAT
  5372. /* basic algorithm is:
  5373. * - ensure sane alignment of input data
  5374. * - copy (conversion happens automatically) input data
  5375. * to output
  5376. * - update xpp to point at next unconverted input, and tp to point
  5377. * at next location for converted output
  5378. */
  5379. long i, j, ni;
  5380. float tmp[LOOPCNT]; /* in case input is misaligned */
  5381. float *xp;
  5382. int nrange = 0; /* number of range errors */
  5383. int realign = 0; /* "do we need to fix input data alignment?" */
  5384. long cxp = (long) *((char**)xpp);
  5385. realign = (cxp & 7) % SIZEOF_FLOAT;
  5386. /* sjl: manually stripmine so we can limit amount of
  5387. * vector work space reserved to LOOPCNT elements. Also
  5388. * makes vectorisation easy */
  5389. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  5390. ni=Min(nelems-j,LOOPCNT);
  5391. if (realign) {
  5392. memcpy(tmp, *xpp, ni*SIZEOF_FLOAT);
  5393. xp = tmp;
  5394. } else {
  5395. xp = (float *) *xpp;
  5396. }
  5397. /* copy the next block */
  5398. #pragma cdir loopcnt=LOOPCNT
  5399. #pragma cdir shortloop
  5400. for (i=0; i<ni; i++) {
  5401. tp[i] = (double) Max( DOUBLE_MIN, Min(DOUBLE_MAX, (double) xp[i]));
  5402. /* test for range errors (not always needed but do it anyway) */
  5403. nrange += xp[i] < DOUBLE_MIN || xp[i] > DOUBLE_MAX;
  5404. }
  5405. /* update xpp and tp */
  5406. if (realign) xp = (float *) *xpp;
  5407. xp += ni;
  5408. tp += ni;
  5409. *xpp = (void*)xp;
  5410. }
  5411. return nrange == 0 ? ENOERR : NC_ERANGE;
  5412. #else /* not SX */
  5413. const char *xp = (const char *) *xpp;
  5414. int status = ENOERR;
  5415. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  5416. {
  5417. const int lstatus = ncx_get_float_double(xp, tp);
  5418. if(lstatus != ENOERR)
  5419. status = lstatus;
  5420. }
  5421. *xpp = (const void *)xp;
  5422. return status;
  5423. # endif
  5424. }
  5425. int
  5426. ncx_getn_float_uint(const void **xpp, size_t nelems, uint *tp)
  5427. {
  5428. #if _SX && \
  5429. X_SIZEOF_FLOAT == SIZEOF_FLOAT
  5430. /* basic algorithm is:
  5431. * - ensure sane alignment of input data
  5432. * - copy (conversion happens automatically) input data
  5433. * to output
  5434. * - update xpp to point at next unconverted input, and tp to point
  5435. * at next location for converted output
  5436. */
  5437. long i, j, ni;
  5438. float tmp[LOOPCNT]; /* in case input is misaligned */
  5439. float *xp;
  5440. int nrange = 0; /* number of range errors */
  5441. int realign = 0; /* "do we need to fix input data alignment?" */
  5442. long cxp = (long) *((char**)xpp);
  5443. realign = (cxp & 7) % SIZEOF_FLOAT;
  5444. /* sjl: manually stripmine so we can limit amount of
  5445. * vector work space reserved to LOOPCNT elements. Also
  5446. * makes vectorisation easy */
  5447. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  5448. ni=Min(nelems-j,LOOPCNT);
  5449. if (realign) {
  5450. memcpy(tmp, *xpp, ni*SIZEOF_FLOAT);
  5451. xp = tmp;
  5452. } else {
  5453. xp = (float *) *xpp;
  5454. }
  5455. /* copy the next block */
  5456. #pragma cdir loopcnt=LOOPCNT
  5457. #pragma cdir shortloop
  5458. for (i=0; i<ni; i++) {
  5459. tp[i] = (uint) Max( UINT_MIN, Min(UINT_MAX, (uint) xp[i]));
  5460. /* test for range errors (not always needed but do it anyway) */
  5461. nrange += xp[i] < UINT_MIN || xp[i] > UINT_MAX;
  5462. }
  5463. /* update xpp and tp */
  5464. if (realign) xp = (float *) *xpp;
  5465. xp += ni;
  5466. tp += ni;
  5467. *xpp = (void*)xp;
  5468. }
  5469. return nrange == 0 ? ENOERR : NC_ERANGE;
  5470. #else /* not SX */
  5471. const char *xp = (const char *) *xpp;
  5472. int status = ENOERR;
  5473. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  5474. {
  5475. const int lstatus = ncx_get_float_uint(xp, tp);
  5476. if(lstatus != ENOERR)
  5477. status = lstatus;
  5478. }
  5479. *xpp = (const void *)xp;
  5480. return status;
  5481. # endif
  5482. }
  5483. int
  5484. ncx_getn_float_longlong(const void **xpp, size_t nelems, longlong *tp)
  5485. {
  5486. #if _SX && \
  5487. X_SIZEOF_FLOAT == SIZEOF_FLOAT
  5488. /* basic algorithm is:
  5489. * - ensure sane alignment of input data
  5490. * - copy (conversion happens automatically) input data
  5491. * to output
  5492. * - update xpp to point at next unconverted input, and tp to point
  5493. * at next location for converted output
  5494. */
  5495. long i, j, ni;
  5496. float tmp[LOOPCNT]; /* in case input is misaligned */
  5497. float *xp;
  5498. int nrange = 0; /* number of range errors */
  5499. int realign = 0; /* "do we need to fix input data alignment?" */
  5500. long cxp = (long) *((char**)xpp);
  5501. realign = (cxp & 7) % SIZEOF_FLOAT;
  5502. /* sjl: manually stripmine so we can limit amount of
  5503. * vector work space reserved to LOOPCNT elements. Also
  5504. * makes vectorisation easy */
  5505. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  5506. ni=Min(nelems-j,LOOPCNT);
  5507. if (realign) {
  5508. memcpy(tmp, *xpp, ni*SIZEOF_FLOAT);
  5509. xp = tmp;
  5510. } else {
  5511. xp = (float *) *xpp;
  5512. }
  5513. /* copy the next block */
  5514. #pragma cdir loopcnt=LOOPCNT
  5515. #pragma cdir shortloop
  5516. for (i=0; i<ni; i++) {
  5517. tp[i] = (longlong) Max( LONGLONG_MIN, Min(LONGLONG_MAX, (longlong) xp[i]));
  5518. /* test for range errors (not always needed but do it anyway) */
  5519. nrange += xp[i] < LONGLONG_MIN || xp[i] > LONGLONG_MAX;
  5520. }
  5521. /* update xpp and tp */
  5522. if (realign) xp = (float *) *xpp;
  5523. xp += ni;
  5524. tp += ni;
  5525. *xpp = (void*)xp;
  5526. }
  5527. return nrange == 0 ? ENOERR : NC_ERANGE;
  5528. #else /* not SX */
  5529. const char *xp = (const char *) *xpp;
  5530. int status = ENOERR;
  5531. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  5532. {
  5533. const int lstatus = ncx_get_float_longlong(xp, tp);
  5534. if(lstatus != ENOERR)
  5535. status = lstatus;
  5536. }
  5537. *xpp = (const void *)xp;
  5538. return status;
  5539. # endif
  5540. }
  5541. int
  5542. ncx_getn_float_ulonglong(const void **xpp, size_t nelems, ulonglong *tp)
  5543. {
  5544. #if _SX && \
  5545. X_SIZEOF_FLOAT == SIZEOF_FLOAT
  5546. /* basic algorithm is:
  5547. * - ensure sane alignment of input data
  5548. * - copy (conversion happens automatically) input data
  5549. * to output
  5550. * - update xpp to point at next unconverted input, and tp to point
  5551. * at next location for converted output
  5552. */
  5553. long i, j, ni;
  5554. float tmp[LOOPCNT]; /* in case input is misaligned */
  5555. float *xp;
  5556. int nrange = 0; /* number of range errors */
  5557. int realign = 0; /* "do we need to fix input data alignment?" */
  5558. long cxp = (long) *((char**)xpp);
  5559. realign = (cxp & 7) % SIZEOF_FLOAT;
  5560. /* sjl: manually stripmine so we can limit amount of
  5561. * vector work space reserved to LOOPCNT elements. Also
  5562. * makes vectorisation easy */
  5563. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  5564. ni=Min(nelems-j,LOOPCNT);
  5565. if (realign) {
  5566. memcpy(tmp, *xpp, ni*SIZEOF_FLOAT);
  5567. xp = tmp;
  5568. } else {
  5569. xp = (float *) *xpp;
  5570. }
  5571. /* copy the next block */
  5572. #pragma cdir loopcnt=LOOPCNT
  5573. #pragma cdir shortloop
  5574. for (i=0; i<ni; i++) {
  5575. tp[i] = (ulonglong) Max( ULONGLONG_MIN, Min(ULONGLONG_MAX, (ulonglong) xp[i]));
  5576. /* test for range errors (not always needed but do it anyway) */
  5577. nrange += xp[i] < ULONGLONG_MIN || xp[i] > ULONGLONG_MAX;
  5578. }
  5579. /* update xpp and tp */
  5580. if (realign) xp = (float *) *xpp;
  5581. xp += ni;
  5582. tp += ni;
  5583. *xpp = (void*)xp;
  5584. }
  5585. return nrange == 0 ? ENOERR : NC_ERANGE;
  5586. #else /* not SX */
  5587. const char *xp = (const char *) *xpp;
  5588. int status = ENOERR;
  5589. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  5590. {
  5591. const int lstatus = ncx_get_float_ulonglong(xp, tp);
  5592. if(lstatus != ENOERR)
  5593. status = lstatus;
  5594. }
  5595. *xpp = (const void *)xp;
  5596. return status;
  5597. # endif
  5598. }
  5599. int
  5600. ncx_putn_float_schar(void **xpp, size_t nelems, const schar *tp)
  5601. {
  5602. #if _SX && \
  5603. X_SIZEOF_FLOAT == SIZEOF_FLOAT
  5604. /* basic algorithm is:
  5605. * - ensure sane alignment of output data
  5606. * - copy (conversion happens automatically) input data
  5607. * to output
  5608. * - update tp to point at next unconverted input, and xpp to point
  5609. * at next location for converted output
  5610. */
  5611. long i, j, ni;
  5612. float tmp[LOOPCNT]; /* in case input is misaligned */
  5613. float *xp;
  5614. int nrange = 0; /* number of range errors */
  5615. int realign = 0; /* "do we need to fix input data alignment?" */
  5616. long cxp = (long) *((char**)xpp);
  5617. realign = (cxp & 7) % SIZEOF_FLOAT;
  5618. /* sjl: manually stripmine so we can limit amount of
  5619. * vector work space reserved to LOOPCNT elements. Also
  5620. * makes vectorisation easy */
  5621. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  5622. ni=Min(nelems-j,LOOPCNT);
  5623. if (realign) {
  5624. xp = tmp;
  5625. } else {
  5626. xp = (float *) *xpp;
  5627. }
  5628. /* copy the next block */
  5629. #pragma cdir loopcnt=LOOPCNT
  5630. #pragma cdir shortloop
  5631. for (i=0; i<ni; i++) {
  5632. /* the normal case: */
  5633. xp[i] = (float) Max( X_FLOAT_MIN, Min(X_FLOAT_MAX, (float) tp[i]));
  5634. /* test for range errors (not always needed but do it anyway) */
  5635. nrange += tp[i] < X_FLOAT_MIN || tp[i] > X_FLOAT_MAX;
  5636. }
  5637. /* copy workspace back if necessary */
  5638. if (realign) {
  5639. memcpy(*xpp, tmp, ni*X_SIZEOF_FLOAT);
  5640. xp = (float *) *xpp;
  5641. }
  5642. /* update xpp and tp */
  5643. xp += ni;
  5644. tp += ni;
  5645. *xpp = (void*)xp;
  5646. }
  5647. return nrange == 0 ? ENOERR : NC_ERANGE;
  5648. #else /* not SX */
  5649. char *xp = (char *) *xpp;
  5650. int status = ENOERR;
  5651. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  5652. {
  5653. int lstatus = ncx_put_float_schar(xp, tp);
  5654. if(lstatus != ENOERR)
  5655. status = lstatus;
  5656. }
  5657. *xpp = (void *)xp;
  5658. return status;
  5659. #endif
  5660. }
  5661. int
  5662. ncx_putn_float_uchar(void **xpp, size_t nelems, const uchar *tp)
  5663. {
  5664. #if _SX && \
  5665. X_SIZEOF_FLOAT == SIZEOF_FLOAT
  5666. /* basic algorithm is:
  5667. * - ensure sane alignment of output data
  5668. * - copy (conversion happens automatically) input data
  5669. * to output
  5670. * - update tp to point at next unconverted input, and xpp to point
  5671. * at next location for converted output
  5672. */
  5673. long i, j, ni;
  5674. float tmp[LOOPCNT]; /* in case input is misaligned */
  5675. float *xp;
  5676. int nrange = 0; /* number of range errors */
  5677. int realign = 0; /* "do we need to fix input data alignment?" */
  5678. long cxp = (long) *((char**)xpp);
  5679. realign = (cxp & 7) % SIZEOF_FLOAT;
  5680. /* sjl: manually stripmine so we can limit amount of
  5681. * vector work space reserved to LOOPCNT elements. Also
  5682. * makes vectorisation easy */
  5683. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  5684. ni=Min(nelems-j,LOOPCNT);
  5685. if (realign) {
  5686. xp = tmp;
  5687. } else {
  5688. xp = (float *) *xpp;
  5689. }
  5690. /* copy the next block */
  5691. #pragma cdir loopcnt=LOOPCNT
  5692. #pragma cdir shortloop
  5693. for (i=0; i<ni; i++) {
  5694. /* the normal case: */
  5695. xp[i] = (float) Max( X_FLOAT_MIN, Min(X_FLOAT_MAX, (float) tp[i]));
  5696. /* test for range errors (not always needed but do it anyway) */
  5697. nrange += tp[i] < X_FLOAT_MIN || tp[i] > X_FLOAT_MAX;
  5698. }
  5699. /* copy workspace back if necessary */
  5700. if (realign) {
  5701. memcpy(*xpp, tmp, ni*X_SIZEOF_FLOAT);
  5702. xp = (float *) *xpp;
  5703. }
  5704. /* update xpp and tp */
  5705. xp += ni;
  5706. tp += ni;
  5707. *xpp = (void*)xp;
  5708. }
  5709. return nrange == 0 ? ENOERR : NC_ERANGE;
  5710. #else /* not SX */
  5711. char *xp = (char *) *xpp;
  5712. int status = ENOERR;
  5713. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  5714. {
  5715. int lstatus = ncx_put_float_uchar(xp, tp);
  5716. if(lstatus != ENOERR)
  5717. status = lstatus;
  5718. }
  5719. *xpp = (void *)xp;
  5720. return status;
  5721. #endif
  5722. }
  5723. int
  5724. ncx_putn_float_short(void **xpp, size_t nelems, const short *tp)
  5725. {
  5726. #if _SX && \
  5727. X_SIZEOF_FLOAT == SIZEOF_FLOAT
  5728. /* basic algorithm is:
  5729. * - ensure sane alignment of output data
  5730. * - copy (conversion happens automatically) input data
  5731. * to output
  5732. * - update tp to point at next unconverted input, and xpp to point
  5733. * at next location for converted output
  5734. */
  5735. long i, j, ni;
  5736. float tmp[LOOPCNT]; /* in case input is misaligned */
  5737. float *xp;
  5738. int nrange = 0; /* number of range errors */
  5739. int realign = 0; /* "do we need to fix input data alignment?" */
  5740. long cxp = (long) *((char**)xpp);
  5741. realign = (cxp & 7) % SIZEOF_FLOAT;
  5742. /* sjl: manually stripmine so we can limit amount of
  5743. * vector work space reserved to LOOPCNT elements. Also
  5744. * makes vectorisation easy */
  5745. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  5746. ni=Min(nelems-j,LOOPCNT);
  5747. if (realign) {
  5748. xp = tmp;
  5749. } else {
  5750. xp = (float *) *xpp;
  5751. }
  5752. /* copy the next block */
  5753. #pragma cdir loopcnt=LOOPCNT
  5754. #pragma cdir shortloop
  5755. for (i=0; i<ni; i++) {
  5756. /* the normal case: */
  5757. xp[i] = (float) Max( X_FLOAT_MIN, Min(X_FLOAT_MAX, (float) tp[i]));
  5758. /* test for range errors (not always needed but do it anyway) */
  5759. nrange += tp[i] < X_FLOAT_MIN || tp[i] > X_FLOAT_MAX;
  5760. }
  5761. /* copy workspace back if necessary */
  5762. if (realign) {
  5763. memcpy(*xpp, tmp, ni*X_SIZEOF_FLOAT);
  5764. xp = (float *) *xpp;
  5765. }
  5766. /* update xpp and tp */
  5767. xp += ni;
  5768. tp += ni;
  5769. *xpp = (void*)xp;
  5770. }
  5771. return nrange == 0 ? ENOERR : NC_ERANGE;
  5772. #else /* not SX */
  5773. char *xp = (char *) *xpp;
  5774. int status = ENOERR;
  5775. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  5776. {
  5777. int lstatus = ncx_put_float_short(xp, tp);
  5778. if(lstatus != ENOERR)
  5779. status = lstatus;
  5780. }
  5781. *xpp = (void *)xp;
  5782. return status;
  5783. #endif
  5784. }
  5785. int
  5786. ncx_putn_float_int(void **xpp, size_t nelems, const int *tp)
  5787. {
  5788. #if _SX && \
  5789. X_SIZEOF_FLOAT == SIZEOF_FLOAT
  5790. /* basic algorithm is:
  5791. * - ensure sane alignment of output data
  5792. * - copy (conversion happens automatically) input data
  5793. * to output
  5794. * - update tp to point at next unconverted input, and xpp to point
  5795. * at next location for converted output
  5796. */
  5797. long i, j, ni;
  5798. float tmp[LOOPCNT]; /* in case input is misaligned */
  5799. float *xp;
  5800. int nrange = 0; /* number of range errors */
  5801. int realign = 0; /* "do we need to fix input data alignment?" */
  5802. long cxp = (long) *((char**)xpp);
  5803. realign = (cxp & 7) % SIZEOF_FLOAT;
  5804. /* sjl: manually stripmine so we can limit amount of
  5805. * vector work space reserved to LOOPCNT elements. Also
  5806. * makes vectorisation easy */
  5807. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  5808. ni=Min(nelems-j,LOOPCNT);
  5809. if (realign) {
  5810. xp = tmp;
  5811. } else {
  5812. xp = (float *) *xpp;
  5813. }
  5814. /* copy the next block */
  5815. #pragma cdir loopcnt=LOOPCNT
  5816. #pragma cdir shortloop
  5817. for (i=0; i<ni; i++) {
  5818. /* the normal case: */
  5819. xp[i] = (float) Max( X_FLOAT_MIN, Min(X_FLOAT_MAX, (float) tp[i]));
  5820. /* test for range errors (not always needed but do it anyway) */
  5821. nrange += tp[i] < X_FLOAT_MIN || tp[i] > X_FLOAT_MAX;
  5822. }
  5823. /* copy workspace back if necessary */
  5824. if (realign) {
  5825. memcpy(*xpp, tmp, ni*X_SIZEOF_FLOAT);
  5826. xp = (float *) *xpp;
  5827. }
  5828. /* update xpp and tp */
  5829. xp += ni;
  5830. tp += ni;
  5831. *xpp = (void*)xp;
  5832. }
  5833. return nrange == 0 ? ENOERR : NC_ERANGE;
  5834. #else /* not SX */
  5835. char *xp = (char *) *xpp;
  5836. int status = ENOERR;
  5837. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  5838. {
  5839. int lstatus = ncx_put_float_int(xp, tp);
  5840. if(lstatus != ENOERR)
  5841. status = lstatus;
  5842. }
  5843. *xpp = (void *)xp;
  5844. return status;
  5845. #endif
  5846. }
  5847. #if X_SIZEOF_FLOAT == SIZEOF_FLOAT && !defined(NO_IEEE_FLOAT)
  5848. /* optimized version */
  5849. int
  5850. ncx_putn_float_float(void **xpp, size_t nelems, const float *tp)
  5851. {
  5852. #ifdef WORDS_BIGENDIAN
  5853. (void) memcpy(*xpp, tp, nelems * X_SIZEOF_FLOAT);
  5854. # else
  5855. swapn4b(*xpp, tp, nelems);
  5856. # endif
  5857. *xpp = (void *)((char *)(*xpp) + nelems * X_SIZEOF_FLOAT);
  5858. return ENOERR;
  5859. }
  5860. #elif vax
  5861. int
  5862. ncx_putn_float_float(void **xpp, size_t nfloats, const float *ip)
  5863. {
  5864. const float *const end = ip + nfloats;
  5865. while(ip < end)
  5866. {
  5867. const struct vax_single *const vsp =
  5868. (const struct vax_single *)ip;
  5869. struct ieee_single *const isp = (struct ieee_single *) (*xpp);
  5870. switch(vsp->exp){
  5871. case 0 :
  5872. /* all vax float with zero exponent map to zero */
  5873. *isp = min.ieee;
  5874. break;
  5875. case 2 :
  5876. case 1 :
  5877. {
  5878. /* These will map to subnormals */
  5879. unsigned mantissa = (vsp->mantissa1 << 16)
  5880. | vsp->mantissa2;
  5881. mantissa >>= 3 - vsp->exp;
  5882. mantissa += (1 << (20 + vsp->exp));
  5883. isp->mant_lo_lo = mantissa;
  5884. isp->mant_lo_hi = mantissa >> 8;
  5885. isp->mant_hi = mantissa >> 16;
  5886. isp->exp_lo = 0;
  5887. isp->exp_hi = 0;
  5888. }
  5889. break;
  5890. case 0xff : /* max.s.exp */
  5891. if( vsp->mantissa2 == max.s.mantissa2
  5892. && vsp->mantissa1 == max.s.mantissa1)
  5893. {
  5894. /* map largest vax float to ieee infinity */
  5895. *isp = max.ieee;
  5896. break;
  5897. } /* else, fall thru */
  5898. default :
  5899. {
  5900. unsigned exp = vsp->exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
  5901. isp->exp_hi = exp >> 1;
  5902. isp->exp_lo = exp;
  5903. isp->mant_lo_lo = vsp->mantissa2;
  5904. isp->mant_lo_hi = vsp->mantissa2 >> 8;
  5905. isp->mant_hi = vsp->mantissa1;
  5906. }
  5907. }
  5908. isp->sign = vsp->sign;
  5909. ip++;
  5910. *xpp = (char *)(*xpp) + X_SIZEOF_FLOAT;
  5911. }
  5912. return ENOERR;
  5913. }
  5914. #else
  5915. int
  5916. ncx_putn_float_float(void **xpp, size_t nelems, const float *tp)
  5917. {
  5918. char *xp = *xpp;
  5919. int status = ENOERR;
  5920. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  5921. {
  5922. int lstatus = ncx_put_float_float(xp, tp);
  5923. if(lstatus != ENOERR)
  5924. status = lstatus;
  5925. }
  5926. *xpp = (void *)xp;
  5927. return status;
  5928. }
  5929. #endif
  5930. int
  5931. ncx_putn_float_double(void **xpp, size_t nelems, const double *tp)
  5932. {
  5933. #if _SX && \
  5934. X_SIZEOF_FLOAT == SIZEOF_FLOAT
  5935. /* basic algorithm is:
  5936. * - ensure sane alignment of output data
  5937. * - copy (conversion happens automatically) input data
  5938. * to output
  5939. * - update tp to point at next unconverted input, and xpp to point
  5940. * at next location for converted output
  5941. */
  5942. long i, j, ni;
  5943. float tmp[LOOPCNT]; /* in case input is misaligned */
  5944. float *xp;
  5945. int nrange = 0; /* number of range errors */
  5946. int realign = 0; /* "do we need to fix input data alignment?" */
  5947. long cxp = (long) *((char**)xpp);
  5948. realign = (cxp & 7) % SIZEOF_FLOAT;
  5949. /* sjl: manually stripmine so we can limit amount of
  5950. * vector work space reserved to LOOPCNT elements. Also
  5951. * makes vectorisation easy */
  5952. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  5953. ni=Min(nelems-j,LOOPCNT);
  5954. if (realign) {
  5955. xp = tmp;
  5956. } else {
  5957. xp = (float *) *xpp;
  5958. }
  5959. /* copy the next block */
  5960. #pragma cdir loopcnt=LOOPCNT
  5961. #pragma cdir shortloop
  5962. for (i=0; i<ni; i++) {
  5963. /* the normal case: */
  5964. xp[i] = (float) Max( X_FLOAT_MIN, Min(X_FLOAT_MAX, (float) tp[i]));
  5965. /* test for range errors (not always needed but do it anyway) */
  5966. nrange += tp[i] < X_FLOAT_MIN || tp[i] > X_FLOAT_MAX;
  5967. }
  5968. /* copy workspace back if necessary */
  5969. if (realign) {
  5970. memcpy(*xpp, tmp, ni*X_SIZEOF_FLOAT);
  5971. xp = (float *) *xpp;
  5972. }
  5973. /* update xpp and tp */
  5974. xp += ni;
  5975. tp += ni;
  5976. *xpp = (void*)xp;
  5977. }
  5978. return nrange == 0 ? ENOERR : NC_ERANGE;
  5979. #else /* not SX */
  5980. char *xp = (char *) *xpp;
  5981. int status = ENOERR;
  5982. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  5983. {
  5984. int lstatus = ncx_put_float_double(xp, tp);
  5985. if(lstatus != ENOERR)
  5986. status = lstatus;
  5987. }
  5988. *xpp = (void *)xp;
  5989. return status;
  5990. #endif
  5991. }
  5992. int
  5993. ncx_putn_float_uint(void **xpp, size_t nelems, const uint *tp)
  5994. {
  5995. #if _SX && \
  5996. X_SIZEOF_FLOAT == SIZEOF_FLOAT
  5997. /* basic algorithm is:
  5998. * - ensure sane alignment of output data
  5999. * - copy (conversion happens automatically) input data
  6000. * to output
  6001. * - update tp to point at next unconverted input, and xpp to point
  6002. * at next location for converted output
  6003. */
  6004. long i, j, ni;
  6005. float tmp[LOOPCNT]; /* in case input is misaligned */
  6006. float *xp;
  6007. int nrange = 0; /* number of range errors */
  6008. int realign = 0; /* "do we need to fix input data alignment?" */
  6009. long cxp = (long) *((char**)xpp);
  6010. realign = (cxp & 7) % SIZEOF_FLOAT;
  6011. /* sjl: manually stripmine so we can limit amount of
  6012. * vector work space reserved to LOOPCNT elements. Also
  6013. * makes vectorisation easy */
  6014. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  6015. ni=Min(nelems-j,LOOPCNT);
  6016. if (realign) {
  6017. xp = tmp;
  6018. } else {
  6019. xp = (float *) *xpp;
  6020. }
  6021. /* copy the next block */
  6022. #pragma cdir loopcnt=LOOPCNT
  6023. #pragma cdir shortloop
  6024. for (i=0; i<ni; i++) {
  6025. /* the normal case: */
  6026. xp[i] = (float) Max( X_FLOAT_MIN, Min(X_FLOAT_MAX, (float) tp[i]));
  6027. /* test for range errors (not always needed but do it anyway) */
  6028. nrange += tp[i] < X_FLOAT_MIN || tp[i] > X_FLOAT_MAX;
  6029. }
  6030. /* copy workspace back if necessary */
  6031. if (realign) {
  6032. memcpy(*xpp, tmp, ni*X_SIZEOF_FLOAT);
  6033. xp = (float *) *xpp;
  6034. }
  6035. /* update xpp and tp */
  6036. xp += ni;
  6037. tp += ni;
  6038. *xpp = (void*)xp;
  6039. }
  6040. return nrange == 0 ? ENOERR : NC_ERANGE;
  6041. #else /* not SX */
  6042. char *xp = (char *) *xpp;
  6043. int status = ENOERR;
  6044. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  6045. {
  6046. int lstatus = ncx_put_float_uint(xp, tp);
  6047. if(lstatus != ENOERR)
  6048. status = lstatus;
  6049. }
  6050. *xpp = (void *)xp;
  6051. return status;
  6052. #endif
  6053. }
  6054. int
  6055. ncx_putn_float_longlong(void **xpp, size_t nelems, const longlong *tp)
  6056. {
  6057. #if _SX && \
  6058. X_SIZEOF_FLOAT == SIZEOF_FLOAT
  6059. /* basic algorithm is:
  6060. * - ensure sane alignment of output data
  6061. * - copy (conversion happens automatically) input data
  6062. * to output
  6063. * - update tp to point at next unconverted input, and xpp to point
  6064. * at next location for converted output
  6065. */
  6066. long i, j, ni;
  6067. float tmp[LOOPCNT]; /* in case input is misaligned */
  6068. float *xp;
  6069. int nrange = 0; /* number of range errors */
  6070. int realign = 0; /* "do we need to fix input data alignment?" */
  6071. long cxp = (long) *((char**)xpp);
  6072. realign = (cxp & 7) % SIZEOF_FLOAT;
  6073. /* sjl: manually stripmine so we can limit amount of
  6074. * vector work space reserved to LOOPCNT elements. Also
  6075. * makes vectorisation easy */
  6076. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  6077. ni=Min(nelems-j,LOOPCNT);
  6078. if (realign) {
  6079. xp = tmp;
  6080. } else {
  6081. xp = (float *) *xpp;
  6082. }
  6083. /* copy the next block */
  6084. #pragma cdir loopcnt=LOOPCNT
  6085. #pragma cdir shortloop
  6086. for (i=0; i<ni; i++) {
  6087. /* the normal case: */
  6088. xp[i] = (float) Max( X_FLOAT_MIN, Min(X_FLOAT_MAX, (float) tp[i]));
  6089. /* test for range errors (not always needed but do it anyway) */
  6090. nrange += tp[i] < X_FLOAT_MIN || tp[i] > X_FLOAT_MAX;
  6091. }
  6092. /* copy workspace back if necessary */
  6093. if (realign) {
  6094. memcpy(*xpp, tmp, ni*X_SIZEOF_FLOAT);
  6095. xp = (float *) *xpp;
  6096. }
  6097. /* update xpp and tp */
  6098. xp += ni;
  6099. tp += ni;
  6100. *xpp = (void*)xp;
  6101. }
  6102. return nrange == 0 ? ENOERR : NC_ERANGE;
  6103. #else /* not SX */
  6104. char *xp = (char *) *xpp;
  6105. int status = ENOERR;
  6106. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  6107. {
  6108. int lstatus = ncx_put_float_longlong(xp, tp);
  6109. if(lstatus != ENOERR)
  6110. status = lstatus;
  6111. }
  6112. *xpp = (void *)xp;
  6113. return status;
  6114. #endif
  6115. }
  6116. int
  6117. ncx_putn_float_ulonglong(void **xpp, size_t nelems, const ulonglong *tp)
  6118. {
  6119. #if _SX && \
  6120. X_SIZEOF_FLOAT == SIZEOF_FLOAT
  6121. /* basic algorithm is:
  6122. * - ensure sane alignment of output data
  6123. * - copy (conversion happens automatically) input data
  6124. * to output
  6125. * - update tp to point at next unconverted input, and xpp to point
  6126. * at next location for converted output
  6127. */
  6128. long i, j, ni;
  6129. float tmp[LOOPCNT]; /* in case input is misaligned */
  6130. float *xp;
  6131. int nrange = 0; /* number of range errors */
  6132. int realign = 0; /* "do we need to fix input data alignment?" */
  6133. long cxp = (long) *((char**)xpp);
  6134. realign = (cxp & 7) % SIZEOF_FLOAT;
  6135. /* sjl: manually stripmine so we can limit amount of
  6136. * vector work space reserved to LOOPCNT elements. Also
  6137. * makes vectorisation easy */
  6138. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  6139. ni=Min(nelems-j,LOOPCNT);
  6140. if (realign) {
  6141. xp = tmp;
  6142. } else {
  6143. xp = (float *) *xpp;
  6144. }
  6145. /* copy the next block */
  6146. #pragma cdir loopcnt=LOOPCNT
  6147. #pragma cdir shortloop
  6148. for (i=0; i<ni; i++) {
  6149. /* the normal case: */
  6150. xp[i] = (float) Max( X_FLOAT_MIN, Min(X_FLOAT_MAX, (float) tp[i]));
  6151. /* test for range errors (not always needed but do it anyway) */
  6152. nrange += tp[i] < X_FLOAT_MIN || tp[i] > X_FLOAT_MAX;
  6153. }
  6154. /* copy workspace back if necessary */
  6155. if (realign) {
  6156. memcpy(*xpp, tmp, ni*X_SIZEOF_FLOAT);
  6157. xp = (float *) *xpp;
  6158. }
  6159. /* update xpp and tp */
  6160. xp += ni;
  6161. tp += ni;
  6162. *xpp = (void*)xp;
  6163. }
  6164. return nrange == 0 ? ENOERR : NC_ERANGE;
  6165. #else /* not SX */
  6166. char *xp = (char *) *xpp;
  6167. int status = ENOERR;
  6168. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  6169. {
  6170. int lstatus = ncx_put_float_ulonglong(xp, tp);
  6171. if(lstatus != ENOERR)
  6172. status = lstatus;
  6173. }
  6174. *xpp = (void *)xp;
  6175. return status;
  6176. #endif
  6177. }
  6178. /* double */
  6179. int
  6180. ncx_getn_double_schar(const void **xpp, size_t nelems, schar *tp)
  6181. {
  6182. #if _SX && \
  6183. X_SIZEOF_DOUBLE == SIZEOF_DOUBLE
  6184. /* basic algorithm is:
  6185. * - ensure sane alignment of input data
  6186. * - copy (conversion happens automatically) input data
  6187. * to output
  6188. * - update xpp to point at next unconverted input, and tp to point
  6189. * at next location for converted output
  6190. */
  6191. long i, j, ni;
  6192. double tmp[LOOPCNT]; /* in case input is misaligned */
  6193. double *xp;
  6194. int nrange = 0; /* number of range errors */
  6195. int realign = 0; /* "do we need to fix input data alignment?" */
  6196. long cxp = (long) *((char**)xpp);
  6197. realign = (cxp & 7) % SIZEOF_DOUBLE;
  6198. /* sjl: manually stripmine so we can limit amount of
  6199. * vector work space reserved to LOOPCNT elements. Also
  6200. * makes vectorisation easy */
  6201. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  6202. ni=Min(nelems-j,LOOPCNT);
  6203. if (realign) {
  6204. memcpy(tmp, *xpp, ni*SIZEOF_DOUBLE);
  6205. xp = tmp;
  6206. } else {
  6207. xp = (double *) *xpp;
  6208. }
  6209. /* copy the next block */
  6210. #pragma cdir loopcnt=LOOPCNT
  6211. #pragma cdir shortloop
  6212. for (i=0; i<ni; i++) {
  6213. tp[i] = (schar) Max( SCHAR_MIN, Min(SCHAR_MAX, (schar) xp[i]));
  6214. /* test for range errors (not always needed but do it anyway) */
  6215. nrange += xp[i] < SCHAR_MIN || xp[i] > SCHAR_MAX;
  6216. }
  6217. /* update xpp and tp */
  6218. if (realign) xp = (double *) *xpp;
  6219. xp += ni;
  6220. tp += ni;
  6221. *xpp = (void*)xp;
  6222. }
  6223. return nrange == 0 ? ENOERR : NC_ERANGE;
  6224. #else /* not SX */
  6225. const char *xp = (const char *) *xpp;
  6226. int status = ENOERR;
  6227. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  6228. {
  6229. const int lstatus = ncx_get_double_schar(xp, tp);
  6230. if(lstatus != ENOERR)
  6231. status = lstatus;
  6232. }
  6233. *xpp = (const void *)xp;
  6234. return status;
  6235. # endif
  6236. }
  6237. int
  6238. ncx_getn_double_uchar(const void **xpp, size_t nelems, uchar *tp)
  6239. {
  6240. #if _SX && \
  6241. X_SIZEOF_DOUBLE == SIZEOF_DOUBLE
  6242. /* basic algorithm is:
  6243. * - ensure sane alignment of input data
  6244. * - copy (conversion happens automatically) input data
  6245. * to output
  6246. * - update xpp to point at next unconverted input, and tp to point
  6247. * at next location for converted output
  6248. */
  6249. long i, j, ni;
  6250. double tmp[LOOPCNT]; /* in case input is misaligned */
  6251. double *xp;
  6252. int nrange = 0; /* number of range errors */
  6253. int realign = 0; /* "do we need to fix input data alignment?" */
  6254. long cxp = (long) *((char**)xpp);
  6255. realign = (cxp & 7) % SIZEOF_DOUBLE;
  6256. /* sjl: manually stripmine so we can limit amount of
  6257. * vector work space reserved to LOOPCNT elements. Also
  6258. * makes vectorisation easy */
  6259. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  6260. ni=Min(nelems-j,LOOPCNT);
  6261. if (realign) {
  6262. memcpy(tmp, *xpp, ni*SIZEOF_DOUBLE);
  6263. xp = tmp;
  6264. } else {
  6265. xp = (double *) *xpp;
  6266. }
  6267. /* copy the next block */
  6268. #pragma cdir loopcnt=LOOPCNT
  6269. #pragma cdir shortloop
  6270. for (i=0; i<ni; i++) {
  6271. tp[i] = (uchar) Max( UCHAR_MIN, Min(UCHAR_MAX, (uchar) xp[i]));
  6272. /* test for range errors (not always needed but do it anyway) */
  6273. nrange += xp[i] < UCHAR_MIN || xp[i] > UCHAR_MAX;
  6274. }
  6275. /* update xpp and tp */
  6276. if (realign) xp = (double *) *xpp;
  6277. xp += ni;
  6278. tp += ni;
  6279. *xpp = (void*)xp;
  6280. }
  6281. return nrange == 0 ? ENOERR : NC_ERANGE;
  6282. #else /* not SX */
  6283. const char *xp = (const char *) *xpp;
  6284. int status = ENOERR;
  6285. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  6286. {
  6287. const int lstatus = ncx_get_double_uchar(xp, tp);
  6288. if(lstatus != ENOERR)
  6289. status = lstatus;
  6290. }
  6291. *xpp = (const void *)xp;
  6292. return status;
  6293. # endif
  6294. }
  6295. int
  6296. ncx_getn_double_short(const void **xpp, size_t nelems, short *tp)
  6297. {
  6298. #if _SX && \
  6299. X_SIZEOF_DOUBLE == SIZEOF_DOUBLE
  6300. /* basic algorithm is:
  6301. * - ensure sane alignment of input data
  6302. * - copy (conversion happens automatically) input data
  6303. * to output
  6304. * - update xpp to point at next unconverted input, and tp to point
  6305. * at next location for converted output
  6306. */
  6307. long i, j, ni;
  6308. double tmp[LOOPCNT]; /* in case input is misaligned */
  6309. double *xp;
  6310. int nrange = 0; /* number of range errors */
  6311. int realign = 0; /* "do we need to fix input data alignment?" */
  6312. long cxp = (long) *((char**)xpp);
  6313. realign = (cxp & 7) % SIZEOF_DOUBLE;
  6314. /* sjl: manually stripmine so we can limit amount of
  6315. * vector work space reserved to LOOPCNT elements. Also
  6316. * makes vectorisation easy */
  6317. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  6318. ni=Min(nelems-j,LOOPCNT);
  6319. if (realign) {
  6320. memcpy(tmp, *xpp, ni*SIZEOF_DOUBLE);
  6321. xp = tmp;
  6322. } else {
  6323. xp = (double *) *xpp;
  6324. }
  6325. /* copy the next block */
  6326. #pragma cdir loopcnt=LOOPCNT
  6327. #pragma cdir shortloop
  6328. for (i=0; i<ni; i++) {
  6329. tp[i] = (short) Max( SHORT_MIN, Min(SHORT_MAX, (short) xp[i]));
  6330. /* test for range errors (not always needed but do it anyway) */
  6331. nrange += xp[i] < SHORT_MIN || xp[i] > SHORT_MAX;
  6332. }
  6333. /* update xpp and tp */
  6334. if (realign) xp = (double *) *xpp;
  6335. xp += ni;
  6336. tp += ni;
  6337. *xpp = (void*)xp;
  6338. }
  6339. return nrange == 0 ? ENOERR : NC_ERANGE;
  6340. #else /* not SX */
  6341. const char *xp = (const char *) *xpp;
  6342. int status = ENOERR;
  6343. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  6344. {
  6345. const int lstatus = ncx_get_double_short(xp, tp);
  6346. if(lstatus != ENOERR)
  6347. status = lstatus;
  6348. }
  6349. *xpp = (const void *)xp;
  6350. return status;
  6351. # endif
  6352. }
  6353. int
  6354. ncx_getn_double_int(const void **xpp, size_t nelems, int *tp)
  6355. {
  6356. #if _SX && \
  6357. X_SIZEOF_DOUBLE == SIZEOF_DOUBLE
  6358. /* basic algorithm is:
  6359. * - ensure sane alignment of input data
  6360. * - copy (conversion happens automatically) input data
  6361. * to output
  6362. * - update xpp to point at next unconverted input, and tp to point
  6363. * at next location for converted output
  6364. */
  6365. long i, j, ni;
  6366. double tmp[LOOPCNT]; /* in case input is misaligned */
  6367. double *xp;
  6368. int nrange = 0; /* number of range errors */
  6369. int realign = 0; /* "do we need to fix input data alignment?" */
  6370. long cxp = (long) *((char**)xpp);
  6371. realign = (cxp & 7) % SIZEOF_DOUBLE;
  6372. /* sjl: manually stripmine so we can limit amount of
  6373. * vector work space reserved to LOOPCNT elements. Also
  6374. * makes vectorisation easy */
  6375. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  6376. ni=Min(nelems-j,LOOPCNT);
  6377. if (realign) {
  6378. memcpy(tmp, *xpp, ni*SIZEOF_DOUBLE);
  6379. xp = tmp;
  6380. } else {
  6381. xp = (double *) *xpp;
  6382. }
  6383. /* copy the next block */
  6384. #pragma cdir loopcnt=LOOPCNT
  6385. #pragma cdir shortloop
  6386. for (i=0; i<ni; i++) {
  6387. tp[i] = (int) Max( INT_MIN, Min(INT_MAX, (int) xp[i]));
  6388. /* test for range errors (not always needed but do it anyway) */
  6389. nrange += xp[i] < INT_MIN || xp[i] > INT_MAX;
  6390. }
  6391. /* update xpp and tp */
  6392. if (realign) xp = (double *) *xpp;
  6393. xp += ni;
  6394. tp += ni;
  6395. *xpp = (void*)xp;
  6396. }
  6397. return nrange == 0 ? ENOERR : NC_ERANGE;
  6398. #else /* not SX */
  6399. const char *xp = (const char *) *xpp;
  6400. int status = ENOERR;
  6401. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  6402. {
  6403. const int lstatus = ncx_get_double_int(xp, tp);
  6404. if(lstatus != ENOERR)
  6405. status = lstatus;
  6406. }
  6407. *xpp = (const void *)xp;
  6408. return status;
  6409. # endif
  6410. }
  6411. int
  6412. ncx_getn_double_float(const void **xpp, size_t nelems, float *tp)
  6413. {
  6414. #if _SX && \
  6415. X_SIZEOF_DOUBLE == SIZEOF_DOUBLE
  6416. /* basic algorithm is:
  6417. * - ensure sane alignment of input data
  6418. * - copy (conversion happens automatically) input data
  6419. * to output
  6420. * - update xpp to point at next unconverted input, and tp to point
  6421. * at next location for converted output
  6422. */
  6423. long i, j, ni;
  6424. double tmp[LOOPCNT]; /* in case input is misaligned */
  6425. double *xp;
  6426. int nrange = 0; /* number of range errors */
  6427. int realign = 0; /* "do we need to fix input data alignment?" */
  6428. long cxp = (long) *((char**)xpp);
  6429. realign = (cxp & 7) % SIZEOF_DOUBLE;
  6430. /* sjl: manually stripmine so we can limit amount of
  6431. * vector work space reserved to LOOPCNT elements. Also
  6432. * makes vectorisation easy */
  6433. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  6434. ni=Min(nelems-j,LOOPCNT);
  6435. if (realign) {
  6436. memcpy(tmp, *xpp, ni*SIZEOF_DOUBLE);
  6437. xp = tmp;
  6438. } else {
  6439. xp = (double *) *xpp;
  6440. }
  6441. /* copy the next block */
  6442. #pragma cdir loopcnt=LOOPCNT
  6443. #pragma cdir shortloop
  6444. for (i=0; i<ni; i++) {
  6445. tp[i] = (float) Max( FLOAT_MIN, Min(FLOAT_MAX, (float) xp[i]));
  6446. /* test for range errors (not always needed but do it anyway) */
  6447. nrange += xp[i] < FLOAT_MIN || xp[i] > FLOAT_MAX;
  6448. }
  6449. /* update xpp and tp */
  6450. if (realign) xp = (double *) *xpp;
  6451. xp += ni;
  6452. tp += ni;
  6453. *xpp = (void*)xp;
  6454. }
  6455. return nrange == 0 ? ENOERR : NC_ERANGE;
  6456. #else /* not SX */
  6457. const char *xp = (const char *) *xpp;
  6458. int status = ENOERR;
  6459. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  6460. {
  6461. const int lstatus = ncx_get_double_float(xp, tp);
  6462. if(lstatus != ENOERR)
  6463. status = lstatus;
  6464. }
  6465. *xpp = (const void *)xp;
  6466. return status;
  6467. # endif
  6468. }
  6469. int
  6470. ncx_getn_double_uint(const void **xpp, size_t nelems, uint *tp)
  6471. {
  6472. #if _SX && \
  6473. X_SIZEOF_DOUBLE == SIZEOF_DOUBLE
  6474. /* basic algorithm is:
  6475. * - ensure sane alignment of input data
  6476. * - copy (conversion happens automatically) input data
  6477. * to output
  6478. * - update xpp to point at next unconverted input, and tp to point
  6479. * at next location for converted output
  6480. */
  6481. long i, j, ni;
  6482. double tmp[LOOPCNT]; /* in case input is misaligned */
  6483. double *xp;
  6484. int nrange = 0; /* number of range errors */
  6485. int realign = 0; /* "do we need to fix input data alignment?" */
  6486. long cxp = (long) *((char**)xpp);
  6487. realign = (cxp & 7) % SIZEOF_DOUBLE;
  6488. /* sjl: manually stripmine so we can limit amount of
  6489. * vector work space reserved to LOOPCNT elements. Also
  6490. * makes vectorisation easy */
  6491. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  6492. ni=Min(nelems-j,LOOPCNT);
  6493. if (realign) {
  6494. memcpy(tmp, *xpp, ni*SIZEOF_DOUBLE);
  6495. xp = tmp;
  6496. } else {
  6497. xp = (double *) *xpp;
  6498. }
  6499. /* copy the next block */
  6500. #pragma cdir loopcnt=LOOPCNT
  6501. #pragma cdir shortloop
  6502. for (i=0; i<ni; i++) {
  6503. tp[i] = (uint) Max( UINT_MIN, Min(UINT_MAX, (uint) xp[i]));
  6504. /* test for range errors (not always needed but do it anyway) */
  6505. nrange += xp[i] < UINT_MIN || xp[i] > UINT_MAX;
  6506. }
  6507. /* update xpp and tp */
  6508. if (realign) xp = (double *) *xpp;
  6509. xp += ni;
  6510. tp += ni;
  6511. *xpp = (void*)xp;
  6512. }
  6513. return nrange == 0 ? ENOERR : NC_ERANGE;
  6514. #else /* not SX */
  6515. const char *xp = (const char *) *xpp;
  6516. int status = ENOERR;
  6517. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  6518. {
  6519. const int lstatus = ncx_get_double_uint(xp, tp);
  6520. if(lstatus != ENOERR)
  6521. status = lstatus;
  6522. }
  6523. *xpp = (const void *)xp;
  6524. return status;
  6525. # endif
  6526. }
  6527. int
  6528. ncx_getn_double_longlong(const void **xpp, size_t nelems, longlong *tp)
  6529. {
  6530. #if _SX && \
  6531. X_SIZEOF_DOUBLE == SIZEOF_DOUBLE
  6532. /* basic algorithm is:
  6533. * - ensure sane alignment of input data
  6534. * - copy (conversion happens automatically) input data
  6535. * to output
  6536. * - update xpp to point at next unconverted input, and tp to point
  6537. * at next location for converted output
  6538. */
  6539. long i, j, ni;
  6540. double tmp[LOOPCNT]; /* in case input is misaligned */
  6541. double *xp;
  6542. int nrange = 0; /* number of range errors */
  6543. int realign = 0; /* "do we need to fix input data alignment?" */
  6544. long cxp = (long) *((char**)xpp);
  6545. realign = (cxp & 7) % SIZEOF_DOUBLE;
  6546. /* sjl: manually stripmine so we can limit amount of
  6547. * vector work space reserved to LOOPCNT elements. Also
  6548. * makes vectorisation easy */
  6549. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  6550. ni=Min(nelems-j,LOOPCNT);
  6551. if (realign) {
  6552. memcpy(tmp, *xpp, ni*SIZEOF_DOUBLE);
  6553. xp = tmp;
  6554. } else {
  6555. xp = (double *) *xpp;
  6556. }
  6557. /* copy the next block */
  6558. #pragma cdir loopcnt=LOOPCNT
  6559. #pragma cdir shortloop
  6560. for (i=0; i<ni; i++) {
  6561. tp[i] = (longlong) Max( LONGLONG_MIN, Min(LONGLONG_MAX, (longlong) xp[i]));
  6562. /* test for range errors (not always needed but do it anyway) */
  6563. nrange += xp[i] < LONGLONG_MIN || xp[i] > LONGLONG_MAX;
  6564. }
  6565. /* update xpp and tp */
  6566. if (realign) xp = (double *) *xpp;
  6567. xp += ni;
  6568. tp += ni;
  6569. *xpp = (void*)xp;
  6570. }
  6571. return nrange == 0 ? ENOERR : NC_ERANGE;
  6572. #else /* not SX */
  6573. const char *xp = (const char *) *xpp;
  6574. int status = ENOERR;
  6575. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  6576. {
  6577. const int lstatus = ncx_get_double_longlong(xp, tp);
  6578. if(lstatus != ENOERR)
  6579. status = lstatus;
  6580. }
  6581. *xpp = (const void *)xp;
  6582. return status;
  6583. # endif
  6584. }
  6585. int
  6586. ncx_getn_double_ulonglong(const void **xpp, size_t nelems, ulonglong *tp)
  6587. {
  6588. #if _SX && \
  6589. X_SIZEOF_DOUBLE == SIZEOF_DOUBLE
  6590. /* basic algorithm is:
  6591. * - ensure sane alignment of input data
  6592. * - copy (conversion happens automatically) input data
  6593. * to output
  6594. * - update xpp to point at next unconverted input, and tp to point
  6595. * at next location for converted output
  6596. */
  6597. long i, j, ni;
  6598. double tmp[LOOPCNT]; /* in case input is misaligned */
  6599. double *xp;
  6600. int nrange = 0; /* number of range errors */
  6601. int realign = 0; /* "do we need to fix input data alignment?" */
  6602. long cxp = (long) *((char**)xpp);
  6603. realign = (cxp & 7) % SIZEOF_DOUBLE;
  6604. /* sjl: manually stripmine so we can limit amount of
  6605. * vector work space reserved to LOOPCNT elements. Also
  6606. * makes vectorisation easy */
  6607. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  6608. ni=Min(nelems-j,LOOPCNT);
  6609. if (realign) {
  6610. memcpy(tmp, *xpp, ni*SIZEOF_DOUBLE);
  6611. xp = tmp;
  6612. } else {
  6613. xp = (double *) *xpp;
  6614. }
  6615. /* copy the next block */
  6616. #pragma cdir loopcnt=LOOPCNT
  6617. #pragma cdir shortloop
  6618. for (i=0; i<ni; i++) {
  6619. tp[i] = (ulonglong) Max( ULONGLONG_MIN, Min(ULONGLONG_MAX, (ulonglong) xp[i]));
  6620. /* test for range errors (not always needed but do it anyway) */
  6621. nrange += xp[i] < ULONGLONG_MIN || xp[i] > ULONGLONG_MAX;
  6622. }
  6623. /* update xpp and tp */
  6624. if (realign) xp = (double *) *xpp;
  6625. xp += ni;
  6626. tp += ni;
  6627. *xpp = (void*)xp;
  6628. }
  6629. return nrange == 0 ? ENOERR : NC_ERANGE;
  6630. #else /* not SX */
  6631. const char *xp = (const char *) *xpp;
  6632. int status = ENOERR;
  6633. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  6634. {
  6635. const int lstatus = ncx_get_double_ulonglong(xp, tp);
  6636. if(lstatus != ENOERR)
  6637. status = lstatus;
  6638. }
  6639. *xpp = (const void *)xp;
  6640. return status;
  6641. # endif
  6642. }
  6643. #if X_SIZEOF_DOUBLE == SIZEOF_DOUBLE && !defined(NO_IEEE_FLOAT)
  6644. /* optimized version */
  6645. int
  6646. ncx_getn_double_double(const void **xpp, size_t nelems, double *tp)
  6647. {
  6648. #ifdef WORDS_BIGENDIAN
  6649. (void) memcpy(tp, *xpp, nelems * sizeof(double));
  6650. # else
  6651. swapn8b(tp, *xpp, nelems);
  6652. # endif
  6653. *xpp = (const void *)((const char *)(*xpp) + nelems * X_SIZEOF_DOUBLE);
  6654. return ENOERR;
  6655. }
  6656. #elif vax
  6657. int
  6658. ncx_getn_double_double(const void **xpp, size_t ndoubles, double *ip)
  6659. {
  6660. double *const end = ip + ndoubles;
  6661. while(ip < end)
  6662. {
  6663. struct vax_double *const vdp =
  6664. (struct vax_double *)ip;
  6665. const struct ieee_double *const idp =
  6666. (const struct ieee_double *) (*xpp);
  6667. {
  6668. const struct dbl_limits *lim;
  6669. int ii;
  6670. for (ii = 0, lim = dbl_limits;
  6671. ii < sizeof(dbl_limits)/sizeof(struct dbl_limits);
  6672. ii++, lim++)
  6673. {
  6674. if ((idp->mant_lo == lim->ieee.mant_lo)
  6675. && (idp->mant_4 == lim->ieee.mant_4)
  6676. && (idp->mant_5 == lim->ieee.mant_5)
  6677. && (idp->mant_6 == lim->ieee.mant_6)
  6678. && (idp->exp_lo == lim->ieee.exp_lo)
  6679. && (idp->exp_hi == lim->ieee.exp_hi)
  6680. )
  6681. {
  6682. *vdp = lim->d;
  6683. goto doneit;
  6684. }
  6685. }
  6686. }
  6687. {
  6688. unsigned exp = idp->exp_hi << 4 | idp->exp_lo;
  6689. vdp->exp = exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
  6690. }
  6691. {
  6692. unsigned mant_hi = ((idp->mant_6 << 16)
  6693. | (idp->mant_5 << 8)
  6694. | idp->mant_4);
  6695. unsigned mant_lo = SWAP4(idp->mant_lo);
  6696. vdp->mantissa1 = (mant_hi >> 13);
  6697. vdp->mantissa2 = ((mant_hi & MASK(13)) << 3)
  6698. | (mant_lo >> 29);
  6699. vdp->mantissa3 = (mant_lo >> 13);
  6700. vdp->mantissa4 = (mant_lo << 3);
  6701. }
  6702. doneit:
  6703. vdp->sign = idp->sign;
  6704. ip++;
  6705. *xpp = (char *)(*xpp) + X_SIZEOF_DOUBLE;
  6706. }
  6707. return ENOERR;
  6708. }
  6709. /* vax */
  6710. #else
  6711. int
  6712. ncx_getn_double_double(const void **xpp, size_t nelems, double *tp)
  6713. {
  6714. const char *xp = *xpp;
  6715. int status = ENOERR;
  6716. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  6717. {
  6718. const int lstatus = ncx_get_double_double(xp, tp);
  6719. if(lstatus != ENOERR)
  6720. status = lstatus;
  6721. }
  6722. *xpp = (const void *)xp;
  6723. return status;
  6724. }
  6725. #endif
  6726. int
  6727. ncx_putn_double_schar(void **xpp, size_t nelems, const schar *tp)
  6728. {
  6729. #if _SX && \
  6730. X_SIZEOF_DOUBLE == SIZEOF_DOUBLE
  6731. /* basic algorithm is:
  6732. * - ensure sane alignment of output data
  6733. * - copy (conversion happens automatically) input data
  6734. * to output
  6735. * - update tp to point at next unconverted input, and xpp to point
  6736. * at next location for converted output
  6737. */
  6738. long i, j, ni;
  6739. double tmp[LOOPCNT]; /* in case input is misaligned */
  6740. double *xp;
  6741. int nrange = 0; /* number of range errors */
  6742. int realign = 0; /* "do we need to fix input data alignment?" */
  6743. long cxp = (long) *((char**)xpp);
  6744. realign = (cxp & 7) % SIZEOF_DOUBLE;
  6745. /* sjl: manually stripmine so we can limit amount of
  6746. * vector work space reserved to LOOPCNT elements. Also
  6747. * makes vectorisation easy */
  6748. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  6749. ni=Min(nelems-j,LOOPCNT);
  6750. if (realign) {
  6751. xp = tmp;
  6752. } else {
  6753. xp = (double *) *xpp;
  6754. }
  6755. /* copy the next block */
  6756. #pragma cdir loopcnt=LOOPCNT
  6757. #pragma cdir shortloop
  6758. for (i=0; i<ni; i++) {
  6759. /* the normal case: */
  6760. xp[i] = (double) Max( X_DOUBLE_MIN, Min(X_DOUBLE_MAX, (double) tp[i]));
  6761. /* test for range errors (not always needed but do it anyway) */
  6762. nrange += tp[i] < X_DOUBLE_MIN || tp[i] > X_DOUBLE_MAX;
  6763. }
  6764. /* copy workspace back if necessary */
  6765. if (realign) {
  6766. memcpy(*xpp, tmp, ni*X_SIZEOF_DOUBLE);
  6767. xp = (double *) *xpp;
  6768. }
  6769. /* update xpp and tp */
  6770. xp += ni;
  6771. tp += ni;
  6772. *xpp = (void*)xp;
  6773. }
  6774. return nrange == 0 ? ENOERR : NC_ERANGE;
  6775. #else /* not SX */
  6776. char *xp = (char *) *xpp;
  6777. int status = ENOERR;
  6778. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  6779. {
  6780. int lstatus = ncx_put_double_schar(xp, tp);
  6781. if(lstatus != ENOERR)
  6782. status = lstatus;
  6783. }
  6784. *xpp = (void *)xp;
  6785. return status;
  6786. #endif
  6787. }
  6788. int
  6789. ncx_putn_double_uchar(void **xpp, size_t nelems, const uchar *tp)
  6790. {
  6791. #if _SX && \
  6792. X_SIZEOF_DOUBLE == SIZEOF_DOUBLE
  6793. /* basic algorithm is:
  6794. * - ensure sane alignment of output data
  6795. * - copy (conversion happens automatically) input data
  6796. * to output
  6797. * - update tp to point at next unconverted input, and xpp to point
  6798. * at next location for converted output
  6799. */
  6800. long i, j, ni;
  6801. double tmp[LOOPCNT]; /* in case input is misaligned */
  6802. double *xp;
  6803. int nrange = 0; /* number of range errors */
  6804. int realign = 0; /* "do we need to fix input data alignment?" */
  6805. long cxp = (long) *((char**)xpp);
  6806. realign = (cxp & 7) % SIZEOF_DOUBLE;
  6807. /* sjl: manually stripmine so we can limit amount of
  6808. * vector work space reserved to LOOPCNT elements. Also
  6809. * makes vectorisation easy */
  6810. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  6811. ni=Min(nelems-j,LOOPCNT);
  6812. if (realign) {
  6813. xp = tmp;
  6814. } else {
  6815. xp = (double *) *xpp;
  6816. }
  6817. /* copy the next block */
  6818. #pragma cdir loopcnt=LOOPCNT
  6819. #pragma cdir shortloop
  6820. for (i=0; i<ni; i++) {
  6821. /* the normal case: */
  6822. xp[i] = (double) Max( X_DOUBLE_MIN, Min(X_DOUBLE_MAX, (double) tp[i]));
  6823. /* test for range errors (not always needed but do it anyway) */
  6824. nrange += tp[i] < X_DOUBLE_MIN || tp[i] > X_DOUBLE_MAX;
  6825. }
  6826. /* copy workspace back if necessary */
  6827. if (realign) {
  6828. memcpy(*xpp, tmp, ni*X_SIZEOF_DOUBLE);
  6829. xp = (double *) *xpp;
  6830. }
  6831. /* update xpp and tp */
  6832. xp += ni;
  6833. tp += ni;
  6834. *xpp = (void*)xp;
  6835. }
  6836. return nrange == 0 ? ENOERR : NC_ERANGE;
  6837. #else /* not SX */
  6838. char *xp = (char *) *xpp;
  6839. int status = ENOERR;
  6840. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  6841. {
  6842. int lstatus = ncx_put_double_uchar(xp, tp);
  6843. if(lstatus != ENOERR)
  6844. status = lstatus;
  6845. }
  6846. *xpp = (void *)xp;
  6847. return status;
  6848. #endif
  6849. }
  6850. int
  6851. ncx_putn_double_short(void **xpp, size_t nelems, const short *tp)
  6852. {
  6853. #if _SX && \
  6854. X_SIZEOF_DOUBLE == SIZEOF_DOUBLE
  6855. /* basic algorithm is:
  6856. * - ensure sane alignment of output data
  6857. * - copy (conversion happens automatically) input data
  6858. * to output
  6859. * - update tp to point at next unconverted input, and xpp to point
  6860. * at next location for converted output
  6861. */
  6862. long i, j, ni;
  6863. double tmp[LOOPCNT]; /* in case input is misaligned */
  6864. double *xp;
  6865. int nrange = 0; /* number of range errors */
  6866. int realign = 0; /* "do we need to fix input data alignment?" */
  6867. long cxp = (long) *((char**)xpp);
  6868. realign = (cxp & 7) % SIZEOF_DOUBLE;
  6869. /* sjl: manually stripmine so we can limit amount of
  6870. * vector work space reserved to LOOPCNT elements. Also
  6871. * makes vectorisation easy */
  6872. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  6873. ni=Min(nelems-j,LOOPCNT);
  6874. if (realign) {
  6875. xp = tmp;
  6876. } else {
  6877. xp = (double *) *xpp;
  6878. }
  6879. /* copy the next block */
  6880. #pragma cdir loopcnt=LOOPCNT
  6881. #pragma cdir shortloop
  6882. for (i=0; i<ni; i++) {
  6883. /* the normal case: */
  6884. xp[i] = (double) Max( X_DOUBLE_MIN, Min(X_DOUBLE_MAX, (double) tp[i]));
  6885. /* test for range errors (not always needed but do it anyway) */
  6886. nrange += tp[i] < X_DOUBLE_MIN || tp[i] > X_DOUBLE_MAX;
  6887. }
  6888. /* copy workspace back if necessary */
  6889. if (realign) {
  6890. memcpy(*xpp, tmp, ni*X_SIZEOF_DOUBLE);
  6891. xp = (double *) *xpp;
  6892. }
  6893. /* update xpp and tp */
  6894. xp += ni;
  6895. tp += ni;
  6896. *xpp = (void*)xp;
  6897. }
  6898. return nrange == 0 ? ENOERR : NC_ERANGE;
  6899. #else /* not SX */
  6900. char *xp = (char *) *xpp;
  6901. int status = ENOERR;
  6902. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  6903. {
  6904. int lstatus = ncx_put_double_short(xp, tp);
  6905. if(lstatus != ENOERR)
  6906. status = lstatus;
  6907. }
  6908. *xpp = (void *)xp;
  6909. return status;
  6910. #endif
  6911. }
  6912. int
  6913. ncx_putn_double_int(void **xpp, size_t nelems, const int *tp)
  6914. {
  6915. #if _SX && \
  6916. X_SIZEOF_DOUBLE == SIZEOF_DOUBLE
  6917. /* basic algorithm is:
  6918. * - ensure sane alignment of output data
  6919. * - copy (conversion happens automatically) input data
  6920. * to output
  6921. * - update tp to point at next unconverted input, and xpp to point
  6922. * at next location for converted output
  6923. */
  6924. long i, j, ni;
  6925. double tmp[LOOPCNT]; /* in case input is misaligned */
  6926. double *xp;
  6927. int nrange = 0; /* number of range errors */
  6928. int realign = 0; /* "do we need to fix input data alignment?" */
  6929. long cxp = (long) *((char**)xpp);
  6930. realign = (cxp & 7) % SIZEOF_DOUBLE;
  6931. /* sjl: manually stripmine so we can limit amount of
  6932. * vector work space reserved to LOOPCNT elements. Also
  6933. * makes vectorisation easy */
  6934. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  6935. ni=Min(nelems-j,LOOPCNT);
  6936. if (realign) {
  6937. xp = tmp;
  6938. } else {
  6939. xp = (double *) *xpp;
  6940. }
  6941. /* copy the next block */
  6942. #pragma cdir loopcnt=LOOPCNT
  6943. #pragma cdir shortloop
  6944. for (i=0; i<ni; i++) {
  6945. /* the normal case: */
  6946. xp[i] = (double) Max( X_DOUBLE_MIN, Min(X_DOUBLE_MAX, (double) tp[i]));
  6947. /* test for range errors (not always needed but do it anyway) */
  6948. nrange += tp[i] < X_DOUBLE_MIN || tp[i] > X_DOUBLE_MAX;
  6949. }
  6950. /* copy workspace back if necessary */
  6951. if (realign) {
  6952. memcpy(*xpp, tmp, ni*X_SIZEOF_DOUBLE);
  6953. xp = (double *) *xpp;
  6954. }
  6955. /* update xpp and tp */
  6956. xp += ni;
  6957. tp += ni;
  6958. *xpp = (void*)xp;
  6959. }
  6960. return nrange == 0 ? ENOERR : NC_ERANGE;
  6961. #else /* not SX */
  6962. char *xp = (char *) *xpp;
  6963. int status = ENOERR;
  6964. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  6965. {
  6966. int lstatus = ncx_put_double_int(xp, tp);
  6967. if(lstatus != ENOERR)
  6968. status = lstatus;
  6969. }
  6970. *xpp = (void *)xp;
  6971. return status;
  6972. #endif
  6973. }
  6974. int
  6975. ncx_putn_double_float(void **xpp, size_t nelems, const float *tp)
  6976. {
  6977. #if _SX && \
  6978. X_SIZEOF_DOUBLE == SIZEOF_DOUBLE
  6979. /* basic algorithm is:
  6980. * - ensure sane alignment of output data
  6981. * - copy (conversion happens automatically) input data
  6982. * to output
  6983. * - update tp to point at next unconverted input, and xpp to point
  6984. * at next location for converted output
  6985. */
  6986. long i, j, ni;
  6987. double tmp[LOOPCNT]; /* in case input is misaligned */
  6988. double *xp;
  6989. int nrange = 0; /* number of range errors */
  6990. int realign = 0; /* "do we need to fix input data alignment?" */
  6991. long cxp = (long) *((char**)xpp);
  6992. realign = (cxp & 7) % SIZEOF_DOUBLE;
  6993. /* sjl: manually stripmine so we can limit amount of
  6994. * vector work space reserved to LOOPCNT elements. Also
  6995. * makes vectorisation easy */
  6996. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  6997. ni=Min(nelems-j,LOOPCNT);
  6998. if (realign) {
  6999. xp = tmp;
  7000. } else {
  7001. xp = (double *) *xpp;
  7002. }
  7003. /* copy the next block */
  7004. #pragma cdir loopcnt=LOOPCNT
  7005. #pragma cdir shortloop
  7006. for (i=0; i<ni; i++) {
  7007. /* the normal case: */
  7008. xp[i] = (double) Max( X_DOUBLE_MIN, Min(X_DOUBLE_MAX, (double) tp[i]));
  7009. /* test for range errors (not always needed but do it anyway) */
  7010. nrange += tp[i] < X_DOUBLE_MIN || tp[i] > X_DOUBLE_MAX;
  7011. }
  7012. /* copy workspace back if necessary */
  7013. if (realign) {
  7014. memcpy(*xpp, tmp, ni*X_SIZEOF_DOUBLE);
  7015. xp = (double *) *xpp;
  7016. }
  7017. /* update xpp and tp */
  7018. xp += ni;
  7019. tp += ni;
  7020. *xpp = (void*)xp;
  7021. }
  7022. return nrange == 0 ? ENOERR : NC_ERANGE;
  7023. #else /* not SX */
  7024. char *xp = (char *) *xpp;
  7025. int status = ENOERR;
  7026. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  7027. {
  7028. int lstatus = ncx_put_double_float(xp, tp);
  7029. if(lstatus != ENOERR)
  7030. status = lstatus;
  7031. }
  7032. *xpp = (void *)xp;
  7033. return status;
  7034. #endif
  7035. }
  7036. int
  7037. ncx_putn_double_uint(void **xpp, size_t nelems, const uint *tp)
  7038. {
  7039. #if _SX && \
  7040. X_SIZEOF_DOUBLE == SIZEOF_DOUBLE
  7041. /* basic algorithm is:
  7042. * - ensure sane alignment of output data
  7043. * - copy (conversion happens automatically) input data
  7044. * to output
  7045. * - update tp to point at next unconverted input, and xpp to point
  7046. * at next location for converted output
  7047. */
  7048. long i, j, ni;
  7049. double tmp[LOOPCNT]; /* in case input is misaligned */
  7050. double *xp;
  7051. int nrange = 0; /* number of range errors */
  7052. int realign = 0; /* "do we need to fix input data alignment?" */
  7053. long cxp = (long) *((char**)xpp);
  7054. realign = (cxp & 7) % SIZEOF_DOUBLE;
  7055. /* sjl: manually stripmine so we can limit amount of
  7056. * vector work space reserved to LOOPCNT elements. Also
  7057. * makes vectorisation easy */
  7058. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  7059. ni=Min(nelems-j,LOOPCNT);
  7060. if (realign) {
  7061. xp = tmp;
  7062. } else {
  7063. xp = (double *) *xpp;
  7064. }
  7065. /* copy the next block */
  7066. #pragma cdir loopcnt=LOOPCNT
  7067. #pragma cdir shortloop
  7068. for (i=0; i<ni; i++) {
  7069. /* the normal case: */
  7070. xp[i] = (double) Max( X_DOUBLE_MIN, Min(X_DOUBLE_MAX, (double) tp[i]));
  7071. /* test for range errors (not always needed but do it anyway) */
  7072. nrange += tp[i] < X_DOUBLE_MIN || tp[i] > X_DOUBLE_MAX;
  7073. }
  7074. /* copy workspace back if necessary */
  7075. if (realign) {
  7076. memcpy(*xpp, tmp, ni*X_SIZEOF_DOUBLE);
  7077. xp = (double *) *xpp;
  7078. }
  7079. /* update xpp and tp */
  7080. xp += ni;
  7081. tp += ni;
  7082. *xpp = (void*)xp;
  7083. }
  7084. return nrange == 0 ? ENOERR : NC_ERANGE;
  7085. #else /* not SX */
  7086. char *xp = (char *) *xpp;
  7087. int status = ENOERR;
  7088. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  7089. {
  7090. int lstatus = ncx_put_double_uint(xp, tp);
  7091. if(lstatus != ENOERR)
  7092. status = lstatus;
  7093. }
  7094. *xpp = (void *)xp;
  7095. return status;
  7096. #endif
  7097. }
  7098. int
  7099. ncx_putn_double_longlong(void **xpp, size_t nelems, const longlong *tp)
  7100. {
  7101. #if _SX && \
  7102. X_SIZEOF_DOUBLE == SIZEOF_DOUBLE
  7103. /* basic algorithm is:
  7104. * - ensure sane alignment of output data
  7105. * - copy (conversion happens automatically) input data
  7106. * to output
  7107. * - update tp to point at next unconverted input, and xpp to point
  7108. * at next location for converted output
  7109. */
  7110. long i, j, ni;
  7111. double tmp[LOOPCNT]; /* in case input is misaligned */
  7112. double *xp;
  7113. int nrange = 0; /* number of range errors */
  7114. int realign = 0; /* "do we need to fix input data alignment?" */
  7115. long cxp = (long) *((char**)xpp);
  7116. realign = (cxp & 7) % SIZEOF_DOUBLE;
  7117. /* sjl: manually stripmine so we can limit amount of
  7118. * vector work space reserved to LOOPCNT elements. Also
  7119. * makes vectorisation easy */
  7120. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  7121. ni=Min(nelems-j,LOOPCNT);
  7122. if (realign) {
  7123. xp = tmp;
  7124. } else {
  7125. xp = (double *) *xpp;
  7126. }
  7127. /* copy the next block */
  7128. #pragma cdir loopcnt=LOOPCNT
  7129. #pragma cdir shortloop
  7130. for (i=0; i<ni; i++) {
  7131. /* the normal case: */
  7132. xp[i] = (double) Max( X_DOUBLE_MIN, Min(X_DOUBLE_MAX, (double) tp[i]));
  7133. /* test for range errors (not always needed but do it anyway) */
  7134. nrange += tp[i] < X_DOUBLE_MIN || tp[i] > X_DOUBLE_MAX;
  7135. }
  7136. /* copy workspace back if necessary */
  7137. if (realign) {
  7138. memcpy(*xpp, tmp, ni*X_SIZEOF_DOUBLE);
  7139. xp = (double *) *xpp;
  7140. }
  7141. /* update xpp and tp */
  7142. xp += ni;
  7143. tp += ni;
  7144. *xpp = (void*)xp;
  7145. }
  7146. return nrange == 0 ? ENOERR : NC_ERANGE;
  7147. #else /* not SX */
  7148. char *xp = (char *) *xpp;
  7149. int status = ENOERR;
  7150. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  7151. {
  7152. int lstatus = ncx_put_double_longlong(xp, tp);
  7153. if(lstatus != ENOERR)
  7154. status = lstatus;
  7155. }
  7156. *xpp = (void *)xp;
  7157. return status;
  7158. #endif
  7159. }
  7160. int
  7161. ncx_putn_double_ulonglong(void **xpp, size_t nelems, const ulonglong *tp)
  7162. {
  7163. #if _SX && \
  7164. X_SIZEOF_DOUBLE == SIZEOF_DOUBLE
  7165. /* basic algorithm is:
  7166. * - ensure sane alignment of output data
  7167. * - copy (conversion happens automatically) input data
  7168. * to output
  7169. * - update tp to point at next unconverted input, and xpp to point
  7170. * at next location for converted output
  7171. */
  7172. long i, j, ni;
  7173. double tmp[LOOPCNT]; /* in case input is misaligned */
  7174. double *xp;
  7175. int nrange = 0; /* number of range errors */
  7176. int realign = 0; /* "do we need to fix input data alignment?" */
  7177. long cxp = (long) *((char**)xpp);
  7178. realign = (cxp & 7) % SIZEOF_DOUBLE;
  7179. /* sjl: manually stripmine so we can limit amount of
  7180. * vector work space reserved to LOOPCNT elements. Also
  7181. * makes vectorisation easy */
  7182. for (j=0; j<nelems && nrange==0; j+=LOOPCNT) {
  7183. ni=Min(nelems-j,LOOPCNT);
  7184. if (realign) {
  7185. xp = tmp;
  7186. } else {
  7187. xp = (double *) *xpp;
  7188. }
  7189. /* copy the next block */
  7190. #pragma cdir loopcnt=LOOPCNT
  7191. #pragma cdir shortloop
  7192. for (i=0; i<ni; i++) {
  7193. /* the normal case: */
  7194. xp[i] = (double) Max( X_DOUBLE_MIN, Min(X_DOUBLE_MAX, (double) tp[i]));
  7195. /* test for range errors (not always needed but do it anyway) */
  7196. nrange += tp[i] < X_DOUBLE_MIN || tp[i] > X_DOUBLE_MAX;
  7197. }
  7198. /* copy workspace back if necessary */
  7199. if (realign) {
  7200. memcpy(*xpp, tmp, ni*X_SIZEOF_DOUBLE);
  7201. xp = (double *) *xpp;
  7202. }
  7203. /* update xpp and tp */
  7204. xp += ni;
  7205. tp += ni;
  7206. *xpp = (void*)xp;
  7207. }
  7208. return nrange == 0 ? ENOERR : NC_ERANGE;
  7209. #else /* not SX */
  7210. char *xp = (char *) *xpp;
  7211. int status = ENOERR;
  7212. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  7213. {
  7214. int lstatus = ncx_put_double_ulonglong(xp, tp);
  7215. if(lstatus != ENOERR)
  7216. status = lstatus;
  7217. }
  7218. *xpp = (void *)xp;
  7219. return status;
  7220. #endif
  7221. }
  7222. #if X_SIZEOF_DOUBLE == SIZEOF_DOUBLE && !defined(NO_IEEE_FLOAT)
  7223. /* optimized version */
  7224. int
  7225. ncx_putn_double_double(void **xpp, size_t nelems, const double *tp)
  7226. {
  7227. #ifdef WORDS_BIGENDIAN
  7228. (void) memcpy(*xpp, tp, nelems * X_SIZEOF_DOUBLE);
  7229. # else
  7230. swapn8b(*xpp, tp, nelems);
  7231. # endif
  7232. *xpp = (void *)((char *)(*xpp) + nelems * X_SIZEOF_DOUBLE);
  7233. return ENOERR;
  7234. }
  7235. #elif vax
  7236. int
  7237. ncx_putn_double_double(void **xpp, size_t ndoubles, const double *ip)
  7238. {
  7239. const double *const end = ip + ndoubles;
  7240. while(ip < end)
  7241. {
  7242. const struct vax_double *const vdp =
  7243. (const struct vax_double *)ip;
  7244. struct ieee_double *const idp =
  7245. (struct ieee_double *) (*xpp);
  7246. if ((vdp->mantissa4 > (dbl_limits[0].d.mantissa4 - 3)) &&
  7247. (vdp->mantissa3 == dbl_limits[0].d.mantissa3) &&
  7248. (vdp->mantissa2 == dbl_limits[0].d.mantissa2) &&
  7249. (vdp->mantissa1 == dbl_limits[0].d.mantissa1) &&
  7250. (vdp->exp == dbl_limits[0].d.exp))
  7251. {
  7252. *idp = dbl_limits[0].ieee;
  7253. goto shipit;
  7254. }
  7255. if ((vdp->mantissa4 == dbl_limits[1].d.mantissa4) &&
  7256. (vdp->mantissa3 == dbl_limits[1].d.mantissa3) &&
  7257. (vdp->mantissa2 == dbl_limits[1].d.mantissa2) &&
  7258. (vdp->mantissa1 == dbl_limits[1].d.mantissa1) &&
  7259. (vdp->exp == dbl_limits[1].d.exp))
  7260. {
  7261. *idp = dbl_limits[1].ieee;
  7262. goto shipit;
  7263. }
  7264. {
  7265. unsigned exp = vdp->exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
  7266. unsigned mant_lo = ((vdp->mantissa2 & MASK(3)) << 29) |
  7267. (vdp->mantissa3 << 13) |
  7268. ((vdp->mantissa4 >> 3) & MASK(13));
  7269. unsigned mant_hi = (vdp->mantissa1 << 13)
  7270. | (vdp->mantissa2 >> 3);
  7271. if((vdp->mantissa4 & 7) > 4)
  7272. {
  7273. /* round up */
  7274. mant_lo++;
  7275. if(mant_lo == 0)
  7276. {
  7277. mant_hi++;
  7278. if(mant_hi > 0xffffff)
  7279. {
  7280. mant_hi = 0;
  7281. exp++;
  7282. }
  7283. }
  7284. }
  7285. idp->mant_lo = SWAP4(mant_lo);
  7286. idp->mant_6 = mant_hi >> 16;
  7287. idp->mant_5 = (mant_hi & 0xff00) >> 8;
  7288. idp->mant_4 = mant_hi;
  7289. idp->exp_hi = exp >> 4;
  7290. idp->exp_lo = exp;
  7291. }
  7292. shipit:
  7293. idp->sign = vdp->sign;
  7294. ip++;
  7295. *xpp = (char *)(*xpp) + X_SIZEOF_DOUBLE;
  7296. }
  7297. return ENOERR;
  7298. }
  7299. /* vax */
  7300. #else
  7301. int
  7302. ncx_putn_double_double(void **xpp, size_t nelems, const double *tp)
  7303. {
  7304. char *xp = *xpp;
  7305. int status = ENOERR;
  7306. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  7307. {
  7308. int lstatus = ncx_put_double_double(xp, tp);
  7309. if(lstatus != ENOERR)
  7310. status = lstatus;
  7311. }
  7312. *xpp = (void *)xp;
  7313. return status;
  7314. }
  7315. #endif
  7316. /*
  7317. * Other aggregate conversion functions.
  7318. */
  7319. /* text */
  7320. int
  7321. ncx_getn_text(const void **xpp, size_t nelems, char *tp)
  7322. {
  7323. (void) memcpy(tp, *xpp, nelems);
  7324. *xpp = (void *)((char *)(*xpp) + nelems);
  7325. return ENOERR;
  7326. }
  7327. int
  7328. ncx_pad_getn_text(const void **xpp, size_t nelems, char *tp)
  7329. {
  7330. size_t rndup = nelems % X_ALIGN;
  7331. if(rndup)
  7332. rndup = X_ALIGN - rndup;
  7333. (void) memcpy(tp, *xpp, nelems);
  7334. *xpp = (void *)((char *)(*xpp) + nelems + rndup);
  7335. return ENOERR;
  7336. }
  7337. int
  7338. ncx_putn_text(void **xpp, size_t nelems, const char *tp)
  7339. {
  7340. (void) memcpy(*xpp, tp, nelems);
  7341. *xpp = (void *)((char *)(*xpp) + nelems);
  7342. return ENOERR;
  7343. }
  7344. int
  7345. ncx_pad_putn_text(void **xpp, size_t nelems, const char *tp)
  7346. {
  7347. size_t rndup = nelems % X_ALIGN;
  7348. if(rndup)
  7349. rndup = X_ALIGN - rndup;
  7350. (void) memcpy(*xpp, tp, nelems);
  7351. *xpp = (void *)((char *)(*xpp) + nelems);
  7352. if(rndup)
  7353. {
  7354. (void) memcpy(*xpp, nada, rndup);
  7355. *xpp = (void *)((char *)(*xpp) + rndup);
  7356. }
  7357. return ENOERR;
  7358. }
  7359. /* opaque */
  7360. int
  7361. ncx_getn_void(const void **xpp, size_t nelems, void *tp)
  7362. {
  7363. (void) memcpy(tp, *xpp, nelems);
  7364. *xpp = (void *)((char *)(*xpp) + nelems);
  7365. return ENOERR;
  7366. }
  7367. int
  7368. ncx_pad_getn_void(const void **xpp, size_t nelems, void *tp)
  7369. {
  7370. size_t rndup = nelems % X_ALIGN;
  7371. if(rndup)
  7372. rndup = X_ALIGN - rndup;
  7373. (void) memcpy(tp, *xpp, nelems);
  7374. *xpp = (void *)((char *)(*xpp) + nelems + rndup);
  7375. return ENOERR;
  7376. }
  7377. int
  7378. ncx_putn_void(void **xpp, size_t nelems, const void *tp)
  7379. {
  7380. (void) memcpy(*xpp, tp, nelems);
  7381. *xpp = (void *)((char *)(*xpp) + nelems);
  7382. return ENOERR;
  7383. }
  7384. int
  7385. ncx_pad_putn_void(void **xpp, size_t nelems, const void *tp)
  7386. {
  7387. size_t rndup = nelems % X_ALIGN;
  7388. if(rndup)
  7389. rndup = X_ALIGN - rndup;
  7390. (void) memcpy(*xpp, tp, nelems);
  7391. *xpp = (void *)((char *)(*xpp) + nelems);
  7392. if(rndup)
  7393. {
  7394. (void) memcpy(*xpp, nada, rndup);
  7395. *xpp = (void *)((char *)(*xpp) + rndup);
  7396. }
  7397. return ENOERR;
  7398. }