next up previous
Next: 3.3 Computer Circuits Up: 3 Example Models Previous: 3.1.1 Experimentation


3.2 Computer Arithmetic

Arithmetic representations are easily modeled. Blaauw[Blaa 1976], one of the designers of the IBM Stretch and System/360 computers, recognized the importance of software modeling in the design process. Blaauw used APL for his software models. Following are models of IEEE 754 single precision inexact representations. fs2bin gives the binary representation of an inexact value.

fs2bin =: monad define
NB. check for infinities and nan's
if. _ = y.
   do. 0 1 1 1 1 1 1 1 1 , 23 copy 0
       return.
  elseif. __ = y.
   do. 1 1 1 1 1 1 1 1 1 , 23 copy 0
       return.
NB. this case is tricky since (for some reason)
NB. _. = _. is false.  Perhaps this is because
NB. IEEE 754 specifies _1 + 2 ^ 23 different
NB. representations for nan.
  elseif. 0 not_equal y. - y.
   do. 0 1 1 1 1 1 1 1 1 , 23 copy 1
       return.
end.
NB. compute the characteristic
e =. 1 + floor log2 | y. + y. = 0
NB. now the mantissa
f =. 1 drop (24 copy 2) rep floor (2 ^ 24 - e) * | y.
NB. finally get the sign and exponent (binary form)
se =. (9 copy 2) rep (y. not_equal 0) * (256 * y. < 0) + e + 126
se , f
)

bin2fs =: monad define
s =. 0 from y.
e =. base 1 2 3 4 5 6 7 8 from y.
f =. base 9 drop y.
if. (0 = e) and 0 = f
  do. 0
elseif. (255 = e) and 0 = f
  do. _ * _1 ^ s
elseif. (255 = e) and 0 not_equal f
  do. _.
elseif. (0 = e) and 0 not_equal f
  do. (_1 ^ s) * f * 2 ^ e - 126
elseif. 1
  do. (_1 ^ s) * (2 ^ e - 127) * (base 1 , 9 drop y.) % 2 ^ 23
end.
)

   fs2bin 0.01
0 0 1 1 1 1 0 0 0 0 1 0 0 0 1 1 1 1 0 1 0 1 1 1 0 0 0 0 1 0 1 0
   bin2fs 0 0 1 1 1 1 0 0 0 0 1 0 0 0 1 1 1 1 0 1 0 1 1 1 0 0 0 0 1 0 1 0
0.01


next up previous
Next: 3.3 Computer Circuits Up: 3 Example Models Previous: 3.1.1 Experimentation
2003-12-20