32_scheme_2.v 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. `timescale 1ns/1ps
  2. ///////////// 32*32 bit multiplier scheme 2/////////
  3. module approx_multiplier_1 (input [31:0]a, input [31:0]b, output reg [63:0]y );
  4. integer i,j,sum1,sum2,sum,k,l,num; //k and l are the bits which will detect leading 1
  5. reg [7:0]m=0; // m and n are the two numbers to be multiplied
  6. reg [7:0]n=0;
  7. always @(a or b)
  8. begin
  9. if (a[31] || a[30] || a[29] || a[28] || b[31] || b[30] || b[29] || b[28] )
  10. begin
  11. num=8;
  12. end
  13. else if ( a[27] || a[26] || a[25] || a[24] || b[27] || b[26] || b[25] || b[24] )
  14. begin
  15. num=7;
  16. end
  17. else
  18. begin
  19. num=6;
  20. end
  21. // while statement is not synthesisable in verilog so we nested if else statement is used
  22. if (a[31]==1)
  23. begin
  24. k=31;
  25. end
  26. else if(a[30]==1)
  27. begin
  28. k=30;
  29. end
  30. else if(a[29]==1)
  31. begin
  32. k=29;
  33. end
  34. else if(a[28]==1)
  35. begin
  36. k=28;
  37. end
  38. else if(a[27]==1)
  39. begin
  40. k=27;
  41. end
  42. else if(a[26]==1)
  43. begin
  44. k=26;
  45. end
  46. else if (a[25]==1)
  47. begin
  48. k=25;
  49. end
  50. else if(a[24]==1)
  51. begin
  52. k=24;
  53. end
  54. else if(a[23]==1)
  55. begin
  56. k=23;
  57. end
  58. else if(a[22]==1)
  59. begin
  60. k=22;
  61. end
  62. else if(a[21]==1)
  63. begin
  64. k=21;
  65. end
  66. else if(a[20]==1)
  67. begin
  68. k=20;
  69. end
  70. else if(a[19]==1)
  71. begin
  72. k=19;
  73. end
  74. else if(a[18]==1)
  75. begin
  76. k=18;
  77. end
  78. else if(a[17]==1)
  79. begin
  80. k=17;
  81. end
  82. else if(a[16]==1)
  83. begin
  84. k=16;
  85. end
  86. else if(a[15]==1)
  87. begin
  88. k=15;
  89. end
  90. else if(a[14]==1)
  91. begin
  92. k=14;
  93. end
  94. else if(a[13]==1)
  95. begin
  96. k=13;
  97. end
  98. else if(a[12]==1)
  99. begin
  100. k=12;
  101. end
  102. else if(a[11]==1)
  103. begin
  104. k=11;
  105. end
  106. else if(a[10]==1)
  107. begin
  108. k=10;
  109. end
  110. else if(a[9]==1)
  111. begin
  112. k=9;
  113. end
  114. else if(a[8]==1)
  115. begin
  116. k=8;
  117. end
  118. else if(a[7]==1)
  119. begin
  120. k=7;
  121. end
  122. else if(a[6]==1)
  123. begin
  124. k=6;
  125. end
  126. else if(a[5]==1)
  127. begin
  128. k=5;
  129. end
  130. else if(a[4]==1)
  131. begin
  132. k=4;
  133. end
  134. else if(a[3]==1)
  135. begin
  136. k=3;
  137. end
  138. else if(a[2]==1)
  139. begin
  140. k=2;
  141. end
  142. else if(a[1]==1)
  143. begin
  144. k=1;
  145. end
  146. else
  147. begin
  148. k=0;
  149. end
  150. if (b[31]==1)
  151. begin
  152. l=31;
  153. end
  154. else if(b[30]==1)
  155. begin
  156. l=30;
  157. end
  158. else if(b[29]==1)
  159. begin
  160. l=29;
  161. end
  162. else if(b[28]==1)
  163. begin
  164. l=28;
  165. end
  166. else if(b[27]==1)
  167. begin
  168. l=27;
  169. end
  170. else if(b[26]==1)
  171. begin
  172. l=26;
  173. end
  174. else if (b[25]==1)
  175. begin
  176. l=25;
  177. end
  178. else if(b[24]==1)
  179. begin
  180. l=24;
  181. end
  182. else if(b[23]==1)
  183. begin
  184. l=23;
  185. end
  186. else if(b[22]==1)
  187. begin
  188. l=22;
  189. end
  190. else if(b[21]==1)
  191. begin
  192. l=21;
  193. end
  194. else if(b[20]==1)
  195. begin
  196. l=20;
  197. end
  198. else if(b[19]==1)
  199. begin
  200. l=19;
  201. end
  202. else if(b[18]==1)
  203. begin
  204. l=18;
  205. end
  206. else if(b[17]==1)
  207. begin
  208. l=17;
  209. end
  210. else if(b[16]==1)
  211. begin
  212. l=16;
  213. end
  214. else if (b[15]==1)
  215. begin
  216. l=15;
  217. end
  218. else if(b[14]==1)
  219. begin
  220. l=14;
  221. end
  222. else if(b[13]==1)
  223. begin
  224. l=13;
  225. end
  226. else if(b[12]==1)
  227. begin
  228. l=12;
  229. end
  230. else if(b[11]==1)
  231. begin
  232. l=11;
  233. end
  234. else if(b[10]==1)
  235. begin
  236. l=10;
  237. end
  238. else if(b[9]==1)
  239. begin
  240. l=9;
  241. end
  242. else if(b[8]==1)
  243. begin
  244. l=8;
  245. end
  246. else if(b[7]==1)
  247. begin
  248. l=7;
  249. end
  250. else if(a[6]==1)
  251. begin
  252. l=6;
  253. end
  254. else if(b[5]==1)
  255. begin
  256. l=5;
  257. end
  258. else if(b[4]==1)
  259. begin
  260. l=4;
  261. end
  262. else if(b[3]==1)
  263. begin
  264. l=3;
  265. end
  266. else if(b[2]==1)
  267. begin
  268. l=2;
  269. end
  270. else if(b[1]==1)
  271. begin
  272. l=1;
  273. end
  274. else
  275. begin
  276. l=0;
  277. end
  278. m=0;
  279. n=0;
  280. sum1 = k-num;
  281. sum2 = l-num;
  282. if (sum1<0)
  283. begin
  284. sum1 = -1;
  285. end
  286. if (sum2 <0)
  287. begin
  288. sum2 = -1;
  289. end
  290. sum = sum1+sum2+2; // number of zeros which will be appended at the endof multiplication
  291. if (num==8)
  292. begin
  293. for (i=0;i<8;i=i+1)
  294. begin
  295. m[8-1-i]=a[k-i];
  296. n[8-1-i]=b[l-i];
  297. end
  298. end
  299. else if (num==7)
  300. begin
  301. for (i=0;i<7;i=i+1)
  302. begin
  303. m[7-1-i]=a[k-i];
  304. n[7-1-i]=b[l-i];
  305. end
  306. end
  307. else
  308. begin
  309. for (i=0;i<6;i=i+1)
  310. begin
  311. m[6-1-i]=a[k-i];
  312. n[6-1-i]=b[l-i];
  313. end
  314. end
  315. if (k<=num)
  316. begin
  317. m = a;
  318. end
  319. if (l<=num)
  320. begin
  321. n = b;
  322. end
  323. y=0;
  324. y=m*n;
  325. y=y<<sum; // appending of zeros at the end of multiplication
  326. end
  327. endmodule