mult_32_2.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. ## 32 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(10000))
  9. x1=list(range(6))
  10. yans=list(range(10000))
  11. yans1=list(range(10000))
  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(10000):
  20. res.append(random.randint(1, 4294967295))
  21. for d in range(10000):
  22. res1.append(random.randint(1, 4294967295))
  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 (10000):
  30. a=res[f]
  31. b=res1[f]
  32. y=a*b
  33. abin = get_bin(a,32)
  34. bbin = get_bin(b,32)
  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=7
  45. elif bbin[2]=='1':
  46. num=7
  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=7
  61. elif bbin[6]=='1':
  62. num=7
  63. elif abin[7]=='1':
  64. num=7
  65. elif bbin[7]=='1':
  66. num=7
  67. elif abin[8]=='1':
  68. num=7
  69. elif bbin[8]=='1':
  70. num=7
  71. elif abin[9]=='1':
  72. num=7
  73. elif bbin[9]=='1':
  74. num=7
  75. else:
  76. num=6
  77. i=0
  78. while (abin[i]=='0'):
  79. i=i+1
  80. j=0
  81. while (bbin[j]=='0'):
  82. j=j+1
  83. k=i
  84. l=j
  85. sum1=32-k-num
  86. sum2=32-l-num
  87. if (sum1<0):
  88. sum1=0
  89. if (sum2<0):
  90. sum2=0
  91. sum3=(sum1+sum2)*-1
  92. if (k+num>32):
  93. k = 32-num
  94. if (l+num>32):
  95. l = 32-num
  96. amul=abin[k:k+num]
  97. bmul=bbin[l:l+num]
  98. q=int(amul,2)
  99. w=int(bmul,2)
  100. e=q*w
  101. yapp = get_bin(e,64)
  102. yapp = [int(x) for x in yapp]
  103. yapp = np.roll(yapp,sum3)
  104. yapp = ' '.join(str(e) for e in yapp)
  105. yapp = str.replace(yapp," ","")
  106. w=int(yapp,2)
  107. ans=((y-w)/y)*100
  108. yans[f]=ans
  109. yans1[f] = (y-w)
  110. mean_error_distance = (y-w) + mean_error_distance
  111. mean_relative_error_distance = mean_relative_error_distance + ans
  112. max1 = max(yans1)
  113. max2 = max(yans)
  114. if (ans>1):
  115. prob = prob + 1
  116. plt.plot(x,yans)
  117. mean_error_distance = mean_error_distance/10000
  118. mean_relative_error_distance = mean_relative_error_distance/10000
  119. normalized_error_distance = mean_error_distance/max1
  120. normailzed_relative_error_distance = mean_relative_error_distance/max2
  121. acceptance_prob = prob/10000
  122. acceptance_prob = 1-acceptance_prob
  123. print ("*******************************")
  124. print ("MEAN ERROR DISTANCE : ", mean_error_distance)
  125. print ("mean_relative_error_distance : ", mean_relative_error_distance)
  126. print ("normalized_error_distance : ", normalized_error_distance)
  127. print ("normailzed_relative_error_distance : ", normailzed_relative_error_distance)
  128. print ("acceptance_probability at 1% : ", acceptance_prob)
  129. plt.title ('32 BIT MULTIPLIER SCHEME 2')
  130. plt.xlabel('Random Numbers Generated')
  131. plt.ylabel('Relative Error(%)')
  132. plt.show()