home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume3 / ieee / part2 < prev    next >
Encoding:
Internet Message Format  |  1986-11-30  |  24.7 KB

  1. From: genrad!decvax!decwrl!sun!dgh!dgh (David Hough)
  2. Subject: IEEE Calculator (part 2 of 6)
  3. Newsgroups: mod.sources
  4. Approved: jpn@panda.UUCP
  5.  
  6. Mod.sources:  Volume 3, Issue 4
  7. Submitted by: decvax!decwrl!sun!dgh!dgh (David Hough)
  8.  
  9. #! /bin/sh
  10. : make a directory, cd to it, and run this through sh
  11. echo If this kit is complete, "End of Kit" will echo at the end
  12. echo Extracting calc.p
  13. cat >calc.p <<'End-Of-File'
  14. program calculator (input, output) ;
  15.  
  16. (* File calc.p, Version 9 October 1984.  *)
  17.         
  18.         (* calc is a calculator style program to demonstrate the proposed
  19.         IEEE floating point arithmetic *)
  20.  
  21. #include 'sane.h'
  22. #include 'oldfplib.h'
  23. #include 'calctest.h'
  24. #include 'calcsingle.h'
  25. #include 'calcdouble.h'
  26. #include 'global.i'
  27. #include 'forward.i'
  28. #include 'init.i'
  29. #include 'divrem.i'
  30. #include 'extra.i'
  31. #include 'storage.i'
  32. #include 'addsubpas.i'
  33. #include 'utility.i'
  34. #include 'function.i'
  35. #include 'hex.i'
  36. #include 'base.i'
  37.  
  38. procedure store (* var x : internal *) ;
  39.  
  40.         (* Rounds x to current storage mode, setting exceptions accordingly,
  41.         then puts result back in internal format.  *)
  42.         
  43. var 
  44. yx : cextended ;
  45. yd : cdouble ;
  46. ys : csingle ;
  47. yi : cint64 ;
  48.  
  49. begin
  50. case storagemode of 
  51. i16, i32,  i64 : tointeger( storagemode, x, yi ) ;
  52. flt32 : tosingle ( x, ys ) ;
  53. f64 : todouble ( x, yd ) ;
  54. ext80 : toextended ( x, yx ) ;
  55. otherwise
  56. end ;
  57. end ;
  58.  
  59. procedure commandloop ;
  60. var
  61. c : char ;
  62. s : strng ;
  63. i,j : integer ;
  64. found, exit : boolean ;
  65. ps : pstack ;
  66. badnan, x, y, z, r  : internal ; 
  67.                 (* Rule is: x gets the top of stack, y the next,
  68.                         for use in DOTEST *)
  69. error : boolean ;
  70. cc : conditioncode ;
  71. oldtop : internal ; (* Saves previous top of stack, so we can tell when it
  72.         changes.  New tops of stack are displayed.  *)
  73. heap : ^ integer ; (* Heap marker.  *)
  74. yx : cextended ;
  75. yd : cdouble ;
  76. yi : cint64 ;
  77. xs, ys, zs  : csingle ;
  78. tx : real ;
  79. es : integer ;
  80. fpe : xcpn ;
  81. buffer : strng ; (* Used to buffer multiple commands.  *)
  82. semipos : integer ; (* Used to record end of command. *)
  83. fulldisplay : boolean ; (* Flag set at the end of a calculator operation;
  84.                         if true, the top of stack will be displayed;
  85.                         if false, only traps, if any, will be displayed.  *)
  86.  
  87. procedure clear ;
  88.  
  89.         (* Clears stack and heap.  *)
  90.         
  91. begin
  92. stack := nil ;
  93. end ;
  94.  
  95. procedure docommand ( var found : boolean ) ;
  96.  
  97. var fpe : xcpn ;
  98.  
  99. procedure subc ;
  100.  
  101. var i : integer ;
  102.  
  103. begin
  104. if sequal(s , 'COMPARE') then begin
  105. found := true ;
  106. popstack(x) ;
  107. popstack(y) ;
  108. compare( y, x,  cc) ;
  109. write(' Compare result: ') ;
  110. case cc of
  111. lesser : writeln(' < ') ;
  112. equal : writeln(' = ' ) ;
  113. greater : writeln(' > ') ;
  114. notord : writeln(' Unordered ') ;
  115. end ;
  116. for i := 0 to 6 do yi[i] := 0 ;
  117. yi[7] := ord(cc) ;
  118. unpackinteger(yi,z,i16) ;
  119. pushstack(z) ;
  120. end else
  121. if sequal(s , 'CLEAR') then  begin (* CLEAR *) found := true ; 
  122. clear end
  123. else if sequal(s , 'CRUNCH') then begin (* Clear stack except for top two entries.  *)
  124. found := true ;
  125. popstack(x) ;
  126. popstack(y) ;
  127. clear ;
  128. pushstack(y) ;
  129. pushstack(x) ;
  130. end
  131.  ;
  132. end ;
  133.  
  134. procedure subd ;
  135.  
  136. begin
  137. if sequal(s , 'DUP') then begin (* Duplicate top of stack *)
  138. popstack(x) ;
  139. pushstack(x) ;
  140. pushstack(x) ;
  141. found := true ;
  142. end 
  143. else if sequal(s , 'DIV') then begin
  144. found := true ;
  145. popstack(x) ;
  146. popstack(y) ;
  147. divrem( y, x,  z, r ) ;
  148. writeln(' REM: ') ;
  149. display(r) ;
  150. pushstack(z) ;
  151. end else if sequal(s,  'DUMP') then begin (* DUMP STACK *)
  152. found := true ;
  153. ps := stack ;
  154. while ps <> nil do begin
  155. display(ps^.x ) ;
  156. ps := ps^.next ;
  157. end ;
  158. end ;
  159. end ;
  160.  
  161. procedure subRR ;
  162.  
  163. begin
  164. if sequal(s, 'REV') then begin (* reverse top two entries on stack *)
  165. found := true ;
  166. popstack(x) ;
  167. popstack(y) ;
  168. pushstack(x) ;
  169. pushstack(y) ;
  170. end 
  171. else if sequal(s, 'REM') then begin
  172. found := true ;
  173. popstack(x) ;
  174. popstack(y) ;
  175. divrem( y, x, z, r ) ;
  176. writeln(' DIV: ') ;
  177. display(z) ;
  178. pushstack(r) ;
  179. end else if sequal(s, 'RN') then begin
  180. found := true ;
  181. fpstatus.mode.round := rnear ;
  182. end
  183. else if sequal(s, 'RM') then begin
  184. found := true ;
  185. fpstatus.mode.round := rneg ;
  186. end
  187. else if sequal(s, 'RP') then begin
  188. found := true ;
  189. fpstatus.mode.round := rpos ;
  190. end
  191. else if sequal(s, 'RZ') then begin
  192. found := true ;
  193. fpstatus.mode.round := rzero ;
  194. end else if sequal(s, 'R24') then begin
  195. found := true ;
  196. fpstatus.mode.precision := sprec ; (* Round cextended to 24 significant bits.  *)
  197. end else if sequal(s, 'R53') then begin
  198. found := true ;
  199. fpstatus.mode.precision := dprec ; 
  200.         (* Round cextended to 53 significant bits.  *)
  201. end end ;
  202.  
  203. procedure subS ;
  204.  
  205. begin
  206. {
  207. if sequal(s, 'SOFT') then begin
  208. found := true ;
  209. ffloat_ ; ffunc_ ;
  210. end else if sequal(s, 'SKY') then begin
  211. found := true ;
  212. sfloat_ ; sfunc_ ;
  213. end else }
  214. if sequal(s, 'SCALE') then begin
  215. found := true ;
  216. popstack(x) ;
  217. popstack(y) ;
  218. cscale( y, x,  z ) ;
  219. pushstack( z ) ;
  220. end else if sequal(s, 'SQRT') then begin
  221. found := true ;
  222. popstack(x) ;
  223. csqrt( x, z) ;
  224. pushstack(z) ;
  225. end else if sequal(s, 'STOF32') then begin (* Set storage mode.  *)
  226. found := true ;
  227. storagemode := flt32 ;
  228. end
  229. else if sequal(s, 'STOF64') then begin
  230. found := true ;
  231. storagemode := f64 ;
  232. end
  233. else if sequal(s, 'STOX80') then begin
  234. found := true ;
  235. storagemode := ext80 ;
  236. end
  237. else if sequal(s, 'STOI16') then begin
  238. found := true ;
  239. storagemode := i16 ;
  240. end else if sequal(s, 'STOI32') then begin
  241. found := true ;
  242. storagemode := i32 ;
  243. end else if sequal(s, 'STOI64') then begin
  244. found := true ;
  245. storagemode := i64
  246. end 
  247. end ;
  248.  
  249. procedure subT ;
  250.  
  251. var yi : cint64 ;
  252.  
  253. begin
  254. if sequal(s, 'TOF32') then begin (* Convert to csingle.  *)
  255. found := true ;
  256. popstack(x);z:=x ;
  257. tosingle( z, ys) ;
  258. pushstack(z) ;
  259. end else if sequal(s, 'TOF32I') then begin (* Convert to csingle integral.  *)
  260. found := true ;
  261. popstack(x);z:=x ;
  262. roundint( z, fpstatus.mode.round, sprec ) ;
  263. tosingle( z, ys) ;
  264. pushstack(z) ;
  265. end else if sequal(s, 'TOF64') then begin (* Convert to cdouble.  *)
  266. found := true ;
  267. popstack(x);z:=x ;
  268. todouble( z, yd) ;
  269. pushstack(z) ;
  270. end else if sequal(s, 'TOF64I') then begin (* Convert to cdouble integral.  *)
  271. found := true ;
  272. popstack(x);z:=x ;
  273. roundint( z, fpstatus.mode.round, dprec ) ;
  274. todouble( z, yd) ;
  275. pushstack(z) ;
  276. end else  if sequal(s, 'TOX80' )then begin (* Convert to cextended.  *)
  277. found := true ;
  278. popstack(x);z:=x ;
  279. toextended( z, yx) ;
  280. pushstack(z) ;
  281. end else if sequal(s, 'TOX80I') then begin (* Convert to cextended integral.  *)
  282. found := true ;
  283. popstack(x);z:=x ;
  284. roundint( z, fpstatus.mode.round, xprec ) ;
  285. toextended( z, yx) ;
  286. pushstack(z) ;
  287. end else if sequal(s, 'TOI16') then begin (* Convert to 16 bit integer.  *)
  288. found := true ;
  289. popstack(x);z:=x ;
  290. tointeger( i16, z, yi) ;
  291. pushstack(z) ;
  292. end else if sequal(s, 'TOI32') then begin (* Convert to 32 bit integer.  *)
  293. found := true ;
  294. popstack(x);z:=x ;
  295. tointeger( i32, z, yi) ;
  296. pushstack(z) ;
  297. end else if sequal(s, 'TOI64' )then begin (* Convert to 64 bit integer.  *)
  298. found := true ;
  299. popstack(x);z:=x ;
  300. tointeger(i64, z, yi) ;
  301. pushstack(z) ;
  302. end 
  303. else if sequal(s, 'TEST') then begin (* Toggle test flag *)
  304. found := true ;
  305. testflag := not testflag ;
  306. end ;
  307. end ;
  308.  
  309.  
  310. begin
  311. found := false ;
  312. if length(s) > 0 then case s[1] of
  313.  
  314. '+' : if length(s)=1 then begin
  315. found := true ;
  316. popstack(x) ;popstack(y) ; 
  317. add( y, x,  z ) ;
  318. pushstack(z) ;
  319. end ;
  320.  
  321. '-' : if length(s)=1 then begin
  322. found := true ;
  323. popstack(x) ;popstack(y) ; 
  324. r := x ; r.sign := not x.sign ;
  325. add( y, r, z ) ;
  326. pushstack(z) ;
  327. end ;
  328.  
  329. '*' : if length(s)=1 then begin
  330. found := true ;
  331. popstack(x) ; popstack(y) ;
  332. multiply ( y, x,  z) ;
  333. pushstack(z) ;
  334. end ;
  335.  
  336. '/' : if length(s)=1 then begin
  337. found := true ;
  338. popstack(x) ;popstack(y) ; 
  339. divide ( y, x,  z) ;
  340. pushstack(z) ;
  341. end ;
  342. 'A' : if sequal(s, 'ABS') then begin
  343. found := true ;
  344. popstack(x) ;
  345. z := x ;
  346. z.sign := false ;
  347. pushstack(z) ;
  348. end
  349. else if sequal(s, 'AFF') then begin
  350. found := true ;
  351. fpstatus.mode.clos := affine ;
  352. end ;
  353.  
  354. 'C' : subc ;
  355.  
  356. 'D' : subd ;
  357.  
  358. 'E' : if length(s)=1 then begin
  359. found := true ;
  360. pushstack(e) ;
  361. end else if sequal(s, 'EXIT') then begin (* EXIT *) found := true ; exit := true end ;
  362.  
  363. 'L' : if sequal(s, 'LOGB') then begin
  364. found := true ;
  365. popstack(x) ;
  366. clogb( x,  z ) ;
  367. pushstack( z ) ;
  368. end ;
  369.  
  370. 'N' : if sequal(s, 'NEG') then begin (* NEGATE top of stack *)
  371. found := true ;
  372. popstack(x) ;
  373. z := x ;
  374. z.sign := not z.sign ;
  375. pushstack(z) ;
  376. end 
  377. else if sequal(s, 'NORM') then begin
  378. found := true ;
  379. fpstatus.mode.norm := normalizing ;
  380. end else if sequal(s, 'NEXT') then begin (* Compute NEXTAFTER function.  *)
  381. found := true ;
  382. popstack(x) ;
  383. popstack(y) ;
  384. cnextafter( y, x,  z ) ;
  385. pushstack ( z ) ;
  386. end ;
  387.  
  388. 'P' : if sequal(s, 'POP') then begin
  389. found := true ;
  390. if stack <> nil then stack := stack^.next ;
  391. end
  392. else if sequal(s, 'PI') then begin
  393. found := true ;
  394. pushstack(pi) ;
  395. end else if sequal(s, 'PROJ') then begin 
  396. found := true ;
  397. fpstatus.mode.clos := proj ;
  398. end ;
  399.  
  400. 'R' : subRr ;
  401. 'S' : subS ;
  402. 'T' : subT ;
  403.  
  404. 'U' : if sequal(s, 'UNROUNDED') then begin
  405. found := true ;
  406. storagemode := unrounded ;
  407. fpstatus.mode.precision := xprec ;
  408. end ;
  409.  
  410. 'W' : if sequal(s, 'WARN') then begin 
  411. found := true ;
  412. fpstatus.mode.norm := warning ;
  413. end ;
  414.  
  415. otherwise
  416. end ;
  417.  
  418. if found then writeln( ' Did ',s) ;
  419.  
  420. if (length(s)=2) and not found then begin (* Is is a trap enable toggle?  *)
  421. for fpe := invop to inexact do 
  422. if (s[1]=xcpnname[fpe,1]) and (s[2]=xcpnname[fpe,2]) then begin
  423. found := true ; (* Command was name of exception so toggle that trap enable. *)
  424. if fpe in fpstatus.trap then 
  425. fpstatus.trap := fpstatus.trap - [fpe] (* If on, turn off.  *)
  426. else
  427. fpstatus.trap := fpstatus.trap + [fpe] ; (* If off, turn on.  *)
  428. end ;
  429. end ;
  430.  
  431. if not found then begin (* check for decimal input *)
  432. decbin(s, x, error ) ;
  433. fpstatus.curexcep := fpstatus.curexcep - [invop] ;
  434. badnan.sign := x.sign ; (* Set up BadNaN to compare correctly with x.  *)
  435. if (not error) and (not equalinternal(x,badnan))  then begin
  436. found := true ;
  437. pushstack(x) ;
  438. end
  439. end ;
  440. if not found then begin (* check for  hex input *)
  441. hexbin(s, x, error ) ;
  442. fpstatus.curexcep := fpstatus.curexcep - [invop] ;
  443. if not error then begin
  444. found := true ;
  445. pushstack(x) ;
  446. end
  447. end ;
  448. if  found then begin
  449. if stack <> nil then store(stack^.x) ;
  450. fpstatus.excep := fpstatus.excep + fpstatus.curexcep ; (* Add in current
  451.         exceptions.  *)
  452. fulldisplay := stack <> nil ; 
  453. if fulldisplay then
  454. fulldisplay := (fpstatus.curexcep <> []) or 
  455. (not equalinternal( stack^.x, oldtop )) ;
  456. if fulldisplay then  begin
  457. displaystatus ;
  458. trapmessage ;
  459. fpstatus.curexcep := [] ;
  460. display(stack^.x) ;
  461. end
  462. else trapmessage 
  463. end
  464. else  writeln(' Command not recognized: ',s) ;
  465.  
  466. end ;
  467.  
  468.  
  469. begin
  470. clear ;
  471. makenan(nanascnan,badnan) ; (* Create a Bad-NaN Nan for use later.  *)
  472. repeat
  473.  
  474. exit := false ;
  475. fpstatus.excep := [] ; 
  476.         (* Reset exception flag register for new command strng.  *)
  477. writeln(' Command: ') ;
  478. i := 1 ;
  479. while not eoln do
  480.     begin
  481.     read(c) ;
  482.     buffer[i] := c ;
  483.     i := i + 1 ;
  484.     end ;
  485. buffer[0] := chr(i-1) ;
  486. readln ;
  487. concatchar( buffer, ';' ) ;
  488.  
  489. while (not exit) and (length(buffer) > 1) do begin (* Get next command.  *)
  490. semipos := pos(';', buffer) ; (* Find boundary of next command.  *)
  491. copy( buffer, 1, semipos - 1,s ) ; (* Extract next command.  *)
  492. delete ( buffer, 1, semipos ) ;  (* Remove next command from buffer.  *)
  493. if stack <> nil then oldtop := stack^.x ; (* Save old top of stack.  *)
  494. fpstatus.curexcep := [] ; (* Reset exception flags for new operation.  *)
  495. j := 0 ;
  496. for i := 1 to length(s) do if s[i] <> ' ' then begin (* suppress blanks
  497. and lower case *)
  498. j := j + 1 ;
  499. s[j] := upcase(s[i]) ;
  500. end ;
  501. copy(s,1,j,s) ;
  502. for i := ord(s[0])+1 to maxfpstring do s[i] := ' ' ;
  503. docommand ( found ) ;
  504. if  found and testflag then dotest ( s, found, x, y  ) ;
  505. end ;
  506. until exit ;
  507. end ;
  508.  
  509. procedure execute ;
  510. begin
  511. writeln(' Begin IEEE Calculator version 2 September 1985 ') ;
  512. initialize ;
  513. commandloop ;
  514. end ;
  515.  
  516. #include 'dotest.i'
  517.  
  518. begin (* Outer block.  *)
  519. execute ;
  520. end .
  521.  
  522.  
  523. End-Of-File
  524. echo Extracting calcf32.p
  525. cat >calcf32.p <<'End-Of-File'
  526.  
  527. (* File calcf32.p, Version 9 October 1984.  *)
  528.  
  529. (* This version of the calculator test unit tests 32 bit single precision
  530. IEEE arithmetic accessed by the "shortreal" type.  *)
  531.  
  532. #include 'sane.h'
  533. #include 'oldfplib.h'
  534. #include 'calctest.h'
  535. #include 'calcsingle.h'
  536.  
  537. type bite = -128..+127 ;
  538.  
  539. function getbite ( b : bite ) : byt  ;
  540. begin
  541. if b >= 0 then getbite := b else getbite := b + 256 ;
  542. end   ;
  543.  
  544. function setbite ( b : byt ) : bite   ;
  545. begin
  546. if b < 128 then setbite := b else setbite := b - 256 ;
  547. end   ;
  548.  
  549. procedure swapexcep (* var e : excepset *) ;
  550.  
  551. var t : excepset ;
  552.  
  553. begin
  554. e := [] ;
  555. end ;
  556.  
  557. procedure swaptrap (* var e : excepset *) ;
  558.  
  559. var t : excepset ;
  560.  
  561. begin
  562. e := [] ;
  563. end ;
  564.  
  565. procedure swapmode (* var e : fpmodetype *) ;
  566.  
  567. var t : fpmodetype ;
  568.  
  569. begin
  570. t.round := rnear ;
  571. t.clos := affine ;
  572. t.norm := normalizing ;
  573. t.precision := xprec ;
  574. e := t ;
  575. end ;
  576.  
  577. procedure toreal ( s : csingle ; var r : shortreal ) ;
  578.         (* kluge to call a csingle a shortreal *)
  579. type
  580. klugetype = record
  581.         case boolean of
  582.         false : ( sk : packed array[0..3] of -128..+127  ) ;
  583.         true : ( rk : shortreal ) ;
  584.         end ;
  585. var
  586. kluge : klugetype ;
  587. i : 0..3 ;
  588.  
  589. begin
  590. for i := 0 to 3 do kluge.sk[i] := setbite(s[i]) ;
  591. r := kluge.rk ;
  592. end ;
  593.  
  594. procedure fromreal ( r : shortreal ; var s : csingle ) ;
  595.         (* kluge to call a shortreal a csingle  *)
  596. type
  597. klugetype = record
  598.         case boolean of
  599.         false : ( sk : packed array[0..3] of -128..+127  ) ;
  600.         true : ( rk : shortreal ) ;
  601.         end ;
  602. var
  603. kluge : klugetype ;
  604. i : 0..3 ;
  605.  
  606. begin
  607. kluge.rk := r  ;
  608. for i := 0 to 3 do s[i] := getbite(kluge.sk[i]) ;
  609. end ;
  610.  
  611. procedure pretest (* var storemode : arithtype *) ;
  612. begin
  613. storemode := flt32 ;
  614. end ;
  615.  
  616.  
  617. procedure tstore (* var z : internal *) ;
  618. begin end ;
  619.  
  620. procedure tabs(* x : internal ; var z : internal *) ;
  621. var
  622. xs : csingle ; xr : shortreal ;
  623. begin
  624. tosingle(x,xs) ; toreal (xs,xr) ;
  625. xr := abs(xr) ;
  626. fromreal(xr,xs) ; unpacksingle(xs,z) ;
  627. end ;
  628.  
  629. procedure tsqrt(* x : internal ; var z : internal *) ;
  630. var
  631. xs : csingle ; xr : shortreal ;
  632. begin
  633. tosingle(x,xs) ; toreal (xs,xr) ;
  634. fromreal(sqrt(xr),xs) ; unpacksingle(xs,z) ;
  635. end ;
  636.  
  637. procedure tneg(* x : internal ; var z : internal *) ;
  638. var
  639. xs : csingle ; xr : shortreal ;
  640. begin
  641. tosingle(x,xs) ; toreal (xs,xr) ;
  642. xr := -xr ;
  643. fromreal(xr,xs) ; unpacksingle(xs,z) ;
  644. end ;
  645.  
  646.  
  647. procedure tadd (* x, y : internal ; var z : internal *) ; 
  648. var
  649. xs,ys,zs : csingle ;
  650. xr,yr,zr : shortreal ;
  651. begin
  652. tosingle(x,xs) ; toreal(xs,xr) ; tosingle(y,ys) ; toreal(ys,yr) ;
  653. zr := xr + yr ;
  654. fromreal(zr,zs) ;
  655. unpacksingle(zs,z) ;
  656. end ;
  657.  
  658. procedure tsub (* x, y : internal ; var z : internal *) ; 
  659. var
  660. xs,ys,zs : csingle ;
  661. xr,yr,zr : shortreal ;
  662. begin
  663. tosingle(x,xs) ; toreal(xs,xr) ; tosingle(y,ys) ; toreal(ys,yr) ;
  664. zr := xr - yr ;
  665. fromreal(zr,zs) ;
  666. unpacksingle(zs,z) ;
  667. end ;
  668.  
  669. procedure tmul (* x, y : internal ; var z : internal *) ; 
  670. var
  671. xs,ys,zs : csingle ;
  672. xr,yr,zr : shortreal ;
  673. begin
  674. tosingle(x,xs) ; toreal(xs,xr) ; tosingle(y,ys) ; toreal(ys,yr) ;
  675. zr := xr * yr ;
  676. fromreal(zr,zs) ;
  677. unpacksingle(zs,z) ;
  678. end ;
  679.  
  680. procedure tdiv (* x, y : internal ; var z : internal *) ; 
  681. var
  682. xs,ys,zs : csingle ;
  683. xr,yr,zr : shortreal ;
  684. begin
  685. tosingle(x,xs) ; toreal(xs,xr) ; tosingle(y,ys) ; toreal(ys,yr) ;
  686. zr := xr / yr ;
  687. fromreal(zr,zs) ;
  688. unpacksingle(zs,z) ;
  689. end ;
  690.  
  691. procedure trem (* x, y : internal ; var z : internal *) ; 
  692. var
  693. xs,ys,zs : csingle ;
  694. xr,yr,zr : shortreal ;
  695. begin
  696. tosingle(x,xs) ; toreal(xs,xr) ; tosingle(y,ys) ; toreal(ys,yr) ;
  697. fromreal(xr - yr * round(xr/yr),zs) ;
  698. unpacksingle(zs,z) ;
  699. end ;
  700.  
  701. procedure tcompare (* x, y : internal ; var cc : conditioncode *) ;
  702. var
  703. xs,ys,zs : csingle ;
  704. xr,yr,zr : shortreal ;
  705. begin
  706. tosingle(x,xs) ; toreal(xs,xr) ; tosingle(y,ys) ; toreal(ys,yr) ;
  707. write ( ' Tests affirm these predicates: ') ;
  708. if xr=yr then write(' EQ ') ;
  709. IF XR<>YR THEN write(' NE ') ;
  710. IF XR<YR THEN write(' LT ') ;
  711. IF XR<=YR THEN write(' LE ') ;
  712. IF XR>YR THEN write(' GT ') ;
  713. IF XR>=YR THEN write(' GE ') ;
  714. writeln ;
  715. IF xr=yr then cc := equal else
  716. if xr>yr then cc := greater else
  717. if xr<yr then cc := lesser else
  718. cc := notord ;
  719. end ;
  720.  
  721. procedure tconvert(* x : internal ; var z : internal ; a : arithtype *) ;
  722. var yx : cextended ; yd : cdouble ; ys : csingle ; 
  723. yi64 : cint64 ; yi16 : integer ;
  724. xs : csingle ; xr : shortreal ; xl : longint ;
  725. begin
  726. If a=i32 then begin
  727. tosingle(x,xs) ; toreal(xs,xr) ;
  728. xl := round(xr) ;
  729. yi16 := xl ;
  730. writeln(' Intermediate i16 ',yi16) ;
  731. xr := xl ;
  732. fromreal(xr,xs) ; unpacksingle(xs,z) ;
  733. end 
  734. else begin
  735. z := x ;
  736. end
  737. end ;
  738.  
  739. procedure tintconvert(* x : internal ; var z : internal ; a : arithtype *) ;
  740. var yx : cextended ; yd : cdouble ; ys : csingle ; 
  741. yi64 : cint64 ; yi16 : integer ;
  742. xs : csingle ; xr : shortreal ; xl : longint ;
  743. begin
  744. If a=i32 then begin
  745. tosingle(x,xs) ; toreal(xs,xr) ;
  746. xl := trunc(xr) ;
  747. yi16 := xl ;
  748. writeln(' Intermediate i16 ',yi16) ;
  749. xr := xl ;
  750. fromreal(xr,xs) ; unpacksingle(xs,z) ;
  751. end 
  752. else begin
  753. z := x ;
  754. end
  755. end ;
  756.  
  757. procedure tdisplay(* x : internal *) ;
  758.  
  759. var
  760. xs : csingle ; xr : shortreal ;
  761. s : fpstring ; i,j : integer ; error : boolean ;
  762.  
  763. begin
  764. tosingle(x,xs) ; toreal(xs,xr) ;
  765. {write  (' Free ') ;
  766. for i := 1 to 4 do begin
  767. f32_ascii(xr,5*i- 1,0,0,fp_free,s,error ) ;
  768. for j := length(s)+1 to 5*i-1 do write(' ') ;
  769. write(' ',s) ;
  770. end ;
  771. writeln ;
  772. write  (' Lisa ') ;
  773. for i := 1 to 4 do begin
  774. f32_ascii(xr,5*i-1,0,0,fp_lisa,s,error ) ;
  775. for j := length(s)+1 to 5*i-1 do write(' ') ;
  776. write(' ',s) ;
  777. end ;
  778. writeln ;
  779. }
  780. writeln(' Efmt ',xr:5, xr:10, xr : 15, xr : 20 ) ;
  781. writeln(' Ffmt ', xr : 5 : 0, xr : 10 : 5, xr : 15 : 7, xr : 20 : 10 ) ;
  782. end ;
  783.  
  784. procedure tdecbin 
  785.         (* s : fpstring ; var xout : internal  ; var error : boolean *) ;
  786. (* converts decimal fpstring s to internal format *)
  787. (* error is set true if bad format *)
  788.  
  789.  
  790. var
  791. r : shortreal ;
  792. xs : csingle ;
  793. next : integer ;
  794. f : text ;
  795. i : integer ;
  796.  
  797. begin
  798. rewrite(f) ;
  799. for i := 1 to ord(s[0]) do write(f,s[i]) ;
  800. writeln(f) ;
  801. reset(f) ;
  802. readln(f,r) ;
  803. fromreal(r,xs) ; unpacksingle(xs,x) ;
  804. end ;
  805. End-Of-File
  806. echo Extracting calcf64.p
  807. cat >calcf64.p <<'End-Of-File'
  808.  
  809. (* File calcf64.p, Version 8 October 1984.  *)
  810.  
  811. (* This version of the calculator test unit tests 64 bit double precision
  812. IEEE arithmetic accessed by the "real" type.  *)
  813.  
  814. #include 'sane.h'
  815. #include 'oldfplib.h'
  816. #include 'calctest.h'
  817. #include 'calcdouble.h'
  818.  
  819. type bite = -128..+127 ;
  820.  
  821. function getbite ( b : bite ) : byt  ;
  822. begin
  823. if b >= 0 then getbite := b else getbite := b + 256 ;
  824. end   ;
  825.  
  826. function setbite ( b : byt ) : bite   ;
  827. begin
  828. if b < 128 then setbite := b else setbite := b - 256 ;
  829. end   ;
  830.  
  831. procedure swapexcep (* var e : excepset *) ;
  832.  
  833. var t : excepset ;
  834.  
  835. begin
  836. e := [] ;
  837. end ;
  838.  
  839. procedure swaptrap (* var e : excepset *) ;
  840.  
  841. var t : excepset ;
  842.  
  843. begin
  844. e := [] ;
  845. end ;
  846.  
  847. procedure swapmode (* var e : fpmodetype *) ;
  848.  
  849. var t : fpmodetype ;
  850.  
  851. begin
  852. t.round := rnear ;
  853. t.clos := affine ;
  854. t.norm := normalizing ;
  855. t.precision := xprec ;
  856. e := t ;
  857. end ;
  858.  
  859. procedure toreal ( s : cdouble ; var r : real ) ;
  860.         (* kluge to call a cdouble a real *)
  861. type
  862. klugetype = record
  863.         case boolean of
  864.         false : ( sk : packed array[0..7] of -128..+127  ) ;
  865.         true : ( rk : real ) ;
  866.         end ;
  867. var
  868. kluge : klugetype ;
  869. i : 0..7 ;
  870.  
  871. begin
  872. for i := 0 to 7 do kluge.sk[i] := setbite(s[i]) ;
  873. r := kluge.rk ;
  874. end ;
  875.  
  876. procedure fromreal ( r : real ; var s : cdouble ) ;
  877.         (* kluge to call a real a cdouble  *)
  878. type
  879. klugetype = record
  880.         case boolean of
  881.         false : ( sk : packed array[0..7] of -128..+127  ) ;
  882.         true : ( rk : real ) ;
  883.         end ;
  884. var
  885. kluge : klugetype ;
  886. i : 0..7 ;
  887.  
  888. begin
  889. kluge.rk := r  ;
  890. for i := 0 to 7 do s[i] := getbite(kluge.sk[i]) ;
  891. end ;
  892.  
  893. procedure pretest (* var storemode : arithtype *) ;
  894. begin
  895. storemode := f64 ;
  896. end ;
  897.  
  898.  
  899. procedure tstore (* var z : internal *) ;
  900. begin end ;
  901.  
  902. procedure tabs(* x : internal ; var z : internal *) ;
  903. var
  904. xs : cdouble ; xr : real ;
  905. begin
  906. todouble(x,xs) ; toreal (xs,xr) ;
  907. xr := abs(xr) ;
  908. fromreal(xr,xs) ; unpackdouble(xs,z) ;
  909. end ;
  910.  
  911. procedure tsqrt(* x : internal ; var z : internal *) ;
  912. var
  913. xs : cdouble ; xr : real ;
  914. begin
  915. todouble(x,xs) ; toreal (xs,xr) ;
  916. fromreal(sqrt(xr),xs) ; unpackdouble(xs,z) ;
  917. end ;
  918.  
  919. procedure tneg(* x : internal ; var z : internal *) ;
  920. var
  921. xs : cdouble ; xr : real ;
  922. begin
  923. todouble(x,xs) ; toreal (xs,xr) ;
  924. xr := -xr ;
  925. fromreal(xr,xs) ; unpackdouble(xs,z) ;
  926. end ;
  927.  
  928.  
  929. procedure tadd (* x, y : internal ; var z : internal *) ; 
  930. var
  931. xs,ys,zs : cdouble ;
  932. xr,yr,zr : real ;
  933. begin
  934. todouble(x,xs) ; toreal(xs,xr) ; todouble(y,ys) ; toreal(ys,yr) ;
  935. zr := xr + yr ;
  936. fromreal(zr,zs) ;
  937. unpackdouble(zs,z) ;
  938. end ;
  939.  
  940. procedure tsub (* x, y : internal ; var z : internal *) ; 
  941. var
  942. xs,ys,zs : cdouble ;
  943. xr,yr,zr : real ;
  944. begin
  945. todouble(x,xs) ; toreal(xs,xr) ; todouble(y,ys) ; toreal(ys,yr) ;
  946. zr := xr - yr ;
  947. fromreal(zr,zs) ;
  948. unpackdouble(zs,z) ;
  949. end ;
  950.  
  951. procedure tmul (* x, y : internal ; var z : internal *) ; 
  952. var
  953. xs,ys,zs : cdouble ;
  954. xr,yr,zr : real ;
  955. begin
  956. todouble(x,xs) ; toreal(xs,xr) ; todouble(y,ys) ; toreal(ys,yr) ;
  957. zr := xr * yr ;
  958. fromreal(zr,zs) ;
  959. unpackdouble(zs,z) ;
  960. end ;
  961.  
  962. procedure tdiv (* x, y : internal ; var z : internal *) ; 
  963. var
  964. xs,ys,zs : cdouble ;
  965. xr,yr,zr : real ;
  966. begin
  967. todouble(x,xs) ; toreal(xs,xr) ; todouble(y,ys) ; toreal(ys,yr) ;
  968. zr := xr / yr ;
  969. fromreal(zr,zs) ;
  970. unpackdouble(zs,z) ;
  971. end ;
  972.  
  973. procedure trem (* x, y : internal ; var z : internal *) ; 
  974. var
  975. xs,ys,zs : cdouble ;
  976. xr,yr,zr : real ;
  977. begin
  978. todouble(x,xs) ; toreal(xs,xr) ; todouble(y,ys) ; toreal(ys,yr) ;
  979. fromreal(xr - yr * round(xr/yr),zs) ;
  980. unpackdouble(zs,z) ;
  981. end ;
  982.  
  983. procedure tcompare (* x, y : internal ; var cc : conditioncode *) ;
  984. var
  985. xs,ys,zs : cdouble ;
  986. xr,yr,zr : real ;
  987. begin
  988. todouble(x,xs) ; toreal(xs,xr) ; todouble(y,ys) ; toreal(ys,yr) ;
  989. write ( ' Tests affirm these predicates: ') ;
  990. if xr=yr then write(' EQ ') ;
  991. IF XR<>YR THEN write(' NE ') ;
  992. IF XR<YR THEN write(' LT ') ;
  993. IF XR<=YR THEN write(' LE ') ;
  994. IF XR>YR THEN write(' GT ') ;
  995. IF XR>=YR THEN write(' GE ') ;
  996. writeln ;
  997. IF xr=yr then cc := equal else
  998. if xr>yr then cc := greater else
  999. if xr<yr then cc := lesser else
  1000. cc := notord ;
  1001. end ;
  1002.  
  1003. procedure tconvert(* x : internal ; var z : internal ; a : arithtype *) ;
  1004. var yx : cextended ; yd : cdouble ; ys : csingle ; 
  1005. yi64 : cint64 ; yi16 : integer ;
  1006. xs : cdouble ; xr : real ; xl : longint ;
  1007. begin
  1008. If a=i32 then begin
  1009. todouble(x,xs) ; toreal(xs,xr) ;
  1010. xl := round(xr) ;
  1011. yi16 := xl ;
  1012. writeln(' Intermediate i16 ',yi16) ;
  1013. xr := xl ;
  1014. fromreal(xr,xs) ; unpackdouble(xs,z) ;
  1015. end 
  1016. else begin
  1017. z := x ;
  1018. end
  1019. end ;
  1020.  
  1021. procedure tintconvert(* x : internal ; var z : internal ; a : arithtype *) ;
  1022. var yx : cextended ; yd : cdouble ; ys : csingle ; 
  1023. yi64 : cint64 ; yi16 : integer ;
  1024. xs : cdouble ; xr : real ; xl : longint ;
  1025. begin
  1026. If a=i32 then begin
  1027. todouble(x,xs) ; toreal(xs,xr) ;
  1028. xl := trunc(xr) ;
  1029. yi16 := xl ;
  1030. writeln(' Intermediate i16 ',yi16) ;
  1031. xr := xl ;
  1032. fromreal(xr,xs) ; unpackdouble(xs,z) ;
  1033. end 
  1034. else begin
  1035. z := x ;
  1036. end
  1037. end ;
  1038.  
  1039. procedure tdisplay(* x : internal *) ;
  1040.  
  1041. var
  1042. xs : cdouble ; xr : real ;
  1043. s : fpstring ; i,j : integer ; error : boolean ;
  1044.  
  1045. begin
  1046. todouble(x,xs) ; toreal(xs,xr) ;
  1047. {write  (' Free ') ;
  1048. for i := 1 to 4 do begin
  1049. f32_ascii(xr,5*i- 1,0,0,fp_free,s,error ) ;
  1050. for j := length(s)+1 to 5*i-1 do write(' ') ;
  1051. write(' ',s) ;
  1052. end ;
  1053. writeln ;
  1054. write  (' Lisa ') ;
  1055. for i := 1 to 4 do begin
  1056. f32_ascii(xr,5*i-1,0,0,fp_lisa,s,error ) ;
  1057. for j := length(s)+1 to 5*i-1 do write(' ') ;
  1058. write(' ',s) ;
  1059. end ;
  1060. writeln ;
  1061. }
  1062. writeln(' Efmt ',xr:5, xr:10, xr : 15, xr : 20 ) ;
  1063. writeln(' Ffmt ', xr : 5 : 0, xr : 10 : 5, xr : 15 : 7, xr : 20 : 10 ) ;
  1064. end ;
  1065.  
  1066. procedure tdecbin 
  1067.         (* s : fpstring ; var xout : internal  ; var error : boolean *) ;
  1068. (* converts decimal fpstring s to internal format *)
  1069. (* error is set true if bad format *)
  1070.  
  1071.  
  1072. var
  1073. r : real ;
  1074. xs : cdouble ;
  1075. next : integer ;
  1076. f : text ;
  1077. i : integer ;
  1078.  
  1079. begin
  1080. rewrite(f) ;
  1081. for i := 1 to ord(s[0]) do write(f,s[i]) ;
  1082. writeln(f) ;
  1083. reset(f) ;
  1084. readln(f,r) ;
  1085. fromreal(r,xs) ; unpackdouble(xs,x) ;
  1086. end ;
  1087. End-Of-File
  1088. echo Extracting calctest.p
  1089. cat >calctest.p <<'End-Of-File'
  1090.  
  1091. (* File calctest.p, Version 5 October 1984. *)
  1092.  
  1093. #include 'sane.h'
  1094. #include 'oldfplib.h'
  1095. #include 'calctest.h'
  1096.  
  1097. procedure pretest (* var storemode : arithtype *)  ;
  1098. begin
  1099. end ;
  1100.  
  1101. procedure tstore (* var z : internal *) ;
  1102. begin
  1103. end ;
  1104.  
  1105. procedure tadd (* x, y : internal ; var z : internal *) ; 
  1106. begin 
  1107. end ;
  1108.  
  1109. procedure tsub (* x, y : internal ; var z : internal *) ; 
  1110. begin 
  1111. end ;
  1112.  
  1113. procedure tmul  (*  x, y : internal ; var z : internal  *) ;  
  1114. begin 
  1115. end ;
  1116.  
  1117. procedure tdiv  (*  x, y : internal ; var z : internal  *) ;  
  1118. begin 
  1119. end ;
  1120.  
  1121. procedure trem  (*  x, y : internal ; var z : internal  *) ;  
  1122. begin 
  1123. end ;
  1124.  
  1125. procedure tcompare  (*  x, y : internal ; var cc : conditioncode  *) ; 
  1126. begin 
  1127. end ;
  1128.  
  1129. procedure tconvert (*  x : internal ; var z : internal ; a : arithtype  *) ; 
  1130. begin
  1131. end ;
  1132.  
  1133. procedure tintconvert 
  1134.         (*  x : internal ; var z : internal ; a : arithtype  *) ; 
  1135. begin
  1136. end ;
  1137.  
  1138. procedure tabs (*  x : internal ; var z : internal  *) ; 
  1139. begin
  1140. end ;
  1141.  
  1142. procedure tsqrt (*  x : internal ; var z : internal  *) ; 
  1143. begin
  1144. end ;
  1145.  
  1146. procedure tneg (*  x : internal ; var z : internal  *) ; 
  1147. begin
  1148. end ;
  1149.  
  1150. procedure tdisplay (*  x : internal  *) ; 
  1151.  
  1152. begin
  1153. end ;
  1154.  
  1155. procedure tdecbin
  1156.   (*  s : fpstring ; var x : internal ; var error : boolean  *) ; 
  1157. begin
  1158. end ;
  1159.  
  1160. procedure swapexcep  (*  var e : excepset  *) ; 
  1161. begin
  1162. end ;
  1163.  
  1164. procedure swaptrap  (*  var e : excepset  *) ; 
  1165. begin
  1166. end ;
  1167.  
  1168. procedure swapmode  (*  var e : fpmodetype  *) ; 
  1169. begin
  1170. end ;
  1171. End-Of-File
  1172. echo ""
  1173. echo "End of Kit"
  1174. exit
  1175.  
  1176.