ieee.c 70 KB

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