mult_64_1.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. ## 64 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 = [7, 8, 9, 10, 11, 12]
  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, 18446744073709551615))
  22. for d in range(100000):
  23. res1.append(random.randint(1, 18446744073709551615))
  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,64)
  35. bbin = get_bin(b,64)
  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=64-k-num
  46. sum2=64-l-num
  47. if (sum1<0):
  48. sum1=0
  49. if (sum2<0):
  50. sum2=0
  51. sum3=(sum1+sum2)*-1
  52. sum3=(sum1+sum2)*-1
  53. if (k+num>64):
  54. k = 64-num
  55. if (l+num>64):
  56. l = 64-num
  57. amul=abin[k:k+num]
  58. bmul=bbin[l:l+num]
  59. q=int(amul,2)
  60. w=int(bmul,2)
  61. e=q*w
  62. yapp = get_bin(e,128)
  63. yapp = [int(x) for x in yapp]
  64. yapp = np.roll(yapp,sum3)
  65. yapp = ' '.join(str(e) for e in yapp)
  66. yapp = str.replace(yapp," ","")
  67. w=int(yapp,2)
  68. ans=((y-w)/y)*100
  69. yans[f]=ans
  70. yans1[f] = (y-w)
  71. mean_error_distance[g] = (y-w) + mean_error_distance[g]
  72. mean_relative_error_distance[g] = mean_relative_error_distance[g] + ans
  73. max1 = max(yans1)
  74. max2 = max(yans)
  75. if (ans>1):
  76. prob = prob + 1
  77. mean_error_distance[g] = mean_error_distance[g]/100000
  78. mean_relative_error_distance[g] = mean_relative_error_distance[g]/100000
  79. normalized_error_distance[g] = mean_error_distance[g]/max1
  80. normailzed_relative_error_distance[g] = mean_relative_error_distance[g]/max2
  81. acceptance_prob[g] = prob/100000
  82. acceptance_prob[g] = 1-acceptance_prob[g]
  83. print ("*******************************")
  84. print ("latency parameter: ",num1[g])
  85. print ("mean_error_distance : ", mean_error_distance[g])
  86. print ("mean_relative_error_distance : ", mean_relative_error_distance[g])
  87. print ("normalized_error_distance : ", normalized_error_distance[g])
  88. print ("normailzed_relative_error_distance : ", normailzed_relative_error_distance[g])
  89. print ("acceptance_probability at 1% : ", acceptance_prob[g])
  90. plt.plot(x,yans)
  91. plt.title ('RELATIVE ERROR(%) 64 BIT MULTIPLIER SCHEME 1')
  92. plt.xlabel('Random Numbers Generated')
  93. plt.ylabel('Relative Error(%)')
  94. plt.show()
  95. p = (0,1,2,3,4,5)
  96. l = (7,8,9,10,11,12)
  97. plt.plot (x1,mean_error_distance)
  98. plt.title ('64 BIT MULTIPLIER SCHEME 1')
  99. plt.xticks(p,l)
  100. plt.xlabel('Latency Parameter')
  101. plt.ylabel('Mean Error Distance')
  102. plt.show()
  103. plt.plot (x1,mean_relative_error_distance)
  104. plt.title ('64 BIT MULTIPLIER SCHEME 1')
  105. plt.xticks(p,l)
  106. plt.xlabel('Latency Parameter')
  107. plt.ylabel('Mean Relative Error Distance')
  108. plt.show()
  109. plt.plot (x1,normalized_error_distance)
  110. plt.title ('64 BIT MULTIPLIER SCHEME 1')
  111. plt.xticks(p,l)
  112. plt.xlabel('Latency Parameter')
  113. plt.ylabel('Normalized Error Distance ')
  114. plt.show()
  115. plt.plot (x1,normailzed_relative_error_distance)
  116. plt.title ('64 BIT MULTIPLIER SCHEME 1')
  117. plt.xticks(p,l)
  118. plt.xlabel('Latency Parameter')
  119. plt.ylabel('Normailzed Relative Error Distance')
  120. plt.show()
  121. plt.plot (x1,acceptance_prob)
  122. plt.title ('64 BIT MULTIPLIER SCHEME 1')
  123. plt.xticks(p,l)
  124. plt.xlabel('Latency Parameter')
  125. plt.ylabel('Acceptance Probability(1% Relative Error)')
  126. plt.show()