tbsx1.pp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. {$mode objfpc}
  2. program testbitscan;
  3. function test_byte: boolean;
  4. var
  5. x8,f,r: byte;
  6. i: integer;
  7. begin
  8. for i:=0 to 7 do
  9. begin
  10. x8:=1 shl i;
  11. f:=BsfByte(x8 or ((7-i) shl i));
  12. if (f<>i) then
  13. begin
  14. writeln('BsfByte($',hexstr(x8 or ((7-i) shl i),2),') returned ',f,', should be ',i);
  15. exit(false);
  16. end;
  17. r:=BsrByte(x8 or i);
  18. if r<>i then
  19. begin
  20. writeln('BsrByte($',hexstr(x8 or i,2),') returned ',r,', should be ',i);
  21. exit(false);
  22. end;
  23. end;
  24. x8:=0;
  25. f:=BsfByte(x8);
  26. if (f<>$ff) then
  27. begin
  28. writeln('BsfByte(',x8,') returned ',f,', should be ',$ff);
  29. exit(false);
  30. end;
  31. r:=BsrByte(x8);
  32. if r<>$ff then
  33. begin
  34. writeln('BsrByte(',x8,') returned ',r,', should be ',$ff);
  35. exit(false);
  36. end;
  37. result:=true;
  38. end;
  39. function test_word: boolean;
  40. var
  41. x16: word;
  42. i,f,r: integer;
  43. begin
  44. for i:=0 to 15 do
  45. begin
  46. x16:=1 shl i;
  47. f:=BsfWord(x16 or ((15-i) shl i));
  48. if (f<>i) then
  49. begin
  50. writeln('BsfWord(',x16,') returned ',f,', should be ',i);
  51. exit(false);
  52. end;
  53. r:=BsrWord(x16 or i);
  54. if r<>i then
  55. begin
  56. writeln('BsrWord(',x16,') returned ',r,', should be ',i);
  57. exit(false);
  58. end;
  59. end;
  60. x16:=0;
  61. f:=BsfWord(x16);
  62. if (f<>$ff) then
  63. begin
  64. writeln('BsfWord(',x16,') returned ',f,', should be ',$ff);
  65. exit(false);
  66. end;
  67. r:=BsrWord(x16);
  68. if r<>$ff then
  69. begin
  70. writeln('BsrWord(',x16,') returned ',r,', should be ',$ff);
  71. exit(false);
  72. end;
  73. result:=true;
  74. end;
  75. function test_dword: boolean;
  76. var
  77. x32: cardinal;
  78. i,f,r: integer;
  79. begin
  80. for i:=0 to 31 do
  81. begin
  82. x32:=cardinal(1) shl i;
  83. f:=BsfDWord(x32 or ((31-i) shl i));
  84. if (f<>i) then
  85. begin
  86. writeln('BsfDWord(',x32,') returned ',f,', should be ',i);
  87. exit(false);
  88. end;
  89. r:=BsrDWord(x32 or i);
  90. if r<>i then
  91. begin
  92. writeln('BsrDWord(',x32,') returned ',r,', should be ',i);
  93. exit(false);
  94. end;
  95. end;
  96. x32:=0;
  97. f:=BsfDWord(x32);
  98. if (f<>$ff) then
  99. begin
  100. writeln('BsfDWord(',x32,') returned ',f,', should be ',$ff);
  101. exit(false);
  102. end;
  103. r:=BsrDWord(x32);
  104. if r<>$ff then
  105. begin
  106. writeln('BsrDWord(',x32,') returned ',r,', should be ',$ff);
  107. exit(false);
  108. end;
  109. result:=true;
  110. end;
  111. function test_qword: boolean;
  112. var
  113. x64: qword;
  114. i, f, r: integer;
  115. begin
  116. for i:=0 to 63 do
  117. begin
  118. x64:=uint64(1) shl i;
  119. f:=BsfQWord(x64 or (uint64(63-i) shl i));
  120. if f<>i then begin
  121. writeln('BsfQWord(',x64,') returned ',f,', should be ',i);
  122. exit(false);
  123. end;
  124. r:=BsrQWord(x64 or i);
  125. if r<>i then begin
  126. writeln('BsrQWord(',x64,') returned ',r,', should be ',i);
  127. exit(false);
  128. end;
  129. end;
  130. x64:=0;
  131. f:=BsfQWord(x64);
  132. if (f<>$ff) then
  133. begin
  134. writeln('BsfQWord(',x64,') returned ',f,', should be ',$ff);
  135. exit(false);
  136. end;
  137. r:=BsrQWord(x64);
  138. if r<>$ff then
  139. begin
  140. writeln('BsrQWord(',x64,') returned ',r,', should be ',$ff);
  141. exit(false);
  142. end;
  143. result:=true;
  144. end;
  145. begin
  146. if test_byte then writeln('passed') else halt(1);
  147. if test_word then writeln('passed') else halt(1);
  148. if test_dword then writeln('passed') else halt(1);
  149. if test_qword then writeln('passed') else halt(1);
  150. end.