simlib.pas 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. {
  2. $Id$
  3. This file is part of the Free Pascal simulator environment
  4. Copyright (c) 1999-2000 by Florian Klaempfl
  5. This unit implemements routines for data types which aren't
  6. support by commonly used compilers
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. {$N+}
  14. { we do some strange things here }
  15. {$O-}
  16. {$R-}
  17. unit simlib;
  18. interface
  19. uses
  20. simbase;
  21. procedure byte_zap(q : qword;b : byte;var r : qword);
  22. { shifts q b bytes left }
  23. procedure shift_left_q(q : qword;b : byte;var r : qword);
  24. { shifts q b bytes right }
  25. procedure shift_right_q(q : qword;b : byte;var r : qword);
  26. { returns true if i1<i2 assuming that c1 and c2 are unsigned !}
  27. function ltu(c1,c2 : qword) : boolean;
  28. { returns true if i1=<i2 assuming that c1 and c2 are unsigned !}
  29. function leu(c1,c2 : qword) : boolean;
  30. { adds to owords, returns true if an overflow occurs }
  31. function addoword(o1,o2 : oword;var r : oword) : boolean;
  32. { adds two words, returns true if an overflow occurs }
  33. function addword(w1,w2 : word;var r : word) : boolean;
  34. { sets an oword to zero }
  35. procedure zerooword(var o : oword);
  36. { multiplies two qwords into a full oword }
  37. procedure mulqword(q1,q2 : qword;var r : oword);
  38. implementation
  39. procedure byte_zap(q : qword;b : byte;var r : qword);
  40. var
  41. i : tindex;
  42. begin
  43. for i:=0 to 7 do
  44. if ((1 shl i) and b)=0 then
  45. tqwordrec(r).bytes[i]:=tqwordrec(q).bytes[i]
  46. else
  47. tqwordrec(r).bytes[i]:=0;
  48. end;
  49. { shifts q b bytes left }
  50. procedure shift_left_q(q : qword;b : byte;var r : qword);
  51. var
  52. i : tindex;
  53. begin
  54. r:=0;
  55. if b>63 then
  56. else if b>31 then
  57. tqwordrec(r).high32:=tqwordrec(q).low32 shl (b-32)
  58. else
  59. begin
  60. { bad solution! A qword shift would be nice! }
  61. r:=q;
  62. for i:=1 to b do
  63. begin
  64. tqwordrec(r).high32:=tqwordrec(r).high32 shl 1;
  65. if (tqwordrec(r).low32 and $80000000)<>0 then
  66. tqwordrec(r).high32:=tqwordrec(r).high32 or 1;
  67. tqwordrec(r).low32:=tqwordrec(r).low32 shl 1;
  68. end;
  69. end;
  70. end;
  71. { shifts q b bytes right }
  72. procedure shift_right_q(q : qword;b : byte;var r : qword);
  73. var
  74. i : tindex;
  75. begin
  76. r:=0;
  77. if b>63 then
  78. else if b>31 then
  79. tqwordrec(r).low32:=tqwordrec(q).high32 shr (b-32)
  80. else
  81. begin
  82. { bad solution! A qword shift would be nice! }
  83. r:=q;
  84. for i:=1 to b do
  85. begin
  86. tqwordrec(r).low32:=tqwordrec(r).low32 shr 1;
  87. if (tqwordrec(r).high32 and 1)<>0 then
  88. tqwordrec(r).low32:=tqwordrec(r).low32 or
  89. $80000000;
  90. tqwordrec(r).high32:=tqwordrec(r).high32 shr 1;
  91. end;
  92. end;
  93. end;
  94. { returns true if i1<i2 assuming that c1 and c2 are unsigned !}
  95. function ltu(c1,c2 : qword) : boolean;
  96. begin
  97. if (c1>=0) and (c2>=0) then
  98. ltu:=c1<c2
  99. else if (c1<0) and (c2>=0) then
  100. ltu:=false
  101. else if (c1>=0) and (c2<0) then
  102. ltu:=true
  103. else
  104. ltu:=c1<c2
  105. end;
  106. { returns true if i1=<i2 assuming that c1 and c2 are unsigned !}
  107. function leu(c1,c2 : qword) : boolean;
  108. begin
  109. if (c1>=0) and (c2>=0) then
  110. leu:=c1<=c2
  111. else if (c1<0) and (c2>=0) then
  112. leu:=false
  113. else if (c1>=0) and (c2<0) then
  114. leu:=true
  115. else
  116. leu:=c1<=c2
  117. end;
  118. { "ands" two qwords }
  119. procedure andqword(w1,w2 : qword;var r : qword);
  120. begin
  121. tqwordrec(r).low32:=tqwordrec(w1).low32 and tqwordrec(w2).low32;
  122. tqwordrec(r).high32:=tqwordrec(w1).high32 and tqwordrec(w2).high32;
  123. end;
  124. { adds two words, returns true if an overflow occurs }
  125. function addword(w1,w2 : word;var r : word) : boolean;
  126. var
  127. l : longint;
  128. begin
  129. l:=w1+w2;
  130. addword:=(l and $10000)<>0;
  131. r:=l and $ffff;
  132. end;
  133. { adds two owords, returns true if an overflow occurs }
  134. function addoword(o1,o2 : oword;var r : oword) : boolean;
  135. var
  136. i : tindex;
  137. carry : word;
  138. begin
  139. carry:=0;
  140. for i:=0 to 7 do
  141. begin
  142. r[i]:=o1[i]+o2[i]+carry;
  143. { an overflow has occured, if the r is less
  144. than one of the summands
  145. }
  146. if (r[i]<o1[i]) or (r[i]<o2[i]) then
  147. carry:=1
  148. else
  149. carry:=0;
  150. end;
  151. addoword:=carry=1;
  152. end;
  153. { sets an oword to zero }
  154. procedure zerooword(var o : oword);
  155. begin
  156. fillchar(o,sizeof(o),0);
  157. end;
  158. { multiplies two qwords into a full oword }
  159. procedure mulqword(q1,q2 : qword;var r : oword);
  160. var
  161. i : tindex;
  162. h,bitpos : qword;
  163. ho1 : oword;
  164. begin
  165. { r is zero }
  166. zerooword(ho1);
  167. r:=ho1;
  168. towordrec(ho1).low64:=q1;
  169. bitpos:=1;
  170. for i:=0 to 63 do
  171. begin
  172. andqword(q2,bitpos,h);
  173. if h<>0 then
  174. addoword(r,ho1,r);
  175. { ho1:=2*ho1 }
  176. addoword(ho1,ho1,ho1);
  177. shift_left_q(bitpos,1,bitpos);
  178. end;
  179. end;
  180. end.
  181. {
  182. $Log$
  183. Revision 1.2 2002-09-07 15:40:37 peter
  184. * old logs removed and tabs fixed
  185. }
  186. =======
  187. }
  188. >>>>>>> 1.3