scheme_1_mult_bits.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. ##### 128 ###########
  2. import numpy as np
  3. from matplotlib import pyplot as plt
  4. import pandas as pd
  5. from pandas import Series
  6. import random
  7. num = 8
  8. err = [0, 0, 0, 0, 0,0]
  9. err1 = [0, 0, 0, 0, 0,0]
  10. get_bin = lambda x, n: format(x, 'b').zfill(n)
  11. x=list(range(100000))
  12. x1=list(range(6))
  13. yans=list(range(100000))
  14. yans1=list(range(100000))
  15. res = []
  16. res1 = []
  17. mean_error_distance = 0
  18. mean_relative_error_distance = 0
  19. normalized_error_distance = 0
  20. normailzed_relative_error_distance = 0
  21. acceptance_prob = 0
  22. for c in range(100000):
  23. res.append(random.randint(1, 340282366920938463463374607431768211455))
  24. for d in range(100000):
  25. res1.append(random.randint(1, 340282366920938463463374607431768211455))
  26. for g in range (6):
  27. mean_error_distance_1 = 0
  28. mean_relative_error_distance_1 = 0
  29. normalized_error_distance_1 = 0
  30. normailzed_relative_error_distance_1 = 0
  31. prob = 0
  32. for f in range (100000):
  33. a=res[f]
  34. b=res1[f]
  35. y=a*b
  36. abin = get_bin(a,128)
  37. bbin = get_bin(b,128)
  38. i=0
  39. while (abin[i]=='0'):
  40. i=i+1
  41. j=0
  42. while (bbin[j]=='0'):
  43. j=j+1
  44. k=i
  45. l=j
  46. sum1=128-k-num
  47. sum2=128-l-num
  48. if (sum1<0):
  49. sum1=0
  50. if (sum2<0):
  51. sum2=0
  52. sum3=(sum1+sum2)*-1
  53. amul=abin[k:k+num]
  54. bmul=bbin[l:l+num]
  55. q=int(amul,2)
  56. w=int(bmul,2)
  57. e=q*w
  58. yapp = get_bin(e,256)
  59. yapp = [int(x) for x in yapp]
  60. yapp = np.roll(yapp,sum3)
  61. yapp = ' '.join(str(e) for e in yapp)
  62. yapp = str.replace(yapp," ","")
  63. w=int(yapp,2)
  64. ans=((y-w)/y)*100
  65. yans[f]=ans
  66. yans1[f] = (y-w)
  67. mean_error_distance = (y-w) + mean_error_distance
  68. mean_relative_error_distance = mean_relative_error_distance + ans
  69. max1 = max(yans1)
  70. max2 = max(yans)
  71. if (ans>1):
  72. prob = prob + 1
  73. mean_error_distance = mean_error_distance/100000
  74. mean_relative_error_distance = mean_relative_error_distance/100000
  75. err[5] = mean_relative_error_distance
  76. normalized_error_distance = mean_error_distance/max1
  77. normailzed_relative_error_distance = mean_relative_error_distance/max2
  78. acceptance_prob = prob/100000
  79. acceptance_prob = 1-acceptance_prob
  80. err1[5] = acceptance_prob
  81. print ("*******************************")
  82. print ("MEAN ERROR DISTANCE : ", mean_error_distance)
  83. print ("mean_relative_error_distance : ", mean_relative_error_distance)
  84. print ("normalized_error_distance : ", normalized_error_distance)
  85. print ("normailzed_relative_error_distance : ", normailzed_relative_error_distance)
  86. print ("acceptance_probability at 1% : ", acceptance_prob)
  87. ##### 64 ###########
  88. import numpy as np
  89. from matplotlib import pyplot as plt
  90. import pandas as pd
  91. from pandas import Series
  92. import random
  93. get_bin = lambda x, n: format(x, 'b').zfill(n)
  94. x=list(range(100000))
  95. x1=list(range(6))
  96. yans=list(range(100000))
  97. yans1=list(range(100000))
  98. res = []
  99. res1 = []
  100. mean_error_distance = 0
  101. mean_relative_error_distance = 0
  102. normalized_error_distance = 0
  103. normailzed_relative_error_distance = 0
  104. acceptance_prob = 0
  105. for c in range(100000):
  106. res.append(random.randint(1, 18446744073709551615))
  107. for d in range(100000):
  108. res1.append(random.randint(1, 18446744073709551615))
  109. for g in range (6):
  110. mean_error_distance_1 = 0
  111. mean_relative_error_distance_1 = 0
  112. normalized_error_distance_1 = 0
  113. normailzed_relative_error_distance_1 = 0
  114. prob = 0
  115. for f in range (100000):
  116. a=res[f]
  117. b=res1[f]
  118. y=a*b
  119. abin = get_bin(a,64)
  120. bbin = get_bin(b,64)
  121. i=0
  122. while (abin[i]=='0'):
  123. i=i+1
  124. j=0
  125. while (bbin[j]=='0'):
  126. j=j+1
  127. k=i
  128. l=j
  129. sum1=64-k-num
  130. sum2=64-l-num
  131. if (sum1<0):
  132. sum1=0
  133. if (sum2<0):
  134. sum2=0
  135. sum3=(sum1+sum2)*-1
  136. amul=abin[k:k+num]
  137. bmul=bbin[l:l+num]
  138. q=int(amul,2)
  139. w=int(bmul,2)
  140. e=q*w
  141. yapp = get_bin(e,128)
  142. yapp = [int(x) for x in yapp]
  143. yapp = np.roll(yapp,sum3)
  144. yapp = ' '.join(str(e) for e in yapp)
  145. yapp = str.replace(yapp," ","")
  146. w=int(yapp,2)
  147. ans=((y-w)/y)*100
  148. yans[f]=ans
  149. yans1[f] = (y-w)
  150. mean_error_distance = (y-w) + mean_error_distance
  151. mean_relative_error_distance = mean_relative_error_distance + ans
  152. max1 = max(yans1)
  153. max2 = max(yans)
  154. if (ans>1):
  155. prob = prob + 1
  156. mean_error_distance = mean_error_distance/100000
  157. mean_relative_error_distance = mean_relative_error_distance/100000
  158. err[4] = mean_relative_error_distance
  159. normalized_error_distance = mean_error_distance/max1
  160. normailzed_relative_error_distance = mean_relative_error_distance/max2
  161. acceptance_prob = prob/100000
  162. acceptance_prob = 1-acceptance_prob
  163. err1[4] = acceptance_prob
  164. print ("*******************************")
  165. print ("MEAN ERROR DISTANCE : ", mean_error_distance)
  166. print ("mean_relative_error_distance : ", mean_relative_error_distance)
  167. print ("normalized_error_distance : ", normalized_error_distance)
  168. print ("normailzed_relative_error_distance : ", normailzed_relative_error_distance)
  169. print ("acceptance_probability at 1% : ", acceptance_prob)
  170. ##### 32 ###########
  171. import numpy as np
  172. from matplotlib import pyplot as plt
  173. import pandas as pd
  174. from pandas import Series
  175. import random
  176. get_bin = lambda x, n: format(x, 'b').zfill(n)
  177. x=list(range(100000))
  178. x1=list(range(6))
  179. yans=list(range(100000))
  180. yans1=list(range(100000))
  181. res = []
  182. res1 = []
  183. mean_error_distance = 0
  184. mean_relative_error_distance = 0
  185. normalized_error_distance = 0
  186. normailzed_relative_error_distance = 0
  187. acceptance_prob = 0
  188. for c in range(100000):
  189. res.append(random.randint(1, 4294967295))
  190. for d in range(100000):
  191. res1.append(random.randint(1, 4294967295))
  192. for g in range (6):
  193. mean_error_distance_1 = 0
  194. mean_relative_error_distance_1 = 0
  195. normalized_error_distance_1 = 0
  196. normailzed_relative_error_distance_1 = 0
  197. prob = 0
  198. for f in range (100000):
  199. a=res[f]
  200. b=res1[f]
  201. y=a*b
  202. abin = get_bin(a,32)
  203. bbin = get_bin(b,32)
  204. i=0
  205. while (abin[i]=='0'):
  206. i=i+1
  207. j=0
  208. while (bbin[j]=='0'):
  209. j=j+1
  210. k=i
  211. l=j
  212. sum1=32-k-num
  213. sum2=32-l-num
  214. if (sum1<0):
  215. sum1=0
  216. if (sum2<0):
  217. sum2=0
  218. sum3=(sum1+sum2)*-1
  219. amul=abin[k:k+num]
  220. bmul=bbin[l:l+num]
  221. q=int(amul,2)
  222. w=int(bmul,2)
  223. e=q*w
  224. yapp = get_bin(e,64)
  225. yapp = [int(x) for x in yapp]
  226. yapp = np.roll(yapp,sum3)
  227. yapp = ' '.join(str(e) for e in yapp)
  228. yapp = str.replace(yapp," ","")
  229. w=int(yapp,2)
  230. ans=((y-w)/y)*100
  231. yans[f]=ans
  232. yans1[f] = (y-w)
  233. mean_error_distance = (y-w) + mean_error_distance
  234. mean_relative_error_distance = mean_relative_error_distance + ans
  235. max1 = max(yans1)
  236. max2 = max(yans)
  237. if (ans>1):
  238. prob = prob + 1
  239. mean_error_distance = mean_error_distance/100000
  240. mean_relative_error_distance = mean_relative_error_distance/100000
  241. err[3] = mean_relative_error_distance
  242. normalized_error_distance = mean_error_distance/max1
  243. normailzed_relative_error_distance = mean_relative_error_distance/max2
  244. acceptance_prob = prob/100000
  245. acceptance_prob = 1-acceptance_prob
  246. err1[3] = acceptance_prob
  247. print ("*******************************")
  248. print ("MEAN ERROR DISTANCE : ", mean_error_distance)
  249. print ("mean_relative_error_distance : ", mean_relative_error_distance)
  250. print ("normalized_error_distance : ", normalized_error_distance)
  251. print ("normailzed_relative_error_distance : ", normailzed_relative_error_distance)
  252. print ("acceptance_probability at 1% : ", acceptance_prob)
  253. ######### 16 bit ######
  254. get_bin = lambda x, n: format(x, 'b').zfill(n)
  255. x=list(range(100000))
  256. x1=list(range(6))
  257. yans=list(range(100000))
  258. yans1=list(range(100000))
  259. res = []
  260. res1 = []
  261. mean_error_distance = 0
  262. mean_relative_error_distance = 0
  263. normalized_error_distance = 0
  264. normailzed_relative_error_distance = 0
  265. acceptance_prob = 0
  266. for c in range(100000):
  267. res.append(random.randint(1, 65535))
  268. for d in range(100000):
  269. res1.append(random.randint(1, 65535))
  270. for g in range (6):
  271. mean_error_distance_1 = 0
  272. mean_relative_error_distance_1 = 0
  273. normalized_error_distance_1 = 0
  274. normailzed_relative_error_distance_1 = 0
  275. prob = 0
  276. for f in range (100000):
  277. a=res[f]
  278. b=res1[f]
  279. y=a*b
  280. abin = get_bin(a,16)
  281. bbin = get_bin(b,16)
  282. i=0
  283. while (abin[i]=='0'):
  284. i=i+1
  285. j=0
  286. while (bbin[j]=='0'):
  287. j=j+1
  288. k=i
  289. l=j
  290. sum1=16-k-num
  291. sum2=16-l-num
  292. if (sum1<0):
  293. sum1=0
  294. if (sum2<0):
  295. sum2=0
  296. sum3=(sum1+sum2)*-1
  297. amul=abin[k:k+num]
  298. bmul=bbin[l:l+num]
  299. q=int(amul,2)
  300. w=int(bmul,2)
  301. e=q*w
  302. yapp = get_bin(e,32)
  303. yapp = [int(x) for x in yapp]
  304. yapp = np.roll(yapp,sum3)
  305. yapp = ' '.join(str(e) for e in yapp)
  306. yapp = str.replace(yapp," ","")
  307. w=int(yapp,2)
  308. ans=((y-w)/y)*100
  309. yans[f]=ans
  310. yans1[f] = (y-w)
  311. mean_error_distance = (y-w) + mean_error_distance
  312. mean_relative_error_distance = mean_relative_error_distance + ans
  313. max1 = max(yans1)
  314. max2 = max(yans)
  315. if (ans>1):
  316. prob = prob + 1
  317. mean_error_distance = mean_error_distance/100000
  318. mean_relative_error_distance = mean_relative_error_distance/100000
  319. err[2] = mean_relative_error_distance
  320. normalized_error_distance = mean_error_distance/max1
  321. normailzed_relative_error_distance = mean_relative_error_distance/max2
  322. acceptance_prob = prob/100000
  323. acceptance_prob = 1-acceptance_prob
  324. err1[2] = acceptance_prob
  325. print ("*******************************")
  326. print ("MEAN ERROR DISTANCE : ", mean_error_distance)
  327. print ("mean_relative_error_distance : ", mean_relative_error_distance)
  328. print ("normalized_error_distance : ", normalized_error_distance)
  329. print ("normailzed_relative_error_distance : ", normailzed_relative_error_distance)
  330. print ("acceptance_probability at 1% : ", acceptance_prob)
  331. ######### 8 bit ######
  332. get_bin = lambda x, n: format(x, 'b').zfill(n)
  333. x=list(range(100000))
  334. x1=list(range(6))
  335. yans=list(range(100000))
  336. yans1=list(range(100000))
  337. res = []
  338. res1 = []
  339. mean_error_distance = 0
  340. mean_relative_error_distance = 0
  341. normalized_error_distance = 0
  342. normailzed_relative_error_distance = 0
  343. acceptance_prob = 0
  344. for c in range(100000):
  345. res.append(random.randint(1, 255))
  346. for d in range(100000):
  347. res1.append(random.randint(1, 255))
  348. for g in range (6):
  349. mean_error_distance_1 = 0
  350. mean_relative_error_distance_1 = 0
  351. normalized_error_distance_1 = 0
  352. normailzed_relative_error_distance_1 = 0
  353. prob = 0
  354. for f in range (100000):
  355. a=res[f]
  356. b=res1[f]
  357. y=a*b
  358. abin = get_bin(a,8)
  359. bbin = get_bin(b,8)
  360. i=0
  361. while (abin[i]=='0'):
  362. i=i+1
  363. j=0
  364. while (bbin[j]=='0'):
  365. j=j+1
  366. k=i
  367. l=j
  368. sum1=8-k-num
  369. sum2=8-l-num
  370. if (sum1<0):
  371. sum1=0
  372. if (sum2<0):
  373. sum2=0
  374. sum3=(sum1+sum2)*-1
  375. amul=abin[k:k+num]
  376. bmul=bbin[l:l+num]
  377. q=int(amul,2)
  378. w=int(bmul,2)
  379. e=q*w
  380. yapp = get_bin(e,16)
  381. yapp = [int(x) for x in yapp]
  382. yapp = np.roll(yapp,sum3)
  383. yapp = ' '.join(str(e) for e in yapp)
  384. yapp = str.replace(yapp," ","")
  385. w=int(yapp,2)
  386. ans=((y-w)/y)*100
  387. yans[f]=ans
  388. yans1[f] = (y-w)
  389. mean_error_distance = (y-w) + mean_error_distance
  390. mean_relative_error_distance = mean_relative_error_distance + ans
  391. max1 = max(yans1)
  392. max2 = max(yans)
  393. if (ans>1):
  394. prob = prob + 1
  395. mean_error_distance = mean_error_distance/100000
  396. mean_relative_error_distance = mean_relative_error_distance/100000
  397. err[1] = mean_relative_error_distance
  398. #normalized_error_distance = mean_error_distance/max1
  399. #normailzed_relative_error_distance = mean_relative_error_distance/max2
  400. acceptance_prob = prob/100000
  401. acceptance_prob = 1-acceptance_prob
  402. err1[1] = acceptance_prob
  403. print ("*******************************")
  404. print ("MEAN ERROR DISTANCE : ", mean_error_distance)
  405. print ("mean_relative_error_distance : ", mean_relative_error_distance)
  406. print ("normalized_error_distance : ", normalized_error_distance)
  407. print ("normailzed_relative_error_distance : ", normailzed_relative_error_distance)
  408. print ("acceptance_probability at 1% : ", acceptance_prob)
  409. ######### 4 bit ######
  410. get_bin = lambda x, n: format(x, 'b').zfill(n)
  411. x=list(range(100000))
  412. x1=list(range(6))
  413. yans=list(range(100000))
  414. yans1=list(range(100000))
  415. res = []
  416. res1 = []
  417. mean_error_distance = 0
  418. mean_relative_error_distance = 0
  419. normalized_error_distance = 0
  420. normailzed_relative_error_distance = 0
  421. acceptance_prob = 0
  422. for c in range(100000):
  423. res.append(random.randint(1, 15))
  424. for d in range(100000):
  425. res1.append(random.randint(1, 15))
  426. for g in range (6):
  427. mean_error_distance_1 = 0
  428. mean_relative_error_distance_1 = 0
  429. normalized_error_distance_1 = 0
  430. normailzed_relative_error_distance_1 = 0
  431. prob = 0
  432. for f in range (100000):
  433. a=res[f]
  434. b=res1[f]
  435. y=a*b
  436. abin = get_bin(a,4)
  437. bbin = get_bin(b,4)
  438. i=0
  439. while (abin[i]=='0'):
  440. i=i+1
  441. j=0
  442. while (bbin[j]=='0'):
  443. j=j+1
  444. k=i
  445. l=j
  446. sum1=4-k-num
  447. sum2=4-l-num
  448. if (sum1<0):
  449. sum1=0
  450. if (sum2<0):
  451. sum2=0
  452. sum3=(sum1+sum2)*-1
  453. amul=abin[k:k+num]
  454. bmul=bbin[l:l+num]
  455. q=int(amul,2)
  456. w=int(bmul,2)
  457. e=q*w
  458. yapp = get_bin(e,8)
  459. yapp = [int(x) for x in yapp]
  460. yapp = np.roll(yapp,sum3)
  461. yapp = ' '.join(str(e) for e in yapp)
  462. yapp = str.replace(yapp," ","")
  463. w=int(yapp,2)
  464. ans=((y-w)/y)*100
  465. yans[f]=ans
  466. yans1[f] = (y-w)
  467. mean_error_distance = (y-w) + mean_error_distance
  468. mean_relative_error_distance = mean_relative_error_distance + ans
  469. max1 = max(yans1)
  470. max2 = max(yans)
  471. if (ans>1):
  472. prob = prob + 1
  473. mean_error_distance = mean_error_distance/100000
  474. mean_relative_error_distance = mean_relative_error_distance/100000
  475. err[0] = mean_relative_error_distance
  476. #normalized_error_distance = mean_error_distance/max1
  477. #normailzed_relative_error_distance = mean_relative_error_distance/max2
  478. acceptance_prob = prob/100000
  479. acceptance_prob = 1-acceptance_prob
  480. err1[0] = acceptance_prob
  481. print ("*******************************")
  482. print ("MEAN ERROR DISTANCE : ", mean_error_distance)
  483. print ("mean_relative_error_distance : ", mean_relative_error_distance)
  484. print ("normalized_error_distance : ", normalized_error_distance)
  485. print ("normailzed_relative_error_distance : ", normailzed_relative_error_distance)
  486. print ("acceptance_probability at 1% : ", acceptance_prob)
  487. p = (0,1,2,3,4,5,6)
  488. l = (4,8,16,32,64,128)
  489. plt.plot (x1,err)
  490. plt.title ('RELATIVE ERROR VS. LENGTH OF MULTIPLIER')
  491. plt.xticks(p,l)
  492. plt.ylabel('Relative Error')
  493. plt.xlabel('Length Of Multiplier (Latency Parameter = 8)')
  494. plt.show()
  495. plt.plot (x1,err1)
  496. plt.title ('ACCEPTANCE PROBABILITY VS. LENGTH OF MULTIPLIER')
  497. plt.xticks(p,l)
  498. plt.ylabel('Acceptance Probability (1% Relative Error)')
  499. plt.xlabel('Length Of Multiplier (Latency Parameter = 8)')
  500. plt.show()