mult_16_2.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. ## 16 bit multiplier scheme 2
  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. get_bin = lambda x, n: format(x, 'b').zfill(n)
  8. x=list(range(100000))
  9. x1=list(range(6))
  10. yans=list(range(100000))
  11. yans1=list(range(100000))
  12. res = []
  13. res1 = []
  14. mean_error_distance = 0
  15. mean_relative_error_distance = 0
  16. normalized_error_distance = 0
  17. normailzed_relative_error_distance = 0
  18. acceptance_prob = 0
  19. for c in range(100000):
  20. res.append(random.randint(1, 65535))
  21. for d in range(100000):
  22. res1.append(random.randint(1, 65535))
  23. for g in range (6):
  24. mean_error_distance_1 = 0
  25. mean_relative_error_distance_1 = 0
  26. normalized_error_distance_1 = 0
  27. normailzed_relative_error_distance_1 = 0
  28. prob = 0
  29. for f in range (100000):
  30. a=res[f]
  31. b=res1[f]
  32. y=a*b
  33. abin = get_bin(a,16)
  34. bbin = get_bin(b,16)
  35. if abin[0]=='1':
  36. num=8
  37. elif bbin[0]=='1':
  38. num=8
  39. elif abin[1]=='1':
  40. num=8
  41. elif bbin[1]=='1':
  42. num=8
  43. elif abin[2]=='1':
  44. num=8
  45. elif bbin[2]=='1':
  46. num=8
  47. elif abin[3]=='1':
  48. num=7
  49. elif bbin[3]=='1':
  50. num=7
  51. elif abin[4]=='1':
  52. num=7
  53. elif bbin[4]=='1':
  54. num=7
  55. elif abin[5]=='1':
  56. num=7
  57. elif bbin[5]=='1':
  58. num=7
  59. elif abin[6]=='1':
  60. num=6
  61. elif bbin[6]=='1':
  62. num=6
  63. else:
  64. num=5
  65. i=0
  66. while (abin[i]=='0'):
  67. i=i+1
  68. j=0
  69. while (bbin[j]=='0'):
  70. j=j+1
  71. k=i
  72. l=j
  73. sum1=16-k-num
  74. sum2=16-l-num
  75. if (sum1<0):
  76. sum1=0
  77. if (sum2<0):
  78. sum2=0
  79. sum3=(sum1+sum2)*-1
  80. if (k+num>16):
  81. k = 16-num
  82. if (l+num>16):
  83. l = 16-num
  84. amul=abin[k:k+num]
  85. bmul=bbin[l:l+num]
  86. q=int(amul,2)
  87. w=int(bmul,2)
  88. e=q*w
  89. yapp = get_bin(e,32)
  90. yapp = [int(x) for x in yapp]
  91. yapp = np.roll(yapp,sum3)
  92. yapp = ' '.join(str(e) for e in yapp)
  93. yapp = str.replace(yapp," ","")
  94. w=int(yapp,2)
  95. ans=((y-w)/y)*100
  96. yans[f]=ans
  97. yans1[f] = (y-w)
  98. mean_error_distance = (y-w) + mean_error_distance
  99. mean_relative_error_distance = mean_relative_error_distance + ans
  100. max1 = max(yans1)
  101. max2 = max(yans)
  102. if (ans>1):
  103. prob = prob + 1
  104. plt.plot(x,yans)
  105. mean_error_distance = mean_error_distance/100000
  106. mean_relative_error_distance = mean_relative_error_distance/100000
  107. normalized_error_distance = mean_error_distance/max1
  108. normailzed_relative_error_distance = mean_relative_error_distance/max2
  109. acceptance_prob = prob/100000
  110. acceptance_prob = 1-acceptance_prob
  111. print ("*******************************")
  112. print ("MEAN ERROR DISTANCE : ", mean_error_distance)
  113. print ("mean_relative_error_distance : ", mean_relative_error_distance)
  114. print ("normalized_error_distance : ", normalized_error_distance)
  115. print ("normailzed_relative_error_distance : ", normailzed_relative_error_distance)
  116. print ("acceptance_probability at 1% : ", acceptance_prob)
  117. plt.title ('16 BIT MULTIPLIER SCHEME 2')
  118. plt.xlabel('Random Numbers Generated')
  119. plt.ylabel('Relative Error(%)')
  120. plt.show()