mult_16.py 3.6 KB

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