ieee.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182
  1. /* ieee.c
  2. *
  3. * Extended precision IEEE binary floating point arithmetic routines
  4. *
  5. * Numbers are stored in C language as arrays of 16-bit unsigned
  6. * short integers. The arguments of the routines are pointers to
  7. * the arrays.
  8. *
  9. *
  10. * External e type data structure, simulates Intel 8087 chip
  11. * temporary real format but possibly with a larger significand:
  12. *
  13. * NE-1 significand words (least significant word first,
  14. * most significant bit is normally set)
  15. * exponent (value = EXONE for 1.0,
  16. * top bit is the sign)
  17. *
  18. *
  19. * Internal data structure of a number (a "word" is 16 bits):
  20. *
  21. * ei[0] sign word (0 for positive, 0xffff for negative)
  22. * ei[1] biased exponent (value = EXONE for the number 1.0)
  23. * ei[2] high guard word (always zero after normalization)
  24. * ei[3]
  25. * to ei[NI-2] significand (NI-4 significand words,
  26. * most significant word first,
  27. * most significant bit is set)
  28. * ei[NI-1] low guard word (0x8000 bit is rounding place)
  29. *
  30. *
  31. *
  32. * Routines for external format numbers
  33. *
  34. * asctoe( string, e ) ASCII string to extended double e type
  35. * asctoe64( string, &d ) ASCII string to long double
  36. * asctoe53( string, &d ) ASCII string to double
  37. * asctoe24( string, &f ) ASCII string to single
  38. * asctoeg( string, e, prec ) ASCII string to specified precision
  39. * e24toe( &f, e ) IEEE single precision to e type
  40. * e53toe( &d, e ) IEEE double precision to e type
  41. * e64toe( &d, e ) IEEE long double precision to e type
  42. * eabs(e) absolute value
  43. * eadd( a, b, c ) c = b + a
  44. * eclear(e) e = 0
  45. * ecmp (a, b) Returns 1 if a > b, 0 if a == b,
  46. * -1 if a < b, -2 if either a or b is a NaN.
  47. * ediv( a, b, c ) c = b / a
  48. * efloor( a, b ) truncate to integer, toward -infinity
  49. * efrexp( a, exp, s ) extract exponent and significand
  50. * eifrac( e, &l, frac ) e to long integer and e type fraction
  51. * euifrac( e, &l, frac ) e to unsigned long integer and e type fraction
  52. * einfin( e ) set e to infinity, leaving its sign alone
  53. * eldexp( a, n, b ) multiply by 2**n
  54. * emov( a, b ) b = a
  55. * emul( a, b, c ) c = b * a
  56. * eneg(e) e = -e
  57. * eround( a, b ) b = nearest integer value to a
  58. * esub( a, b, c ) c = b - a
  59. * e24toasc( &f, str, n ) single to ASCII string, n digits after decimal
  60. * e53toasc( &d, str, n ) double to ASCII string, n digits after decimal
  61. * e64toasc( &d, str, n ) long double to ASCII string
  62. * etoasc( e, str, n ) e to ASCII string, n digits after decimal
  63. * etoe24( e, &f ) convert e type to IEEE single precision
  64. * etoe53( e, &d ) convert e type to IEEE double precision
  65. * etoe64( e, &d ) convert e type to IEEE long double precision
  66. * ltoe( &l, e ) long (32 bit) integer to e type
  67. * ultoe( &l, e ) unsigned long (32 bit) integer to e type
  68. * eisneg( e ) 1 if sign bit of e != 0, else 0
  69. * eisinf( e ) 1 if e has maximum exponent (non-IEEE)
  70. * or is infinite (IEEE)
  71. * eisnan( e ) 1 if e is a NaN
  72. * esqrt( a, b ) b = square root of a
  73. *
  74. *
  75. * Routines for internal format numbers
  76. *
  77. * eaddm( ai, bi ) add significands, bi = bi + ai
  78. * ecleaz(ei) ei = 0
  79. * ecleazs(ei) set ei = 0 but leave its sign alone
  80. * ecmpm( ai, bi ) compare significands, return 1, 0, or -1
  81. * edivm( ai, bi ) divide significands, bi = bi / ai
  82. * emdnorm(ai,l,s,exp) normalize and round off
  83. * emovi( a, ai ) convert external a to internal ai
  84. * emovo( ai, a ) convert internal ai to external a
  85. * emovz( ai, bi ) bi = ai, low guard word of bi = 0
  86. * emulm( ai, bi ) multiply significands, bi = bi * ai
  87. * enormlz(ei) left-justify the significand
  88. * eshdn1( ai ) shift significand and guards down 1 bit
  89. * eshdn8( ai ) shift down 8 bits
  90. * eshdn6( ai ) shift down 16 bits
  91. * eshift( ai, n ) shift ai n bits up (or down if n < 0)
  92. * eshup1( ai ) shift significand and guards up 1 bit
  93. * eshup8( ai ) shift up 8 bits
  94. * eshup6( ai ) shift up 16 bits
  95. * esubm( ai, bi ) subtract significands, bi = bi - ai
  96. *
  97. *
  98. * The result is always normalized and rounded to NI-4 word precision
  99. * after each arithmetic operation.
  100. *
  101. * Exception flags are NOT fully supported.
  102. *
  103. * Define INFINITY in mconf.h for support of infinity; otherwise a
  104. * saturation arithmetic is implemented.
  105. *
  106. * Define NANS for support of Not-a-Number items; otherwise the
  107. * arithmetic will never produce a NaN output, and might be confused
  108. * by a NaN input.
  109. * If NaN's are supported, the output of ecmp(a,b) is -2 if
  110. * either a or b is a NaN. This means asking if(ecmp(a,b) < 0)
  111. * may not be legitimate. Use if(ecmp(a,b) == -1) for less-than
  112. * if in doubt.
  113. * Signaling NaN's are NOT supported; they are treated the same
  114. * as quiet NaN's.
  115. *
  116. * Denormals are always supported here where appropriate (e.g., not
  117. * for conversion to DEC numbers).
  118. */
  119. /*
  120. * Revision history:
  121. *
  122. * 5 Jan 84 PDP-11 assembly language version
  123. * 2 Mar 86 fixed bug in asctoq()
  124. * 6 Dec 86 C language version
  125. * 30 Aug 88 100 digit version, improved rounding
  126. * 15 May 92 80-bit long double support
  127. *
  128. * Author: S. L. Moshier.
  129. */
  130. #include <stdio.h>
  131. #include <math.h>
  132. #include "ehead.h"
  133. /* Change UNK into something else. */
  134. #ifdef UNK
  135. #undef UNK
  136. #if BIGENDIAN
  137. #define MIEEE 1
  138. #else
  139. #define IBMPC 1
  140. #endif
  141. #endif
  142. /* NaN's require infinity support. */
  143. #ifdef NANS
  144. #ifndef INFINITY
  145. #define INFINITY
  146. #endif
  147. #endif
  148. /* This handles 64-bit long ints. */
  149. #define LONGBITS (8 * sizeof(long))
  150. /* Control register for rounding precision.
  151. * This can be set to 80 (if NE=6), 64, 56, 53, or 24 bits.
  152. */
  153. int rndprc = NBITS;
  154. extern int rndprc;
  155. #ifdef ANSIPROT
  156. extern void eaddm ( unsigned short *, unsigned short * );
  157. extern void esubm ( unsigned short *, unsigned short * );
  158. extern void emdnorm ( unsigned short *, int, int, long, int );
  159. extern void asctoeg ( char *, unsigned short *, int );
  160. extern void enan ( unsigned short *, int );
  161. extern void asctoe24 ( char *, unsigned short * );
  162. extern void asctoe53 ( char *, unsigned short * );
  163. extern void asctoe64 ( char *, unsigned short * );
  164. extern void asctoe113 ( char *, unsigned short * );
  165. extern void eremain ( unsigned short *, unsigned short *, unsigned short * );
  166. extern void einit ( void );
  167. extern void eiremain ( unsigned short *, unsigned short * );
  168. extern int ecmp ( unsigned short *, unsigned short * );
  169. extern int edivm ( unsigned short *, unsigned short * );
  170. extern int emulm ( unsigned short *, unsigned short * );
  171. extern int eisneg ( unsigned short * );
  172. extern int eisinf ( unsigned short * );
  173. extern void emovi ( unsigned short *, unsigned short * );
  174. extern void emovo ( unsigned short *, unsigned short * );
  175. extern void emovz ( unsigned short *, unsigned short * );
  176. extern void ecleaz ( unsigned short * );
  177. extern void eadd1 ( unsigned short *, unsigned short *, unsigned short * );
  178. extern int eisnan ( unsigned short * );
  179. extern int eiisnan ( unsigned short * );
  180. static void toe24( unsigned short *, unsigned short * );
  181. static void toe53( unsigned short *, unsigned short * );
  182. static void toe64( unsigned short *, unsigned short * );
  183. static void toe113( unsigned short *, unsigned short * );
  184. void einfin ( unsigned short * );
  185. void eshdn1 ( unsigned short * );
  186. void eshup1 ( unsigned short * );
  187. void eshup6 ( unsigned short * );
  188. void eshdn6 ( unsigned short * );
  189. void eshup8 ( unsigned short * );
  190. void eshdn8 ( unsigned short * );
  191. void m16m ( unsigned short, unsigned short *, unsigned short * );
  192. int ecmpm ( unsigned short *, unsigned short * );
  193. int enormlz ( unsigned short * );
  194. void ecleazs ( unsigned short * );
  195. int eshift ( unsigned short *, int );
  196. void emov ( unsigned short *, unsigned short * );
  197. void eneg ( unsigned short * );
  198. void eclear ( unsigned short * );
  199. void efloor ( unsigned short *, unsigned short * );
  200. void eadd ( unsigned short *, unsigned short *, unsigned short * );
  201. void esub ( unsigned short *, unsigned short *, unsigned short * );
  202. void ediv ( unsigned short *, unsigned short *, unsigned short * );
  203. void emul ( unsigned short *, unsigned short *, unsigned short * );
  204. void e24toe ( unsigned short *, unsigned short * );
  205. void e53toe ( unsigned short *, unsigned short * );
  206. void e64toe ( unsigned short *, unsigned short * );
  207. void e113toe ( unsigned short *, unsigned short * );
  208. void etoasc ( unsigned short *, char *, int );
  209. static int eiisinf ( unsigned short * );
  210. #else
  211. void eaddm(), esubm(), emdnorm(), asctoeg(), enan();
  212. static void toe24(), toe53(), toe64(), toe113();
  213. void eremain(), einit(), eiremain();
  214. int ecmpm(), edivm(), emulm(), eisneg(), eisinf();
  215. void emovi(), emovo(), emovz(), ecleaz(), eadd1();
  216. /* void etodec(), todec(), dectoe(); */
  217. int eisnan(), eiisnan(), ecmpm(), enormlz(), eshift();
  218. void einfin(), eshdn1(), eshup1(), eshup6(), eshdn6();
  219. void eshup8(), eshdn8(), m16m();
  220. void eadd(), esub(), ediv(), emul();
  221. void ecleazs(), emov(), eneg(), eclear(), efloor();
  222. void e24toe(), e53toe(), e64toe(), e113toe(), etoasc();
  223. static int eiisinf();
  224. #endif
  225. void einit()
  226. {
  227. }
  228. /*
  229. ; Clear out entire external format number.
  230. ;
  231. ; unsigned short x[];
  232. ; eclear( x );
  233. */
  234. void eclear( x )
  235. register unsigned short *x;
  236. {
  237. register int i;
  238. for( i=0; i<NE; i++ )
  239. *x++ = 0;
  240. }
  241. /* Move external format number from a to b.
  242. *
  243. * emov( a, b );
  244. */
  245. void emov( a, b )
  246. register unsigned short *a, *b;
  247. {
  248. register int i;
  249. for( i=0; i<NE; i++ )
  250. *b++ = *a++;
  251. }
  252. /*
  253. ; Absolute value of external format number
  254. ;
  255. ; short x[NE];
  256. ; eabs( x );
  257. */
  258. void eabs(x)
  259. unsigned short x[]; /* x is the memory address of a short */
  260. {
  261. x[NE-1] &= 0x7fff; /* sign is top bit of last word of external format */
  262. }
  263. /*
  264. ; Negate external format number
  265. ;
  266. ; unsigned short x[NE];
  267. ; eneg( x );
  268. */
  269. void eneg(x)
  270. unsigned short x[];
  271. {
  272. #ifdef NANS
  273. if( eisnan(x) )
  274. return;
  275. #endif
  276. x[NE-1] ^= 0x8000; /* Toggle the sign bit */
  277. }
  278. /* Return 1 if external format number is negative,
  279. * else return zero.
  280. */
  281. int eisneg(x)
  282. unsigned short x[];
  283. {
  284. #ifdef NANS
  285. if( eisnan(x) )
  286. return( 0 );
  287. #endif
  288. if( x[NE-1] & 0x8000 )
  289. return( 1 );
  290. else
  291. return( 0 );
  292. }
  293. /* Return 1 if external format number has maximum possible exponent,
  294. * else return zero.
  295. */
  296. int eisinf(x)
  297. unsigned short x[];
  298. {
  299. if( (x[NE-1] & 0x7fff) == 0x7fff )
  300. {
  301. #ifdef NANS
  302. if( eisnan(x) )
  303. return( 0 );
  304. #endif
  305. return( 1 );
  306. }
  307. else
  308. return( 0 );
  309. }
  310. /* Check if e-type number is not a number.
  311. */
  312. int eisnan(x)
  313. unsigned short x[];
  314. {
  315. #ifdef NANS
  316. int i;
  317. /* NaN has maximum exponent */
  318. if( (x[NE-1] & 0x7fff) != 0x7fff )
  319. return (0);
  320. /* ... and non-zero significand field. */
  321. for( i=0; i<NE-1; i++ )
  322. {
  323. if( *x++ != 0 )
  324. return (1);
  325. }
  326. #endif
  327. return (0);
  328. }
  329. /*
  330. ; Fill entire number, including exponent and significand, with
  331. ; largest possible number. These programs implement a saturation
  332. ; value that is an ordinary, legal number. A special value
  333. ; "infinity" may also be implemented; this would require tests
  334. ; for that value and implementation of special rules for arithmetic
  335. ; operations involving inifinity.
  336. */
  337. void einfin(x)
  338. register unsigned short *x;
  339. {
  340. register int i;
  341. #ifdef INFINITY
  342. for( i=0; i<NE-1; i++ )
  343. *x++ = 0;
  344. *x |= 32767;
  345. #else
  346. for( i=0; i<NE-1; i++ )
  347. *x++ = 0xffff;
  348. *x |= 32766;
  349. if( rndprc < NBITS )
  350. {
  351. if (rndprc == 113)
  352. {
  353. *(x - 9) = 0;
  354. *(x - 8) = 0;
  355. }
  356. if( rndprc == 64 )
  357. {
  358. *(x-5) = 0;
  359. }
  360. if( rndprc == 53 )
  361. {
  362. *(x-4) = 0xf800;
  363. }
  364. else
  365. {
  366. *(x-4) = 0;
  367. *(x-3) = 0;
  368. *(x-2) = 0xff00;
  369. }
  370. }
  371. #endif
  372. }
  373. /* Move in external format number,
  374. * converting it to internal format.
  375. */
  376. void emovi( a, b )
  377. unsigned short *a, *b;
  378. {
  379. register unsigned short *p, *q;
  380. int i;
  381. q = b;
  382. p = a + (NE-1); /* point to last word of external number */
  383. /* get the sign bit */
  384. if( *p & 0x8000 )
  385. *q++ = 0xffff;
  386. else
  387. *q++ = 0;
  388. /* get the exponent */
  389. *q = *p--;
  390. *q++ &= 0x7fff; /* delete the sign bit */
  391. #ifdef INFINITY
  392. if( (*(q-1) & 0x7fff) == 0x7fff )
  393. {
  394. #ifdef NANS
  395. if( eisnan(a) )
  396. {
  397. *q++ = 0;
  398. for( i=3; i<NI; i++ )
  399. *q++ = *p--;
  400. return;
  401. }
  402. #endif
  403. for( i=2; i<NI; i++ )
  404. *q++ = 0;
  405. return;
  406. }
  407. #endif
  408. /* clear high guard word */
  409. *q++ = 0;
  410. /* move in the significand */
  411. for( i=0; i<NE-1; i++ )
  412. *q++ = *p--;
  413. /* clear low guard word */
  414. *q = 0;
  415. }
  416. /* Move internal format number out,
  417. * converting it to external format.
  418. */
  419. void emovo( a, b )
  420. unsigned short *a, *b;
  421. {
  422. register unsigned short *p, *q;
  423. unsigned short i;
  424. p = a;
  425. q = b + (NE-1); /* point to output exponent */
  426. /* combine sign and exponent */
  427. i = *p++;
  428. if( i )
  429. *q-- = *p++ | 0x8000;
  430. else
  431. *q-- = *p++;
  432. #ifdef INFINITY
  433. if( *(p-1) == 0x7fff )
  434. {
  435. #ifdef NANS
  436. if( eiisnan(a) )
  437. {
  438. enan( b, NBITS );
  439. return;
  440. }
  441. #endif
  442. einfin(b);
  443. return;
  444. }
  445. #endif
  446. /* skip over guard word */
  447. ++p;
  448. /* move the significand */
  449. for( i=0; i<NE-1; i++ )
  450. *q-- = *p++;
  451. }
  452. /* Clear out internal format number.
  453. */
  454. void ecleaz( xi )
  455. register unsigned short *xi;
  456. {
  457. register int i;
  458. for( i=0; i<NI; i++ )
  459. *xi++ = 0;
  460. }
  461. /* same, but don't touch the sign. */
  462. void ecleazs( xi )
  463. register unsigned short *xi;
  464. {
  465. register int i;
  466. ++xi;
  467. for(i=0; i<NI-1; i++)
  468. *xi++ = 0;
  469. }
  470. /* Move internal format number from a to b.
  471. */
  472. void emovz( a, b )
  473. register unsigned short *a, *b;
  474. {
  475. register int i;
  476. for( i=0; i<NI-1; i++ )
  477. *b++ = *a++;
  478. /* clear low guard word */
  479. *b = 0;
  480. }
  481. /* Return nonzero if internal format number is a NaN.
  482. */
  483. int eiisnan (x)
  484. unsigned short x[];
  485. {
  486. int i;
  487. if( (x[E] & 0x7fff) == 0x7fff )
  488. {
  489. for( i=M+1; i<NI; i++ )
  490. {
  491. if( x[i] != 0 )
  492. return(1);
  493. }
  494. }
  495. return(0);
  496. }
  497. #ifdef INFINITY
  498. /* Return nonzero if internal format number is infinite. */
  499. static int
  500. eiisinf (x)
  501. unsigned short x[];
  502. {
  503. #ifdef NANS
  504. if (eiisnan (x))
  505. return (0);
  506. #endif
  507. if ((x[E] & 0x7fff) == 0x7fff)
  508. return (1);
  509. return (0);
  510. }
  511. #endif
  512. /*
  513. ; Compare significands of numbers in internal format.
  514. ; Guard words are included in the comparison.
  515. ;
  516. ; unsigned short a[NI], b[NI];
  517. ; cmpm( a, b );
  518. ;
  519. ; for the significands:
  520. ; returns +1 if a > b
  521. ; 0 if a == b
  522. ; -1 if a < b
  523. */
  524. int ecmpm( a, b )
  525. register unsigned short *a, *b;
  526. {
  527. int i;
  528. a += M; /* skip up to significand area */
  529. b += M;
  530. for( i=M; i<NI; i++ )
  531. {
  532. if( *a++ != *b++ )
  533. goto difrnt;
  534. }
  535. return(0);
  536. difrnt:
  537. if( *(--a) > *(--b) )
  538. return(1);
  539. else
  540. return(-1);
  541. }
  542. /*
  543. ; Shift significand down by 1 bit
  544. */
  545. void eshdn1(x)
  546. register unsigned short *x;
  547. {
  548. register unsigned short bits;
  549. int i;
  550. x += M; /* point to significand area */
  551. bits = 0;
  552. for( i=M; i<NI; i++ )
  553. {
  554. if( *x & 1 )
  555. bits |= 1;
  556. *x >>= 1;
  557. if( bits & 2 )
  558. *x |= 0x8000;
  559. bits <<= 1;
  560. ++x;
  561. }
  562. }
  563. /*
  564. ; Shift significand up by 1 bit
  565. */
  566. void eshup1(x)
  567. register unsigned short *x;
  568. {
  569. register unsigned short bits;
  570. int i;
  571. x += NI-1;
  572. bits = 0;
  573. for( i=M; i<NI; i++ )
  574. {
  575. if( *x & 0x8000 )
  576. bits |= 1;
  577. *x <<= 1;
  578. if( bits & 2 )
  579. *x |= 1;
  580. bits <<= 1;
  581. --x;
  582. }
  583. }
  584. /*
  585. ; Shift significand down by 8 bits
  586. */
  587. void eshdn8(x)
  588. register unsigned short *x;
  589. {
  590. register unsigned short newbyt, oldbyt;
  591. int i;
  592. x += M;
  593. oldbyt = 0;
  594. for( i=M; i<NI; i++ )
  595. {
  596. newbyt = *x << 8;
  597. *x >>= 8;
  598. *x |= oldbyt;
  599. oldbyt = newbyt;
  600. ++x;
  601. }
  602. }
  603. /*
  604. ; Shift significand up by 8 bits
  605. */
  606. void eshup8(x)
  607. register unsigned short *x;
  608. {
  609. int i;
  610. register unsigned short newbyt, oldbyt;
  611. x += NI-1;
  612. oldbyt = 0;
  613. for( i=M; i<NI; i++ )
  614. {
  615. newbyt = *x >> 8;
  616. *x <<= 8;
  617. *x |= oldbyt;
  618. oldbyt = newbyt;
  619. --x;
  620. }
  621. }
  622. /*
  623. ; Shift significand up by 16 bits
  624. */
  625. void eshup6(x)
  626. register unsigned short *x;
  627. {
  628. int i;
  629. register unsigned short *p;
  630. p = x + M;
  631. x += M + 1;
  632. for( i=M; i<NI-1; i++ )
  633. *p++ = *x++;
  634. *p = 0;
  635. }
  636. /*
  637. ; Shift significand down by 16 bits
  638. */
  639. void eshdn6(x)
  640. register unsigned short *x;
  641. {
  642. int i;
  643. register unsigned short *p;
  644. x += NI-1;
  645. p = x + 1;
  646. for( i=M; i<NI-1; i++ )
  647. *(--p) = *(--x);
  648. *(--p) = 0;
  649. }
  650. /*
  651. ; Add significands
  652. ; x + y replaces y
  653. */
  654. void eaddm( x, y )
  655. unsigned short *x, *y;
  656. {
  657. register unsigned long a;
  658. int i;
  659. unsigned int carry;
  660. x += NI-1;
  661. y += NI-1;
  662. carry = 0;
  663. for( i=M; i<NI; i++ )
  664. {
  665. a = (unsigned long )(*x) + (unsigned long )(*y) + carry;
  666. if( a & 0x10000 )
  667. carry = 1;
  668. else
  669. carry = 0;
  670. *y = (unsigned short )a;
  671. --x;
  672. --y;
  673. }
  674. }
  675. /*
  676. ; Subtract significands
  677. ; y - x replaces y
  678. */
  679. void esubm( x, y )
  680. unsigned short *x, *y;
  681. {
  682. unsigned long a;
  683. int i;
  684. unsigned int carry;
  685. x += NI-1;
  686. y += NI-1;
  687. carry = 0;
  688. for( i=M; i<NI; i++ )
  689. {
  690. a = (unsigned long )(*y) - (unsigned long )(*x) - carry;
  691. if( a & 0x10000 )
  692. carry = 1;
  693. else
  694. carry = 0;
  695. *y = (unsigned short )a;
  696. --x;
  697. --y;
  698. }
  699. }
  700. /* Divide significands */
  701. static unsigned short equot[NI] = {0}; /* was static */
  702. #if 0
  703. int edivm( den, num )
  704. unsigned short den[], num[];
  705. {
  706. int i;
  707. register unsigned short *p, *q;
  708. unsigned short j;
  709. p = &equot[0];
  710. *p++ = num[0];
  711. *p++ = num[1];
  712. for( i=M; i<NI; i++ )
  713. {
  714. *p++ = 0;
  715. }
  716. /* Use faster compare and subtraction if denominator
  717. * has only 15 bits of significane.
  718. */
  719. p = &den[M+2];
  720. if( *p++ == 0 )
  721. {
  722. for( i=M+3; i<NI; i++ )
  723. {
  724. if( *p++ != 0 )
  725. goto fulldiv;
  726. }
  727. if( (den[M+1] & 1) != 0 )
  728. goto fulldiv;
  729. eshdn1(num);
  730. eshdn1(den);
  731. p = &den[M+1];
  732. q = &num[M+1];
  733. for( i=0; i<NBITS+2; i++ )
  734. {
  735. if( *p <= *q )
  736. {
  737. *q -= *p;
  738. j = 1;
  739. }
  740. else
  741. {
  742. j = 0;
  743. }
  744. eshup1(equot);
  745. equot[NI-2] |= j;
  746. eshup1(num);
  747. }
  748. goto divdon;
  749. }
  750. /* The number of quotient bits to calculate is
  751. * NBITS + 1 scaling guard bit + 1 roundoff bit.
  752. */
  753. fulldiv:
  754. p = &equot[NI-2];
  755. for( i=0; i<NBITS+2; i++ )
  756. {
  757. if( ecmpm(den,num) <= 0 )
  758. {
  759. esubm(den, num);
  760. j = 1; /* quotient bit = 1 */
  761. }
  762. else
  763. j = 0;
  764. eshup1(equot);
  765. *p |= j;
  766. eshup1(num);
  767. }
  768. divdon:
  769. eshdn1( equot );
  770. eshdn1( equot );
  771. /* test for nonzero remainder after roundoff bit */
  772. p = &num[M];
  773. j = 0;
  774. for( i=M; i<NI; i++ )
  775. {
  776. j |= *p++;
  777. }
  778. if( j )
  779. j = 1;
  780. for( i=0; i<NI; i++ )
  781. num[i] = equot[i];
  782. return( (int )j );
  783. }
  784. /* Multiply significands */
  785. int emulm( a, b )
  786. unsigned short a[], b[];
  787. {
  788. unsigned short *p, *q;
  789. int i, j, k;
  790. equot[0] = b[0];
  791. equot[1] = b[1];
  792. for( i=M; i<NI; i++ )
  793. equot[i] = 0;
  794. p = &a[NI-2];
  795. k = NBITS;
  796. while( *p == 0 ) /* significand is not supposed to be all zero */
  797. {
  798. eshdn6(a);
  799. k -= 16;
  800. }
  801. if( (*p & 0xff) == 0 )
  802. {
  803. eshdn8(a);
  804. k -= 8;
  805. }
  806. q = &equot[NI-1];
  807. j = 0;
  808. for( i=0; i<k; i++ )
  809. {
  810. if( *p & 1 )
  811. eaddm(b, equot);
  812. /* remember if there were any nonzero bits shifted out */
  813. if( *q & 1 )
  814. j |= 1;
  815. eshdn1(a);
  816. eshdn1(equot);
  817. }
  818. for( i=0; i<NI; i++ )
  819. b[i] = equot[i];
  820. /* return flag for lost nonzero bits */
  821. return(j);
  822. }
  823. #else
  824. /* Multiply significand of e-type number b
  825. by 16-bit quantity a, e-type result to c. */
  826. void m16m( a, b, c )
  827. unsigned short a;
  828. unsigned short b[], c[];
  829. {
  830. register unsigned short *pp;
  831. register unsigned long carry;
  832. unsigned short *ps;
  833. unsigned short p[NI];
  834. unsigned long aa, m;
  835. int i;
  836. aa = a;
  837. pp = &p[NI-2];
  838. *pp++ = 0;
  839. *pp = 0;
  840. ps = &b[NI-1];
  841. for( i=M+1; i<NI; i++ )
  842. {
  843. if( *ps == 0 )
  844. {
  845. --ps;
  846. --pp;
  847. *(pp-1) = 0;
  848. }
  849. else
  850. {
  851. m = (unsigned long) aa * *ps--;
  852. carry = (m & 0xffff) + *pp;
  853. *pp-- = (unsigned short )carry;
  854. carry = (carry >> 16) + (m >> 16) + *pp;
  855. *pp = (unsigned short )carry;
  856. *(pp-1) = carry >> 16;
  857. }
  858. }
  859. for( i=M; i<NI; i++ )
  860. c[i] = p[i];
  861. }
  862. /* Divide significands. Neither the numerator nor the denominator
  863. is permitted to have its high guard word nonzero. */
  864. int edivm( den, num )
  865. unsigned short den[], num[];
  866. {
  867. int i;
  868. register unsigned short *p;
  869. unsigned long tnum;
  870. unsigned short j, tdenm, tquot;
  871. unsigned short tprod[NI+1];
  872. p = &equot[0];
  873. *p++ = num[0];
  874. *p++ = num[1];
  875. for( i=M; i<NI; i++ )
  876. {
  877. *p++ = 0;
  878. }
  879. eshdn1( num );
  880. tdenm = den[M+1];
  881. for( i=M; i<NI; i++ )
  882. {
  883. /* Find trial quotient digit (the radix is 65536). */
  884. tnum = (((unsigned long) num[M]) << 16) + num[M+1];
  885. /* Do not execute the divide instruction if it will overflow. */
  886. if( (tdenm * ((unsigned long)0xffffL)) < tnum )
  887. tquot = 0xffff;
  888. else
  889. tquot = tnum / tdenm;
  890. /* Prove that the divide worked. */
  891. /*
  892. tcheck = (unsigned long )tquot * tdenm;
  893. if( tnum - tcheck > tdenm )
  894. tquot = 0xffff;
  895. */
  896. /* Multiply denominator by trial quotient digit. */
  897. m16m( tquot, den, tprod );
  898. /* The quotient digit may have been overestimated. */
  899. if( ecmpm( tprod, num ) > 0 )
  900. {
  901. tquot -= 1;
  902. esubm( den, tprod );
  903. if( ecmpm( tprod, num ) > 0 )
  904. {
  905. tquot -= 1;
  906. esubm( den, tprod );
  907. }
  908. }
  909. /*
  910. if( ecmpm( tprod, num ) > 0 )
  911. {
  912. eshow( "tprod", tprod );
  913. eshow( "num ", num );
  914. printf( "tnum = %08lx, tden = %04x, tquot = %04x\n",
  915. tnum, den[M+1], tquot );
  916. }
  917. */
  918. esubm( tprod, num );
  919. /*
  920. if( ecmpm( num, den ) >= 0 )
  921. {
  922. eshow( "num ", num );
  923. eshow( "den ", den );
  924. printf( "tnum = %08lx, tden = %04x, tquot = %04x\n",
  925. tnum, den[M+1], tquot );
  926. }
  927. */
  928. equot[i] = tquot;
  929. eshup6(num);
  930. }
  931. /* test for nonzero remainder after roundoff bit */
  932. p = &num[M];
  933. j = 0;
  934. for( i=M; i<NI; i++ )
  935. {
  936. j |= *p++;
  937. }
  938. if( j )
  939. j = 1;
  940. for( i=0; i<NI; i++ )
  941. num[i] = equot[i];
  942. return( (int )j );
  943. }
  944. /* Multiply significands */
  945. int emulm( a, b )
  946. unsigned short a[], b[];
  947. {
  948. unsigned short *p, *q;
  949. unsigned short pprod[NI];
  950. unsigned short j;
  951. int i;
  952. equot[0] = b[0];
  953. equot[1] = b[1];
  954. for( i=M; i<NI; i++ )
  955. equot[i] = 0;
  956. j = 0;
  957. p = &a[NI-1];
  958. q = &equot[NI-1];
  959. for( i=M+1; i<NI; i++ )
  960. {
  961. if( *p == 0 )
  962. {
  963. --p;
  964. }
  965. else
  966. {
  967. m16m( *p--, b, pprod );
  968. eaddm(pprod, equot);
  969. }
  970. j |= *q;
  971. eshdn6(equot);
  972. }
  973. for( i=0; i<NI; i++ )
  974. b[i] = equot[i];
  975. /* return flag for lost nonzero bits */
  976. return( (int)j );
  977. }
  978. /*
  979. eshow(str, x)
  980. char *str;
  981. unsigned short *x;
  982. {
  983. int i;
  984. printf( "%s ", str );
  985. for( i=0; i<NI; i++ )
  986. printf( "%04x ", *x++ );
  987. printf( "\n" );
  988. }
  989. */
  990. #endif
  991. /*
  992. * Normalize and round off.
  993. *
  994. * The internal format number to be rounded is "s".
  995. * Input "lost" indicates whether the number is exact.
  996. * This is the so-called sticky bit.
  997. *
  998. * Input "subflg" indicates whether the number was obtained
  999. * by a subtraction operation. In that case if lost is nonzero
  1000. * then the number is slightly smaller than indicated.
  1001. *
  1002. * Input "exp" is the biased exponent, which may be negative.
  1003. * the exponent field of "s" is ignored but is replaced by
  1004. * "exp" as adjusted by normalization and rounding.
  1005. *
  1006. * Input "rcntrl" is the rounding control.
  1007. */
  1008. static int rlast = -1;
  1009. static int rw = 0;
  1010. static unsigned short rmsk = 0;
  1011. static unsigned short rmbit = 0;
  1012. static unsigned short rebit = 0;
  1013. static int re = 0;
  1014. static unsigned short rbit[NI] = {0,0,0,0,0,0,0,0};
  1015. void emdnorm( s, lost, subflg, exp, rcntrl )
  1016. unsigned short s[];
  1017. int lost;
  1018. int subflg;
  1019. long exp;
  1020. int rcntrl;
  1021. {
  1022. int i, j;
  1023. unsigned short r;
  1024. /* Normalize */
  1025. j = enormlz( s );
  1026. /* a blank significand could mean either zero or infinity. */
  1027. #ifndef INFINITY
  1028. if( j > NBITS )
  1029. {
  1030. ecleazs( s );
  1031. return;
  1032. }
  1033. #endif
  1034. exp -= j;
  1035. #ifndef INFINITY
  1036. if( exp >= 32767L )
  1037. goto overf;
  1038. #else
  1039. if( (j > NBITS) && (exp < 32767L) )
  1040. {
  1041. ecleazs( s );
  1042. return;
  1043. }
  1044. #endif
  1045. if( exp < 0L )
  1046. {
  1047. if( exp > (long )(-NBITS-1) )
  1048. {
  1049. j = (int )exp;
  1050. i = eshift( s, j );
  1051. if( i )
  1052. lost = 1;
  1053. }
  1054. else
  1055. {
  1056. ecleazs( s );
  1057. return;
  1058. }
  1059. }
  1060. /* Round off, unless told not to by rcntrl. */
  1061. if( rcntrl == 0 )
  1062. goto mdfin;
  1063. /* Set up rounding parameters if the control register changed. */
  1064. if( rndprc != rlast )
  1065. {
  1066. ecleaz( rbit );
  1067. switch( rndprc )
  1068. {
  1069. default:
  1070. case NBITS:
  1071. rw = NI-1; /* low guard word */
  1072. rmsk = 0xffff;
  1073. rmbit = 0x8000;
  1074. rebit = 1;
  1075. re = rw - 1;
  1076. break;
  1077. case 113:
  1078. rw = 10;
  1079. rmsk = 0x7fff;
  1080. rmbit = 0x4000;
  1081. rebit = 0x8000;
  1082. re = rw;
  1083. break;
  1084. case 64:
  1085. rw = 7;
  1086. rmsk = 0xffff;
  1087. rmbit = 0x8000;
  1088. rebit = 1;
  1089. re = rw-1;
  1090. break;
  1091. /* For DEC arithmetic */
  1092. case 56:
  1093. rw = 6;
  1094. rmsk = 0xff;
  1095. rmbit = 0x80;
  1096. rebit = 0x100;
  1097. re = rw;
  1098. break;
  1099. case 53:
  1100. rw = 6;
  1101. rmsk = 0x7ff;
  1102. rmbit = 0x0400;
  1103. rebit = 0x800;
  1104. re = rw;
  1105. break;
  1106. case 24:
  1107. rw = 4;
  1108. rmsk = 0xff;
  1109. rmbit = 0x80;
  1110. rebit = 0x100;
  1111. re = rw;
  1112. break;
  1113. }
  1114. rbit[re] = rebit;
  1115. rlast = rndprc;
  1116. }
  1117. /* Shift down 1 temporarily if the data structure has an implied
  1118. * most significant bit and the number is denormal.
  1119. * For rndprc = 64 or NBITS, there is no implied bit.
  1120. * But Intel long double denormals lose one bit of significance even so.
  1121. */
  1122. #ifdef IBMPC
  1123. if( (exp <= 0) && (rndprc != NBITS) )
  1124. #else
  1125. if( (exp <= 0) && (rndprc != 64) && (rndprc != NBITS) )
  1126. #endif
  1127. {
  1128. lost |= s[NI-1] & 1;
  1129. eshdn1(s);
  1130. }
  1131. /* Clear out all bits below the rounding bit,
  1132. * remembering in r if any were nonzero.
  1133. */
  1134. r = s[rw] & rmsk;
  1135. if( rndprc < NBITS )
  1136. {
  1137. i = rw + 1;
  1138. while( i < NI )
  1139. {
  1140. if( s[i] )
  1141. r |= 1;
  1142. s[i] = 0;
  1143. ++i;
  1144. }
  1145. }
  1146. s[rw] &= ~rmsk;
  1147. if( (r & rmbit) != 0 )
  1148. {
  1149. if( r == rmbit )
  1150. {
  1151. if( lost == 0 )
  1152. { /* round to even */
  1153. if( (s[re] & rebit) == 0 )
  1154. goto mddone;
  1155. }
  1156. else
  1157. {
  1158. if( subflg != 0 )
  1159. goto mddone;
  1160. }
  1161. }
  1162. eaddm( rbit, s );
  1163. }
  1164. mddone:
  1165. #ifdef IBMPC
  1166. if( (exp <= 0) && (rndprc != NBITS) )
  1167. #else
  1168. if( (exp <= 0) && (rndprc != 64) && (rndprc != NBITS) )
  1169. #endif
  1170. {
  1171. eshup1(s);
  1172. }
  1173. if( s[2] != 0 )
  1174. { /* overflow on roundoff */
  1175. eshdn1(s);
  1176. exp += 1;
  1177. }
  1178. mdfin:
  1179. s[NI-1] = 0;
  1180. if( exp >= 32767L )
  1181. {
  1182. #ifndef INFINITY
  1183. overf:
  1184. #endif
  1185. #ifdef INFINITY
  1186. s[1] = 32767;
  1187. for( i=2; i<NI-1; i++ )
  1188. s[i] = 0;
  1189. #else
  1190. s[1] = 32766;
  1191. s[2] = 0;
  1192. for( i=M+1; i<NI-1; i++ )
  1193. s[i] = 0xffff;
  1194. s[NI-1] = 0;
  1195. if( (rndprc < 64) || (rndprc == 113) )
  1196. {
  1197. s[rw] &= ~rmsk;
  1198. if( rndprc == 24 )
  1199. {
  1200. s[5] = 0;
  1201. s[6] = 0;
  1202. }
  1203. }
  1204. #endif
  1205. return;
  1206. }
  1207. if( exp < 0 )
  1208. s[1] = 0;
  1209. else
  1210. s[1] = (unsigned short )exp;
  1211. }
  1212. /*
  1213. ; Subtract external format numbers.
  1214. ;
  1215. ; unsigned short a[NE], b[NE], c[NE];
  1216. ; esub( a, b, c ); c = b - a
  1217. */
  1218. static int subflg = 0;
  1219. void esub( a, b, c )
  1220. unsigned short *a, *b, *c;
  1221. {
  1222. #ifdef NANS
  1223. if( eisnan(a) )
  1224. {
  1225. emov (a, c);
  1226. return;
  1227. }
  1228. if( eisnan(b) )
  1229. {
  1230. emov(b,c);
  1231. return;
  1232. }
  1233. /* Infinity minus infinity is a NaN.
  1234. * Test for subtracting infinities of the same sign.
  1235. */
  1236. if( eisinf(a) && eisinf(b) && ((eisneg (a) ^ eisneg (b)) == 0))
  1237. {
  1238. mtherr( "esub", DOMAIN );
  1239. enan( c, NBITS );
  1240. return;
  1241. }
  1242. #endif
  1243. subflg = 1;
  1244. eadd1( a, b, c );
  1245. }
  1246. /*
  1247. ; Add.
  1248. ;
  1249. ; unsigned short a[NE], b[NE], c[NE];
  1250. ; eadd( a, b, c ); c = b + a
  1251. */
  1252. void eadd( a, b, c )
  1253. unsigned short *a, *b, *c;
  1254. {
  1255. #ifdef NANS
  1256. /* NaN plus anything is a NaN. */
  1257. if( eisnan(a) )
  1258. {
  1259. emov(a,c);
  1260. return;
  1261. }
  1262. if( eisnan(b) )
  1263. {
  1264. emov(b,c);
  1265. return;
  1266. }
  1267. /* Infinity minus infinity is a NaN.
  1268. * Test for adding infinities of opposite signs.
  1269. */
  1270. if( eisinf(a) && eisinf(b)
  1271. && ((eisneg(a) ^ eisneg(b)) != 0) )
  1272. {
  1273. mtherr( "eadd", DOMAIN );
  1274. enan( c, NBITS );
  1275. return;
  1276. }
  1277. #endif
  1278. subflg = 0;
  1279. eadd1( a, b, c );
  1280. }
  1281. void eadd1( a, b, c )
  1282. unsigned short *a, *b, *c;
  1283. {
  1284. unsigned short ai[NI], bi[NI], ci[NI];
  1285. int i, lost, j, k;
  1286. long lt, lta, ltb;
  1287. #ifdef INFINITY
  1288. if( eisinf(a) )
  1289. {
  1290. emov(a,c);
  1291. if( subflg )
  1292. eneg(c);
  1293. return;
  1294. }
  1295. if( eisinf(b) )
  1296. {
  1297. emov(b,c);
  1298. return;
  1299. }
  1300. #endif
  1301. emovi( a, ai );
  1302. emovi( b, bi );
  1303. if( subflg )
  1304. ai[0] = ~ai[0];
  1305. /* compare exponents */
  1306. lta = ai[E];
  1307. ltb = bi[E];
  1308. lt = lta - ltb;
  1309. if( lt > 0L )
  1310. { /* put the larger number in bi */
  1311. emovz( bi, ci );
  1312. emovz( ai, bi );
  1313. emovz( ci, ai );
  1314. ltb = bi[E];
  1315. lt = -lt;
  1316. }
  1317. lost = 0;
  1318. if( lt != 0L )
  1319. {
  1320. if( lt < (long )(-NBITS-1) )
  1321. goto done; /* answer same as larger addend */
  1322. k = (int )lt;
  1323. lost = eshift( ai, k ); /* shift the smaller number down */
  1324. }
  1325. else
  1326. {
  1327. /* exponents were the same, so must compare significands */
  1328. i = ecmpm( ai, bi );
  1329. if( i == 0 )
  1330. { /* the numbers are identical in magnitude */
  1331. /* if different signs, result is zero */
  1332. if( ai[0] != bi[0] )
  1333. {
  1334. eclear(c);
  1335. return;
  1336. }
  1337. /* if same sign, result is double */
  1338. /* double denomalized tiny number */
  1339. if( (bi[E] == 0) && ((bi[3] & 0x8000) == 0) )
  1340. {
  1341. eshup1( bi );
  1342. goto done;
  1343. }
  1344. /* add 1 to exponent unless both are zero! */
  1345. for( j=1; j<NI-1; j++ )
  1346. {
  1347. if( bi[j] != 0 )
  1348. {
  1349. /* This could overflow, but let emovo take care of that. */
  1350. ltb += 1;
  1351. break;
  1352. }
  1353. }
  1354. bi[E] = (unsigned short )ltb;
  1355. goto done;
  1356. }
  1357. if( i > 0 )
  1358. { /* put the larger number in bi */
  1359. emovz( bi, ci );
  1360. emovz( ai, bi );
  1361. emovz( ci, ai );
  1362. }
  1363. }
  1364. if( ai[0] == bi[0] )
  1365. {
  1366. eaddm( ai, bi );
  1367. subflg = 0;
  1368. }
  1369. else
  1370. {
  1371. esubm( ai, bi );
  1372. subflg = 1;
  1373. }
  1374. emdnorm( bi, lost, subflg, ltb, 64 );
  1375. done:
  1376. emovo( bi, c );
  1377. }
  1378. /*
  1379. ; Divide.
  1380. ;
  1381. ; unsigned short a[NE], b[NE], c[NE];
  1382. ; ediv( a, b, c ); c = b / a
  1383. */
  1384. void ediv( a, b, c )
  1385. unsigned short *a, *b, *c;
  1386. {
  1387. unsigned short ai[NI], bi[NI];
  1388. int i, sign;
  1389. long lt, lta, ltb;
  1390. /* IEEE says if result is not a NaN, the sign is "-" if and only if
  1391. operands have opposite signs -- but flush -0 to 0 later if not IEEE. */
  1392. sign = eisneg(a) ^ eisneg(b);
  1393. #ifdef NANS
  1394. /* Return any NaN input. */
  1395. if( eisnan(a) )
  1396. {
  1397. emov(a,c);
  1398. return;
  1399. }
  1400. if( eisnan(b) )
  1401. {
  1402. emov(b,c);
  1403. return;
  1404. }
  1405. /* Zero over zero, or infinity over infinity, is a NaN. */
  1406. if( ((ecmp(a,ezero) == 0) && (ecmp(b,ezero) == 0))
  1407. || (eisinf (a) && eisinf (b)) )
  1408. {
  1409. mtherr( "ediv", DOMAIN );
  1410. enan( c, NBITS );
  1411. return;
  1412. }
  1413. #endif
  1414. /* Infinity over anything else is infinity. */
  1415. #ifdef INFINITY
  1416. if( eisinf(b) )
  1417. {
  1418. einfin(c);
  1419. goto divsign;
  1420. }
  1421. if( eisinf(a) )
  1422. {
  1423. eclear(c);
  1424. goto divsign;
  1425. }
  1426. #endif
  1427. emovi( a, ai );
  1428. emovi( b, bi );
  1429. lta = ai[E];
  1430. ltb = bi[E];
  1431. if( bi[E] == 0 )
  1432. { /* See if numerator is zero. */
  1433. for( i=1; i<NI-1; i++ )
  1434. {
  1435. if( bi[i] != 0 )
  1436. {
  1437. ltb -= enormlz( bi );
  1438. goto dnzro1;
  1439. }
  1440. }
  1441. eclear(c);
  1442. goto divsign;
  1443. }
  1444. dnzro1:
  1445. if( ai[E] == 0 )
  1446. { /* possible divide by zero */
  1447. for( i=1; i<NI-1; i++ )
  1448. {
  1449. if( ai[i] != 0 )
  1450. {
  1451. lta -= enormlz( ai );
  1452. goto dnzro2;
  1453. }
  1454. }
  1455. einfin(c);
  1456. mtherr( "ediv", SING );
  1457. goto divsign;
  1458. }
  1459. dnzro2:
  1460. i = edivm( ai, bi );
  1461. /* calculate exponent */
  1462. lt = ltb - lta + EXONE;
  1463. emdnorm( bi, i, 0, lt, 64 );
  1464. emovo( bi, c );
  1465. divsign:
  1466. if( sign )
  1467. *(c+(NE-1)) |= 0x8000;
  1468. else
  1469. *(c+(NE-1)) &= ~0x8000;
  1470. }
  1471. /*
  1472. ; Multiply.
  1473. ;
  1474. ; unsigned short a[NE], b[NE], c[NE];
  1475. ; emul( a, b, c ); c = b * a
  1476. */
  1477. void emul( a, b, c )
  1478. unsigned short *a, *b, *c;
  1479. {
  1480. unsigned short ai[NI], bi[NI];
  1481. int i, j, sign;
  1482. long lt, lta, ltb;
  1483. /* IEEE says if result is not a NaN, the sign is "-" if and only if
  1484. operands have opposite signs -- but flush -0 to 0 later if not IEEE. */
  1485. sign = eisneg(a) ^ eisneg(b);
  1486. #ifdef NANS
  1487. /* NaN times anything is the same NaN. */
  1488. if( eisnan(a) )
  1489. {
  1490. emov(a,c);
  1491. return;
  1492. }
  1493. if( eisnan(b) )
  1494. {
  1495. emov(b,c);
  1496. return;
  1497. }
  1498. /* Zero times infinity is a NaN. */
  1499. if( (eisinf(a) && (ecmp(b,ezero) == 0))
  1500. || (eisinf(b) && (ecmp(a,ezero) == 0)) )
  1501. {
  1502. mtherr( "emul", DOMAIN );
  1503. enan( c, NBITS );
  1504. return;
  1505. }
  1506. #endif
  1507. /* Infinity times anything else is infinity. */
  1508. #ifdef INFINITY
  1509. if( eisinf(a) || eisinf(b) )
  1510. {
  1511. einfin(c);
  1512. goto mulsign;
  1513. }
  1514. #endif
  1515. emovi( a, ai );
  1516. emovi( b, bi );
  1517. lta = ai[E];
  1518. ltb = bi[E];
  1519. if( ai[E] == 0 )
  1520. {
  1521. for( i=1; i<NI-1; i++ )
  1522. {
  1523. if( ai[i] != 0 )
  1524. {
  1525. lta -= enormlz( ai );
  1526. goto mnzer1;
  1527. }
  1528. }
  1529. eclear(c);
  1530. goto mulsign;
  1531. }
  1532. mnzer1:
  1533. if( bi[E] == 0 )
  1534. {
  1535. for( i=1; i<NI-1; i++ )
  1536. {
  1537. if( bi[i] != 0 )
  1538. {
  1539. ltb -= enormlz( bi );
  1540. goto mnzer2;
  1541. }
  1542. }
  1543. eclear(c);
  1544. goto mulsign;
  1545. }
  1546. mnzer2:
  1547. /* Multiply significands */
  1548. j = emulm( ai, bi );
  1549. /* calculate exponent */
  1550. lt = lta + ltb - (EXONE - 1);
  1551. emdnorm( bi, j, 0, lt, 64 );
  1552. emovo( bi, c );
  1553. /* IEEE says sign is "-" if and only if operands have opposite signs. */
  1554. mulsign:
  1555. if( sign )
  1556. *(c+(NE-1)) |= 0x8000;
  1557. else
  1558. *(c+(NE-1)) &= ~0x8000;
  1559. }
  1560. /*
  1561. ; Convert IEEE double precision to e type
  1562. ; double d;
  1563. ; unsigned short x[N+2];
  1564. ; e53toe( &d, x );
  1565. */
  1566. void e53toe( pe, y )
  1567. unsigned short *pe, *y;
  1568. {
  1569. #ifdef DEC
  1570. dectoe( pe, y ); /* see etodec.c */
  1571. #else
  1572. register unsigned short r;
  1573. register unsigned short *p, *e;
  1574. unsigned short yy[NI];
  1575. int denorm, k;
  1576. e = pe;
  1577. denorm = 0; /* flag if denormalized number */
  1578. ecleaz(yy);
  1579. #ifdef IBMPC
  1580. e += 3;
  1581. #endif
  1582. r = *e;
  1583. yy[0] = 0;
  1584. if( r & 0x8000 )
  1585. yy[0] = 0xffff;
  1586. yy[M] = (r & 0x0f) | 0x10;
  1587. r &= ~0x800f; /* strip sign and 4 significand bits */
  1588. #ifdef INFINITY
  1589. if( r == 0x7ff0 )
  1590. {
  1591. #ifdef NANS
  1592. #ifdef IBMPC
  1593. if( ((pe[3] & 0xf) != 0) || (pe[2] != 0)
  1594. || (pe[1] != 0) || (pe[0] != 0) )
  1595. {
  1596. enan( y, NBITS );
  1597. return;
  1598. }
  1599. #else
  1600. if( ((pe[0] & 0xf) != 0) || (pe[1] != 0)
  1601. || (pe[2] != 0) || (pe[3] != 0) )
  1602. {
  1603. enan( y, NBITS );
  1604. return;
  1605. }
  1606. #endif
  1607. #endif /* NANS */
  1608. eclear( y );
  1609. einfin( y );
  1610. if( yy[0] )
  1611. eneg(y);
  1612. return;
  1613. }
  1614. #endif
  1615. r >>= 4;
  1616. /* If zero exponent, then the significand is denormalized.
  1617. * So, take back the understood high significand bit. */
  1618. if( r == 0 )
  1619. {
  1620. denorm = 1;
  1621. yy[M] &= ~0x10;
  1622. }
  1623. r += EXONE - 01777;
  1624. yy[E] = r;
  1625. p = &yy[M+1];
  1626. #ifdef IBMPC
  1627. *p++ = *(--e);
  1628. *p++ = *(--e);
  1629. *p++ = *(--e);
  1630. #endif
  1631. #ifdef MIEEE
  1632. ++e;
  1633. *p++ = *e++;
  1634. *p++ = *e++;
  1635. *p++ = *e++;
  1636. #endif
  1637. (void )eshift( yy, -5 );
  1638. if( denorm )
  1639. { /* if zero exponent, then normalize the significand */
  1640. if( (k = enormlz(yy)) > NBITS )
  1641. ecleazs(yy);
  1642. else
  1643. yy[E] -= (unsigned short )(k-1);
  1644. }
  1645. emovo( yy, y );
  1646. #endif /* not DEC */
  1647. }
  1648. void e64toe( pe, y )
  1649. unsigned short *pe, *y;
  1650. {
  1651. unsigned short yy[NI];
  1652. unsigned short *p, *q, *e;
  1653. int i;
  1654. e = pe;
  1655. p = yy;
  1656. for( i=0; i<NE-5; i++ )
  1657. *p++ = 0;
  1658. #ifdef IBMPC
  1659. for( i=0; i<5; i++ )
  1660. *p++ = *e++;
  1661. #endif
  1662. #ifdef DEC
  1663. for( i=0; i<5; i++ )
  1664. *p++ = *e++;
  1665. #endif
  1666. #ifdef MIEEE
  1667. p = &yy[0] + (NE-1);
  1668. *p-- = *e++;
  1669. ++e;
  1670. for( i=0; i<4; i++ )
  1671. *p-- = *e++;
  1672. #endif
  1673. #ifdef IBMPC
  1674. /* For Intel long double, shift denormal significand up 1
  1675. -- but only if the top significand bit is zero. */
  1676. if((yy[NE-1] & 0x7fff) == 0 && (yy[NE-2] & 0x8000) == 0)
  1677. {
  1678. unsigned short temp[NI+1];
  1679. emovi(yy, temp);
  1680. eshup1(temp);
  1681. emovo(temp,y);
  1682. return;
  1683. }
  1684. #endif
  1685. #ifdef INFINITY
  1686. /* Point to the exponent field. */
  1687. p = &yy[NE-1];
  1688. if( *p == 0x7fff )
  1689. {
  1690. #ifdef NANS
  1691. #ifdef IBMPC
  1692. for( i=0; i<4; i++ )
  1693. {
  1694. if((i != 3 && pe[i] != 0)
  1695. /* Check for Intel long double infinity pattern. */
  1696. || (i == 3 && pe[i] != 0x8000))
  1697. {
  1698. enan( y, NBITS );
  1699. return;
  1700. }
  1701. }
  1702. #else
  1703. for( i=1; i<=4; i++ )
  1704. {
  1705. if( pe[i] != 0 )
  1706. {
  1707. enan( y, NBITS );
  1708. return;
  1709. }
  1710. }
  1711. #endif
  1712. #endif /* NANS */
  1713. eclear( y );
  1714. einfin( y );
  1715. if( *p & 0x8000 )
  1716. eneg(y);
  1717. return;
  1718. }
  1719. #endif
  1720. p = yy;
  1721. q = y;
  1722. for( i=0; i<NE; i++ )
  1723. *q++ = *p++;
  1724. }
  1725. void e113toe(pe,y)
  1726. unsigned short *pe, *y;
  1727. {
  1728. register unsigned short r;
  1729. unsigned short *e, *p;
  1730. unsigned short yy[NI];
  1731. int i;
  1732. e = pe;
  1733. ecleaz(yy);
  1734. #ifdef IBMPC
  1735. e += 7;
  1736. #endif
  1737. r = *e;
  1738. yy[0] = 0;
  1739. if( r & 0x8000 )
  1740. yy[0] = 0xffff;
  1741. r &= 0x7fff;
  1742. #ifdef INFINITY
  1743. if( r == 0x7fff )
  1744. {
  1745. #ifdef NANS
  1746. #ifdef IBMPC
  1747. for( i=0; i<7; i++ )
  1748. {
  1749. if( pe[i] != 0 )
  1750. {
  1751. enan( y, NBITS );
  1752. return;
  1753. }
  1754. }
  1755. #else
  1756. for( i=1; i<8; i++ )
  1757. {
  1758. if( pe[i] != 0 )
  1759. {
  1760. enan( y, NBITS );
  1761. return;
  1762. }
  1763. }
  1764. #endif
  1765. #endif /* NANS */
  1766. eclear( y );
  1767. einfin( y );
  1768. if( *e & 0x8000 )
  1769. eneg(y);
  1770. return;
  1771. }
  1772. #endif /* INFINITY */
  1773. yy[E] = r;
  1774. p = &yy[M + 1];
  1775. #ifdef IBMPC
  1776. for( i=0; i<7; i++ )
  1777. *p++ = *(--e);
  1778. #endif
  1779. #ifdef MIEEE
  1780. ++e;
  1781. for( i=0; i<7; i++ )
  1782. *p++ = *e++;
  1783. #endif
  1784. /* If denormal, remove the implied bit; else shift down 1. */
  1785. if( r == 0 )
  1786. {
  1787. yy[M] = 0;
  1788. }
  1789. else
  1790. {
  1791. yy[M] = 1;
  1792. eshift( yy, -1 );
  1793. }
  1794. emovo(yy,y);
  1795. }
  1796. /*
  1797. ; Convert IEEE single precision to e type
  1798. ; float d;
  1799. ; unsigned short x[N+2];
  1800. ; dtox( &d, x );
  1801. */
  1802. void e24toe( pe, y )
  1803. unsigned short *pe, *y;
  1804. {
  1805. register unsigned short r;
  1806. register unsigned short *p, *e;
  1807. unsigned short yy[NI];
  1808. int denorm, k;
  1809. e = pe;
  1810. denorm = 0; /* flag if denormalized number */
  1811. ecleaz(yy);
  1812. #ifdef IBMPC
  1813. e += 1;
  1814. #endif
  1815. #ifdef DEC
  1816. e += 1;
  1817. #endif
  1818. r = *e;
  1819. yy[0] = 0;
  1820. if( r & 0x8000 )
  1821. yy[0] = 0xffff;
  1822. yy[M] = (r & 0x7f) | 0200;
  1823. r &= ~0x807f; /* strip sign and 7 significand bits */
  1824. #ifdef INFINITY
  1825. if( r == 0x7f80 )
  1826. {
  1827. #ifdef NANS
  1828. #ifdef MIEEE
  1829. if( ((pe[0] & 0x7f) != 0) || (pe[1] != 0) )
  1830. {
  1831. enan( y, NBITS );
  1832. return;
  1833. }
  1834. #else
  1835. if( ((pe[1] & 0x7f) != 0) || (pe[0] != 0) )
  1836. {
  1837. enan( y, NBITS );
  1838. return;
  1839. }
  1840. #endif
  1841. #endif /* NANS */
  1842. eclear( y );
  1843. einfin( y );
  1844. if( yy[0] )
  1845. eneg(y);
  1846. return;
  1847. }
  1848. #endif
  1849. r >>= 7;
  1850. /* If zero exponent, then the significand is denormalized.
  1851. * So, take back the understood high significand bit. */
  1852. if( r == 0 )
  1853. {
  1854. denorm = 1;
  1855. yy[M] &= ~0200;
  1856. }
  1857. r += EXONE - 0177;
  1858. yy[E] = r;
  1859. p = &yy[M+1];
  1860. #ifdef IBMPC
  1861. *p++ = *(--e);
  1862. #endif
  1863. #ifdef DEC
  1864. *p++ = *(--e);
  1865. #endif
  1866. #ifdef MIEEE
  1867. ++e;
  1868. *p++ = *e++;
  1869. #endif
  1870. (void )eshift( yy, -8 );
  1871. if( denorm )
  1872. { /* if zero exponent, then normalize the significand */
  1873. if( (k = enormlz(yy)) > NBITS )
  1874. ecleazs(yy);
  1875. else
  1876. yy[E] -= (unsigned short )(k-1);
  1877. }
  1878. emovo( yy, y );
  1879. }
  1880. void etoe113(x,e)
  1881. unsigned short *x, *e;
  1882. {
  1883. unsigned short xi[NI];
  1884. long exp;
  1885. int rndsav;
  1886. #ifdef NANS
  1887. if( eisnan(x) )
  1888. {
  1889. enan( e, 113 );
  1890. return;
  1891. }
  1892. #endif
  1893. emovi( x, xi );
  1894. exp = (long )xi[E];
  1895. #ifdef INFINITY
  1896. if( eisinf(x) )
  1897. goto nonorm;
  1898. #endif
  1899. /* round off to nearest or even */
  1900. rndsav = rndprc;
  1901. rndprc = 113;
  1902. emdnorm( xi, 0, 0, exp, 64 );
  1903. rndprc = rndsav;
  1904. nonorm:
  1905. toe113 (xi, e);
  1906. }
  1907. /* move out internal format to ieee long double */
  1908. static void toe113(a,b)
  1909. unsigned short *a, *b;
  1910. {
  1911. register unsigned short *p, *q;
  1912. unsigned short i;
  1913. #ifdef NANS
  1914. if( eiisnan(a) )
  1915. {
  1916. enan( b, 113 );
  1917. return;
  1918. }
  1919. #endif
  1920. p = a;
  1921. #ifdef MIEEE
  1922. q = b;
  1923. #else
  1924. q = b + 7; /* point to output exponent */
  1925. #endif
  1926. /* If not denormal, delete the implied bit. */
  1927. if( a[E] != 0 )
  1928. {
  1929. eshup1 (a);
  1930. }
  1931. /* combine sign and exponent */
  1932. i = *p++;
  1933. #ifdef MIEEE
  1934. if( i )
  1935. *q++ = *p++ | 0x8000;
  1936. else
  1937. *q++ = *p++;
  1938. #else
  1939. if( i )
  1940. *q-- = *p++ | 0x8000;
  1941. else
  1942. *q-- = *p++;
  1943. #endif
  1944. /* skip over guard word */
  1945. ++p;
  1946. /* move the significand */
  1947. #ifdef MIEEE
  1948. for (i = 0; i < 7; i++)
  1949. *q++ = *p++;
  1950. #else
  1951. for (i = 0; i < 7; i++)
  1952. *q-- = *p++;
  1953. #endif
  1954. }
  1955. void etoe64( x, e )
  1956. unsigned short *x, *e;
  1957. {
  1958. unsigned short xi[NI];
  1959. long exp;
  1960. int rndsav;
  1961. #ifdef NANS
  1962. if( eisnan(x) )
  1963. {
  1964. enan( e, 64 );
  1965. return;
  1966. }
  1967. #endif
  1968. emovi( x, xi );
  1969. exp = (long )xi[E]; /* adjust exponent for offset */
  1970. #ifdef INFINITY
  1971. if( eisinf(x) )
  1972. goto nonorm;
  1973. #endif
  1974. /* round off to nearest or even */
  1975. rndsav = rndprc;
  1976. rndprc = 64;
  1977. emdnorm( xi, 0, 0, exp, 64 );
  1978. rndprc = rndsav;
  1979. nonorm:
  1980. toe64( xi, e );
  1981. }
  1982. /* move out internal format to ieee long double */
  1983. static void toe64( a, b )
  1984. unsigned short *a, *b;
  1985. {
  1986. register unsigned short *p, *q;
  1987. unsigned short i;
  1988. #ifdef NANS
  1989. if( eiisnan(a) )
  1990. {
  1991. enan( b, 64 );
  1992. return;
  1993. }
  1994. #endif
  1995. #ifdef IBMPC
  1996. /* Shift Intel denormal significand down 1. */
  1997. if( a[E] == 0 )
  1998. eshdn1(a);
  1999. #endif
  2000. p = a;
  2001. #ifdef MIEEE
  2002. q = b;
  2003. #else
  2004. q = b + 4; /* point to output exponent */
  2005. #if 1
  2006. /* NOTE: if data type is 96 bits wide, clear the last word here. */
  2007. *(q+1)= 0;
  2008. #endif
  2009. #endif
  2010. /* combine sign and exponent */
  2011. i = *p++;
  2012. #ifdef MIEEE
  2013. if( i )
  2014. *q++ = *p++ | 0x8000;
  2015. else
  2016. *q++ = *p++;
  2017. *q++ = 0;
  2018. #else
  2019. if( i )
  2020. *q-- = *p++ | 0x8000;
  2021. else
  2022. *q-- = *p++;
  2023. #endif
  2024. /* skip over guard word */
  2025. ++p;
  2026. /* move the significand */
  2027. #ifdef MIEEE
  2028. for( i=0; i<4; i++ )
  2029. *q++ = *p++;
  2030. #else
  2031. #ifdef INFINITY
  2032. if (eiisinf (a))
  2033. {
  2034. /* Intel long double infinity. */
  2035. *q-- = 0x8000;
  2036. *q-- = 0;
  2037. *q-- = 0;
  2038. *q = 0;
  2039. return;
  2040. }
  2041. #endif
  2042. for( i=0; i<4; i++ )
  2043. *q-- = *p++;
  2044. #endif
  2045. }
  2046. /*
  2047. ; e type to IEEE double precision
  2048. ; double d;
  2049. ; unsigned short x[NE];
  2050. ; etoe53( x, &d );
  2051. */
  2052. #ifdef DEC
  2053. void etoe53( x, e )
  2054. unsigned short *x, *e;
  2055. {
  2056. etodec( x, e ); /* see etodec.c */
  2057. }
  2058. static void toe53( x, y )
  2059. unsigned short *x, *y;
  2060. {
  2061. todec( x, y );
  2062. }
  2063. #else
  2064. void etoe53( x, e )
  2065. unsigned short *x, *e;
  2066. {
  2067. unsigned short xi[NI];
  2068. long exp;
  2069. int rndsav;
  2070. #ifdef NANS
  2071. if( eisnan(x) )
  2072. {
  2073. enan( e, 53 );
  2074. return;
  2075. }
  2076. #endif
  2077. emovi( x, xi );
  2078. exp = (long )xi[E] - (EXONE - 0x3ff); /* adjust exponent for offsets */
  2079. #ifdef INFINITY
  2080. if( eisinf(x) )
  2081. goto nonorm;
  2082. #endif
  2083. /* round off to nearest or even */
  2084. rndsav = rndprc;
  2085. rndprc = 53;
  2086. emdnorm( xi, 0, 0, exp, 64 );
  2087. rndprc = rndsav;
  2088. nonorm:
  2089. toe53( xi, e );
  2090. }
  2091. static void toe53( x, y )
  2092. unsigned short *x, *y;
  2093. {
  2094. unsigned short i;
  2095. unsigned short *p;
  2096. #ifdef NANS
  2097. if( eiisnan(x) )
  2098. {
  2099. enan( y, 53 );
  2100. return;
  2101. }
  2102. #endif
  2103. p = &x[0];
  2104. #ifdef IBMPC
  2105. y += 3;
  2106. #endif
  2107. *y = 0; /* output high order */
  2108. if( *p++ )
  2109. *y = 0x8000; /* output sign bit */
  2110. i = *p++;
  2111. if( i >= (unsigned int )2047 )
  2112. { /* Saturate at largest number less than infinity. */
  2113. #ifdef INFINITY
  2114. *y |= 0x7ff0;
  2115. #ifdef IBMPC
  2116. *(--y) = 0;
  2117. *(--y) = 0;
  2118. *(--y) = 0;
  2119. #endif
  2120. #ifdef MIEEE
  2121. ++y;
  2122. *y++ = 0;
  2123. *y++ = 0;
  2124. *y++ = 0;
  2125. #endif
  2126. #else
  2127. *y |= (unsigned short )0x7fef;
  2128. #ifdef IBMPC
  2129. *(--y) = 0xffff;
  2130. *(--y) = 0xffff;
  2131. *(--y) = 0xffff;
  2132. #endif
  2133. #ifdef MIEEE
  2134. ++y;
  2135. *y++ = 0xffff;
  2136. *y++ = 0xffff;
  2137. *y++ = 0xffff;
  2138. #endif
  2139. #endif
  2140. return;
  2141. }
  2142. if( i == 0 )
  2143. {
  2144. (void )eshift( x, 4 );
  2145. }
  2146. else
  2147. {
  2148. i <<= 4;
  2149. (void )eshift( x, 5 );
  2150. }
  2151. i |= *p++ & (unsigned short )0x0f; /* *p = xi[M] */
  2152. *y |= (unsigned short )i; /* high order output already has sign bit set */
  2153. #ifdef IBMPC
  2154. *(--y) = *p++;
  2155. *(--y) = *p++;
  2156. *(--y) = *p;
  2157. #endif
  2158. #ifdef MIEEE
  2159. ++y;
  2160. *y++ = *p++;
  2161. *y++ = *p++;
  2162. *y++ = *p++;
  2163. #endif
  2164. }
  2165. #endif /* not DEC */
  2166. /*
  2167. ; e type to IEEE single precision
  2168. ; float d;
  2169. ; unsigned short x[N+2];
  2170. ; xtod( x, &d );
  2171. */
  2172. void etoe24( x, e )
  2173. unsigned short *x, *e;
  2174. {
  2175. long exp;
  2176. unsigned short xi[NI];
  2177. int rndsav;
  2178. #ifdef NANS
  2179. if( eisnan(x) )
  2180. {
  2181. enan( e, 24 );
  2182. return;
  2183. }
  2184. #endif
  2185. emovi( x, xi );
  2186. exp = (long )xi[E] - (EXONE - 0177); /* adjust exponent for offsets */
  2187. #ifdef INFINITY
  2188. if( eisinf(x) )
  2189. goto nonorm;
  2190. #endif
  2191. /* round off to nearest or even */
  2192. rndsav = rndprc;
  2193. rndprc = 24;
  2194. emdnorm( xi, 0, 0, exp, 64 );
  2195. rndprc = rndsav;
  2196. nonorm:
  2197. toe24( xi, e );
  2198. }
  2199. static void toe24( x, y )
  2200. unsigned short *x, *y;
  2201. {
  2202. unsigned short i;
  2203. unsigned short *p;
  2204. #ifdef NANS
  2205. if( eiisnan(x) )
  2206. {
  2207. enan( y, 24 );
  2208. return;
  2209. }
  2210. #endif
  2211. p = &x[0];
  2212. #ifdef IBMPC
  2213. y += 1;
  2214. #endif
  2215. #ifdef DEC
  2216. y += 1;
  2217. #endif
  2218. *y = 0; /* output high order */
  2219. if( *p++ )
  2220. *y = 0x8000; /* output sign bit */
  2221. i = *p++;
  2222. if( i >= 255 )
  2223. { /* Saturate at largest number less than infinity. */
  2224. #ifdef INFINITY
  2225. *y |= (unsigned short )0x7f80;
  2226. #ifdef IBMPC
  2227. *(--y) = 0;
  2228. #endif
  2229. #ifdef DEC
  2230. *(--y) = 0;
  2231. #endif
  2232. #ifdef MIEEE
  2233. ++y;
  2234. *y = 0;
  2235. #endif
  2236. #else
  2237. *y |= (unsigned short )0x7f7f;
  2238. #ifdef IBMPC
  2239. *(--y) = 0xffff;
  2240. #endif
  2241. #ifdef DEC
  2242. *(--y) = 0xffff;
  2243. #endif
  2244. #ifdef MIEEE
  2245. ++y;
  2246. *y = 0xffff;
  2247. #endif
  2248. #endif
  2249. return;
  2250. }
  2251. if( i == 0 )
  2252. {
  2253. (void )eshift( x, 7 );
  2254. }
  2255. else
  2256. {
  2257. i <<= 7;
  2258. (void )eshift( x, 8 );
  2259. }
  2260. i |= *p++ & (unsigned short )0x7f; /* *p = xi[M] */
  2261. *y |= i; /* high order output already has sign bit set */
  2262. #ifdef IBMPC
  2263. *(--y) = *p;
  2264. #endif
  2265. #ifdef DEC
  2266. *(--y) = *p;
  2267. #endif
  2268. #ifdef MIEEE
  2269. ++y;
  2270. *y = *p;
  2271. #endif
  2272. }
  2273. /* Compare two e type numbers.
  2274. *
  2275. * unsigned short a[NE], b[NE];
  2276. * ecmp( a, b );
  2277. *
  2278. * returns +1 if a > b
  2279. * 0 if a == b
  2280. * -1 if a < b
  2281. * -2 if either a or b is a NaN.
  2282. */
  2283. int ecmp( a, b )
  2284. unsigned short *a, *b;
  2285. {
  2286. unsigned short ai[NI], bi[NI];
  2287. register unsigned short *p, *q;
  2288. register int i;
  2289. int msign;
  2290. #ifdef NANS
  2291. if (eisnan (a) || eisnan (b))
  2292. return( -2 );
  2293. #endif
  2294. emovi( a, ai );
  2295. p = ai;
  2296. emovi( b, bi );
  2297. q = bi;
  2298. if( *p != *q )
  2299. { /* the signs are different */
  2300. /* -0 equals + 0 */
  2301. for( i=1; i<NI-1; i++ )
  2302. {
  2303. if( ai[i] != 0 )
  2304. goto nzro;
  2305. if( bi[i] != 0 )
  2306. goto nzro;
  2307. }
  2308. return(0);
  2309. nzro:
  2310. if( *p == 0 )
  2311. return( 1 );
  2312. else
  2313. return( -1 );
  2314. }
  2315. /* both are the same sign */
  2316. if( *p == 0 )
  2317. msign = 1;
  2318. else
  2319. msign = -1;
  2320. i = NI-1;
  2321. do
  2322. {
  2323. if( *p++ != *q++ )
  2324. {
  2325. goto diff;
  2326. }
  2327. }
  2328. while( --i > 0 );
  2329. return(0); /* equality */
  2330. diff:
  2331. if( *(--p) > *(--q) )
  2332. return( msign ); /* p is bigger */
  2333. else
  2334. return( -msign ); /* p is littler */
  2335. }
  2336. /* Find nearest integer to x = floor( x + 0.5 )
  2337. *
  2338. * unsigned short x[NE], y[NE]
  2339. * eround( x, y );
  2340. */
  2341. void eround( x, y )
  2342. unsigned short *x, *y;
  2343. {
  2344. eadd( ehalf, x, y );
  2345. efloor( y, y );
  2346. }
  2347. /*
  2348. ; convert long (32-bit) integer to e type
  2349. ;
  2350. ; long l;
  2351. ; unsigned short x[NE];
  2352. ; ltoe( &l, x );
  2353. ; note &l is the memory address of l
  2354. */
  2355. void ltoe( lp, y )
  2356. long *lp; /* lp is the memory address of a long integer */
  2357. unsigned short *y; /* y is the address of a short */
  2358. {
  2359. unsigned short yi[NI];
  2360. unsigned long ll;
  2361. int k;
  2362. ecleaz( yi );
  2363. if( *lp < 0 )
  2364. {
  2365. ll = (unsigned long )( -(*lp) ); /* make it positive */
  2366. yi[0] = 0xffff; /* put correct sign in the e type number */
  2367. }
  2368. else
  2369. {
  2370. ll = (unsigned long )( *lp );
  2371. }
  2372. /* move the long integer to yi significand area */
  2373. if( sizeof(long) == 8 )
  2374. {
  2375. yi[M] = (unsigned short) (ll >> (LONGBITS - 16));
  2376. yi[M + 1] = (unsigned short) (ll >> (LONGBITS - 32));
  2377. yi[M + 2] = (unsigned short) (ll >> 16);
  2378. yi[M + 3] = (unsigned short) ll;
  2379. yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
  2380. }
  2381. else
  2382. {
  2383. yi[M] = (unsigned short )(ll >> 16);
  2384. yi[M+1] = (unsigned short )ll;
  2385. yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */
  2386. }
  2387. if( (k = enormlz( yi )) > NBITS ) /* normalize the significand */
  2388. ecleaz( yi ); /* it was zero */
  2389. else
  2390. yi[E] -= (unsigned short )k; /* subtract shift count from exponent */
  2391. emovo( yi, y ); /* output the answer */
  2392. }
  2393. /*
  2394. ; convert unsigned long (32-bit) integer to e type
  2395. ;
  2396. ; unsigned long l;
  2397. ; unsigned short x[NE];
  2398. ; ltox( &l, x );
  2399. ; note &l is the memory address of l
  2400. */
  2401. void ultoe( lp, y )
  2402. unsigned long *lp; /* lp is the memory address of a long integer */
  2403. unsigned short *y; /* y is the address of a short */
  2404. {
  2405. unsigned short yi[NI];
  2406. unsigned long ll;
  2407. int k;
  2408. ecleaz( yi );
  2409. ll = *lp;
  2410. /* move the long integer to ayi significand area */
  2411. if( sizeof(long) == 8 )
  2412. {
  2413. yi[M] = (unsigned short) (ll >> (LONGBITS - 16));
  2414. yi[M + 1] = (unsigned short) (ll >> (LONGBITS - 32));
  2415. yi[M + 2] = (unsigned short) (ll >> 16);
  2416. yi[M + 3] = (unsigned short) ll;
  2417. yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
  2418. }
  2419. else
  2420. {
  2421. yi[M] = (unsigned short )(ll >> 16);
  2422. yi[M+1] = (unsigned short )ll;
  2423. yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */
  2424. }
  2425. if( (k = enormlz( yi )) > NBITS ) /* normalize the significand */
  2426. ecleaz( yi ); /* it was zero */
  2427. else
  2428. yi[E] -= (unsigned short )k; /* subtract shift count from exponent */
  2429. emovo( yi, y ); /* output the answer */
  2430. }
  2431. /*
  2432. ; Find long integer and fractional parts
  2433. ; long i;
  2434. ; unsigned short x[NE], frac[NE];
  2435. ; xifrac( x, &i, frac );
  2436. The integer output has the sign of the input. The fraction is
  2437. the positive fractional part of abs(x).
  2438. */
  2439. void eifrac( x, i, frac )
  2440. unsigned short *x;
  2441. long *i;
  2442. unsigned short *frac;
  2443. {
  2444. unsigned short xi[NI];
  2445. int j, k;
  2446. unsigned long ll;
  2447. emovi( x, xi );
  2448. k = (int )xi[E] - (EXONE - 1);
  2449. if( k <= 0 )
  2450. {
  2451. /* if exponent <= 0, integer = 0 and real output is fraction */
  2452. *i = 0L;
  2453. emovo( xi, frac );
  2454. return;
  2455. }
  2456. if( k > (8 * sizeof(long) - 1) )
  2457. {
  2458. /*
  2459. ; long integer overflow: output large integer
  2460. ; and correct fraction
  2461. */
  2462. j = 8 * sizeof(long) - 1;
  2463. if( xi[0] )
  2464. *i = (long) ((unsigned long) 1) << j;
  2465. else
  2466. *i = (long) (((unsigned long) (~(0L))) >> 1);
  2467. (void )eshift( xi, k );
  2468. }
  2469. if( k > 16 )
  2470. {
  2471. /*
  2472. Shift more than 16 bits: shift up k-16 mod 16
  2473. then shift by 16's.
  2474. */
  2475. j = k - ((k >> 4) << 4);
  2476. eshift (xi, j);
  2477. ll = xi[M];
  2478. k -= j;
  2479. do
  2480. {
  2481. eshup6 (xi);
  2482. ll = (ll << 16) | xi[M];
  2483. }
  2484. while ((k -= 16) > 0);
  2485. *i = ll;
  2486. if (xi[0])
  2487. *i = -(*i);
  2488. }
  2489. else
  2490. {
  2491. /* shift not more than 16 bits */
  2492. eshift( xi, k );
  2493. *i = (long )xi[M] & 0xffff;
  2494. if( xi[0] )
  2495. *i = -(*i);
  2496. }
  2497. xi[0] = 0;
  2498. xi[E] = EXONE - 1;
  2499. xi[M] = 0;
  2500. if( (k = enormlz( xi )) > NBITS )
  2501. ecleaz( xi );
  2502. else
  2503. xi[E] -= (unsigned short )k;
  2504. emovo( xi, frac );
  2505. }
  2506. /*
  2507. ; Find unsigned long integer and fractional parts
  2508. ; unsigned long i;
  2509. ; unsigned short x[NE], frac[NE];
  2510. ; xifrac( x, &i, frac );
  2511. A negative e type input yields integer output = 0
  2512. but correct fraction.
  2513. */
  2514. void euifrac( x, i, frac )
  2515. unsigned short *x;
  2516. unsigned long *i;
  2517. unsigned short *frac;
  2518. {
  2519. unsigned short xi[NI];
  2520. int j, k;
  2521. unsigned long ll;
  2522. emovi( x, xi );
  2523. k = (int )xi[E] - (EXONE - 1);
  2524. if( k <= 0 )
  2525. {
  2526. /* if exponent <= 0, integer = 0 and argument is fraction */
  2527. *i = 0L;
  2528. emovo( xi, frac );
  2529. return;
  2530. }
  2531. if( k > (8 * sizeof(long)) )
  2532. {
  2533. /*
  2534. ; long integer overflow: output large integer
  2535. ; and correct fraction
  2536. */
  2537. *i = ~(0L);
  2538. (void )eshift( xi, k );
  2539. }
  2540. else if( k > 16 )
  2541. {
  2542. /*
  2543. Shift more than 16 bits: shift up k-16 mod 16
  2544. then shift up by 16's.
  2545. */
  2546. j = k - ((k >> 4) << 4);
  2547. eshift (xi, j);
  2548. ll = xi[M];
  2549. k -= j;
  2550. do
  2551. {
  2552. eshup6 (xi);
  2553. ll = (ll << 16) | xi[M];
  2554. }
  2555. while ((k -= 16) > 0);
  2556. *i = ll;
  2557. }
  2558. else
  2559. {
  2560. /* shift not more than 16 bits */
  2561. eshift( xi, k );
  2562. *i = (long )xi[M] & 0xffff;
  2563. }
  2564. if( xi[0] ) /* A negative value yields unsigned integer 0. */
  2565. *i = 0L;
  2566. xi[0] = 0;
  2567. xi[E] = EXONE - 1;
  2568. xi[M] = 0;
  2569. if( (k = enormlz( xi )) > NBITS )
  2570. ecleaz( xi );
  2571. else
  2572. xi[E] -= (unsigned short )k;
  2573. emovo( xi, frac );
  2574. }
  2575. /*
  2576. ; Shift significand
  2577. ;
  2578. ; Shifts significand area up or down by the number of bits
  2579. ; given by the variable sc.
  2580. */
  2581. int eshift( x, sc )
  2582. unsigned short *x;
  2583. int sc;
  2584. {
  2585. unsigned short lost;
  2586. unsigned short *p;
  2587. if( sc == 0 )
  2588. return( 0 );
  2589. lost = 0;
  2590. p = x + NI-1;
  2591. if( sc < 0 )
  2592. {
  2593. sc = -sc;
  2594. while( sc >= 16 )
  2595. {
  2596. lost |= *p; /* remember lost bits */
  2597. eshdn6(x);
  2598. sc -= 16;
  2599. }
  2600. while( sc >= 8 )
  2601. {
  2602. lost |= *p & 0xff;
  2603. eshdn8(x);
  2604. sc -= 8;
  2605. }
  2606. while( sc > 0 )
  2607. {
  2608. lost |= *p & 1;
  2609. eshdn1(x);
  2610. sc -= 1;
  2611. }
  2612. }
  2613. else
  2614. {
  2615. while( sc >= 16 )
  2616. {
  2617. eshup6(x);
  2618. sc -= 16;
  2619. }
  2620. while( sc >= 8 )
  2621. {
  2622. eshup8(x);
  2623. sc -= 8;
  2624. }
  2625. while( sc > 0 )
  2626. {
  2627. eshup1(x);
  2628. sc -= 1;
  2629. }
  2630. }
  2631. if( lost )
  2632. lost = 1;
  2633. return( (int )lost );
  2634. }
  2635. /*
  2636. ; normalize
  2637. ;
  2638. ; Shift normalizes the significand area pointed to by argument
  2639. ; shift count (up = positive) is returned.
  2640. */
  2641. int enormlz(x)
  2642. unsigned short x[];
  2643. {
  2644. register unsigned short *p;
  2645. int sc;
  2646. sc = 0;
  2647. p = &x[M];
  2648. if( *p != 0 )
  2649. goto normdn;
  2650. ++p;
  2651. if( *p & 0x8000 )
  2652. return( 0 ); /* already normalized */
  2653. while( *p == 0 )
  2654. {
  2655. eshup6(x);
  2656. sc += 16;
  2657. /* With guard word, there are NBITS+16 bits available.
  2658. * return true if all are zero.
  2659. */
  2660. if( sc > NBITS )
  2661. return( sc );
  2662. }
  2663. /* see if high byte is zero */
  2664. while( (*p & 0xff00) == 0 )
  2665. {
  2666. eshup8(x);
  2667. sc += 8;
  2668. }
  2669. /* now shift 1 bit at a time */
  2670. while( (*p & 0x8000) == 0)
  2671. {
  2672. eshup1(x);
  2673. sc += 1;
  2674. if( sc > (NBITS+16) )
  2675. {
  2676. mtherr( "enormlz", UNDERFLOW );
  2677. return( sc );
  2678. }
  2679. }
  2680. return( sc );
  2681. /* Normalize by shifting down out of the high guard word
  2682. of the significand */
  2683. normdn:
  2684. if( *p & 0xff00 )
  2685. {
  2686. eshdn8(x);
  2687. sc -= 8;
  2688. }
  2689. while( *p != 0 )
  2690. {
  2691. eshdn1(x);
  2692. sc -= 1;
  2693. if( sc < -NBITS )
  2694. {
  2695. mtherr( "enormlz", OVERFLOW );
  2696. return( sc );
  2697. }
  2698. }
  2699. return( sc );
  2700. }
  2701. /* Convert e type number to decimal format ASCII string.
  2702. * The constants are for 64 bit precision.
  2703. */
  2704. #define NTEN 12
  2705. #define MAXP 4096
  2706. #if NE == 10
  2707. static unsigned short etens[NTEN + 1][NE] =
  2708. {
  2709. {0x6576, 0x4a92, 0x804a, 0x153f,
  2710. 0xc94c, 0x979a, 0x8a20, 0x5202, 0xc460, 0x7525,}, /* 10**4096 */
  2711. {0x6a32, 0xce52, 0x329a, 0x28ce,
  2712. 0xa74d, 0x5de4, 0xc53d, 0x3b5d, 0x9e8b, 0x5a92,}, /* 10**2048 */
  2713. {0x526c, 0x50ce, 0xf18b, 0x3d28,
  2714. 0x650d, 0x0c17, 0x8175, 0x7586, 0xc976, 0x4d48,},
  2715. {0x9c66, 0x58f8, 0xbc50, 0x5c54,
  2716. 0xcc65, 0x91c6, 0xa60e, 0xa0ae, 0xe319, 0x46a3,},
  2717. {0x851e, 0xeab7, 0x98fe, 0x901b,
  2718. 0xddbb, 0xde8d, 0x9df9, 0xebfb, 0xaa7e, 0x4351,},
  2719. {0x0235, 0x0137, 0x36b1, 0x336c,
  2720. 0xc66f, 0x8cdf, 0x80e9, 0x47c9, 0x93ba, 0x41a8,},
  2721. {0x50f8, 0x25fb, 0xc76b, 0x6b71,
  2722. 0x3cbf, 0xa6d5, 0xffcf, 0x1f49, 0xc278, 0x40d3,},
  2723. {0x0000, 0x0000, 0x0000, 0x0000,
  2724. 0xf020, 0xb59d, 0x2b70, 0xada8, 0x9dc5, 0x4069,},
  2725. {0x0000, 0x0000, 0x0000, 0x0000,
  2726. 0x0000, 0x0000, 0x0400, 0xc9bf, 0x8e1b, 0x4034,},
  2727. {0x0000, 0x0000, 0x0000, 0x0000,
  2728. 0x0000, 0x0000, 0x0000, 0x2000, 0xbebc, 0x4019,},
  2729. {0x0000, 0x0000, 0x0000, 0x0000,
  2730. 0x0000, 0x0000, 0x0000, 0x0000, 0x9c40, 0x400c,},
  2731. {0x0000, 0x0000, 0x0000, 0x0000,
  2732. 0x0000, 0x0000, 0x0000, 0x0000, 0xc800, 0x4005,},
  2733. {0x0000, 0x0000, 0x0000, 0x0000,
  2734. 0x0000, 0x0000, 0x0000, 0x0000, 0xa000, 0x4002,}, /* 10**1 */
  2735. };
  2736. static unsigned short emtens[NTEN + 1][NE] =
  2737. {
  2738. {0x2030, 0xcffc, 0xa1c3, 0x8123,
  2739. 0x2de3, 0x9fde, 0xd2ce, 0x04c8, 0xa6dd, 0x0ad8,}, /* 10**-4096 */
  2740. {0x8264, 0xd2cb, 0xf2ea, 0x12d4,
  2741. 0x4925, 0x2de4, 0x3436, 0x534f, 0xceae, 0x256b,}, /* 10**-2048 */
  2742. {0xf53f, 0xf698, 0x6bd3, 0x0158,
  2743. 0x87a6, 0xc0bd, 0xda57, 0x82a5, 0xa2a6, 0x32b5,},
  2744. {0xe731, 0x04d4, 0xe3f2, 0xd332,
  2745. 0x7132, 0xd21c, 0xdb23, 0xee32, 0x9049, 0x395a,},
  2746. {0xa23e, 0x5308, 0xfefb, 0x1155,
  2747. 0xfa91, 0x1939, 0x637a, 0x4325, 0xc031, 0x3cac,},
  2748. {0xe26d, 0xdbde, 0xd05d, 0xb3f6,
  2749. 0xac7c, 0xe4a0, 0x64bc, 0x467c, 0xddd0, 0x3e55,},
  2750. {0x2a20, 0x6224, 0x47b3, 0x98d7,
  2751. 0x3f23, 0xe9a5, 0xa539, 0xea27, 0xa87f, 0x3f2a,},
  2752. {0x0b5b, 0x4af2, 0xa581, 0x18ed,
  2753. 0x67de, 0x94ba, 0x4539, 0x1ead, 0xcfb1, 0x3f94,},
  2754. {0xbf71, 0xa9b3, 0x7989, 0xbe68,
  2755. 0x4c2e, 0xe15b, 0xc44d, 0x94be, 0xe695, 0x3fc9,},
  2756. {0x3d4d, 0x7c3d, 0x36ba, 0x0d2b,
  2757. 0xfdc2, 0xcefc, 0x8461, 0x7711, 0xabcc, 0x3fe4,},
  2758. {0xc155, 0xa4a8, 0x404e, 0x6113,
  2759. 0xd3c3, 0x652b, 0xe219, 0x1758, 0xd1b7, 0x3ff1,},
  2760. {0xd70a, 0x70a3, 0x0a3d, 0xa3d7,
  2761. 0x3d70, 0xd70a, 0x70a3, 0x0a3d, 0xa3d7, 0x3ff8,},
  2762. {0xcccd, 0xcccc, 0xcccc, 0xcccc,
  2763. 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0x3ffb,}, /* 10**-1 */
  2764. };
  2765. #else
  2766. static unsigned short etens[NTEN+1][NE] = {
  2767. {0xc94c,0x979a,0x8a20,0x5202,0xc460,0x7525,},/* 10**4096 */
  2768. {0xa74d,0x5de4,0xc53d,0x3b5d,0x9e8b,0x5a92,},/* 10**2048 */
  2769. {0x650d,0x0c17,0x8175,0x7586,0xc976,0x4d48,},
  2770. {0xcc65,0x91c6,0xa60e,0xa0ae,0xe319,0x46a3,},
  2771. {0xddbc,0xde8d,0x9df9,0xebfb,0xaa7e,0x4351,},
  2772. {0xc66f,0x8cdf,0x80e9,0x47c9,0x93ba,0x41a8,},
  2773. {0x3cbf,0xa6d5,0xffcf,0x1f49,0xc278,0x40d3,},
  2774. {0xf020,0xb59d,0x2b70,0xada8,0x9dc5,0x4069,},
  2775. {0x0000,0x0000,0x0400,0xc9bf,0x8e1b,0x4034,},
  2776. {0x0000,0x0000,0x0000,0x2000,0xbebc,0x4019,},
  2777. {0x0000,0x0000,0x0000,0x0000,0x9c40,0x400c,},
  2778. {0x0000,0x0000,0x0000,0x0000,0xc800,0x4005,},
  2779. {0x0000,0x0000,0x0000,0x0000,0xa000,0x4002,}, /* 10**1 */
  2780. };
  2781. static unsigned short emtens[NTEN+1][NE] = {
  2782. {0x2de4,0x9fde,0xd2ce,0x04c8,0xa6dd,0x0ad8,}, /* 10**-4096 */
  2783. {0x4925,0x2de4,0x3436,0x534f,0xceae,0x256b,}, /* 10**-2048 */
  2784. {0x87a6,0xc0bd,0xda57,0x82a5,0xa2a6,0x32b5,},
  2785. {0x7133,0xd21c,0xdb23,0xee32,0x9049,0x395a,},
  2786. {0xfa91,0x1939,0x637a,0x4325,0xc031,0x3cac,},
  2787. {0xac7d,0xe4a0,0x64bc,0x467c,0xddd0,0x3e55,},
  2788. {0x3f24,0xe9a5,0xa539,0xea27,0xa87f,0x3f2a,},
  2789. {0x67de,0x94ba,0x4539,0x1ead,0xcfb1,0x3f94,},
  2790. {0x4c2f,0xe15b,0xc44d,0x94be,0xe695,0x3fc9,},
  2791. {0xfdc2,0xcefc,0x8461,0x7711,0xabcc,0x3fe4,},
  2792. {0xd3c3,0x652b,0xe219,0x1758,0xd1b7,0x3ff1,},
  2793. {0x3d71,0xd70a,0x70a3,0x0a3d,0xa3d7,0x3ff8,},
  2794. {0xcccd,0xcccc,0xcccc,0xcccc,0xcccc,0x3ffb,}, /* 10**-1 */
  2795. };
  2796. #endif
  2797. void e24toasc( x, string, ndigs )
  2798. unsigned short x[];
  2799. char *string;
  2800. int ndigs;
  2801. {
  2802. unsigned short w[NI];
  2803. e24toe( x, w );
  2804. etoasc( w, string, ndigs );
  2805. }
  2806. void e53toasc( x, string, ndigs )
  2807. unsigned short x[];
  2808. char *string;
  2809. int ndigs;
  2810. {
  2811. unsigned short w[NI];
  2812. e53toe( x, w );
  2813. etoasc( w, string, ndigs );
  2814. }
  2815. void e64toasc( x, string, ndigs )
  2816. unsigned short x[];
  2817. char *string;
  2818. int ndigs;
  2819. {
  2820. unsigned short w[NI];
  2821. e64toe( x, w );
  2822. etoasc( w, string, ndigs );
  2823. }
  2824. void e113toasc (x, string, ndigs)
  2825. unsigned short x[];
  2826. char *string;
  2827. int ndigs;
  2828. {
  2829. unsigned short w[NI];
  2830. e113toe (x, w);
  2831. etoasc (w, string, ndigs);
  2832. }
  2833. void etoasc( x, string, ndigs )
  2834. unsigned short x[];
  2835. char *string;
  2836. int ndigs;
  2837. {
  2838. long digit;
  2839. unsigned short y[NI], t[NI], u[NI], w[NI];
  2840. unsigned short *p, *r, *ten;
  2841. unsigned short sign;
  2842. int i, j, k, expon, rndsav;
  2843. char *s, *ss;
  2844. unsigned short m;
  2845. rndsav = rndprc;
  2846. #ifdef NANS
  2847. if( eisnan(x) )
  2848. {
  2849. sprintf( string, " NaN " );
  2850. goto bxit;
  2851. }
  2852. #endif
  2853. rndprc = NBITS; /* set to full precision */
  2854. emov( x, y ); /* retain external format */
  2855. if( y[NE-1] & 0x8000 )
  2856. {
  2857. sign = 0xffff;
  2858. y[NE-1] &= 0x7fff;
  2859. }
  2860. else
  2861. {
  2862. sign = 0;
  2863. }
  2864. expon = 0;
  2865. ten = &etens[NTEN][0];
  2866. emov( eone, t );
  2867. /* Test for zero exponent */
  2868. if( y[NE-1] == 0 )
  2869. {
  2870. for( k=0; k<NE-1; k++ )
  2871. {
  2872. if( y[k] != 0 )
  2873. goto tnzro; /* denormalized number */
  2874. }
  2875. goto isone; /* legal all zeros */
  2876. }
  2877. tnzro:
  2878. /* Test for infinity.
  2879. */
  2880. if( y[NE-1] == 0x7fff )
  2881. {
  2882. if( sign )
  2883. sprintf( string, " -Infinity " );
  2884. else
  2885. sprintf( string, " Infinity " );
  2886. goto bxit;
  2887. }
  2888. /* Test for exponent nonzero but significand denormalized.
  2889. * This is an error condition.
  2890. */
  2891. if( (y[NE-1] != 0) && ((y[NE-2] & 0x8000) == 0) )
  2892. {
  2893. mtherr( "etoasc", DOMAIN );
  2894. sprintf( string, "NaN" );
  2895. goto bxit;
  2896. }
  2897. /* Compare to 1.0 */
  2898. i = ecmp( eone, y );
  2899. if( i == 0 )
  2900. goto isone;
  2901. if( i < 0 )
  2902. { /* Number is greater than 1 */
  2903. /* Convert significand to an integer and strip trailing decimal zeros. */
  2904. emov( y, u );
  2905. u[NE-1] = EXONE + NBITS - 1;
  2906. p = &etens[NTEN-4][0];
  2907. m = 16;
  2908. do
  2909. {
  2910. ediv( p, u, t );
  2911. efloor( t, w );
  2912. for( j=0; j<NE-1; j++ )
  2913. {
  2914. if( t[j] != w[j] )
  2915. goto noint;
  2916. }
  2917. emov( t, u );
  2918. expon += (int )m;
  2919. noint:
  2920. p += NE;
  2921. m >>= 1;
  2922. }
  2923. while( m != 0 );
  2924. /* Rescale from integer significand */
  2925. u[NE-1] += y[NE-1] - (unsigned int )(EXONE + NBITS - 1);
  2926. emov( u, y );
  2927. /* Find power of 10 */
  2928. emov( eone, t );
  2929. m = MAXP;
  2930. p = &etens[0][0];
  2931. while( ecmp( ten, u ) <= 0 )
  2932. {
  2933. if( ecmp( p, u ) <= 0 )
  2934. {
  2935. ediv( p, u, u );
  2936. emul( p, t, t );
  2937. expon += (int )m;
  2938. }
  2939. m >>= 1;
  2940. if( m == 0 )
  2941. break;
  2942. p += NE;
  2943. }
  2944. }
  2945. else
  2946. { /* Number is less than 1.0 */
  2947. /* Pad significand with trailing decimal zeros. */
  2948. if( y[NE-1] == 0 )
  2949. {
  2950. while( (y[NE-2] & 0x8000) == 0 )
  2951. {
  2952. emul( ten, y, y );
  2953. expon -= 1;
  2954. }
  2955. }
  2956. else
  2957. {
  2958. emovi( y, w );
  2959. for( i=0; i<NDEC+1; i++ )
  2960. {
  2961. if( (w[NI-1] & 0x7) != 0 )
  2962. break;
  2963. /* multiply by 10 */
  2964. emovz( w, u );
  2965. eshdn1( u );
  2966. eshdn1( u );
  2967. eaddm( w, u );
  2968. u[1] += 3;
  2969. while( u[2] != 0 )
  2970. {
  2971. eshdn1(u);
  2972. u[1] += 1;
  2973. }
  2974. if( u[NI-1] != 0 )
  2975. break;
  2976. if( eone[NE-1] <= u[1] )
  2977. break;
  2978. emovz( u, w );
  2979. expon -= 1;
  2980. }
  2981. emovo( w, y );
  2982. }
  2983. k = -MAXP;
  2984. p = &emtens[0][0];
  2985. r = &etens[0][0];
  2986. emov( y, w );
  2987. emov( eone, t );
  2988. while( ecmp( eone, w ) > 0 )
  2989. {
  2990. if( ecmp( p, w ) >= 0 )
  2991. {
  2992. emul( r, w, w );
  2993. emul( r, t, t );
  2994. expon += k;
  2995. }
  2996. k /= 2;
  2997. if( k == 0 )
  2998. break;
  2999. p += NE;
  3000. r += NE;
  3001. }
  3002. ediv( t, eone, t );
  3003. }
  3004. isone:
  3005. /* Find the first (leading) digit. */
  3006. emovi( t, w );
  3007. emovz( w, t );
  3008. emovi( y, w );
  3009. emovz( w, y );
  3010. eiremain( t, y );
  3011. digit = equot[NI-1];
  3012. while( (digit == 0) && (ecmp(y,ezero) != 0) )
  3013. {
  3014. eshup1( y );
  3015. emovz( y, u );
  3016. eshup1( u );
  3017. eshup1( u );
  3018. eaddm( u, y );
  3019. eiremain( t, y );
  3020. digit = equot[NI-1];
  3021. expon -= 1;
  3022. }
  3023. s = string;
  3024. if( sign )
  3025. *s++ = '-';
  3026. else
  3027. *s++ = ' ';
  3028. /* Examine number of digits requested by caller. */
  3029. if( ndigs < 0 )
  3030. ndigs = 0;
  3031. if( ndigs > NDEC )
  3032. ndigs = NDEC;
  3033. if( digit == 10 )
  3034. {
  3035. *s++ = '1';
  3036. *s++ = '.';
  3037. if( ndigs > 0 )
  3038. {
  3039. *s++ = '0';
  3040. ndigs -= 1;
  3041. }
  3042. expon += 1;
  3043. }
  3044. else
  3045. {
  3046. *s++ = (char )digit + '0';
  3047. *s++ = '.';
  3048. }
  3049. /* Generate digits after the decimal point. */
  3050. for( k=0; k<=ndigs; k++ )
  3051. {
  3052. /* multiply current number by 10, without normalizing */
  3053. eshup1( y );
  3054. emovz( y, u );
  3055. eshup1( u );
  3056. eshup1( u );
  3057. eaddm( u, y );
  3058. eiremain( t, y );
  3059. *s++ = (char )equot[NI-1] + '0';
  3060. }
  3061. digit = equot[NI-1];
  3062. --s;
  3063. ss = s;
  3064. /* round off the ASCII string */
  3065. if( digit > 4 )
  3066. {
  3067. /* Test for critical rounding case in ASCII output. */
  3068. if( digit == 5 )
  3069. {
  3070. emovo( y, t );
  3071. if( ecmp(t,ezero) != 0 )
  3072. goto roun; /* round to nearest */
  3073. if( (*(s-1) & 1) == 0 )
  3074. goto doexp; /* round to even */
  3075. }
  3076. /* Round up and propagate carry-outs */
  3077. roun:
  3078. --s;
  3079. k = *s & 0x7f;
  3080. /* Carry out to most significant digit? */
  3081. if( k == '.' )
  3082. {
  3083. --s;
  3084. k = *s;
  3085. k += 1;
  3086. *s = (char )k;
  3087. /* Most significant digit carries to 10? */
  3088. if( k > '9' )
  3089. {
  3090. expon += 1;
  3091. *s = '1';
  3092. }
  3093. goto doexp;
  3094. }
  3095. /* Round up and carry out from less significant digits */
  3096. k += 1;
  3097. *s = (char )k;
  3098. if( k > '9' )
  3099. {
  3100. *s = '0';
  3101. goto roun;
  3102. }
  3103. }
  3104. doexp:
  3105. /*
  3106. if( expon >= 0 )
  3107. sprintf( ss, "e+%d", expon );
  3108. else
  3109. sprintf( ss, "e%d", expon );
  3110. */
  3111. sprintf( ss, "E%d", expon );
  3112. bxit:
  3113. rndprc = rndsav;
  3114. }
  3115. /*
  3116. ; ASCTOQ
  3117. ; ASCTOQ.MAC LATEST REV: 11 JAN 84
  3118. ; SLM, 3 JAN 78
  3119. ;
  3120. ; Convert ASCII string to quadruple precision floating point
  3121. ;
  3122. ; Numeric input is free field decimal number
  3123. ; with max of 15 digits with or without
  3124. ; decimal point entered as ASCII from teletype.
  3125. ; Entering E after the number followed by a second
  3126. ; number causes the second number to be interpreted
  3127. ; as a power of 10 to be multiplied by the first number
  3128. ; (i.e., "scientific" notation).
  3129. ;
  3130. ; Usage:
  3131. ; asctoq( string, q );
  3132. */
  3133. /* ASCII to single */
  3134. void asctoe24( s, y )
  3135. char *s;
  3136. unsigned short *y;
  3137. {
  3138. asctoeg( s, y, 24 );
  3139. }
  3140. /* ASCII to double */
  3141. void asctoe53( s, y )
  3142. char *s;
  3143. unsigned short *y;
  3144. {
  3145. #ifdef DEC
  3146. asctoeg( s, y, 56 );
  3147. #else
  3148. asctoeg( s, y, 53 );
  3149. #endif
  3150. }
  3151. /* ASCII to long double */
  3152. void asctoe64( s, y )
  3153. char *s;
  3154. unsigned short *y;
  3155. {
  3156. asctoeg( s, y, 64 );
  3157. }
  3158. /* ASCII to 128-bit long double */
  3159. void asctoe113 (s, y)
  3160. char *s;
  3161. unsigned short *y;
  3162. {
  3163. asctoeg( s, y, 113 );
  3164. }
  3165. /* ASCII to super double */
  3166. void asctoe( s, y )
  3167. char *s;
  3168. unsigned short *y;
  3169. {
  3170. asctoeg( s, y, NBITS );
  3171. }
  3172. /* Space to make a copy of the input string: */
  3173. static char lstr[82] = {0};
  3174. void asctoeg( ss, y, oprec )
  3175. char *ss;
  3176. unsigned short *y;
  3177. int oprec;
  3178. {
  3179. unsigned short yy[NI], xt[NI], tt[NI];
  3180. int esign, decflg, sgnflg, nexp, exp, prec, lost;
  3181. int k, trail, c, rndsav;
  3182. long lexp;
  3183. unsigned short nsign, *p;
  3184. char *sp, *s;
  3185. /* Copy the input string. */
  3186. s = ss;
  3187. while( *s == ' ' ) /* skip leading spaces */
  3188. ++s;
  3189. sp = lstr;
  3190. for( k=0; k<79; k++ )
  3191. {
  3192. if( (*sp++ = *s++) == '\0' )
  3193. break;
  3194. }
  3195. *sp = '\0';
  3196. s = lstr;
  3197. rndsav = rndprc;
  3198. rndprc = NBITS; /* Set to full precision */
  3199. lost = 0;
  3200. nsign = 0;
  3201. decflg = 0;
  3202. sgnflg = 0;
  3203. nexp = 0;
  3204. exp = 0;
  3205. prec = 0;
  3206. ecleaz( yy );
  3207. trail = 0;
  3208. nxtcom:
  3209. k = *s - '0';
  3210. if( (k >= 0) && (k <= 9) )
  3211. {
  3212. /* Ignore leading zeros */
  3213. if( (prec == 0) && (decflg == 0) && (k == 0) )
  3214. goto donchr;
  3215. /* Identify and strip trailing zeros after the decimal point. */
  3216. if( (trail == 0) && (decflg != 0) )
  3217. {
  3218. sp = s;
  3219. while( (*sp >= '0') && (*sp <= '9') )
  3220. ++sp;
  3221. /* Check for syntax error */
  3222. c = *sp & 0x7f;
  3223. if( (c != 'e') && (c != 'E') && (c != '\0')
  3224. && (c != '\n') && (c != '\r') && (c != ' ')
  3225. && (c != ',') )
  3226. goto error;
  3227. --sp;
  3228. while( *sp == '0' )
  3229. *sp-- = 'z';
  3230. trail = 1;
  3231. if( *s == 'z' )
  3232. goto donchr;
  3233. }
  3234. /* If enough digits were given to more than fill up the yy register,
  3235. * continuing until overflow into the high guard word yy[2]
  3236. * guarantees that there will be a roundoff bit at the top
  3237. * of the low guard word after normalization.
  3238. */
  3239. if( yy[2] == 0 )
  3240. {
  3241. if( decflg )
  3242. nexp += 1; /* count digits after decimal point */
  3243. eshup1( yy ); /* multiply current number by 10 */
  3244. emovz( yy, xt );
  3245. eshup1( xt );
  3246. eshup1( xt );
  3247. eaddm( xt, yy );
  3248. ecleaz( xt );
  3249. xt[NI-2] = (unsigned short )k;
  3250. eaddm( xt, yy );
  3251. }
  3252. else
  3253. {
  3254. /* Mark any lost non-zero digit. */
  3255. lost |= k;
  3256. /* Count lost digits before the decimal point. */
  3257. if (decflg == 0)
  3258. nexp -= 1;
  3259. }
  3260. prec += 1;
  3261. goto donchr;
  3262. }
  3263. switch( *s )
  3264. {
  3265. case 'z':
  3266. break;
  3267. case 'E':
  3268. case 'e':
  3269. goto expnt;
  3270. case '.': /* decimal point */
  3271. if( decflg )
  3272. goto error;
  3273. ++decflg;
  3274. break;
  3275. case '-':
  3276. nsign = 0xffff;
  3277. if( sgnflg )
  3278. goto error;
  3279. ++sgnflg;
  3280. break;
  3281. case '+':
  3282. if( sgnflg )
  3283. goto error;
  3284. ++sgnflg;
  3285. break;
  3286. case ',':
  3287. case ' ':
  3288. case '\0':
  3289. case '\n':
  3290. case '\r':
  3291. goto daldone;
  3292. case 'i':
  3293. case 'I':
  3294. goto infinite;
  3295. default:
  3296. error:
  3297. #ifdef NANS
  3298. enan( yy, NI*16 );
  3299. #else
  3300. mtherr( "asctoe", DOMAIN );
  3301. ecleaz(yy);
  3302. #endif
  3303. goto aexit;
  3304. }
  3305. donchr:
  3306. ++s;
  3307. goto nxtcom;
  3308. /* Exponent interpretation */
  3309. expnt:
  3310. esign = 1;
  3311. exp = 0;
  3312. ++s;
  3313. /* check for + or - */
  3314. if( *s == '-' )
  3315. {
  3316. esign = -1;
  3317. ++s;
  3318. }
  3319. if( *s == '+' )
  3320. ++s;
  3321. while( (*s >= '0') && (*s <= '9') )
  3322. {
  3323. exp *= 10;
  3324. exp += *s++ - '0';
  3325. if (exp > 4977)
  3326. {
  3327. if (esign < 0)
  3328. goto zero;
  3329. else
  3330. goto infinite;
  3331. }
  3332. }
  3333. if( esign < 0 )
  3334. exp = -exp;
  3335. if( exp > 4932 )
  3336. {
  3337. infinite:
  3338. ecleaz(yy);
  3339. yy[E] = 0x7fff; /* infinity */
  3340. goto aexit;
  3341. }
  3342. if( exp < -4977 )
  3343. {
  3344. zero:
  3345. ecleaz(yy);
  3346. goto aexit;
  3347. }
  3348. daldone:
  3349. nexp = exp - nexp;
  3350. /* Pad trailing zeros to minimize power of 10, per IEEE spec. */
  3351. while( (nexp > 0) && (yy[2] == 0) )
  3352. {
  3353. emovz( yy, xt );
  3354. eshup1( xt );
  3355. eshup1( xt );
  3356. eaddm( yy, xt );
  3357. eshup1( xt );
  3358. if( xt[2] != 0 )
  3359. break;
  3360. nexp -= 1;
  3361. emovz( xt, yy );
  3362. }
  3363. if( (k = enormlz(yy)) > NBITS )
  3364. {
  3365. ecleaz(yy);
  3366. goto aexit;
  3367. }
  3368. lexp = (EXONE - 1 + NBITS) - k;
  3369. emdnorm( yy, lost, 0, lexp, 64 );
  3370. /* convert to external format */
  3371. /* Multiply by 10**nexp. If precision is 64 bits,
  3372. * the maximum relative error incurred in forming 10**n
  3373. * for 0 <= n <= 324 is 8.2e-20, at 10**180.
  3374. * For 0 <= n <= 999, the peak relative error is 1.4e-19 at 10**947.
  3375. * For 0 >= n >= -999, it is -1.55e-19 at 10**-435.
  3376. */
  3377. lexp = yy[E];
  3378. if( nexp == 0 )
  3379. {
  3380. k = 0;
  3381. goto expdon;
  3382. }
  3383. esign = 1;
  3384. if( nexp < 0 )
  3385. {
  3386. nexp = -nexp;
  3387. esign = -1;
  3388. if( nexp > 4096 )
  3389. { /* Punt. Can't handle this without 2 divides. */
  3390. emovi( etens[0], tt );
  3391. lexp -= tt[E];
  3392. k = edivm( tt, yy );
  3393. lexp += EXONE;
  3394. nexp -= 4096;
  3395. }
  3396. }
  3397. p = &etens[NTEN][0];
  3398. emov( eone, xt );
  3399. exp = 1;
  3400. do
  3401. {
  3402. if( exp & nexp )
  3403. emul( p, xt, xt );
  3404. p -= NE;
  3405. exp = exp + exp;
  3406. }
  3407. while( exp <= MAXP );
  3408. emovi( xt, tt );
  3409. if( esign < 0 )
  3410. {
  3411. lexp -= tt[E];
  3412. k = edivm( tt, yy );
  3413. lexp += EXONE;
  3414. }
  3415. else
  3416. {
  3417. lexp += tt[E];
  3418. k = emulm( tt, yy );
  3419. lexp -= EXONE - 1;
  3420. }
  3421. expdon:
  3422. /* Round and convert directly to the destination type */
  3423. if( oprec == 53 )
  3424. lexp -= EXONE - 0x3ff;
  3425. else if( oprec == 24 )
  3426. lexp -= EXONE - 0177;
  3427. #ifdef DEC
  3428. else if( oprec == 56 )
  3429. lexp -= EXONE - 0201;
  3430. #endif
  3431. rndprc = oprec;
  3432. emdnorm( yy, k, 0, lexp, 64 );
  3433. aexit:
  3434. rndprc = rndsav;
  3435. yy[0] = nsign;
  3436. switch( oprec )
  3437. {
  3438. #ifdef DEC
  3439. case 56:
  3440. todec( yy, y ); /* see etodec.c */
  3441. break;
  3442. #endif
  3443. case 53:
  3444. toe53( yy, y );
  3445. break;
  3446. case 24:
  3447. toe24( yy, y );
  3448. break;
  3449. case 64:
  3450. toe64( yy, y );
  3451. break;
  3452. case 113:
  3453. toe113( yy, y );
  3454. break;
  3455. case NBITS:
  3456. emovo( yy, y );
  3457. break;
  3458. }
  3459. }
  3460. /* y = largest integer not greater than x
  3461. * (truncated toward minus infinity)
  3462. *
  3463. * unsigned short x[NE], y[NE]
  3464. *
  3465. * efloor( x, y );
  3466. */
  3467. static unsigned short bmask[] = {
  3468. 0xffff,
  3469. 0xfffe,
  3470. 0xfffc,
  3471. 0xfff8,
  3472. 0xfff0,
  3473. 0xffe0,
  3474. 0xffc0,
  3475. 0xff80,
  3476. 0xff00,
  3477. 0xfe00,
  3478. 0xfc00,
  3479. 0xf800,
  3480. 0xf000,
  3481. 0xe000,
  3482. 0xc000,
  3483. 0x8000,
  3484. 0x0000,
  3485. };
  3486. void efloor( x, y )
  3487. unsigned short x[], y[];
  3488. {
  3489. register unsigned short *p;
  3490. int e, expon, i;
  3491. unsigned short f[NE];
  3492. emov( x, f ); /* leave in external format */
  3493. expon = (int )f[NE-1];
  3494. e = (expon & 0x7fff) - (EXONE - 1);
  3495. if( e <= 0 )
  3496. {
  3497. eclear(y);
  3498. goto isitneg;
  3499. }
  3500. /* number of bits to clear out */
  3501. e = NBITS - e;
  3502. emov( f, y );
  3503. if( e <= 0 )
  3504. return;
  3505. p = &y[0];
  3506. while( e >= 16 )
  3507. {
  3508. *p++ = 0;
  3509. e -= 16;
  3510. }
  3511. /* clear the remaining bits */
  3512. *p &= bmask[e];
  3513. /* truncate negatives toward minus infinity */
  3514. isitneg:
  3515. if( (unsigned short )expon & (unsigned short )0x8000 )
  3516. {
  3517. for( i=0; i<NE-1; i++ )
  3518. {
  3519. if( f[i] != y[i] )
  3520. {
  3521. esub( eone, y, y );
  3522. break;
  3523. }
  3524. }
  3525. }
  3526. }
  3527. /* unsigned short x[], s[];
  3528. * long *exp;
  3529. *
  3530. * efrexp( x, exp, s );
  3531. *
  3532. * Returns s and exp such that s * 2**exp = x and .5 <= s < 1.
  3533. * For example, 1.1 = 0.55 * 2**1
  3534. * Handles denormalized numbers properly using long integer exp.
  3535. */
  3536. void efrexp( x, exp, s )
  3537. unsigned short x[];
  3538. long *exp;
  3539. unsigned short s[];
  3540. {
  3541. unsigned short xi[NI];
  3542. long li;
  3543. emovi( x, xi );
  3544. li = (long )((short )xi[1]);
  3545. if( li == 0 )
  3546. {
  3547. li -= enormlz( xi );
  3548. }
  3549. xi[1] = 0x3ffe;
  3550. emovo( xi, s );
  3551. *exp = li - 0x3ffe;
  3552. }
  3553. /* unsigned short x[], y[];
  3554. * long pwr2;
  3555. *
  3556. * eldexp( x, pwr2, y );
  3557. *
  3558. * Returns y = x * 2**pwr2.
  3559. */
  3560. void eldexp( x, pwr2, y )
  3561. unsigned short x[];
  3562. long pwr2;
  3563. unsigned short y[];
  3564. {
  3565. unsigned short xi[NI];
  3566. long li;
  3567. int i;
  3568. emovi( x, xi );
  3569. li = xi[1];
  3570. li += pwr2;
  3571. i = 0;
  3572. emdnorm( xi, i, i, li, 64 );
  3573. emovo( xi, y );
  3574. }
  3575. /* c = remainder after dividing b by a
  3576. * Least significant integer quotient bits left in equot[].
  3577. */
  3578. void eremain( a, b, c )
  3579. unsigned short a[], b[], c[];
  3580. {
  3581. unsigned short den[NI], num[NI];
  3582. #ifdef NANS
  3583. if( eisinf(b) || (ecmp(a,ezero) == 0) || eisnan(a) || eisnan(b))
  3584. {
  3585. enan( c, NBITS );
  3586. return;
  3587. }
  3588. #endif
  3589. if( ecmp(a,ezero) == 0 )
  3590. {
  3591. mtherr( "eremain", SING );
  3592. eclear( c );
  3593. return;
  3594. }
  3595. emovi( a, den );
  3596. emovi( b, num );
  3597. eiremain( den, num );
  3598. /* Sign of remainder = sign of quotient */
  3599. if( a[0] == b[0] )
  3600. num[0] = 0;
  3601. else
  3602. num[0] = 0xffff;
  3603. emovo( num, c );
  3604. }
  3605. void eiremain( den, num )
  3606. unsigned short den[], num[];
  3607. {
  3608. long ld, ln;
  3609. unsigned short j;
  3610. ld = den[E];
  3611. ld -= enormlz( den );
  3612. ln = num[E];
  3613. ln -= enormlz( num );
  3614. ecleaz( equot );
  3615. while( ln >= ld )
  3616. {
  3617. if( ecmpm(den,num) <= 0 )
  3618. {
  3619. esubm(den, num);
  3620. j = 1;
  3621. }
  3622. else
  3623. {
  3624. j = 0;
  3625. }
  3626. eshup1(equot);
  3627. equot[NI-1] |= j;
  3628. eshup1(num);
  3629. ln -= 1;
  3630. }
  3631. emdnorm( num, 0, 0, ln, 0 );
  3632. }
  3633. /* NaN bit patterns
  3634. */
  3635. #ifdef MIEEE
  3636. unsigned short nan113[8] = {
  3637. 0x7fff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff};
  3638. unsigned short nan64[6] = {0x7fff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff};
  3639. unsigned short nan53[4] = {0x7fff, 0xffff, 0xffff, 0xffff};
  3640. unsigned short nan24[2] = {0x7fff, 0xffff};
  3641. #endif
  3642. #ifdef IBMPC
  3643. unsigned short nan113[8] = {0, 0, 0, 0, 0, 0, 0xc000, 0xffff};
  3644. unsigned short nan64[6] = {0, 0, 0, 0xc000, 0xffff, 0};
  3645. unsigned short nan53[4] = {0, 0, 0, 0xfff8};
  3646. unsigned short nan24[2] = {0, 0xffc0};
  3647. #endif
  3648. void enan (nan, size)
  3649. unsigned short *nan;
  3650. int size;
  3651. {
  3652. int i, n;
  3653. unsigned short *p;
  3654. switch( size )
  3655. {
  3656. #ifndef DEC
  3657. case 113:
  3658. n = 8;
  3659. p = nan113;
  3660. break;
  3661. case 64:
  3662. n = 6;
  3663. p = nan64;
  3664. break;
  3665. case 53:
  3666. n = 4;
  3667. p = nan53;
  3668. break;
  3669. case 24:
  3670. n = 2;
  3671. p = nan24;
  3672. break;
  3673. case NBITS:
  3674. for( i=0; i<NE-2; i++ )
  3675. *nan++ = 0;
  3676. *nan++ = 0xc000;
  3677. *nan++ = 0x7fff;
  3678. return;
  3679. case NI*16:
  3680. *nan++ = 0;
  3681. *nan++ = 0x7fff;
  3682. *nan++ = 0;
  3683. *nan++ = 0xc000;
  3684. for( i=4; i<NI; i++ )
  3685. *nan++ = 0;
  3686. return;
  3687. #endif
  3688. default:
  3689. mtherr( "enan", DOMAIN );
  3690. return;
  3691. }
  3692. for (i=0; i < n; i++)
  3693. *nan++ = *p++;
  3694. }
  3695. /* Longhand square root. */
  3696. static int esqinited = 0;
  3697. static unsigned short sqrndbit[NI];
  3698. void esqrt( x, y )
  3699. unsigned short *x, *y;
  3700. {
  3701. unsigned short temp[NI], num[NI], sq[NI], xx[NI];
  3702. int i, j, k, n, nlups;
  3703. long m, exp;
  3704. if( esqinited == 0 )
  3705. {
  3706. ecleaz( sqrndbit );
  3707. sqrndbit[NI-2] = 1;
  3708. esqinited = 1;
  3709. }
  3710. /* Check for arg <= 0 */
  3711. i = ecmp( x, ezero );
  3712. if( i <= 0 )
  3713. {
  3714. #ifdef NANS
  3715. if (i == -2)
  3716. {
  3717. enan (y, NBITS);
  3718. return;
  3719. }
  3720. #endif
  3721. eclear(y);
  3722. if( i < 0 )
  3723. mtherr( "esqrt", DOMAIN );
  3724. return;
  3725. }
  3726. #ifdef INFINITY
  3727. if( eisinf(x) )
  3728. {
  3729. eclear(y);
  3730. einfin(y);
  3731. return;
  3732. }
  3733. #endif
  3734. /* Bring in the arg and renormalize if it is denormal. */
  3735. emovi( x, xx );
  3736. m = (long )xx[1]; /* local long word exponent */
  3737. if( m == 0 )
  3738. m -= enormlz( xx );
  3739. /* Divide exponent by 2 */
  3740. m -= 0x3ffe;
  3741. exp = (unsigned short )( (m / 2) + 0x3ffe );
  3742. /* Adjust if exponent odd */
  3743. if( (m & 1) != 0 )
  3744. {
  3745. if( m > 0 )
  3746. exp += 1;
  3747. eshdn1( xx );
  3748. }
  3749. ecleaz( sq );
  3750. ecleaz( num );
  3751. n = 8; /* get 8 bits of result per inner loop */
  3752. nlups = rndprc;
  3753. j = 0;
  3754. while( nlups > 0 )
  3755. {
  3756. /* bring in next word of arg */
  3757. if( j < NE )
  3758. num[NI-1] = xx[j+3];
  3759. /* Do additional bit on last outer loop, for roundoff. */
  3760. if( nlups <= 8 )
  3761. n = nlups + 1;
  3762. for( i=0; i<n; i++ )
  3763. {
  3764. /* Next 2 bits of arg */
  3765. eshup1( num );
  3766. eshup1( num );
  3767. /* Shift up answer */
  3768. eshup1( sq );
  3769. /* Make trial divisor */
  3770. for( k=0; k<NI; k++ )
  3771. temp[k] = sq[k];
  3772. eshup1( temp );
  3773. eaddm( sqrndbit, temp );
  3774. /* Subtract and insert answer bit if it goes in */
  3775. if( ecmpm( temp, num ) <= 0 )
  3776. {
  3777. esubm( temp, num );
  3778. sq[NI-2] |= 1;
  3779. }
  3780. }
  3781. nlups -= n;
  3782. j += 1;
  3783. }
  3784. /* Adjust for extra, roundoff loop done. */
  3785. exp += (NBITS - 1) - rndprc;
  3786. /* Sticky bit = 1 if the remainder is nonzero. */
  3787. k = 0;
  3788. for( i=3; i<NI; i++ )
  3789. k |= (int )num[i];
  3790. /* Renormalize and round off. */
  3791. emdnorm( sq, k, 0, exp, 64 );
  3792. emovo( sq, y );
  3793. }