eparanoi.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550
  1. /* paranoia.c arithmetic tester
  2. *
  3. * This is an implementation of the PARANOIA program. It substitutes
  4. * subroutine calls for ALL floating point arithmetic operations.
  5. * This permits you to substitute your own experimental versions of
  6. * arithmetic routines. It also defeats compiler optimizations,
  7. * so for native arithmetic you can be pretty sure you are testing
  8. * the arithmetic and not the compiler.
  9. *
  10. * This version of PARANOIA omits the display of division by zero.
  11. * It also omits the test for extra precise subexpressions, since
  12. * they cannot occur in this context. Otherwise it includes all the
  13. * tests of the 27 Jan 86 distribution, plus a few additional tests.
  14. * Commentary has been reduced to a minimum in order to make the program
  15. * smaller.
  16. *
  17. * The original PARANOIA program, written by W. Kahan, C version
  18. * by Thos Sumner and David Gay, can be downloaded free from the
  19. * Internet NETLIB. An MSDOS disk can be obtained for $15 from:
  20. * Richard Karpinski
  21. * 6521 Raymond Street
  22. * Oakland, CA 94609
  23. *
  24. * Steve Moshier, 28 Oct 88
  25. * last rev: 23 May 92
  26. */
  27. #define DEBUG 0
  28. /* To use the native arithmetic of the computer, define NATIVE
  29. * to be 1. To use your own supplied arithmetic routines, NATIVE is 0.
  30. */
  31. #define NATIVE 0
  32. /* gcc real.c interface */
  33. #define L128DOUBLE 0
  34. #include <stdio.h>
  35. /* Data structure of floating point number. If NATIVE was
  36. * selected above, you can define LDOUBLE 1 to test 80-bit long double
  37. * precision or define it 0 to test 64-bit double precision.
  38. */
  39. #define LDOUBLE 0
  40. #if NATIVE
  41. #define NE 1
  42. #if LDOUBLE
  43. #define FSIZE long double
  44. #define FLOAT(x) FSIZE x[NE]
  45. static FSIZE eone[NE] = {1.0L}; /* The constant 1.0 */
  46. #define ZSQRT sqrtl
  47. #define ZLOG logl
  48. #define ZFLOOR floorl
  49. #define ZPOW powl
  50. long double sqrtl(), logl(), floorl(), powl();
  51. #define FSETUP einit
  52. #else /* not LDOUBLE */
  53. #define FSIZE double
  54. #define FLOAT(x) FSIZE x[NE]
  55. static FSIZE eone[NE] = {1.0}; /* The constant 1.0 */
  56. #define ZSQRT sqrt
  57. #define ZLOG log
  58. #define ZFLOOR floor
  59. #define ZPOW pow
  60. double sqrt(), log(), floor(), pow();
  61. /* Coprocessor initialization,
  62. * defeat underflow trap or what have you.
  63. * This is required mainly on i386 and 68K processors.
  64. */
  65. #define FSETUP dprec
  66. #endif /* double, not LDOUBLE */
  67. #else /* not NATIVE */
  68. /* Setup for extended double type.
  69. * Put NE = 10 for real.c operating with TFmode support (16-byte reals)
  70. * Put NE = 6 for real.c operating with XFmode support (10- or 12-byte reals)
  71. * The value of NE must agree with that in ehead.h, if ieee.c is used.
  72. */
  73. #define NE 6
  74. #define FSIZE unsigned short
  75. #define FLOAT(x) unsigned short x[NE]
  76. extern unsigned short eone[];
  77. #define FSETUP einit
  78. /* default for FSETUP */
  79. /*
  80. einit()
  81. {}
  82. */
  83. error(s)
  84. char *s;
  85. {
  86. printf( "error: %s\n", s );
  87. }
  88. #endif /* not NATIVE */
  89. #if L128DOUBLE
  90. /* real.c interface */
  91. #undef FSETUP
  92. #define FSETUP efsetup
  93. FLOAT(enone);
  94. #define ONE enone
  95. /* Use emov to convert from widest type to widest type, ... */
  96. /*
  97. #define ENTOE emov
  98. #define ETOEN emov
  99. */
  100. /* ... else choose e24toe, e53toe, etc. */
  101. #define ENTOE e64toe
  102. #define ETOEN etoe64
  103. #define NNBITS 64
  104. #define NIBITS ((NE-1)*16)
  105. extern int rndprc;
  106. efsetup()
  107. {
  108. rndprc = NNBITS;
  109. ETOEN(eone, enone);
  110. }
  111. add(a,b,c)
  112. FLOAT(a);
  113. FLOAT(b);
  114. FLOAT(c);
  115. {
  116. unsigned short aa[10], bb[10], cc[10];
  117. ENTOE(a,aa);
  118. ENTOE(b,bb);
  119. eadd(aa,bb,cc);
  120. ETOEN(cc,c);
  121. }
  122. sub(a,b,c)
  123. FLOAT(a);
  124. FLOAT(b);
  125. FLOAT(c);
  126. {
  127. unsigned short aa[10], bb[10], cc[10];
  128. ENTOE(a,aa);
  129. ENTOE(b,bb);
  130. esub(aa,bb,cc);
  131. ETOEN(cc,c);
  132. }
  133. mul(a,b,c)
  134. FLOAT(a);
  135. FLOAT(b);
  136. FLOAT(c);
  137. {
  138. unsigned short aa[10], bb[10], cc[10];
  139. ENTOE(a,aa);
  140. ENTOE(b,bb);
  141. emul(aa,bb,cc);
  142. ETOEN(cc,c);
  143. }
  144. div(a,b,c)
  145. FLOAT(a);
  146. FLOAT(b);
  147. FLOAT(c);
  148. {
  149. unsigned short aa[10], bb[10], cc[10];
  150. ENTOE(a,aa);
  151. ENTOE(b,bb);
  152. ediv(aa,bb,cc);
  153. ETOEN(cc,c);
  154. }
  155. int cmp(a,b)
  156. FLOAT(a);
  157. FLOAT(b);
  158. {
  159. unsigned short aa[10], bb[10];
  160. int c;
  161. int ecmp();
  162. ENTOE(a,aa);
  163. ENTOE(b,bb);
  164. c = ecmp(aa,bb);
  165. return(c);
  166. }
  167. mov(a,b)
  168. FLOAT(a);
  169. FLOAT(b);
  170. {
  171. int i;
  172. for( i=0; i<NE; i++ )
  173. b[i] = a[i];
  174. }
  175. neg(a)
  176. FLOAT(a);
  177. {
  178. unsigned short aa[10];
  179. ENTOE(a,aa);
  180. eneg(aa);
  181. ETOEN(aa,a);
  182. }
  183. clear(a)
  184. FLOAT(a);
  185. {
  186. int i;
  187. for( i=0; i<NE; i++ )
  188. a[i] = 0;
  189. }
  190. FABS(a)
  191. FLOAT(a);
  192. {
  193. unsigned short aa[10];
  194. ENTOE(a,aa);
  195. eabs(aa);
  196. ETOEN(aa,a);
  197. }
  198. FLOOR(a,b)
  199. FLOAT(a);
  200. FLOAT(b);
  201. {
  202. unsigned short aa[10], bb[10];
  203. ENTOE(a,aa);
  204. efloor(aa,bb);
  205. ETOEN(bb,b);
  206. }
  207. LOG(a,b)
  208. FLOAT(a);
  209. FLOAT(b);
  210. {
  211. unsigned short aa[10], bb[10];
  212. int rndsav;
  213. ENTOE(a,aa);
  214. rndsav = rndprc;
  215. rndprc = NIBITS;
  216. elog(aa,bb);
  217. rndprc = rndsav;
  218. ETOEN(bb,b);
  219. }
  220. POW(a,b,c)
  221. FLOAT(a);
  222. FLOAT(b);
  223. FLOAT(c);
  224. {
  225. unsigned short aa[10], bb[10], cc[10];
  226. int rndsav;
  227. ENTOE(a,aa);
  228. ENTOE(b,bb);
  229. rndsav = rndprc;
  230. rndprc = NIBITS;
  231. epow(aa,bb,cc);
  232. rndprc = rndsav;
  233. ETOEN(cc,c);
  234. }
  235. SQRT(a,b)
  236. FLOAT(a);
  237. FLOAT(b);
  238. {
  239. unsigned short aa[10], bb[10];
  240. ENTOE(a,aa);
  241. esqrt(aa,bb);
  242. ETOEN(bb,b);
  243. }
  244. FTOL(x,ip,f)
  245. FLOAT(x);
  246. long *ip;
  247. FLOAT(f);
  248. {
  249. unsigned short xx[10], ff[10];
  250. ENTOE(x,xx);
  251. eifrac(xx,ip,ff);
  252. ETOEN(ff,f);
  253. }
  254. LTOF(ip,x)
  255. long *ip;
  256. FLOAT(x);
  257. {
  258. unsigned short xx[10];
  259. ltoe(ip,xx);
  260. ETOEN(xx,x);
  261. }
  262. TOASC(a,b,c)
  263. FLOAT(a);
  264. int b;
  265. char *c;
  266. {
  267. unsigned short xx[10];
  268. ENTOE(a,xx);
  269. etoasc(xx,b,c);
  270. }
  271. #else /* not L128DOUBLE */
  272. #define ONE eone
  273. /* Note all arguments of operation subroutines are pointers. */
  274. /* c = b + a */
  275. #define add(a,b,c) eadd(a,b,c)
  276. /* c = b - a */
  277. #define sub(a,b,c) esub(a,b,c)
  278. /* c = b * a */
  279. #define mul(a,b,c) emul(a,b,c)
  280. /* c = b / a */
  281. #define div(a,b,c) ediv(a,b,c)
  282. /* 1 if a>b, 0 if a==b, -1 if a<b */
  283. #define cmp(a,b) ecmp(a,b)
  284. /* b = a */
  285. #define mov(a,b) emov(a,b)
  286. /* a = -a */
  287. #define neg(a) eneg(a)
  288. /* a = 0 */
  289. #define clear(a) eclear(a)
  290. #define FABS(x) eabs(x)
  291. #define FLOOR(x,y) efloor(x,y)
  292. #define LOG(x,y) elog(x,y)
  293. #define POW(x,y,z) epow(x,y,z)
  294. #define SQRT(x,y) esqrt(x,y)
  295. /* x = &FLOAT input, i = &long integer part, f = &FLOAT fractional part */
  296. #define FTOL(x,i,f) eifrac(x,i,f)
  297. /* i = &long integer input, x = &FLOAT output */
  298. #define LTOF(i,x) ltoe(i,x)
  299. /* Convert FLOAT a to decimal ASCII string with b digits */
  300. #define TOASC(a,b,c) etoasc(a,b,c)
  301. #endif /* not L128DOUBLE */
  302. /* The following subroutines are implementations of the above
  303. * named functions, using the native or default arithmetic.
  304. */
  305. #if NATIVE
  306. eadd(a,b,c)
  307. FSIZE *a, *b, *c;
  308. {
  309. *c = *b + *a;
  310. }
  311. esub(a,b,c)
  312. FSIZE *a, *b, *c;
  313. {
  314. *c = *b - *a;
  315. }
  316. emul(a,b,c)
  317. FSIZE *a, *b, *c;
  318. {
  319. *c = (*b) * (*a);
  320. }
  321. ediv(a,b,c)
  322. FSIZE *a, *b, *c;
  323. {
  324. *c = (*b) / (*a);
  325. }
  326. /* Important note: comparison can be done by subracting
  327. * or by a compare instruction that may or may not be
  328. * equivalent to subtracting.
  329. */
  330. ecmp(a,b)
  331. FSIZE *a, *b;
  332. {
  333. if( (*a) > (*b) )
  334. return( 1 );
  335. if( (*a) < (*b) )
  336. return( -1 );
  337. if( (*a) != (*b) )
  338. goto cmpf;
  339. if( (*a) == (*b) )
  340. return( 0 );
  341. cmpf:
  342. printf( "Compare fails\n" );
  343. return(0);
  344. }
  345. emov( a, b )
  346. FSIZE *a, *b;
  347. {
  348. *b = *a;
  349. }
  350. eneg( a )
  351. FSIZE *a;
  352. {
  353. *a = -(*a);
  354. }
  355. eclear(a)
  356. FSIZE *a;
  357. {
  358. *a = 0.0;
  359. }
  360. eabs(x)
  361. FSIZE *x;
  362. {
  363. if( (*x) < 0.0 )
  364. *x = -(*x);
  365. }
  366. efloor(x,y)
  367. FSIZE *x, *y;
  368. {
  369. *y = (FSIZE )ZFLOOR( *x );
  370. }
  371. elog(x,y)
  372. FSIZE *x, *y;
  373. {
  374. *y = (FSIZE )ZLOG( *x );
  375. }
  376. epow(x,y,z)
  377. FSIZE *x, *y, *z;
  378. {
  379. *z = (FSIZE )ZPOW( *x, *y );
  380. }
  381. esqrt(x,y)
  382. FSIZE *x, *y;
  383. {
  384. *y = (FSIZE )ZSQRT( *x );
  385. }
  386. eifrac(x,i,f)
  387. FSIZE *x;
  388. long *i;
  389. FSIZE *f;
  390. {
  391. FSIZE y;
  392. y = (FSIZE )ZFLOOR( *x );
  393. if( y < 0.0 )
  394. {
  395. *f = y - *x;
  396. *i = -y;
  397. }
  398. else
  399. {
  400. *f = *x - y;
  401. *i = y;
  402. }
  403. }
  404. ltoe(i,x)
  405. long *i;
  406. FSIZE *x;
  407. {
  408. *x = *i;
  409. }
  410. etoasc(a,str,n)
  411. FSIZE *a;
  412. char *str;
  413. int n;
  414. {
  415. double x;
  416. x = (double )(*a);
  417. sprintf( str, " %.17e ", x );
  418. }
  419. /* default for FSETUP */
  420. einit()
  421. {}
  422. #endif /* NATIVE */
  423. FLOAT(Radix);
  424. FLOAT(BInvrse);
  425. FLOAT(RadixD2);
  426. FLOAT(BMinusU2);
  427. /*Small floating point constants.*/
  428. FLOAT(Zero);
  429. FLOAT(Half);
  430. FLOAT(One);
  431. FLOAT(Two);
  432. FLOAT(Three);
  433. FLOAT(Four);
  434. FLOAT(Five);
  435. FLOAT(Six);
  436. FLOAT(Eight);
  437. FLOAT(Nine);
  438. FLOAT(Ten);
  439. FLOAT(TwentySeven);
  440. FLOAT(ThirtyTwo);
  441. FLOAT(TwoForty);
  442. FLOAT(MinusOne );
  443. FLOAT(OneAndHalf);
  444. /*Integer constants*/
  445. int NoTrials = 20; /*Number of tests for commutativity. */
  446. #define False 0
  447. #define True 1
  448. /* Definitions for declared types
  449. Guard == (Yes, No);
  450. Rounding == (Chopped, Rounded, Other);
  451. Message == packed array [1..40] of char;
  452. Class == (Flaw, Defect, Serious, Failure);
  453. */
  454. #define Yes 1
  455. #define No 0
  456. #define Chopped 2
  457. #define Rounded 1
  458. #define Other 0
  459. #define Flaw 3
  460. #define Defect 2
  461. #define Serious 1
  462. #define Failure 0
  463. typedef int Guard, Rounding, Class;
  464. typedef char Message;
  465. /* Declarations of Variables */
  466. FLOAT(AInvrse);
  467. FLOAT(A1);
  468. FLOAT(C);
  469. FLOAT(CInvrse);
  470. FLOAT(D);
  471. FLOAT(FourD);
  472. FLOAT(E0);
  473. FLOAT(E1);
  474. FLOAT(Exp2);
  475. FLOAT(E3);
  476. FLOAT(MinSqEr);
  477. FLOAT(SqEr);
  478. FLOAT(MaxSqEr);
  479. FLOAT(E9);
  480. FLOAT(Third);
  481. FLOAT(F6);
  482. FLOAT(F9);
  483. FLOAT(H);
  484. FLOAT(HInvrse);
  485. FLOAT(StickyBit);
  486. FLOAT(J);
  487. FLOAT(MyZero);
  488. FLOAT(Precision);
  489. FLOAT(Q);
  490. FLOAT(Q9);
  491. FLOAT(R);
  492. FLOAT(Random9);
  493. FLOAT(T);
  494. FLOAT(Underflow);
  495. FLOAT(S);
  496. FLOAT(OneUlp);
  497. FLOAT(UfThold);
  498. FLOAT(U1);
  499. FLOAT(U2);
  500. FLOAT(V);
  501. FLOAT(V0);
  502. FLOAT(V9);
  503. FLOAT(W);
  504. FLOAT(X);
  505. FLOAT(X1);
  506. FLOAT(X2);
  507. FLOAT(X8);
  508. FLOAT(Random1);
  509. FLOAT(Y);
  510. FLOAT(YY1);
  511. FLOAT(Y2);
  512. FLOAT(Random2);
  513. FLOAT(Z);
  514. FLOAT(PseudoZero);
  515. FLOAT(Z1);
  516. FLOAT(Z2);
  517. FLOAT(Z9);
  518. static FLOAT(t);
  519. FLOAT(t2);
  520. FLOAT(Sqarg);
  521. int ErrCnt[4];
  522. int fpecount;
  523. int Milestone;
  524. int PageNo;
  525. int I, M, N, N1, stkflg;
  526. Guard GMult, GDiv, GAddSub;
  527. Rounding RMult, RDiv, RAddSub, RSqrt;
  528. int Break, Done, NotMonot, Monot, Anomaly, IEEE;
  529. int SqRWrng, UfNGrad;
  530. int k, k2;
  531. int Indx;
  532. char ch[8];
  533. long lngint, lng2; /* intermediate for conversion between int and FLOAT */
  534. /* Computed constants. */
  535. /*U1 gap below 1.0, i.e, 1.0-U1 is next number below 1.0 */
  536. /*U2 gap above 1.0, i.e, 1.0+U2 is next number above 1.0 */
  537. show( x )
  538. short x[];
  539. {
  540. int i;
  541. char s[80];
  542. /* Number of 16-bit groups to display */
  543. #if NATIVE
  544. #if LDOUBLE
  545. #define NPRT (sizeof( long double )/2)
  546. #else
  547. #define NPRT (sizeof( double )/2)
  548. #endif
  549. #else
  550. #define NPRT NE
  551. #endif
  552. TOASC( x, s, 70 );
  553. printf( "%s\n", s );
  554. for( i=0; i<NPRT; i++ )
  555. printf( "%04x ", x[i] & 0xffff );
  556. printf( "\n" );
  557. }
  558. /* define NOSIGNAL */
  559. #ifndef NOSIGNAL
  560. #include <signal.h>
  561. #endif
  562. #include <setjmp.h>
  563. jmp_buf ovfl_buf;
  564. /*typedef int (*Sig_type)();*/
  565. typedef void (*Sig_type)();
  566. Sig_type sigsave;
  567. /* Floating point exception receiver */
  568. void sigfpe()
  569. {
  570. fpecount++;
  571. printf( "\n* * * FLOATING-POINT ERROR * * *\n" );
  572. /* reinitialize the floating point unit */
  573. FSETUP();
  574. fflush(stdout);
  575. if( sigsave )
  576. {
  577. #ifndef NOSIGNAL
  578. signal( SIGFPE, sigsave );
  579. #endif
  580. sigsave = 0;
  581. longjmp( ovfl_buf, 1 );
  582. }
  583. abort();
  584. }
  585. main()
  586. {
  587. /* Do coprocessor or other initializations */
  588. FSETUP();
  589. printf(
  590. "This version of paranoia omits test for extra precise subexpressions\n" );
  591. printf( "and includes a few additional tests.\n" );
  592. clear(Zero);
  593. printf( "0 = " );
  594. show( Zero );
  595. mov( ONE, One);
  596. printf( "1 = " );
  597. show( One );
  598. add( One, One, Two );
  599. printf( "1+1 = " );
  600. show( Two );
  601. add( Two, One, Three );
  602. add( Three, One, Four );
  603. add( Four, One, Five );
  604. add( Five, One, Six );
  605. add( Four, Four, Eight );
  606. mul( Three, Three, Nine );
  607. add( Nine, One, Ten );
  608. mul( Nine, Three, TwentySeven );
  609. mul( Four, Eight, ThirtyTwo );
  610. mul( Four, Five, t );
  611. mul( t, Three, t );
  612. mul( t, Four, TwoForty );
  613. mov( One, MinusOne );
  614. neg( MinusOne );
  615. div( Two, One, Half );
  616. add( One, Half, OneAndHalf );
  617. ErrCnt[Failure] = 0;
  618. ErrCnt[Serious] = 0;
  619. ErrCnt[Defect] = 0;
  620. ErrCnt[Flaw] = 0;
  621. PageNo = 1;
  622. #ifndef NOSIGNAL
  623. signal( SIGFPE, sigfpe );
  624. #endif
  625. printf("Program is now RUNNING tests on small integers:\n");
  626. add( Zero, Zero, t );
  627. if( cmp( t, Zero ) != 0)
  628. {
  629. printf( "0+0 != 0\n" );
  630. ErrCnt[Failure] += 1;
  631. }
  632. sub( One, One, t );
  633. if( cmp( t, Zero ) != 0 )
  634. {
  635. printf( "1-1 != 0\n" );
  636. ErrCnt[Failure] += 1;
  637. }
  638. if( cmp( One, Zero ) <= 0 )
  639. {
  640. printf( "1 <= 0\n" );
  641. ErrCnt[Failure] += 1;
  642. }
  643. add( One, One, t );
  644. if( cmp( t, Two ) != 0 )
  645. {
  646. printf( "1+1 != 2\n" );
  647. ErrCnt[Failure] += 1;
  648. }
  649. mov( Zero, Z );
  650. neg( Z );
  651. FLOOR( Z, t );
  652. if( cmp(t,Zero) != 0 )
  653. {
  654. ErrCnt[Serious] += 1;
  655. printf( "FLOOR(-0) should equal 0, is = " );
  656. show( t );
  657. }
  658. if( cmp(Z, Zero) != 0)
  659. {
  660. ErrCnt[Failure] += 1;
  661. printf("Comparison alleges that -0.0 is Non-zero!\n");
  662. }
  663. else
  664. {
  665. div( TwoForty, One, U1 ); /* U1 = 0.001 */
  666. mov( One, Radix );
  667. TstPtUf();
  668. }
  669. add( Two, One, t );
  670. if( cmp( t, Three ) != 0 )
  671. {
  672. printf( "2+1 != 3\n" );
  673. ErrCnt[Failure] += 1;
  674. }
  675. add( Three, One, t );
  676. if( cmp( t, Four ) != 0 )
  677. {
  678. printf( "3+1 != 4\n" );
  679. ErrCnt[Failure] += 1;
  680. }
  681. mov( Two, t );
  682. neg( t );
  683. mul( Two, t, t );
  684. add( Four, t, t );
  685. if( cmp( t, Zero ) != 0 )
  686. {
  687. printf( "4+2*(-2) != 0\n" );
  688. ErrCnt[Failure] += 1;
  689. }
  690. sub( Three, Four, t );
  691. sub( One, t, t );
  692. if( cmp( t, Zero ) != 0 )
  693. {
  694. printf( "4-3-1 != 0\n" );
  695. ErrCnt[Failure] += 1;
  696. }
  697. sub( One, Zero, t );
  698. if( cmp( t, MinusOne ) != 0 )
  699. {
  700. printf( "-1 != 0-1\n" );
  701. ErrCnt[Failure] += 1;
  702. }
  703. add( One, MinusOne, t );
  704. if( cmp( t, Zero ) != 0 )
  705. {
  706. printf( "1+(-1) != 0\n" );
  707. ErrCnt[Failure] += 1;
  708. }
  709. mov( One, t );
  710. FABS( t );
  711. add( MinusOne, t, t );
  712. if( cmp( t, Zero ) != 0 )
  713. {
  714. printf( "-1+abs(1) != 0\n" );
  715. ErrCnt[Failure] += 1;
  716. }
  717. mul( MinusOne, MinusOne, t );
  718. add( MinusOne, t, t );
  719. if( cmp( t, Zero ) != 0 )
  720. {
  721. printf( "-1+(-1)*(-1) != 0\n" );
  722. ErrCnt[Failure] += 1;
  723. }
  724. add( Half, MinusOne, t );
  725. add( Half, t, t );
  726. if( cmp( t, Zero ) != 0 )
  727. {
  728. printf( "1/2 + (-1) + 1/2 != 0\n" );
  729. ErrCnt[Failure] += 1;
  730. }
  731. Milestone = 10;
  732. mul( Three, Three, t );
  733. if( cmp( t, Nine ) != 0 )
  734. {
  735. printf( "3*3 != 9\n" );
  736. ErrCnt[Failure] += 1;
  737. }
  738. mul( Nine, Three, t );
  739. if( cmp( t, TwentySeven ) != 0 )
  740. {
  741. printf( "3*9 != 27\n" );
  742. ErrCnt[Failure] += 1;
  743. }
  744. add( Four, Four, t );
  745. if( cmp( t, Eight ) != 0 )
  746. {
  747. printf( "4+4 != 8\n" );
  748. ErrCnt[Failure] += 1;
  749. }
  750. mul( Eight, Four, t );
  751. if( cmp( t, ThirtyTwo ) != 0 )
  752. {
  753. printf( "8*4 != 32\n" );
  754. ErrCnt[Failure] += 1;
  755. }
  756. sub( TwentySeven, ThirtyTwo, t );
  757. sub( Four, t, t );
  758. sub( One, t, t );
  759. if( cmp( t, Zero ) != 0 )
  760. {
  761. printf( "32-27-4-1 != 0\n" );
  762. ErrCnt[Failure] += 1;
  763. }
  764. add( Four, One, t );
  765. if( cmp( t, Five ) != 0 )
  766. {
  767. printf( "4+1 != 5\n" );
  768. ErrCnt[Failure] += 1;
  769. }
  770. mul( Four, Five, t );
  771. mul( Three, t, t );
  772. mul( Four, t, t );
  773. if( cmp( t, TwoForty ) != 0 )
  774. {
  775. printf( "4*5*3*4 != 240\n" );
  776. ErrCnt[Failure] += 1;
  777. }
  778. div( Three, TwoForty, t );
  779. mul( Four, Four, t2 );
  780. mul( Five, t2, t2 );
  781. sub( t2, t2, t );
  782. if( cmp( t, Zero ) != 0 )
  783. {
  784. printf( "240/3 - 4*4*5 != 0\n" );
  785. ErrCnt[Failure] += 1;
  786. }
  787. div( Four, TwoForty, t );
  788. mul( Five, Three, t2 );
  789. mul( Four, t2, t2 );
  790. sub( t2, t, t );
  791. if( cmp( t, Zero ) != 0 )
  792. {
  793. printf( "240/4 - 5*3*4 != 0\n" );
  794. ErrCnt[Failure] += 1;
  795. }
  796. div( Five, TwoForty, t );
  797. mul( Four, Three, t2 );
  798. mul( Four, t2, t2 );
  799. sub( t2, t, t );
  800. if( cmp( t, Zero ) != 0 )
  801. {
  802. printf( "240/5 - 4*3*4 != 0\n" );
  803. ErrCnt[Failure] += 1;
  804. }
  805. if(ErrCnt[Failure] == 0)
  806. {
  807. printf("-1, 0, 1/2, 1, 2, 3, 4, 5, 9, 27, 32 & 240 are O.K.\n\n");
  808. }
  809. printf("Searching for Radix and Precision.\n");
  810. mov( One, W );
  811. do
  812. {
  813. add( W, W, W );
  814. add( W, One, Y );
  815. sub( W, Y, Z );
  816. sub( One, Z, Y );
  817. mov( Y, t );
  818. FABS(t);
  819. add( MinusOne, t, t );
  820. k = cmp( t, Zero );
  821. }
  822. while( k < 0 );
  823. /*.. now W is just big enough that |((W+1)-W)-1| >= 1 ...*/
  824. mov( Zero, Precision );
  825. mov( One, Y );
  826. do
  827. {
  828. add( W, Y, Radix );
  829. add( Y, Y, Y );
  830. sub( W, Radix, Radix );
  831. k = cmp( Radix, Zero );
  832. }
  833. while( k == 0);
  834. if( cmp(Radix, Two) < 0 )
  835. mov( One, Radix );
  836. printf("Radix = " );
  837. show( Radix );
  838. if( cmp(Radix, One) != 0)
  839. {
  840. mov( One, W );
  841. do
  842. {
  843. add( One, Precision, Precision );
  844. mul( W, Radix, W );
  845. add( W, One, Y );
  846. sub( W, Y, t );
  847. k = cmp( t, One );
  848. }
  849. while( k == 0 );
  850. }
  851. /* now W == Radix^Precision is barely too big to satisfy (W+1)-W == 1 */
  852. div( W, One, U1 );
  853. mul( Radix, U1, U2 );
  854. printf( "Closest relative separation found is U 1 = " );
  855. show( U1 );
  856. printf( "Recalculating radix and precision." );
  857. /*save old values*/
  858. mov( Radix, E0 );
  859. mov( U1, E1 );
  860. mov( U2, E9 );
  861. mov( Precision, E3 );
  862. div( Three, Four, X );
  863. sub( One, X, Third );
  864. sub( Third, Half, F6 );
  865. add( F6, F6, X );
  866. sub( Third, X, X );
  867. FABS( X );
  868. if( cmp(X, U2) < 0 )
  869. mov( U2, X );
  870. /*... now X = (unknown no.) ulps of 1+...*/
  871. do
  872. {
  873. mov( X, U2 );
  874. /* Y = Half * U2 + ThirtyTwo * U2 * U2; */
  875. mul( ThirtyTwo, U2, t );
  876. mul( t, U2, t );
  877. mul( Half, U2, Y );
  878. add( t, Y, Y );
  879. add( One, Y, Y );
  880. sub( One, Y, X );
  881. k = cmp( U2, X );
  882. k2 = cmp( X, Zero );
  883. }
  884. while ( ! ((k <= 0) || (k2 <= 0)));
  885. /*... now U2 == 1 ulp of 1 + ... */
  886. div( Three, Two, X );
  887. sub( Half, X, F6 );
  888. add( F6, F6, Third );
  889. sub( Half, Third, X );
  890. add( F6, X, X );
  891. FABS( X );
  892. if( cmp(X, U1) < 0 )
  893. mov( U1, X );
  894. /*... now X == (unknown no.) ulps of 1 -... */
  895. do
  896. {
  897. mov( X, U1 );
  898. /* Y = Half * U1 + ThirtyTwo * U1 * U1;*/
  899. mul( ThirtyTwo, U1, t );
  900. mul( U1, t, t );
  901. mul( Half, U1, Y );
  902. add( t, Y, Y );
  903. sub( Y, Half, Y );
  904. add( Half, Y, X );
  905. sub( X, Half, Y );
  906. add( Half, Y, X );
  907. k = cmp( U1, X );
  908. k2 = cmp( X, Zero );
  909. } while ( ! ((k <= 0) || (k2 <= 0)));
  910. /*... now U1 == 1 ulp of 1 - ... */
  911. if( cmp( U1, E1 ) == 0 )
  912. printf("confirms closest relative separation U1 .\n");
  913. else
  914. {
  915. printf("gets better closest relative separation U1 = " );
  916. show( U1 );
  917. }
  918. div( U1, One, W );
  919. sub( U1, Half, F9 );
  920. add( F9, Half, F9 );
  921. div( U1, U2, t );
  922. div( TwoForty, One, t2 );
  923. add( t2, t, t );
  924. FLOOR( t, Radix );
  925. if( cmp(Radix, E0) == 0 )
  926. printf("Radix confirmed.\n");
  927. else
  928. {
  929. printf("MYSTERY: recalculated Radix = " );
  930. show( Radix );
  931. mov( E0, Radix );
  932. }
  933. add( Eight, Eight, t );
  934. if( cmp( Radix, t ) > 0 )
  935. {
  936. printf( "Radix is too big: roundoff problems\n" );
  937. ErrCnt[Defect] += 1;
  938. }
  939. k = 1;
  940. if( cmp( Radix, Two ) == 0 )
  941. k = 0;
  942. if( cmp( Radix, Ten ) == 0 )
  943. k = 0;
  944. if( cmp( Radix, One ) == 0 )
  945. k = 0;
  946. if( k != 0 )
  947. {
  948. printf( "Radix is not as good as 2 or 10\n" );
  949. ErrCnt[Flaw] += 1;
  950. }
  951. /*=============================================*/
  952. Milestone = 20;
  953. /*=============================================*/
  954. sub( Half, F9, t );
  955. if( cmp( t, Half ) >= 0 )
  956. {
  957. printf( "(1-U1)-1/2 < 1/2 is FALSE, prog. fails?\n" );
  958. ErrCnt[Failure] += 1;
  959. }
  960. mov( F9, X );
  961. I = 1;
  962. sub( Half, X, Y );
  963. sub( Half, Y, Z );
  964. if( (cmp( X, One ) == 0) && (cmp( Z, Zero) != 0) )
  965. {
  966. printf( "Comparison is fuzzy ,X=1 but X-1/2-1/2 != 0\n" );
  967. ErrCnt[Failure] += 1;
  968. }
  969. add( One, U2, X );
  970. I = 0;
  971. /*=============================================*/
  972. Milestone = 25;
  973. /*=============================================*/
  974. /*... BMinusU2 = nextafter(Radix, 0) */
  975. sub( One, Radix, BMinusU2 );
  976. sub( U2, BMinusU2, t );
  977. add( One, t, BMinusU2 );
  978. /* Purify Integers */
  979. if( cmp(Radix,One) != 0 )
  980. {
  981. /*X = - TwoForty * LOG(U1) / LOG(Radix);*/
  982. LOG( U1, X );
  983. LOG( Radix, t );
  984. div( t, X, X );
  985. mul( TwoForty, X, X );
  986. neg( X );
  987. add( Half, X, Y );
  988. FLOOR( Y, Y );
  989. sub( Y, X, t );
  990. FABS( t );
  991. mul( Four, t, t );
  992. if( cmp( t, One ) < 0 )
  993. mov( Y, X );
  994. div( TwoForty, X, Precision );
  995. add( Half, Precision, Y );
  996. FLOOR( Y, Y );
  997. sub( Y, Precision, t );
  998. FABS( t );
  999. mul( TwoForty, t, t );
  1000. if( cmp( t, Half ) < 0 )
  1001. mov( Y, Precision );
  1002. }
  1003. FLOOR( Precision, t );
  1004. if( (cmp( Precision, t ) != 0) || (cmp( Radix, One ) == 0) )
  1005. {
  1006. printf("Precision cannot be characterized by an Integer number\n");
  1007. printf("of significant digits but, by itself, this is a minor flaw.\n");
  1008. }
  1009. if( cmp(Radix, One) == 0 )
  1010. printf("logarithmic encoding has precision characterized solely by U1.\n");
  1011. else
  1012. {
  1013. printf("The number of significant digits of the Radix is " );
  1014. show( Precision );
  1015. }
  1016. mul( U2, Nine, t );
  1017. mul( Nine, t, t );
  1018. mul( TwoForty, t, t );
  1019. if( cmp( t, One ) >= 0 )
  1020. {
  1021. printf( "Precision worse than 5 decimal figures\n" );
  1022. ErrCnt[Serious] += 1;
  1023. }
  1024. /*=============================================*/
  1025. Milestone = 30;
  1026. /*=============================================*/
  1027. /* Test for extra-precise subepressions has been deleted. */
  1028. Milestone = 35;
  1029. /*=============================================*/
  1030. if( cmp(Radix,Two) >= 0 )
  1031. {
  1032. mul( Radix, Radix, t );
  1033. div( t, W, X );
  1034. add( X, One, Y );
  1035. sub( X, Y, Z );
  1036. add( Z, U2, T );
  1037. sub( Z, T, X );
  1038. if( cmp( X, U2 ) != 0 )
  1039. {
  1040. printf( "Subtraction is not normalized X=Y,X+Z != Y+Z!\n" );
  1041. ErrCnt[Failure] += 1;
  1042. }
  1043. if( cmp(X,U2) == 0 )
  1044. printf("Subtraction appears to be normalized, as it should be.");
  1045. }
  1046. printf("\nChecking for guard digit in *, /, and -.\n");
  1047. mul( F9, One, Y );
  1048. mul( One, F9, Z );
  1049. sub( Half, F9, X );
  1050. sub( Half, Y, Y );
  1051. sub( X, Y, Y );
  1052. sub( Half, Z, Z );
  1053. sub( X, Z, Z );
  1054. add( One, U2, X );
  1055. mul( X, Radix, T );
  1056. mul( Radix, X, R );
  1057. sub( Radix, T, X );
  1058. mul( Radix, U2, t );
  1059. sub( t, X, X );
  1060. sub( Radix, R, T );
  1061. mul( Radix, U2, t );
  1062. sub( t, T, T );
  1063. sub( One, Radix, t );
  1064. mul( t, X, X );
  1065. sub( One, Radix, t );
  1066. mul( t, T, T );
  1067. k = cmp(X,Zero);
  1068. k |= cmp(Y,Zero);
  1069. k |= cmp(Z,Zero);
  1070. k |= cmp(T,Zero);
  1071. if( k == 0 )
  1072. GMult = Yes;
  1073. else
  1074. {
  1075. GMult = No;
  1076. ErrCnt[Serious] += 1;
  1077. printf( "* lacks a Guard Digit, so 1*X != X\n" );
  1078. }
  1079. mul( Radix, U2, Z );
  1080. add( One, Z, X );
  1081. add( X, Z, Y );
  1082. mul( X, X, t );
  1083. sub( t, Y, Y );
  1084. FABS( Y );
  1085. sub( U2, Y, Y );
  1086. sub( U2, One, X );
  1087. sub( U2, X, Z );
  1088. mul( X, X, t );
  1089. sub( t, Z, Z );
  1090. FABS( Z );
  1091. sub( U1, Z, Z );
  1092. if( (cmp(Y,Zero) > 0) || (cmp(Z,Zero) > 0) )
  1093. {
  1094. ErrCnt[Failure] += 1;
  1095. printf( "* gets too many final digits wrong.\n" );
  1096. }
  1097. sub( U2, One, Y );
  1098. add( One, U2, X );
  1099. div( Y, One, Z );
  1100. sub( X, Z, Y );
  1101. div( Three, One, X );
  1102. div( Nine, Three, Z );
  1103. sub( Z, X, X );
  1104. div( TwentySeven, Nine, T );
  1105. sub( T, Z, Z );
  1106. k = cmp( X, Zero );
  1107. k |= cmp( Y, Zero );
  1108. k |= cmp( Z, Zero );
  1109. if( k )
  1110. {
  1111. ErrCnt[Defect] += 1;
  1112. printf( "Division lacks a Guard Digit, so error can exceed 1 ulp\n" );
  1113. printf( "or 1/3 and 3/9 and 9/27 may disagree\n" );
  1114. }
  1115. div( One, F9, Y );
  1116. sub( Half, F9, X );
  1117. sub( Half, Y, Y );
  1118. sub( X, Y, Y );
  1119. add( One, U2, X );
  1120. div( One, X, T );
  1121. sub( X, T, X );
  1122. k = cmp( X, Zero );
  1123. k |= cmp( Y, Zero );
  1124. k |= cmp( Z, Zero );
  1125. if( k == 0 )
  1126. GDiv = Yes;
  1127. else
  1128. {
  1129. GDiv = No;
  1130. ErrCnt[Serious] += 1;
  1131. printf( "Division lacks a Guard Digit, so X/1 != X\n" );
  1132. }
  1133. add( One, U2, X );
  1134. div( X, One, X );
  1135. sub( Half, X, Y );
  1136. sub( Half, Y, Y );
  1137. if( cmp(Y,Zero) >= 0 )
  1138. {
  1139. ErrCnt[Serious] += 1;
  1140. printf( "Computed value of 1/1.000..1 >= 1\n" );
  1141. }
  1142. sub( U2, One, X );
  1143. mul( Radix, U2, Y );
  1144. add( One, Y, Y );
  1145. mul( X, Radix, Z );
  1146. mul( Y, Radix, T );
  1147. div( Radix, Z, R );
  1148. div( Radix, T, StickyBit );
  1149. sub( X, R, X );
  1150. sub( Y, StickyBit, Y );
  1151. k = cmp( X, Zero );
  1152. k |= cmp( Y, Zero );
  1153. if( k )
  1154. {
  1155. ErrCnt[Failure] += 1;
  1156. printf( "* and/or / gets too many last digits wrong\n" );
  1157. }
  1158. sub( U1, One, Y );
  1159. sub( F9, One, X );
  1160. sub( Y, One, Y );
  1161. sub( U2, Radix, T );
  1162. sub( BMinusU2, Radix, Z );
  1163. sub( T, Radix, T );
  1164. k = cmp( X, U1 );
  1165. k |= cmp( Y, U1 );
  1166. k |= cmp( Z, U2 );
  1167. k |= cmp( T, U2 );
  1168. if( k == 0 )
  1169. GAddSub = Yes;
  1170. else
  1171. {
  1172. GAddSub = No;
  1173. ErrCnt[Serious] += 1;
  1174. printf( "- lacks Guard Digit, so cancellation is obscured\n" );
  1175. }
  1176. sub( One, F9, t );
  1177. if( (cmp(F9,One) != 0) && (cmp(t,Zero) >= 0) )
  1178. {
  1179. ErrCnt[Serious] += 1;
  1180. printf("comparison alleges (1-U1) < 1 although\n");
  1181. printf(" subtration yields (1-U1) - 1 = 0 , thereby vitiating\n");
  1182. printf(" such precautions against division by zero as\n");
  1183. printf(" ... if (X == 1.0) {.....} else {.../(X-1.0)...}\n");
  1184. }
  1185. if (GMult == Yes && GDiv == Yes && GAddSub == Yes)
  1186. printf(" *, /, and - appear to have guard digits, as they should.\n");
  1187. /*=============================================*/
  1188. Milestone = 40;
  1189. /*=============================================*/
  1190. printf("Checking rounding on multiply, divide and add/subtract.\n");
  1191. RMult = Other;
  1192. RDiv = Other;
  1193. RAddSub = Other;
  1194. div( Two, Radix, RadixD2 );
  1195. mov( Two, A1 );
  1196. Done = False;
  1197. do
  1198. {
  1199. mov( Radix, AInvrse );
  1200. do
  1201. {
  1202. mov( AInvrse, X );
  1203. div( A1, AInvrse, AInvrse );
  1204. FLOOR( AInvrse, t );
  1205. k = cmp( t, AInvrse );
  1206. }
  1207. while( ! (k != 0 ) );
  1208. k = cmp( X, One );
  1209. k2 = cmp( A1, Three );
  1210. Done = (k == 0) || (k2 > 0);
  1211. if(! Done)
  1212. add( Nine, One, A1 );
  1213. }
  1214. while( ! (Done));
  1215. if( cmp(X, One) == 0 )
  1216. mov( Radix, A1 );
  1217. div( A1, One, AInvrse );
  1218. mov( A1, X );
  1219. mov( AInvrse, Y );
  1220. Done = False;
  1221. do
  1222. {
  1223. mul( X, Y, Z );
  1224. sub( Half, Z, Z );
  1225. if( cmp( Z, Half ) != 0 )
  1226. {
  1227. ErrCnt[Failure] += 1;
  1228. printf( "X * (1/X) differs from 1\n" );
  1229. }
  1230. k = cmp( X, Radix );
  1231. Done = (k == 0);
  1232. mov( Radix, X );
  1233. div( X, One, Y );
  1234. }
  1235. while( ! (Done));
  1236. add( One, U2, Y2 );
  1237. sub( U2, One, YY1 );
  1238. sub( U2, OneAndHalf, X );
  1239. add( OneAndHalf, U2, Y );
  1240. sub( U2, X, Z );
  1241. mul( Z, Y2, Z );
  1242. mul( Y, YY1, T );
  1243. sub( X, Z, Z );
  1244. sub( X, T, T );
  1245. mul( X, Y2, X );
  1246. add( Y, U2, Y );
  1247. mul( Y, YY1, Y );
  1248. sub( OneAndHalf, X, X );
  1249. sub( OneAndHalf, Y, Y );
  1250. k = cmp( X, Zero );
  1251. k |= cmp( Y, Zero );
  1252. k |= cmp( Z, Zero );
  1253. if( cmp( T, Zero ) > 0 )
  1254. k = 1;
  1255. if( k == 0 )
  1256. {
  1257. add( OneAndHalf, U2, X );
  1258. mul( X, Y2, X );
  1259. sub( U2, OneAndHalf, Y );
  1260. sub( U2, Y, Y );
  1261. add( OneAndHalf, U2, Z );
  1262. add( U2, Z, Z );
  1263. sub( U2, OneAndHalf, T );
  1264. mul( T, YY1, T );
  1265. add( Z, U2, t );
  1266. sub( t, X, X );
  1267. mul( Y, YY1, StickyBit );
  1268. mul( Z, Y2, S );
  1269. sub( Y, T, T );
  1270. sub( Y, U2, Y );
  1271. add( StickyBit, Y, Y );
  1272. /* Z = S - (Z + U2 + U2); */
  1273. add( Z, U2, t );
  1274. add( t, U2, t );
  1275. sub( t, S, Z );
  1276. add( Y2, U2, t );
  1277. mul( t, YY1, StickyBit );
  1278. mul( Y2, YY1, YY1 );
  1279. sub( Y2, StickyBit, StickyBit );
  1280. sub( Half, YY1, YY1 );
  1281. k = cmp( X, Zero );
  1282. k |= cmp( Y, Zero );
  1283. k |= cmp( Z, Zero );
  1284. k |= cmp( T, Zero );
  1285. k |= cmp( StickyBit, Zero );
  1286. k |= cmp( YY1, Half );
  1287. if( k == 0 )
  1288. {
  1289. RMult = Rounded;
  1290. printf("Multiplication appears to round correctly.\n");
  1291. }
  1292. else
  1293. {
  1294. add( X, U2, t );
  1295. k = cmp( t, Zero );
  1296. if( cmp( Y, Zero ) >= 0 )
  1297. k |= 1;
  1298. add( Z, U2, t );
  1299. k |= cmp( t, Zero );
  1300. if( cmp( T, Zero ) >= 0 )
  1301. k |= 1;
  1302. add( StickyBit, U2, t );
  1303. k |= cmp( t, Zero );
  1304. if( cmp(YY1, Half) >= 0 )
  1305. k |= 1;
  1306. if( k == 0 )
  1307. {
  1308. printf("Multiplication appears to chop.\n");
  1309. }
  1310. else
  1311. {
  1312. printf("* is neither chopped nor correctly rounded.\n");
  1313. }
  1314. if( (RMult == Rounded) && (GMult == No) )
  1315. printf("Multiplication has inconsistent result");
  1316. }
  1317. }
  1318. else
  1319. printf("* is neither chopped nor correctly rounded.\n");
  1320. /*=============================================*/
  1321. Milestone = 45;
  1322. /*=============================================*/
  1323. add( One, U2, Y2 );
  1324. sub( U2, One, YY1 );
  1325. add( OneAndHalf, U2, Z );
  1326. add( Z, U2, Z );
  1327. div( Y2, Z, X );
  1328. sub( U2, OneAndHalf, T );
  1329. sub( U2, T, T );
  1330. sub( U2, T, Y );
  1331. div( YY1, Y, Y );
  1332. add( Z, U2, Z );
  1333. div( Y2, Z, Z );
  1334. sub( OneAndHalf, X, X );
  1335. sub( T, Y, Y );
  1336. div( YY1, T, T );
  1337. add( OneAndHalf, U2, t );
  1338. sub( t, Z, Z );
  1339. sub( OneAndHalf, U2, t );
  1340. add( t, T, T );
  1341. k = 0;
  1342. if( cmp( X, Zero ) > 0 )
  1343. k = 1;
  1344. if( cmp( Y, Zero ) > 0 )
  1345. k = 1;
  1346. if( cmp( Z, Zero ) > 0 )
  1347. k = 1;
  1348. if( cmp( T, Zero ) > 0 )
  1349. k = 1;
  1350. if( k == 0 )
  1351. {
  1352. div( Y2, OneAndHalf, X );
  1353. sub( U2, OneAndHalf, Y );
  1354. add( U2, OneAndHalf, Z );
  1355. sub( Y, X, X );
  1356. div( YY1, OneAndHalf, T );
  1357. div( YY1, Y, Y );
  1358. add( Z, U2, t );
  1359. sub( t, T, T );
  1360. sub( Z, Y, Y );
  1361. div( Y2, Z, Z );
  1362. add( Y2, U2, YY1 );
  1363. div( Y2, YY1, YY1 );
  1364. sub( OneAndHalf, Z, Z );
  1365. sub( Y2, YY1, Y2 );
  1366. sub( U1, F9, YY1 );
  1367. div( F9, YY1, YY1 );
  1368. k = cmp( X, Zero );
  1369. k |= cmp( Y, Zero );
  1370. k |= cmp( Z, Zero );
  1371. k |= cmp( T, Zero );
  1372. k |= cmp( Y2, Zero );
  1373. sub( Half, YY1, t );
  1374. sub( Half, F9, t2 );
  1375. k |= cmp( t, t2 );
  1376. if( k == 0 )
  1377. {
  1378. RDiv = Rounded;
  1379. printf("Division appears to round correctly.\n");
  1380. if(GDiv == No)
  1381. printf("Division test inconsistent\n");
  1382. }
  1383. else
  1384. {
  1385. k = 0;
  1386. if( cmp( X, Zero ) >= 0 )
  1387. k = 1;
  1388. if( cmp( Y, Zero ) >= 0 )
  1389. k = 1;
  1390. if( cmp( Z, Zero ) >= 0 )
  1391. k = 1;
  1392. if( cmp( T, Zero ) >= 0 )
  1393. k = 1;
  1394. if( cmp( Y, Zero ) >= 0 )
  1395. k = 1;
  1396. sub( Half, YY1, t );
  1397. sub( Half, F9, t2 );
  1398. if( cmp( t, t2 ) >= 0 )
  1399. k = 1;
  1400. if( k == 0 )
  1401. {
  1402. RDiv = Chopped;
  1403. printf("Division appears to chop.\n");
  1404. }
  1405. }
  1406. }
  1407. if(RDiv == Other)
  1408. printf("/ is neither chopped nor correctly rounded.\n");
  1409. div( Radix, One, BInvrse );
  1410. mul( BInvrse, Radix, t );
  1411. sub( Half, t, t );
  1412. if( cmp( t, Half ) != 0 )
  1413. {
  1414. ErrCnt[Failure] += 1;
  1415. printf( "Radix * ( 1 / Radix ) differs from 1\n" );
  1416. }
  1417. Milestone = 50;
  1418. /*=============================================*/
  1419. add( F9, U1, t );
  1420. sub( Half, t, t );
  1421. k = cmp( t, Half );
  1422. add( BMinusU2, U2, t );
  1423. sub( One, t, t );
  1424. sub( One, Radix, t2 );
  1425. k |= cmp( t, t2 );
  1426. if( k != 0 )
  1427. {
  1428. ErrCnt[Failure] += 1;
  1429. printf( "Incomplete carry-propagation in Addition\n" );
  1430. }
  1431. mul( U1, U1, X );
  1432. sub( X, One, X );
  1433. sub( U2, One, Y );
  1434. mul( U2, Y, Y );
  1435. add( One, Y, Y );
  1436. sub( Half, F9, Z );
  1437. sub( Half, X, X );
  1438. sub( Z, X, X );
  1439. sub( One, Y, Y );
  1440. if( (cmp(X,Zero) == 0) && (cmp(Y,Zero) == 0) )
  1441. {
  1442. RAddSub = Chopped;
  1443. printf("Add/Subtract appears to be chopped.\n");
  1444. }
  1445. if(GAddSub == Yes)
  1446. {
  1447. add( Half, U2, X );
  1448. mul( X, U2, X );
  1449. sub( U2, Half, Y );
  1450. mul( Y, U2, Y );
  1451. add( One, X, X );
  1452. add( One, Y, Y );
  1453. add( One, U2, t );
  1454. sub( X, t, X );
  1455. sub( Y, One, Y );
  1456. k = cmp(X,Zero);
  1457. if( k )
  1458. printf( "1+U2-[u2(1/2+U2)+1] != 0\n" );
  1459. k2 = cmp(Y,Zero);
  1460. if( k2 )
  1461. printf( "1-[U2(1/2-U2)+1] != 0\n" );
  1462. k |= k2;
  1463. if( k == 0 )
  1464. {
  1465. add( Half, U2, X );
  1466. mul( X, U1, X );
  1467. sub( U2, Half, Y );
  1468. mul( Y, U1, Y );
  1469. sub( X, One, X );
  1470. sub( Y, One, Y );
  1471. sub( X, F9, X );
  1472. sub( Y, One, Y );
  1473. k = cmp(X,Zero);
  1474. if( k )
  1475. printf( "F9-[1-U1(1/2+U2)] != 0\n" );
  1476. k2 = cmp(Y,Zero);
  1477. if( k2 )
  1478. printf( "1-[1-U1(1/2-U2)] != 0\n" );
  1479. k |= k2;
  1480. if( k == 0 )
  1481. {
  1482. RAddSub = Rounded;
  1483. printf("Addition/Subtraction appears to round correctly.\n");
  1484. if(GAddSub == No)
  1485. printf( "Add/Subtract test inconsistent\n");
  1486. }
  1487. else
  1488. {
  1489. printf("Addition/Subtraction neither rounds nor chops.\n");
  1490. }
  1491. }
  1492. else
  1493. printf("Addition/Subtraction neither rounds nor chops.\n");
  1494. }
  1495. else
  1496. printf("Addition/Subtraction neither rounds nor chops.\n");
  1497. mov( One, S );
  1498. add( One, Half, X );
  1499. mul( Half, X, X );
  1500. add( One, X, X );
  1501. add( One, U2, Y );
  1502. mul( Y, Half, Y );
  1503. sub( Y, X, Z );
  1504. sub( X, Y, T );
  1505. add( Z, T, StickyBit );
  1506. if( cmp(StickyBit, Zero) != 0 )
  1507. {
  1508. mov( Zero, S );
  1509. ErrCnt[Flaw] += 1;
  1510. printf( "(X - Y) + (Y - X) is non zero!\n" );
  1511. }
  1512. mov( Zero, StickyBit );
  1513. FLOOR( RadixD2, t );
  1514. k2 = cmp( t, RadixD2 );
  1515. k = 1;
  1516. if( (GMult == Yes) && (GDiv == Yes) && (GAddSub == Yes)
  1517. && (RMult == Rounded) && (RDiv == Rounded)
  1518. && (RAddSub == Rounded) && (k2 == 0) )
  1519. {
  1520. printf("Checking for sticky bit.\n");
  1521. k = 0;
  1522. add( Half, U1, X );
  1523. mul( X, U2, X );
  1524. mul( Half, U2, Y );
  1525. add( One, Y, Z );
  1526. add( One, X, T );
  1527. sub( One, Z, t );
  1528. sub( One, T, t2 );
  1529. if( cmp(t,Zero) > 0 )
  1530. {
  1531. k = 1;
  1532. printf( "[1+(1/2)U2]-1 > 0\n" );
  1533. }
  1534. if( cmp(t2,U2) < 0 )
  1535. {
  1536. k = 1;
  1537. printf( "[1+U2(1/2+U1)]-1 < U2\n" );
  1538. }
  1539. add( T, Y, Z );
  1540. sub( X, Z, Y );
  1541. sub( T, Z, t );
  1542. sub( T, Y, t2 );
  1543. if( cmp(t,U2) < 0 )
  1544. {
  1545. k = 1;
  1546. printf( "[[1+U2(1/2+U1)]+(1/2)U2]-[1+U2(1/2+U1)] < U2\n" );
  1547. }
  1548. if( cmp(t2,Zero) != 0 )
  1549. {
  1550. k = 1;
  1551. printf( "(1/2)U2-[1+U2(1/2+U1)] != 0\n" );
  1552. }
  1553. add( Half, U1, X );
  1554. mul( X, U1, X );
  1555. mul( Half, U1, Y );
  1556. sub( Y, One, Z );
  1557. sub( X, One, T );
  1558. sub( One, Z, t );
  1559. sub( F9, T, t2 );
  1560. if( cmp(t,Zero) != 0 )
  1561. {
  1562. k = 1;
  1563. printf( "(1-(1/2)U1)-1 != 0\n" );
  1564. }
  1565. if( cmp(t2,Zero) != 0 )
  1566. {
  1567. k = 1;
  1568. printf( "[1-U1(1/2+U1)]-F9 != 0\n" );
  1569. }
  1570. sub( U1, Half, Z );
  1571. mul( Z, U1, Z );
  1572. sub( Z, F9, T );
  1573. sub( Y, F9, Q );
  1574. sub( F9, T, t );
  1575. if( cmp( t, Zero ) != 0 )
  1576. {
  1577. k = 1;
  1578. printf( "[F9-U1(1/2-U1)]-F9 != 0\n" );
  1579. }
  1580. sub( U1, F9, t );
  1581. sub( Q, t, t );
  1582. if( cmp( t, Zero ) != 0 )
  1583. {
  1584. k = 1;
  1585. printf( "(F9-U1)-(F9-(1/2)U1) != 0\n" );
  1586. }
  1587. add( One, U2, Z );
  1588. mul( Z, OneAndHalf, Z );
  1589. add( OneAndHalf, U2, T );
  1590. sub( Z, T, T );
  1591. add( U2, T, T );
  1592. div( Radix, Half, X );
  1593. add( One, X, X );
  1594. mul( Radix, U2, Y );
  1595. add( One, Y, Y );
  1596. mul( X, Y, Z );
  1597. if( cmp( T, Zero ) != 0 )
  1598. {
  1599. k = 1;
  1600. printf( "(3/2+U2)-3/2(1+U2)+U2 != 0\n" );
  1601. }
  1602. mul( Radix, U2, t );
  1603. add( X, t, t );
  1604. sub( Z, t, t );
  1605. if( cmp( t, Zero ) != 0 )
  1606. {
  1607. k = 1;
  1608. printf( "(1+1/2Radix)+Radix*U2-[1+1/(2Radix)][1+Radix*U2] != 0\n" );
  1609. }
  1610. if( cmp(Radix, Two) != 0 )
  1611. {
  1612. add( Two, U2, X );
  1613. div( Two, X, Y );
  1614. sub( One, Y, t );
  1615. if( cmp( t, Zero) != 0 )
  1616. k = 1;
  1617. }
  1618. }
  1619. if( k == 0 )
  1620. {
  1621. printf("Sticky bit apparently used correctly.\n");
  1622. mov( One, StickyBit );
  1623. }
  1624. else
  1625. {
  1626. printf("Sticky bit used incorrectly or not at all.\n");
  1627. }
  1628. if( GMult == No || GDiv == No || GAddSub == No ||
  1629. RMult == Other || RDiv == Other || RAddSub == Other)
  1630. {
  1631. ErrCnt[Flaw] += 1;
  1632. printf("lack(s) of guard digits or failure(s) to correctly round or chop\n");
  1633. printf( "(noted above) count as one flaw in the final tally below\n" );
  1634. }
  1635. /*=============================================*/
  1636. Milestone = 60;
  1637. /*=============================================*/
  1638. printf("\n");
  1639. printf("Does Multiplication commute? ");
  1640. printf("Testing on %d random pairs.\n", NoTrials);
  1641. SQRT( Three, Random9 );
  1642. mov( Third, Random1 );
  1643. I = 1;
  1644. do
  1645. {
  1646. Random();
  1647. mov( Random1, X );
  1648. Random();
  1649. mov( Random1, Y );
  1650. mul( Y, X, Z9 );
  1651. mul( X, Y, Z );
  1652. sub( Z9, Z, Z9 );
  1653. I = I + 1;
  1654. }
  1655. while ( ! ((I > NoTrials) || (cmp(Z9,Zero) != 0)));
  1656. if(I == NoTrials)
  1657. {
  1658. div( Three, Half, t );
  1659. add( One, t, Random1 );
  1660. add( U2, U1, t );
  1661. add( t, One, Random2 );
  1662. mul( Random1, Random2, Z );
  1663. mul( Random2, Random1, Y );
  1664. /* Z9 = (One + Half / Three) * ((U2 + U1) + One) - (One + Half /
  1665. * Three) * ((U2 + U1) + One);
  1666. */
  1667. div( Three, Half, t2 );
  1668. add( One, t2, t2 );
  1669. add( U2, U1, t );
  1670. add( t, One, t );
  1671. mul( t2, t, Z9 );
  1672. mul( t2, t, t );
  1673. sub( t, Z9, Z9 );
  1674. }
  1675. if(! ((I == NoTrials) || (cmp(Z9,Zero) == 0)))
  1676. {
  1677. ErrCnt[Defect] += 1;
  1678. printf( "X * Y == Y * X trial fails.\n");
  1679. }
  1680. else
  1681. {
  1682. printf(" No failures found in %d integer pairs.\n", NoTrials);
  1683. }
  1684. /*=============================================*/
  1685. Milestone = 70;
  1686. /*=============================================*/
  1687. sqtest();
  1688. Milestone = 90;
  1689. pow1test();
  1690. Milestone = 110;
  1691. printf("Seeking Underflow thresholds UfThold and E0.\n");
  1692. mov( U1, D );
  1693. FLOOR( Precision, t );
  1694. if( cmp(Precision, t) != 0 )
  1695. {
  1696. mov( BInvrse, D );
  1697. mov( Precision, X );
  1698. do
  1699. {
  1700. mul( D, BInvrse, D );
  1701. sub( One, X, X );
  1702. }
  1703. while( cmp(X, Zero) > 0 );
  1704. }
  1705. mov( One, Y );
  1706. mov( D, Z );
  1707. /* ... D is power of 1/Radix < 1. */
  1708. sigsave = sigfpe;
  1709. if( setjmp(ovfl_buf) )
  1710. goto under0;
  1711. do
  1712. {
  1713. mov( Y, C );
  1714. mov( Z, Y );
  1715. mul( Y, Y, Z );
  1716. add( Z, Z, t );
  1717. }
  1718. while( (cmp(Y,Z) > 0) && (cmp(t,Z) > 0) );
  1719. under0:
  1720. sigsave = 0;
  1721. mov( C, Y );
  1722. mul( Y, D, Z );
  1723. sigsave = sigfpe;
  1724. if( setjmp(ovfl_buf) )
  1725. goto under1;
  1726. do
  1727. {
  1728. mov( Y, C );
  1729. mov( Z, Y );
  1730. mul( Y, D, Z );
  1731. add( Z, Z, t );
  1732. }
  1733. while( (cmp(Y,Z) > 0) && (cmp(t,Z) > 0) );
  1734. under1:
  1735. sigsave = 0;
  1736. if( cmp(Radix,Two) < 0 )
  1737. mov( Two, HInvrse );
  1738. else
  1739. mov( Radix, HInvrse );
  1740. div( HInvrse, One, H );
  1741. /* ... 1/HInvrse == H == Min(1/Radix, 1/2) */
  1742. div( C, One, CInvrse );
  1743. mov( C, E0 );
  1744. mul( E0, H, Z );
  1745. /* ...1/Radix^(BIG Integer) << 1 << CInvrse == 1/C */
  1746. sigsave = sigfpe;
  1747. if( setjmp(ovfl_buf) )
  1748. goto under2;
  1749. do
  1750. {
  1751. mov( E0, Y );
  1752. mov( Z, E0 );
  1753. mul( E0, H, Z );
  1754. add( Z, Z, t );
  1755. }
  1756. while( (cmp(E0,Z) > 0) && (cmp(t,Z) > 0) );
  1757. under2:
  1758. sigsave = 0;
  1759. mov( E0, UfThold );
  1760. mov( Zero, E1 );
  1761. mov( Zero, Q );
  1762. mov( U2, E9 );
  1763. add( One, E9, S );
  1764. mul( C, S, D );
  1765. if( cmp(D,C) <= 0 )
  1766. {
  1767. mul( Radix, U2, E9 );
  1768. add( One, E9, S );
  1769. mul( C, S, D );
  1770. if( cmp(D, C) <= 0 )
  1771. {
  1772. ErrCnt[Failure] += 1;
  1773. printf( "multiplication gets too many last digits wrong.\n" );
  1774. mov( E0, Underflow );
  1775. mov( Zero, YY1 );
  1776. mov( Z, PseudoZero );
  1777. }
  1778. }
  1779. else
  1780. {
  1781. mov( D, Underflow );
  1782. mul( Underflow, H, PseudoZero );
  1783. mov( Zero, UfThold );
  1784. do
  1785. {
  1786. mov( Underflow, YY1 );
  1787. mov( PseudoZero, Underflow );
  1788. add( E1, E1, t );
  1789. if( cmp(t, E1) <= 0)
  1790. {
  1791. mul( Underflow, HInvrse, Y2 );
  1792. sub( Y2, YY1, E1 );
  1793. FABS( E1 );
  1794. mov( YY1, Q );
  1795. if( (cmp( UfThold, Zero ) == 0)
  1796. && (cmp(YY1, Y2) != 0) )
  1797. mov( YY1, UfThold );
  1798. }
  1799. mul( PseudoZero, H, PseudoZero );
  1800. add( PseudoZero, PseudoZero, t );
  1801. }
  1802. while( (cmp(Underflow, PseudoZero) > 0)
  1803. && (cmp(t, PseudoZero) > 0) );
  1804. }
  1805. /* Comment line 4530 .. 4560 */
  1806. if( cmp(PseudoZero, Zero) != 0 )
  1807. {
  1808. printf("\n");
  1809. mov(PseudoZero, Z );
  1810. /* ... Test PseudoZero for "phoney- zero" violates */
  1811. /* ... PseudoZero < Underflow or PseudoZero < PseudoZero + PseudoZero
  1812. ... */
  1813. if( cmp(PseudoZero, Zero) <= 0 )
  1814. {
  1815. ErrCnt[Failure] += 1;
  1816. printf("Positive expressions can underflow to an\n");
  1817. printf("allegedly negative value\n");
  1818. printf("PseudoZero that prints out as: " );
  1819. show( PseudoZero );
  1820. mov( PseudoZero, X );
  1821. neg( X );
  1822. if( cmp(X, Zero) <= 0 )
  1823. {
  1824. printf("But -PseudoZero, which should be\n");
  1825. printf("positive, isn't; it prints out as " );
  1826. show( X );
  1827. }
  1828. }
  1829. else
  1830. {
  1831. ErrCnt[Flaw] += 1;
  1832. printf( "Underflow can stick at an allegedly positive\n");
  1833. printf("value PseudoZero that prints out as " );
  1834. show( PseudoZero );
  1835. }
  1836. /* TstPtUf();*/
  1837. }
  1838. /*=============================================*/
  1839. Milestone = 120;
  1840. /*=============================================*/
  1841. mul( CInvrse, Y, t );
  1842. mul( CInvrse, YY1, t2 );
  1843. if( cmp(t,t2) > 0 )
  1844. {
  1845. mul( H, S, S );
  1846. mov( Underflow, E0 );
  1847. }
  1848. if(! ((cmp(E1,Zero) == 0) || (cmp(E1,E0) == 0)) )
  1849. {
  1850. ErrCnt[Defect] += 1;
  1851. if( cmp(E1,E0) < 0 )
  1852. {
  1853. printf("Products underflow at a higher");
  1854. printf(" threshold than differences.\n");
  1855. if( cmp(PseudoZero,Zero) == 0 )
  1856. mov( E1, E0 );
  1857. }
  1858. else
  1859. {
  1860. printf("Difference underflows at a higher");
  1861. printf(" threshold than products.\n");
  1862. }
  1863. }
  1864. printf("Smallest strictly positive number found is E0 = " );
  1865. show( E0 );
  1866. mov( E0, Z );
  1867. TstPtUf();
  1868. mov( E0, Underflow );
  1869. if(N == 1)
  1870. mov( Y, Underflow );
  1871. I = 4;
  1872. if( cmp(E1,Zero) == 0 )
  1873. I = 3;
  1874. if( cmp( UfThold,Zero) == 0 )
  1875. I = I - 2;
  1876. UfNGrad = True;
  1877. switch(I)
  1878. {
  1879. case 1:
  1880. mov( Underflow, UfThold );
  1881. mul( CInvrse, Q, t );
  1882. mul( CInvrse, Y, t2 );
  1883. mul( t2, S, t2 );
  1884. if( cmp( t, t2 ) != 0 )
  1885. {
  1886. mov( Y, UfThold );
  1887. ErrCnt[Failure] += 1;
  1888. printf( "Either accuracy deteriorates as numbers\n");
  1889. printf("approach a threshold = " );
  1890. show( UfThold );
  1891. printf(" coming down from " );
  1892. show( C );
  1893. printf(" or else multiplication gets too many last digits wrong.\n");
  1894. }
  1895. break;
  1896. case 2:
  1897. ErrCnt[Failure] += 1;
  1898. printf( "Underflow confuses Comparison which alleges that\n");
  1899. printf("Q == Y while denying that |Q - Y| == 0; these values\n");
  1900. printf("print out as Q = " );
  1901. show( Q );
  1902. printf( ", Y = " );
  1903. show( Y );
  1904. sub( Y2, Q, t );
  1905. FABS(t);
  1906. printf ("|Q - Y| = " );
  1907. show( t );
  1908. mov( Q, UfThold );
  1909. break;
  1910. case 3:
  1911. mov( X, X );
  1912. break;
  1913. case 4:
  1914. div( E9, E1, t );
  1915. sub( t, UfThold, t );
  1916. FABS(t);
  1917. if( (cmp(Q,UfThold) == 0) && (cmp(E1,E0) == 0)
  1918. && (cmp(t,E1) <= 0) )
  1919. {
  1920. UfNGrad = False;
  1921. printf("Underflow is gradual; it incurs Absolute Error =\n");
  1922. printf("(roundoff in UfThold) < E0.\n");
  1923. mul( E0, CInvrse, Y );
  1924. add( OneAndHalf, U2, t );
  1925. mul( Y, t, Y );
  1926. add( One, U2, X );
  1927. mul( CInvrse, X, X );
  1928. div( X, Y, t );
  1929. IEEE = (cmp(t,E0) == 0);
  1930. if( IEEE == 0 )
  1931. {
  1932. printf( "((CInvrse E0) (1.5+U2)) / (CInvrse (1+U2)) != E0\n" );
  1933. printf( "CInvrse = " );
  1934. show( CInvrse );
  1935. printf( "E0 = " );
  1936. show( E0 );
  1937. printf( "U2 = " );
  1938. show( U2 );
  1939. printf( "X = " );
  1940. show(X);
  1941. printf( "Y = " );
  1942. show(Y);
  1943. printf( "Y/X = " );
  1944. show(t);
  1945. }
  1946. }
  1947. }
  1948. if(UfNGrad)
  1949. {
  1950. printf("\n");
  1951. div( UfThold, Underflow, R );
  1952. SQRT( R, R );
  1953. if( cmp(R,H) <= 0)
  1954. {
  1955. mul( R, UfThold, Z );
  1956. /* X = Z * (One + R * H * (One + H));*/
  1957. add( One, H, X );
  1958. mul( H, X, X );
  1959. mul( R, X, X );
  1960. add( One, X, X );
  1961. mul( Z, X, X );
  1962. }
  1963. else
  1964. {
  1965. mov( UfThold, Z );
  1966. /*X = Z * (One + H * H * (One + H));*/
  1967. add( One, H, X );
  1968. mul( H, X, X );
  1969. mul( H, X, X );
  1970. add( One, X, X );
  1971. mul( Z, X, X );
  1972. }
  1973. sub( Z, X, t );
  1974. /* if(! ((cmp(X,Z) == 0) || (cmp(t,Zero) != 0)) )*/
  1975. if( (cmp(X,Z) != 0) && (cmp(t,Zero) == 0) )
  1976. {
  1977. /* ErrCnt[Flaw] += 1;*/
  1978. ErrCnt[Serious] += 1;
  1979. printf("X = " );
  1980. show( X );
  1981. printf( "\tis not equal to Z = " );
  1982. show( Z );
  1983. /* sub( Z, X, Z9 );*/
  1984. printf("yet X - Z yields " );
  1985. show( t );
  1986. printf("which compares equal to " );
  1987. show( Zero );
  1988. printf(" Should this NOT signal Underflow, ");
  1989. printf("this is a SERIOUS DEFECT\nthat causes ");
  1990. printf("confusion when innocent statements like\n");;
  1991. printf(" if (X == Z) ... else");
  1992. printf(" ... (f(X) - f(Z)) / (X - Z) ...\n");
  1993. printf("encounter Division by Zero although actually\n");
  1994. printf("X / Z = 1 + " );
  1995. div( Z, X, t );
  1996. sub( Half, t, t );
  1997. sub( Half, t, t );
  1998. show(t);
  1999. }
  2000. }
  2001. printf("The Underflow threshold is " );
  2002. show( UfThold );
  2003. printf( "below which calculation may suffer larger Relative error than" );
  2004. printf( " merely roundoff.\n");
  2005. mul( U1, U1, Y2 );
  2006. mul( Y2, Y2, Y );
  2007. mul( Y, U1, Y2 );
  2008. if( cmp( Y2,UfThold) <= 0 )
  2009. {
  2010. if( cmp(Y,E0) > 0 )
  2011. {
  2012. ErrCnt[Defect] += 1;
  2013. I = 5;
  2014. }
  2015. else
  2016. {
  2017. ErrCnt[Serious] += 1;
  2018. I = 4;
  2019. }
  2020. printf("Range is too narrow; U1^%d Underflows.\n", I);
  2021. }
  2022. Milestone = 130;
  2023. /*Y = - FLOOR(Half - TwoForty * LOG(UfThold) / LOG(HInvrse)) / TwoForty;*/
  2024. LOG( UfThold, Y );
  2025. LOG( HInvrse, t );
  2026. div( t, Y, Y );
  2027. mul( TwoForty, Y, Y );
  2028. sub( Y, Half, Y );
  2029. FLOOR( Y, Y );
  2030. div( TwoForty, Y, Y );
  2031. neg(Y);
  2032. sub( One, Y, Y2 ); /* ***** changed from Y2 = Y + Y */
  2033. printf("Since underflow occurs below the threshold\n");
  2034. printf("UfThold = " );
  2035. show( HInvrse );
  2036. printf( "\tto the power " );
  2037. show( Y );
  2038. printf( "only underflow should afflict the expression " );
  2039. show( HInvrse );
  2040. printf( "\tto the power " );
  2041. show( Y2 );
  2042. POW( HInvrse, Y2, V9 );
  2043. printf("Actually calculating yields: " );
  2044. show( V9 );
  2045. add( Radix, Radix, t );
  2046. add( t, E9, t );
  2047. mul( t, UfThold, t );
  2048. if( (cmp(V9,Zero) < 0) || (cmp(V9,t) > 0) )
  2049. {
  2050. ErrCnt[Serious] += 1;
  2051. printf( "this is not between 0 and underflow\n");
  2052. printf(" threshold = " );
  2053. show( UfThold );
  2054. }
  2055. else
  2056. {
  2057. add( One, E9, t );
  2058. mul( UfThold, t, t );
  2059. if( cmp(V9,t) <= 0 )
  2060. printf("This computed value is O.K.\n");
  2061. else
  2062. {
  2063. ErrCnt[Defect] += 1;
  2064. printf( "this is not between 0 and underflow\n");
  2065. printf(" threshold = " );
  2066. show( UfThold );
  2067. }
  2068. }
  2069. Milestone = 140;
  2070. pow2test();
  2071. /*=============================================*/
  2072. Milestone = 160;
  2073. /*=============================================*/
  2074. Pause();
  2075. printf("Searching for Overflow threshold:\n");
  2076. printf("This may generate an error.\n");
  2077. sigsave = sigfpe;
  2078. I = 0;
  2079. mov( CInvrse, Y ); /* a large power of 2 */
  2080. neg(Y);
  2081. mul( HInvrse, Y, V9 ); /* HInvrse = 2 */
  2082. if (setjmp(ovfl_buf))
  2083. goto overflow;
  2084. do
  2085. {
  2086. mov( Y, V );
  2087. mov( V9, Y );
  2088. mul( HInvrse, Y, V9 );
  2089. }
  2090. while( cmp(V9,Y) < 0 ); /* V9 = 2 * Y */
  2091. I = 1;
  2092. overflow:
  2093. show( HInvrse );
  2094. printf( "\ttimes " );
  2095. show( Y );
  2096. printf( "\tequals " );
  2097. show( V9 );
  2098. mov( V9, Z );
  2099. printf("Can `Z = -Y' overflow?\n");
  2100. printf("Trying it on Y = " );
  2101. show(Y);
  2102. mov( Y, V9 );
  2103. neg( V9 );
  2104. mov( V9, V0 );
  2105. sub( Y, V, t );
  2106. add( V, V0, t2 );
  2107. if( cmp(t,t2) == 0 )
  2108. printf("Seems O.K.\n");
  2109. else
  2110. {
  2111. printf("finds a Flaw, -(-Y) differs from Y.\n");
  2112. printf( "V-Y=t:" );
  2113. show(V);
  2114. show(Y);
  2115. show(t);
  2116. printf( "V+V0=t2:" );
  2117. show(V);
  2118. show(V0);
  2119. show(t2);
  2120. ErrCnt[Flaw] += 1;
  2121. }
  2122. if( (cmp(Z, Y) != 0) && (I != 0) )
  2123. {
  2124. ErrCnt[Serious] += 1;
  2125. printf("overflow past " );
  2126. show( Y );
  2127. printf( "\tshrinks to " );
  2128. show( Z );
  2129. printf( "= Y * " );
  2130. show( HInvrse );
  2131. }
  2132. /*Y = V * (HInvrse * U2 - HInvrse);*/
  2133. mul( HInvrse, U2, Y );
  2134. sub( HInvrse, Y, Y );
  2135. mul( V, Y, Y );
  2136. /*Z = Y + ((One - HInvrse) * U2) * V;*/
  2137. sub( HInvrse, One, Z );
  2138. mul( Z, U2, Z );
  2139. mul( Z, V, Z );
  2140. add( Y, Z, Z );
  2141. if( cmp(Z,V0) < 0 )
  2142. mov( Z, Y );
  2143. if( cmp(Y,V0) < 0)
  2144. mov( Y, V );
  2145. sub( V, V0, t );
  2146. if( cmp(t,V0) < 0 )
  2147. mov( V0, V );
  2148. printf("Overflow threshold is V = " );
  2149. show( V );
  2150. if(I)
  2151. {
  2152. printf("Overflow saturates at V0 = " );
  2153. show( V0 );
  2154. }
  2155. else
  2156. printf("There is no saturation value because the system traps on overflow.\n");
  2157. mul( V, One, V9 );
  2158. printf("No Overflow should be signaled for V * 1 = " );
  2159. show( V9 );
  2160. div( One, V, V9 );
  2161. printf(" nor for V / 1 = " );
  2162. show( V9 );
  2163. printf("Any overflow signal separating this * from the one\n");
  2164. printf("above is a DEFECT.\n");
  2165. /*=============================================*/
  2166. Milestone = 170;
  2167. /*=============================================*/
  2168. mov( V, t );
  2169. neg( t );
  2170. k = 0;
  2171. if( cmp(t,V) >= 0 )
  2172. k = 1;
  2173. mov( V0, t );
  2174. neg( t );
  2175. if( cmp(t,V0) >= 0 )
  2176. k = 1;
  2177. mov( UfThold, t );
  2178. neg(t);
  2179. if( cmp(t,V) >= 0 )
  2180. k = 1;
  2181. if( cmp(UfThold,V) >= 0 )
  2182. k = 1;
  2183. if( k != 0 )
  2184. {
  2185. ErrCnt[Failure] += 1;
  2186. printf( "Comparisons involving +-");
  2187. show( V );
  2188. show( V0 );
  2189. show( UfThold );
  2190. printf("are confused by Overflow." );
  2191. }
  2192. /*=============================================*/
  2193. Milestone = 175;
  2194. /*=============================================*/
  2195. printf("\n");
  2196. for(Indx = 1; Indx <= 3; ++Indx) {
  2197. switch(Indx)
  2198. {
  2199. case 1: mov(UfThold, Z); break;
  2200. case 2: mov( E0, Z); break;
  2201. case 3: mov(PseudoZero, Z); break;
  2202. }
  2203. if( cmp(Z, Zero) != 0 )
  2204. {
  2205. SQRT( Z, V9 );
  2206. mul( V9, V9, Y );
  2207. mul( Radix, E9, t );
  2208. sub( t, One, t );
  2209. div( t, Y, t );
  2210. add( One, Radix, t2 );
  2211. add( t2, E9, t2 );
  2212. mul( t2, Z, t2 );
  2213. if( (cmp(t,Z) < 0) || (cmp(Y,t2) > 0) )
  2214. {
  2215. if( cmp(V9,U1) > 0 )
  2216. ErrCnt[Serious] += 1;
  2217. else
  2218. ErrCnt[Defect] += 1;
  2219. printf("Comparison alleges that what prints as Z = " );
  2220. show( Z );
  2221. printf(" is too far from sqrt(Z) ^ 2 = " );
  2222. show( Y );
  2223. }
  2224. }
  2225. }
  2226. Milestone = 180;
  2227. for(Indx = 1; Indx <= 2; ++Indx)
  2228. {
  2229. if(Indx == 1)
  2230. mov( V, Z );
  2231. else
  2232. mov( V0, Z );
  2233. SQRT( Z, V9 );
  2234. mul( Radix, E9, X );
  2235. sub( X, One, X );
  2236. mul( X, V9, X );
  2237. mul( V9, X, V9 );
  2238. mul( Two, Radix, t );
  2239. mul( t, E9, t );
  2240. sub( t, One, t );
  2241. mul( t, Z, t );
  2242. if( (cmp(V9,t) < 0) || (cmp(V9,Z) > 0) )
  2243. {
  2244. mov( V9, Y );
  2245. if( cmp(X,W) < 0 )
  2246. ErrCnt[Serious] += 1;
  2247. else
  2248. ErrCnt[Defect] += 1;
  2249. printf("Comparison alleges that Z = " );
  2250. show( Z );
  2251. printf(" is too far from sqrt(Z) ^ 2 :" );
  2252. show( Y );
  2253. }
  2254. }
  2255. Milestone = 190;
  2256. Pause();
  2257. mul( UfThold, V, X );
  2258. mul( Radix, Radix, Y );
  2259. mul( X, Y, t );
  2260. if( (cmp(t,One) < 0) || (cmp(X,Y) > 0) )
  2261. {
  2262. mul( X, Y, t );
  2263. div( U1, Y, t2 );
  2264. if( (cmp(t,U1) < 0) || (cmp(X,t2) > 0) )
  2265. {
  2266. ErrCnt[Defect] += 1;
  2267. printf( "Badly " );
  2268. }
  2269. else
  2270. {
  2271. ErrCnt[Flaw] += 1;
  2272. }
  2273. printf(" unbalanced range; UfThold * V = " );
  2274. show( X );
  2275. printf( "\tis too far from 1.\n");
  2276. }
  2277. Milestone = 200;
  2278. for(Indx = 1; Indx <= 5; ++Indx)
  2279. {
  2280. mov( F9, X );
  2281. switch(Indx)
  2282. {
  2283. case 2: add( One, U2, X ); break;
  2284. case 3: mov( V, X ); break;
  2285. case 4: mov(UfThold,X); break;
  2286. case 5: mov(Radix,X);
  2287. }
  2288. mov( X, Y );
  2289. sigsave = sigfpe;
  2290. if (setjmp(ovfl_buf))
  2291. {
  2292. printf(" X / X traps when X = " );
  2293. show( X );
  2294. }
  2295. else
  2296. {
  2297. /*V9 = (Y / X - Half) - Half;*/
  2298. div( X, Y, t );
  2299. sub( Half, t, t );
  2300. sub( Half, t, V9 );
  2301. if( cmp(V9,Zero) == 0 )
  2302. continue;
  2303. mov( U1, t );
  2304. neg(t);
  2305. if( (cmp(V9,t) == 0) && (Indx < 5) )
  2306. {
  2307. ErrCnt[Flaw] += 1;
  2308. }
  2309. else
  2310. {
  2311. ErrCnt[Serious] += 1;
  2312. }
  2313. printf(" X / X differs from 1 when X = " );
  2314. show( X );
  2315. printf(" instead, X / X - 1/2 - 1/2 = " );
  2316. show( V9 );
  2317. }
  2318. }
  2319. Pause();
  2320. printf("\n");
  2321. {
  2322. static char *msg[] = {
  2323. "FAILUREs encountered =",
  2324. "SERIOUS DEFECTs discovered =",
  2325. "DEFECTs discovered =",
  2326. "FLAWs discovered =" };
  2327. int i;
  2328. for(i = 0; i < 4; i++) if (ErrCnt[i])
  2329. printf("The number of %-29s %d.\n",
  2330. msg[i], ErrCnt[i]);
  2331. }
  2332. printf("\n");
  2333. if ((ErrCnt[Failure] + ErrCnt[Serious] + ErrCnt[Defect]
  2334. + ErrCnt[Flaw]) > 0) {
  2335. if ((ErrCnt[Failure] + ErrCnt[Serious] + ErrCnt[
  2336. Defect] == 0) && (ErrCnt[Flaw] > 0)) {
  2337. printf("The arithmetic diagnosed seems ");
  2338. printf("satisfactory though flawed.\n");
  2339. }
  2340. if ((ErrCnt[Failure] + ErrCnt[Serious] == 0)
  2341. && ( ErrCnt[Defect] > 0)) {
  2342. printf("The arithmetic diagnosed may be acceptable\n");
  2343. printf("despite inconvenient Defects.\n");
  2344. }
  2345. if ((ErrCnt[Failure] + ErrCnt[Serious]) > 0) {
  2346. printf("The arithmetic diagnosed has ");
  2347. printf("unacceptable serious defects.\n");
  2348. }
  2349. if (ErrCnt[Failure] > 0) {
  2350. printf("Fatal FAILURE may have spoiled this");
  2351. printf(" program's subsequent diagnoses.\n");
  2352. }
  2353. }
  2354. else {
  2355. printf("No failures, defects nor flaws have been discovered.\n");
  2356. if (! ((RMult == Rounded) && (RDiv == Rounded)
  2357. && (RAddSub == Rounded) && (RSqrt == Rounded)))
  2358. printf("The arithmetic diagnosed seems satisfactory.\n");
  2359. else {
  2360. k = 0;
  2361. if( cmp( Radix, Two ) == 0 )
  2362. k = 1;
  2363. if( cmp( Radix, Ten ) == 0 )
  2364. k = 1;
  2365. if( (cmp(StickyBit,One) >= 0) && (k == 1) )
  2366. {
  2367. printf("Rounding appears to conform to ");
  2368. printf("the proposed IEEE standard P");
  2369. k = 0;
  2370. k |= cmp( Radix, Two );
  2371. mul( Four, Three, t );
  2372. mul( t, Two, t );
  2373. sub( t, Precision, t );
  2374. sub( TwentySeven, Precision, t2 );
  2375. sub( TwentySeven, t2, t2 );
  2376. add( t2, One, t2 );
  2377. mul( t2, t, t );
  2378. if( (cmp(Radix,Two) == 0)
  2379. && (cmp(t,Zero) == 0) )
  2380. printf("754");
  2381. else
  2382. printf("854");
  2383. if(IEEE)
  2384. printf(".\n");
  2385. else
  2386. {
  2387. printf(",\nexcept for possibly Double Rounding");
  2388. printf(" during Gradual Underflow.\n");
  2389. }
  2390. }
  2391. printf("The arithmetic diagnosed appears to be excellent!\n");
  2392. }
  2393. }
  2394. if (fpecount)
  2395. printf("\nA total of %d floating point exceptions were registered.\n",
  2396. fpecount);
  2397. printf("END OF TEST.\n");
  2398. }
  2399. /* Random */
  2400. /* Random computes
  2401. X = (Random1 + Random9)^5
  2402. Random1 = X - FLOOR(X) + 0.000005 * X;
  2403. and returns the new value of Random1
  2404. */
  2405. static int randflg = 0;
  2406. FLOAT(C5em6);
  2407. Random()
  2408. {
  2409. if( randflg == 0 )
  2410. {
  2411. mov( Six, t );
  2412. neg(t);
  2413. POW( Ten, t, t );
  2414. mul( Five, t, C5em6 );
  2415. randflg = 1;
  2416. }
  2417. add( Random1, Random9, t );
  2418. mul( t, t, t2 );
  2419. mul( t2, t2, t2 );
  2420. mul( t, t2, t );
  2421. FLOOR(t, t2 );
  2422. sub( t2, t, t2 );
  2423. mul( t, C5em6, t );
  2424. add( t, t2, Random1 );
  2425. /*return(Random1);*/
  2426. }
  2427. /* SqXMinX */
  2428. SqXMinX( ErrKind )
  2429. int ErrKind;
  2430. {
  2431. mul( X, BInvrse, t2 );
  2432. sub( t2, X, t );
  2433. /*SqEr = ((SQRT(X * X) - XB) - XA) / OneUlp;*/
  2434. mul( X, X, Sqarg );
  2435. SQRT( Sqarg, SqEr );
  2436. sub( t2, SqEr, SqEr );
  2437. sub( t, SqEr, SqEr );
  2438. div( OneUlp, SqEr, SqEr );
  2439. if( cmp(SqEr,Zero) != 0)
  2440. {
  2441. Showsq( 0 );
  2442. add( J, One, J );
  2443. ErrCnt[ErrKind] += 1;
  2444. printf("sqrt of " );
  2445. mul( X, X, t );
  2446. show( t );
  2447. printf( "minus " );
  2448. show( X );
  2449. printf( "equals " );
  2450. mul( OneUlp, SqEr, t );
  2451. show( t );
  2452. printf("\tinstead of correct value 0 .\n");
  2453. }
  2454. }
  2455. /* NewD */
  2456. NewD()
  2457. {
  2458. mul( Z1, Q, X );
  2459. /*X = FLOOR(Half - X / Radix) * Radix + X;*/
  2460. div( Radix, X, t );
  2461. sub( t, Half, t );
  2462. FLOOR( t, t );
  2463. mul( t, Radix, t );
  2464. add( t, X, X );
  2465. /*Q = (Q - X * Z) / Radix + X * X * (D / Radix);*/
  2466. mul( X, Z, t );
  2467. sub( t, Q, t );
  2468. div( Radix, t, t );
  2469. div( Radix, D, t2 );
  2470. mul( X, t2, t2 );
  2471. mul( X, t2, t2 );
  2472. add( t, t2, Q );
  2473. /*Z = Z - Two * X * D;*/
  2474. mul( Two, X, t );
  2475. mul( t, D, t );
  2476. sub( t, Z, Z );
  2477. if( cmp(Z,Zero) <= 0)
  2478. {
  2479. neg(Z);
  2480. neg(Z1);
  2481. }
  2482. mul( Radix, D, D );
  2483. }
  2484. /* SR3750 */
  2485. SR3750()
  2486. {
  2487. sub( Radix, X, t );
  2488. sub( Radix, Z2, t2 );
  2489. k = 0;
  2490. if( cmp(t,t2) < 0 )
  2491. k = 1;
  2492. sub( Z2, X, t );
  2493. sub( Z2, W, t2 );
  2494. if( cmp(t,t2) > 0 )
  2495. k = 1;
  2496. /*if (! ((X - Radix < Z2 - Radix) || (X - Z2 > W - Z2))) {*/
  2497. if( k == 0 )
  2498. {
  2499. I = I + 1;
  2500. mul( X, D, X2 );
  2501. mov( X2, Sqarg );
  2502. SQRT( X2, X2 );
  2503. /*Y2 = (X2 - Z2) - (Y - Z2);*/
  2504. sub( Z2, X2, Y2 );
  2505. sub( Z2, Y, t );
  2506. sub( t, Y2, Y2 );
  2507. sub( Half, Y, X2 );
  2508. div( X2, X8, X2 );
  2509. mul( Half, X2, t );
  2510. mul( t, X2, t );
  2511. sub( t, X2, X2 );
  2512. /*SqEr = (Y2 + Half) + (Half - X2);*/
  2513. add( Y2, Half, SqEr );
  2514. sub( X2, Half, t );
  2515. add( t, SqEr, SqEr );
  2516. Showsq( -1 );
  2517. sub( X2, Y2, SqEr );
  2518. Showsq( 1 );
  2519. }
  2520. }
  2521. /* IsYeqX */
  2522. IsYeqX()
  2523. {
  2524. if( cmp(Y,X) != 0 )
  2525. {
  2526. if (N <= 0)
  2527. {
  2528. if( (cmp(Z,Zero) == 0) && (cmp(Q,Zero) <= 0) )
  2529. printf("WARNING: computing\n");
  2530. else
  2531. {
  2532. ErrCnt[Defect] += 1;
  2533. printf( "computing\n");
  2534. }
  2535. show( Z );
  2536. printf( "\tto the power " );
  2537. show( Q );
  2538. printf("\tyielded " );
  2539. show( Y );
  2540. printf("\twhich compared unequal to correct " );
  2541. show( X );
  2542. sub( X, Y, t );
  2543. printf("\t\tthey differ by " );
  2544. show( t );
  2545. }
  2546. N = N + 1; /* ... count discrepancies. */
  2547. }
  2548. }
  2549. /* SR3980 */
  2550. SR3980()
  2551. {
  2552. long li;
  2553. do
  2554. {
  2555. /*Q = (FLOAT) I;*/
  2556. li = I;
  2557. LTOF( &li, Q );
  2558. POW( Z, Q, Y );
  2559. IsYeqX();
  2560. if(++I > M)
  2561. break;
  2562. mul( Z, X, X );
  2563. }
  2564. while( cmp(X,W) < 0 );
  2565. }
  2566. /* PrintIfNPositive */
  2567. PrintIfNPositive()
  2568. {
  2569. if(N > 0)
  2570. printf("Similar discrepancies have occurred %d times.\n", N);
  2571. }
  2572. /* TstPtUf */
  2573. TstPtUf()
  2574. {
  2575. N = 0;
  2576. if( cmp(Z,Zero) != 0)
  2577. {
  2578. printf( "Z = " );
  2579. show(Z);
  2580. printf("Since comparison denies Z = 0, evaluating ");
  2581. printf("(Z + Z) / Z should be safe.\n");
  2582. sigsave = sigfpe;
  2583. if (setjmp(ovfl_buf))
  2584. goto very_serious;
  2585. add( Z, Z, Q9 );
  2586. div( Z, Q9, Q9 );
  2587. printf("What the machine gets for (Z + Z) / Z is " );
  2588. show( Q9 );
  2589. sub( Two, Q9, t );
  2590. FABS(t);
  2591. mul( Radix, U2, t2 );
  2592. if( cmp(t,t2) < 0 )
  2593. {
  2594. printf("This is O.K., provided Over/Underflow");
  2595. printf(" has NOT just been signaled.\n");
  2596. }
  2597. else
  2598. {
  2599. if( (cmp(Q9,One) < 0) || (cmp(Q9,Two) > 0) )
  2600. {
  2601. very_serious:
  2602. N = 1;
  2603. ErrCnt [Serious] = ErrCnt [Serious] + 1;
  2604. printf("This is a VERY SERIOUS DEFECT!\n");
  2605. }
  2606. else
  2607. {
  2608. N = 1;
  2609. ErrCnt[Defect] += 1;
  2610. printf("This is a DEFECT!\n");
  2611. }
  2612. }
  2613. mul( Z, One, V9 );
  2614. mov( V9, Random1 );
  2615. mul( One, Z, V9 );
  2616. mov( V9, Random2 );
  2617. div( One, Z, V9 );
  2618. if( (cmp(Z,Random1) == 0) && (cmp(Z,Random2) == 0)
  2619. && (cmp(Z,V9) == 0) )
  2620. {
  2621. if (N > 0)
  2622. Pause();
  2623. }
  2624. else
  2625. {
  2626. N = 1;
  2627. ErrCnt[Defect] += 1;
  2628. printf( "What prints as Z = ");
  2629. show( Z );
  2630. printf( "\tcompares different from " );
  2631. if( cmp(Z,Random1) != 0)
  2632. {
  2633. printf("Z * 1 = " );
  2634. show( Random1 );
  2635. }
  2636. if( (cmp(Z,Random2) != 0)
  2637. || (cmp(Random2,Random1) != 0) )
  2638. {
  2639. printf("1 * Z == " );
  2640. show( Random2 );
  2641. }
  2642. if( cmp(Z,V9) != 0 )
  2643. {
  2644. printf("Z / 1 = " );
  2645. show( V9 );
  2646. }
  2647. if( cmp(Random2,Random1) != 0 )
  2648. {
  2649. ErrCnt[Defect] += 1;
  2650. printf( "Multiplication does not commute!\n");
  2651. printf("\tComparison alleges that 1 * Z = " );
  2652. show(Random2);
  2653. printf("\tdiffers from Z * 1 = " );
  2654. show(Random1);
  2655. }
  2656. Pause();
  2657. }
  2658. }
  2659. }
  2660. Pause()
  2661. {
  2662. }
  2663. Sign( x, y )
  2664. FSIZE *x, *y;
  2665. {
  2666. if( cmp( x, Zero ) < 0 )
  2667. {
  2668. mov( One, y );
  2669. neg( y );
  2670. }
  2671. else
  2672. {
  2673. mov( One, y );
  2674. }
  2675. }
  2676. sqtest()
  2677. {
  2678. printf("\nRunning test of square root(x).\n");
  2679. RSqrt = Other;
  2680. k = 0;
  2681. SQRT( Zero, t );
  2682. k |= cmp( Zero, t );
  2683. mov( Zero, t );
  2684. neg(t);
  2685. SQRT( t, t2 );
  2686. k |= cmp( t, t2 );
  2687. SQRT( One, t );
  2688. k |= cmp( One, t );
  2689. if( k != 0 )
  2690. {
  2691. ErrCnt[Failure] += 1;
  2692. printf( "Square root of 0.0, -0.0 or 1.0 wrong\n");
  2693. }
  2694. mov( Zero, MinSqEr );
  2695. mov( Zero, MaxSqEr );
  2696. mov( Zero, J );
  2697. mov( Radix, X );
  2698. mov( U2, OneUlp );
  2699. SqXMinX( Serious );
  2700. mov( BInvrse, X );
  2701. mul( BInvrse, U1, OneUlp );
  2702. SqXMinX( Serious );
  2703. mov( U1, X );
  2704. mul( U1, U1, OneUlp );
  2705. SqXMinX( Serious );
  2706. if( cmp(J,Zero) != 0)
  2707. Pause();
  2708. printf("Testing if sqrt(X * X) == X for %d Integers X.\n", NoTrials);
  2709. mov( Zero, J );
  2710. mov( Two, X );
  2711. mov( Radix, Y );
  2712. if( cmp(Radix,One) != 0 )
  2713. {
  2714. lngint = NoTrials;
  2715. LTOF( &lngint, t );
  2716. FTOL( t, &lng2, X );
  2717. if( lngint != lng2 )
  2718. {
  2719. printf( "Integer conversion error\n" );
  2720. exit(1);
  2721. }
  2722. do
  2723. {
  2724. mov( Y, X );
  2725. mul( Radix, Y, Y );
  2726. sub( X, Y, t2 );
  2727. }
  2728. while( ! (cmp(t2,t) >= 0) );
  2729. }
  2730. mul( X, U2, OneUlp );
  2731. I = 1;
  2732. while(I < 10)
  2733. {
  2734. add( X, One, X );
  2735. SqXMinX( Defect );
  2736. if( cmp(J,Zero) > 0 )
  2737. break;
  2738. I = I + 1;
  2739. }
  2740. printf("Test for sqrt monotonicity.\n");
  2741. I = - 1;
  2742. mov( BMinusU2, X );
  2743. mov( Radix, Y );
  2744. mul( Radix, U2, Z );
  2745. add( Radix, Z, Z );
  2746. NotMonot = False;
  2747. Monot = False;
  2748. while( ! (NotMonot || Monot))
  2749. {
  2750. I = I + 1;
  2751. SQRT(X, X);
  2752. SQRT(Y,Q);
  2753. SQRT(Z,Z);
  2754. if( (cmp(X,Q) > 0) || (cmp(Q,Z) > 0) )
  2755. NotMonot = True;
  2756. else
  2757. {
  2758. add( Q, Half, Q );
  2759. FLOOR( Q, Q );
  2760. mul( Q, Q, t );
  2761. if( (I > 0) || (cmp(Radix,t) == 0) )
  2762. Monot = True;
  2763. else if (I > 0)
  2764. {
  2765. if(I > 1)
  2766. Monot = True;
  2767. else
  2768. {
  2769. mul( Y, BInvrse, Y );
  2770. sub( U1, Y, X );
  2771. add( Y, U1, Z );
  2772. }
  2773. }
  2774. else
  2775. {
  2776. mov( Q, Y );
  2777. sub( U2, Y, X );
  2778. add( Y, U2, Z );
  2779. }
  2780. }
  2781. }
  2782. if( Monot )
  2783. printf("sqrt has passed a test for Monotonicity.\n");
  2784. else
  2785. {
  2786. ErrCnt[Defect] += 1;
  2787. printf("sqrt(X) is non-monotonic for X near " );
  2788. show(Y);
  2789. }
  2790. /*=============================================*/
  2791. Milestone = 80;
  2792. /*=============================================*/
  2793. add( MinSqEr, Half, MinSqEr );
  2794. sub( Half, MaxSqEr, MaxSqEr);
  2795. /*Y = (SQRT(One + U2) - One) / U2;*/
  2796. add( One, U2, Sqarg );
  2797. SQRT( Sqarg, Y );
  2798. sub( One, Y, Y );
  2799. div( U2, Y, Y );
  2800. /*SqEr = (Y - One) + U2 / Eight;*/
  2801. sub( One, Y, t );
  2802. div( Eight, U2, SqEr );
  2803. add( t, SqEr, SqEr );
  2804. Showsq( 1 );
  2805. div( Eight, U2, SqEr );
  2806. add( Y, SqEr, SqEr );
  2807. Showsq( -1 );
  2808. /*Y = ((SQRT(F9) - U2) - (One - U2)) / U1;*/
  2809. mov( F9, Sqarg );
  2810. SQRT( Sqarg, Y );
  2811. sub( U2, Y, Y );
  2812. sub( U2, One, t );
  2813. sub( t, Y, Y );
  2814. div( U1, Y, Y );
  2815. div( Eight, U1, SqEr );
  2816. add( Y, SqEr, SqEr );
  2817. Showsq( 1 );
  2818. /*SqEr = (Y + One) + U1 / Eight;*/
  2819. div( Eight, U1, t );
  2820. add( Y, One, SqEr );
  2821. add( SqEr, t, SqEr );
  2822. Showsq( -1 );
  2823. mov( U2, OneUlp );
  2824. mov( OneUlp, X );
  2825. for( Indx = 1; Indx <= 3; ++Indx)
  2826. {
  2827. /*Y = SQRT((X + U1 + X) + F9);*/
  2828. add( X, U1, Y );
  2829. add( Y, X, Y );
  2830. add( Y, F9, Y );
  2831. mov( Y, Sqarg );
  2832. SQRT( Sqarg, Y );
  2833. /*Y = ((Y - U2) - ((One - U2) + X)) / OneUlp;*/
  2834. sub( U2, One, t );
  2835. add( t, X, t );
  2836. sub( U2, Y, Y );
  2837. sub( t, Y, Y );
  2838. div( OneUlp, Y, Y );
  2839. /*Z = ((U1 - X) + F9) * Half * X * X / OneUlp;*/
  2840. sub( X, U1, t );
  2841. add( t, F9, t );
  2842. mul( t, Half, t );
  2843. mul( t, X, t );
  2844. mul( t, X, t );
  2845. div( OneUlp, t, Z );
  2846. add( Y, Half, SqEr );
  2847. add( SqEr, Z, SqEr );
  2848. Showsq( -1 );
  2849. sub( Half, Y, SqEr );
  2850. add( SqEr, Z, SqEr );
  2851. Showsq( 1 );
  2852. if(((Indx == 1) || (Indx == 3)))
  2853. {
  2854. /*X = OneUlp * Sign (X) * FLOOR(Eight / (Nine * SQRT(OneUlp)));*/
  2855. mov( OneUlp, Sqarg );
  2856. SQRT( Sqarg, t );
  2857. mul( Nine, t, t );
  2858. div( t, Eight, t );
  2859. FLOOR( t, t );
  2860. Sign( X, t2 );
  2861. mul( t2, t, t );
  2862. mul( OneUlp, t, X );
  2863. }
  2864. else
  2865. {
  2866. mov( U1, OneUlp );
  2867. mov( OneUlp, X );
  2868. neg( X );
  2869. }
  2870. }
  2871. /*=============================================*/
  2872. Milestone = 85;
  2873. /*=============================================*/
  2874. SqRWrng = False;
  2875. Anomaly = False;
  2876. if( cmp(Radix,One) != 0 )
  2877. {
  2878. printf("Testing whether sqrt is rounded or chopped.\n");
  2879. /*D = FLOOR(Half + POW(Radix, One + Precision - FLOOR(Precision)));*/
  2880. FLOOR( Precision, t2 );
  2881. add( One, Precision, t );
  2882. sub( t2, t, t );
  2883. POW( Radix, t, D );
  2884. add( Half, D, D );
  2885. FLOOR( D, D );
  2886. /* ... == Radix^(1 + fract) if (Precision == Integer + fract. */
  2887. div( Radix, D, X );
  2888. div( A1, D, Y );
  2889. FLOOR( X, t );
  2890. FLOOR( Y, t2 );
  2891. if( (cmp(X,t) != 0) || (cmp(Y,t2) != 0) )
  2892. {
  2893. Anomaly = True;
  2894. printf( "Anomaly 1\n" );
  2895. }
  2896. else
  2897. {
  2898. mov( Zero, X );
  2899. mov( X, Z2 );
  2900. mov( One, Y );
  2901. mov( Y, Y2 );
  2902. sub( One, Radix, Z1 );
  2903. mul( Four, D, FourD );
  2904. do
  2905. {
  2906. if( cmp(Y2,Z2) >0 )
  2907. {
  2908. mov( Radix, Q );
  2909. mov( Y, YY1 );
  2910. do
  2911. {
  2912. /*X1 = FABS(Q + FLOOR(Half - Q / YY1) * YY1);*/
  2913. div( YY1, Q, t );
  2914. sub( t, Half, t );
  2915. FLOOR( t, t );
  2916. mul( t, YY1, t );
  2917. add( Q, t, X1 );
  2918. FABS( X1 );
  2919. mov( YY1, Q );
  2920. mov( X1, YY1 );
  2921. }
  2922. while( ! (cmp(X1,Zero) <= 0) );
  2923. if( cmp(Q,One) <= 0 )
  2924. {
  2925. mov( Y2, Z2 );
  2926. mov( Y, Z );
  2927. }
  2928. }
  2929. add( Y, Two, Y );
  2930. add( X, Eight, X );
  2931. add( Y2, X, Y2 );
  2932. if( cmp(Y2,FourD) >= 0 )
  2933. sub( FourD, Y2, Y2 );
  2934. }
  2935. while( ! (cmp(Y,D) >= 0) );
  2936. sub( Z2, FourD, X8 );
  2937. mul( Z, Z, Q );
  2938. add( X8, Q, Q );
  2939. div( FourD, Q, Q );
  2940. div( Eight, X8, X8 );
  2941. FLOOR( Q, t );
  2942. if( cmp(Q,t) != 0 )
  2943. {
  2944. Anomaly = True;
  2945. printf( "Anomaly 2\n" );
  2946. }
  2947. else
  2948. {
  2949. Break = False;
  2950. do
  2951. {
  2952. mul( Z1, Z, X );
  2953. /*X = X - FLOOR(X / Radix) * Radix;*/
  2954. div( Radix, X, t );
  2955. FLOOR( t, t );
  2956. mul( t, Radix, t );
  2957. sub( t, X, X );
  2958. if( cmp(X,One) == 0 )
  2959. Break = True;
  2960. else
  2961. sub( One, Z1, Z1 );
  2962. }
  2963. while( ! (Break || (cmp(Z1,Zero) <= 0)) );
  2964. if( (cmp(Z1,Zero) <= 0) && (! Break))
  2965. {
  2966. printf( "Anomaly 3\n" );
  2967. Anomaly = True;
  2968. }
  2969. else
  2970. {
  2971. if( cmp(Z1,RadixD2) > 0)
  2972. sub( Radix, Z1, Z1 );
  2973. do
  2974. {
  2975. NewD();
  2976. mul( U2, D, t );
  2977. }
  2978. while( ! (cmp(t,F9) >= 0) );
  2979. mul( D, Radix, t );
  2980. sub( D, t, t );
  2981. sub( D, W, t2 );
  2982. if (cmp(t,t2) != 0 )
  2983. {
  2984. printf( "Anomaly 4\n" );
  2985. Anomaly = True;
  2986. }
  2987. else
  2988. {
  2989. mov( D, Z2 );
  2990. I = 0;
  2991. add( One, Z, t );
  2992. mul( t, Half, t );
  2993. add( D, t, Y );
  2994. add( D, Z, X );
  2995. add( X, Q, X );
  2996. SR3750();
  2997. sub( Z, One, t );
  2998. mul( t, Half, t );
  2999. add( D, t, Y );
  3000. add( Y, D, Y );
  3001. sub( Z, D, X );
  3002. add( X, D, X );
  3003. add( X, Q, t );
  3004. add( t, X, X );
  3005. SR3750();
  3006. NewD();
  3007. sub( Z2, D, t );
  3008. sub( Z2, W, t2 );
  3009. if(cmp(t,t2) != 0 )
  3010. {
  3011. printf( "Anomaly 5\n" );
  3012. Anomaly = True;
  3013. }
  3014. else
  3015. {
  3016. /*Y = (D - Z2) + (Z2 + (One - Z) * Half);*/
  3017. sub( Z, One, t );
  3018. mul( t, Half, t );
  3019. add( Z2, t, t );
  3020. sub( Z2, D, Y );
  3021. add( Y, t, Y );
  3022. /*X = (D - Z2) + (Z2 - Z + Q);*/
  3023. sub( Z, Z2, t );
  3024. add( t, Q, t );
  3025. sub( Z2, D, X );
  3026. add( X, t, X );
  3027. SR3750();
  3028. add( One, Z, Y );
  3029. mul( Y, Half, Y );
  3030. mov( Q, X );
  3031. SR3750();
  3032. if(I == 0)
  3033. {
  3034. printf( "Anomaly 6\n" );
  3035. Anomaly = True;
  3036. }
  3037. }
  3038. }
  3039. }
  3040. }
  3041. }
  3042. if ((I == 0) || Anomaly)
  3043. {
  3044. ErrCnt[Failure] += 1;
  3045. printf( "Anomalous arithmetic with Integer < \n");
  3046. printf("Radix^Precision = " );
  3047. show( W );
  3048. printf(" fails test whether sqrt rounds or chops.\n");
  3049. SqRWrng = True;
  3050. }
  3051. }
  3052. if(! Anomaly)
  3053. {
  3054. if(! ((cmp(MinSqEr,Zero) < 0) || (cmp(MaxSqEr,Zero) > 0))) {
  3055. RSqrt = Rounded;
  3056. printf("Square root appears to be correctly rounded.\n");
  3057. }
  3058. else
  3059. {
  3060. k = 0;
  3061. add( MaxSqEr, U2, t );
  3062. sub( Half, U2, t2 );
  3063. if( cmp(t,t2) > 0 )
  3064. k = 1;
  3065. if( cmp( MinSqEr, Half ) > 0 )
  3066. k = 1;
  3067. add( MinSqEr, Radix, t );
  3068. if( cmp( t, Half ) < 0 )
  3069. k = 1;
  3070. if( k == 1 )
  3071. SqRWrng = True;
  3072. else
  3073. {
  3074. RSqrt = Chopped;
  3075. printf("Square root appears to be chopped.\n");
  3076. }
  3077. }
  3078. }
  3079. if( SqRWrng )
  3080. {
  3081. printf("Square root is neither chopped nor correctly rounded.\n");
  3082. printf("Observed errors run from " );
  3083. sub( Half, MinSqEr, t );
  3084. show( t );
  3085. printf("\tto " );
  3086. add( Half, MaxSqEr, t );
  3087. show( t );
  3088. printf( "ulps.\n" );
  3089. sub( MinSqEr, MaxSqEr, t );
  3090. mul( Radix, Radix, t2 );
  3091. if( cmp( t, t2 ) >= 0 )
  3092. {
  3093. ErrCnt[Serious] += 1;
  3094. printf( "sqrt gets too many last digits wrong\n");
  3095. }
  3096. }
  3097. }
  3098. Showsq( arg )
  3099. int arg;
  3100. {
  3101. k = 0;
  3102. if( arg <= 0 )
  3103. {
  3104. if( cmp(SqEr,MinSqEr) < 0 )
  3105. {
  3106. k = 1;
  3107. mov( SqEr, MinSqEr );
  3108. }
  3109. }
  3110. if( arg >= 0 )
  3111. {
  3112. if( cmp(SqEr,MaxSqEr) > 0 )
  3113. {
  3114. k = 2;
  3115. mov( SqEr, MaxSqEr );
  3116. }
  3117. }
  3118. #if DEBUG
  3119. if( k != 0 )
  3120. {
  3121. printf( "Square root of " );
  3122. show( arg );
  3123. printf( "\tis in error by " );
  3124. show( SqEr );
  3125. }
  3126. #endif
  3127. }
  3128. pow1test()
  3129. {
  3130. /*=============================================*/
  3131. Milestone = 90;
  3132. /*=============================================*/
  3133. Pause();
  3134. printf("Testing powers Z^i for small Integers Z and i.\n");
  3135. N = 0;
  3136. /* ... test powers of zero. */
  3137. I = 0;
  3138. mov( Zero, Z );
  3139. neg(Z);
  3140. M = 3;
  3141. Break = False;
  3142. do
  3143. {
  3144. mov( One, X );
  3145. SR3980();
  3146. if(I <= 10)
  3147. {
  3148. I = 1023;
  3149. SR3980();
  3150. }
  3151. if( cmp(Z,MinusOne) == 0 )
  3152. Break = True;
  3153. else
  3154. {
  3155. mov( MinusOne, Z );
  3156. PrintIfNPositive();
  3157. N = 0;
  3158. /* .. if(-1)^N is invalid, replace MinusOne by One. */
  3159. I = - 4;
  3160. }
  3161. }
  3162. while( ! Break );
  3163. PrintIfNPositive();
  3164. N1 = N;
  3165. N = 0;
  3166. mov( A1, Z );
  3167. /*M = FLOOR(Two * LOG(W) / LOG(A1));*/
  3168. LOG( W, t );
  3169. mul( Two, t, t );
  3170. FLOOR( t, t );
  3171. LOG( A1, t2 );
  3172. div( t2, t, t );
  3173. FTOL( t, &lngint, t2 );
  3174. M = lngint;
  3175. Break = False;
  3176. do
  3177. {
  3178. mov( Z, X );
  3179. I = 1;
  3180. SR3980();
  3181. if( cmp(Z,AInvrse) == 0 )
  3182. Break = True;
  3183. else
  3184. mov( AInvrse, Z );
  3185. }
  3186. while( ! (Break) );
  3187. /*=============================================*/
  3188. Milestone = 100;
  3189. /*=============================================*/
  3190. /* Powers of Radix have been tested, */
  3191. /* next try a few primes */
  3192. M = NoTrials;
  3193. mov( Three, Z );
  3194. do
  3195. {
  3196. mov( Z, X );
  3197. I = 1;
  3198. SR3980();
  3199. do
  3200. {
  3201. add( Z, Two, Z );
  3202. div( Three, Z, t );
  3203. FLOOR( t, t );
  3204. mul( Three, t, t );
  3205. }
  3206. while( cmp(t,Z) == 0 );
  3207. mul( Eight, Three, t );
  3208. }
  3209. while( cmp(Z,t) < 0 );
  3210. if(N > 0)
  3211. {
  3212. printf("Errors like this may invalidate financial calculations\n");
  3213. printf("\tinvolving interest rates.\n");
  3214. }
  3215. PrintIfNPositive();
  3216. N += N1;
  3217. if(N == 0)
  3218. printf("... no discrepancies found.\n");
  3219. if(N > 0)
  3220. Pause();
  3221. else printf("\n");
  3222. }
  3223. pow2test()
  3224. {
  3225. printf("\n");
  3226. /* ...calculate Exp2 == exp(2) == 7.38905 60989 30650 22723 04275-... */
  3227. mov( Zero, X );
  3228. mov( Two, t2 ); /*I = 2;*/
  3229. mul( Two, Three, Y );
  3230. mov( Zero, Q );
  3231. N = 0;
  3232. do
  3233. {
  3234. mov( X, Z );
  3235. add( t2, One, t2 ); /*I = I + 1;*/
  3236. add( t2, t2, t );
  3237. div( t, Y, Y ); /*Y = Y / (I + I);*/
  3238. add( Y, Q, R );
  3239. add( Z, R, X );
  3240. sub( X, Z, Q );
  3241. add( Q, R, Q );
  3242. }
  3243. while( cmp(X,Z) > 0 );
  3244. /*Z = (OneAndHalf + One / Eight) + X / (OneAndHalf * ThirtyTwo);*/
  3245. div( Eight, One, t );
  3246. add( OneAndHalf, t, Z );
  3247. mul( OneAndHalf, ThirtyTwo, t );
  3248. div( t, X, t );
  3249. add( Z, t, Z );
  3250. mul( Z, Z, X );
  3251. mul( X, X, Exp2 );
  3252. mov( F9, X );
  3253. sub( U1, X, Y );
  3254. printf("Testing X^((X + 1) / (X - 1)) vs. exp(2) = " );
  3255. show( Exp2 );
  3256. printf( "\tas X -> 1.\n" );
  3257. for(I = 1;;)
  3258. {
  3259. sub( BInvrse, X, Z );
  3260. /*Z = (X + One) / (Z - (One - BInvrse));*/
  3261. add( X, One, t2 );
  3262. sub( BInvrse, One, t );
  3263. sub( t, Z, t );
  3264. div( t, t2, Z );
  3265. POW( X, Z, Sqarg );
  3266. sub( Exp2, Sqarg, Q );
  3267. mov( Q, t );
  3268. FABS( t );
  3269. mul( TwoForty, U2, t2 );
  3270. if( cmp( t, t2 ) > 0 )
  3271. {
  3272. N = 1;
  3273. sub( BInvrse, X, V9 );
  3274. sub( BInvrse, One, t );
  3275. sub( t, V9, V9 );
  3276. ErrCnt[Defect] += 1;
  3277. printf( "Calculated " );
  3278. show( Sqarg );
  3279. printf(" for \t(1 + " );
  3280. show( V9 );
  3281. printf( "\tto the power " );
  3282. show( Z );
  3283. printf("\tdiffers from correct value by " );
  3284. show( Q );
  3285. printf("\tThis much error may spoil financial\n");
  3286. printf("\tcalculations involving tiny interest rates.\n");
  3287. break;
  3288. }
  3289. else
  3290. {
  3291. sub( X, Y, Z );
  3292. mul( Z, Two, Z );
  3293. add( Z, Y, Z );
  3294. mov( Y, X );
  3295. mov( Z, Y );
  3296. sub( F9, X, Z );
  3297. mul( Z, Z, Z );
  3298. add( Z, One, Z );
  3299. if( (cmp(Z,One) > 0) && (I < NoTrials) )
  3300. I++;
  3301. else
  3302. {
  3303. if( cmp(X,One) > 0 )
  3304. {
  3305. if(N == 0)
  3306. printf("Accuracy seems adequate.\n");
  3307. break;
  3308. }
  3309. else
  3310. {
  3311. add( One, U2, X );
  3312. add( U2, U2, Y );
  3313. add( X, Y, Y );
  3314. I = 1;
  3315. }
  3316. }
  3317. }
  3318. }
  3319. /*=============================================*/
  3320. Milestone = 150;
  3321. /*=============================================*/
  3322. printf("Testing powers Z^Q at four nearly extreme values.\n");
  3323. N = 0;
  3324. mov( A1, Z );
  3325. /*Q = FLOOR(Half - LOG(C) / LOG(A1));*/
  3326. LOG( C, t );
  3327. LOG( A1, t2 );
  3328. div( t2, t, t );
  3329. sub( t, Half, t );
  3330. FLOOR( t, Q );
  3331. Break = False;
  3332. do
  3333. {
  3334. mov( CInvrse, X );
  3335. POW( Z, Q, Y );
  3336. IsYeqX();
  3337. neg(Q);
  3338. mov( C, X );
  3339. POW( Z, Q, Y );
  3340. IsYeqX();
  3341. if( cmp(Z,One) < 0 )
  3342. Break = True;
  3343. else
  3344. mov( AInvrse, Z );
  3345. }
  3346. while( ! (Break));
  3347. PrintIfNPositive();
  3348. if(N == 0)
  3349. printf(" ... no discrepancies found.\n");
  3350. printf("\n");
  3351. }