16_scheme_2.v 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. `timescale 1ns/1ps
  2. ///////////// a6*16 bit multiplier scheme 2/////////
  3. // acceptance prob = 0.96, mean relative error = 0.5273
  4. module approx_multiplier_1 (input [15:0]a, input [15:0]b, output reg [31:0]y);
  5. integer i,j,sum1,sum2,sum,c,k,l,num; //k and l are the bits which will detect leading 1
  6. reg [7:0]m; // m and n are the two numbers to be multiplied
  7. reg [7:0]n;
  8. always @(a or b)
  9. begin
  10. if (a[15] || a[14] || b[15] || b[14] || a[13] || b[13] )
  11. begin
  12. num=8;
  13. end
  14. else if (a[11] || a[10] || b[11] || b[10] || a[12] || b[12])
  15. begin
  16. num=7;
  17. end
  18. else if (a[9] || b[9] )
  19. begin
  20. num=6;
  21. end
  22. else
  23. begin
  24. num=5;
  25. end
  26. // while statement is not synthesisable in verilog so we nested if else statement is used
  27. if (a[15]==1)
  28. begin
  29. k=15;
  30. end
  31. else if(a[14]==1)
  32. begin
  33. k=14;
  34. end
  35. else if(a[13]==1)
  36. begin
  37. k=13;
  38. end
  39. else if(a[12]==1)
  40. begin
  41. k=12;
  42. end
  43. else if(a[11]==1)
  44. begin
  45. k=11;
  46. end
  47. else if(a[10]==1)
  48. begin
  49. k=10;
  50. end
  51. else if(a[9]==1)
  52. begin
  53. k=9;
  54. end
  55. else if(a[8]==1)
  56. begin
  57. k=8;
  58. end
  59. else if(a[7]==1)
  60. begin
  61. k=7;
  62. end
  63. else if(a[6]==1)
  64. begin
  65. k=6;
  66. end
  67. else if(a[5]==1)
  68. begin
  69. k=5;
  70. end
  71. else if(a[4]==1)
  72. begin
  73. k=4;
  74. end
  75. else if(a[3]==1)
  76. begin
  77. k=3;
  78. end
  79. else if(a[2]==1)
  80. begin
  81. k=2;
  82. end
  83. else if(a[1]==1)
  84. begin
  85. k=1;
  86. end
  87. else
  88. begin
  89. k=0;
  90. end
  91. if (b[15]==1)
  92. begin
  93. l=15;
  94. end
  95. else if(b[14]==1)
  96. begin
  97. l=14;
  98. end
  99. else if(b[13]==1)
  100. begin
  101. l=13;
  102. end
  103. else if(b[12]==1)
  104. begin
  105. l=12;
  106. end
  107. else if(b[11]==1)
  108. begin
  109. l=11;
  110. end
  111. else if(b[10]==1)
  112. begin
  113. l=10;
  114. end
  115. else if(b[9]==1)
  116. begin
  117. l=9;
  118. end
  119. else if(b[8]==1)
  120. begin
  121. l=8;
  122. end
  123. else if(b[7]==1)
  124. begin
  125. l=7;
  126. end
  127. else if(a[6]==1)
  128. begin
  129. l=6;
  130. end
  131. else if(b[5]==1)
  132. begin
  133. l=5;
  134. end
  135. else if(b[4]==1)
  136. begin
  137. l=4;
  138. end
  139. else if(b[3]==1)
  140. begin
  141. l=3;
  142. end
  143. else if(b[2]==1)
  144. begin
  145. l=2;
  146. end
  147. else if(b[1]==1)
  148. begin
  149. l=1;
  150. end
  151. else
  152. begin
  153. l=0;
  154. end
  155. // m and n are the number of bits used for approximate multiplication
  156. m=0;
  157. n=0;
  158. sum1 = k-num;
  159. sum2 = l-num;
  160. if (sum1<0)
  161. begin
  162. sum1 = -1;
  163. end
  164. if (sum2 <0)
  165. begin
  166. sum2 = -1;
  167. end
  168. sum = sum1+sum2+2; // number of zeros which will be appended at the end of multiplication
  169. if (num==8)
  170. begin
  171. for (i=0;i<8;i=i+1)
  172. begin
  173. m[8-1-i]=a[k-i];
  174. n[8-1-i]=b[l-i];
  175. end
  176. end
  177. else if (num==7)
  178. begin
  179. for (i=0;i<7;i=i+1)
  180. begin
  181. m[7-1-i]=a[k-i];
  182. n[7-1-i]=b[l-i];
  183. end
  184. end
  185. else if (num==6)
  186. begin
  187. for (i=0;i<6;i=i+1)
  188. begin
  189. m[6-1-i]=a[k-i];
  190. n[6-1-i]=b[l-i];
  191. end
  192. end
  193. else
  194. begin
  195. for (i=0;i<5;i=i+1)
  196. begin
  197. m[5-1-i]=a[k-i];
  198. n[5-1-i]=b[l-i];
  199. end
  200. end
  201. if (k<=num)
  202. begin
  203. m = a;
  204. end
  205. if (l<=num)
  206. begin
  207. n = b;
  208. end
  209. y=0;
  210. y=m*n;
  211. y=y<<sum; // appending of zeros at the end of multiplication
  212. end
  213. endmodule
  214. /* module tb_approx_multiplier_1();
  215. wire [31:0]y;
  216. reg [15:0]a;
  217. reg [15:0]b;
  218. approx_multiplier_1 M1(a,b,y);
  219. initial begin
  220. #0 a=56783; b=6723;
  221. #5 a=16'hFFAA; b=16'h08FA;
  222. #5 a=16'h0892; b=16'h081A;
  223. #5 a=16'h0A12; b=16'h01FB;
  224. #5 a=16'h0392; b=16'hC8CA;
  225. end
  226. initial begin
  227. $display ("time a b y ");
  228. $monitor (" %0d %0d %0d %0d ",$time,a,b,y);
  229. end
  230. endmodule
  231. */