FastMath.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import os.path
  2. import numpy as np
  3. import itertools
  4. import Tools
  5. import math
  6. import numpy as np
  7. def q31accuracy(x):
  8. return(np.round(1.0*x * (1<<31)))
  9. def q15accuracy(x):
  10. return(np.round(1.0*x * (1<<15)))
  11. def q7accuracy(x):
  12. return(np.round(1.0*x * (1<<7)))
  13. def Q31toF32(x):
  14. return(1.0*x / 2**31)
  15. def Q15toF32(x):
  16. return(1.0*x / 2**15)
  17. def Q7toF32(x):
  18. return(1.0*x / 2**7)
  19. # Those patterns are used for tests and benchmarks.
  20. # For tests, there is the need to add tests for saturation
  21. # For benchmarks
  22. NBSAMPLES=256
  23. def cartesian(*somelists):
  24. r=[]
  25. for element in itertools.product(*somelists):
  26. r.append(element)
  27. return(r)
  28. # Fixed point division should not be called with a denominator of zero.
  29. # But if it is, it should return a saturated result.
  30. def divide(f,r):
  31. e = 0
  32. a,b=r
  33. if f == Tools.Q31:
  34. e = 1.0 / (1<<31)
  35. a = 1.0*q31accuracy(a) / (2**31)
  36. b = 1.0*q31accuracy(b) / (2**31)
  37. if f == Tools.Q15:
  38. e = 1.0 / (1<<15)
  39. a = 1.0*q15accuracy(a) / (2**15)
  40. b = 1.0*q15accuracy(b) / (2**15)
  41. if f == Tools.Q7:
  42. e = 1.0 / (1<<7)
  43. a = 1.0*q7accuracy(a) / (2**7)
  44. b = 1.0*q7accuracy(b) / (2**7)
  45. if b == 0.0:
  46. if a >= 0.0:
  47. return(1.0,0)
  48. else:
  49. return(-1.0,0)
  50. k = 0
  51. while abs(a) > abs(b):
  52. a = a / 2.0
  53. k = k + 1
  54. # In C code we don't saturate but instead generate the right value
  55. # with a shift of 1.
  56. # So this test is to ease the comparison between the Python reference
  57. # and the output of the division algorithm in C
  58. if abs(a/b) > 1 - e:
  59. a = a / 2.0
  60. k = k + 1
  61. return(a/b,k)
  62. def initLogValues(format):
  63. if format == Tools.Q15:
  64. vals=np.linspace(np.float_power(2,-15),1.0,num=125)
  65. elif format == Tools.F16:
  66. vals=np.linspace(np.float_power(2,-10),1.0,num=125)
  67. else:
  68. vals=np.linspace(np.float_power(2,-31),1.0,num=125)
  69. ref=np.log(vals)
  70. if format==Tools.Q31 :
  71. # Format must be Q5.26
  72. ref = ref / 32.0
  73. if format == Tools.Q15:
  74. # Format must be Q4.11
  75. ref = ref / 16.0
  76. return(vals,ref)
  77. def writeTests(config,format):
  78. a1=np.array([0,math.pi/4,math.pi/2,3*math.pi/4,math.pi,5*math.pi/4,3*math.pi/2,2*math.pi-1e-6])
  79. a2=np.array([-math.pi/4,-math.pi/2,-3*math.pi/4,-math.pi,-5*math.pi/4,-3*math.pi/2,-2*math.pi-1e-6])
  80. a3 = a1 + 2*math.pi
  81. angles=np.concatenate((a1,a2,a3))
  82. refcos = np.cos(angles)
  83. refsin = np.sin(angles)
  84. vals=np.linspace(0.0,1.0,1024)
  85. sqrtvals=np.sqrt(vals)
  86. # Negative values in CMSIS are giving 0
  87. vals[0] = -0.4
  88. sqrtvals[0] = 0.0
  89. if format != Tools.F64 and format != 0 and format != 16:
  90. angles=np.concatenate((a1,a2,a1))
  91. angles = angles / (2*math.pi)
  92. config.writeInput(1, angles,"Angles")
  93. config.writeInput(1, vals,"SqrtInput")
  94. config.writeReference(1, sqrtvals,"Sqrt")
  95. config.writeReference(1, refcos,"Cos")
  96. config.writeReference(1, refsin,"Sin")
  97. # For benchmarks
  98. samples=np.random.randn(NBSAMPLES)
  99. samples = np.abs(Tools.normalize(samples))
  100. config.writeInput(1, samples,"Samples")
  101. numerator=np.linspace(-0.9,0.9)
  102. numerator=np.hstack([numerator,np.array([-1.0,1.0])])
  103. denominator=np.linspace(-0.9,0.9)
  104. denominator=np.hstack([denominator,np.array([-1.0,1.0])])
  105. samples=cartesian(numerator,denominator)
  106. numerator=[x[0] for x in samples]
  107. denominator=[x[1] for x in samples]
  108. result=[divide(format,x) for x in samples]
  109. resultValue=[x[0] for x in result]
  110. resultShift=[x[1] for x in result]
  111. config.setOverwrite(True)
  112. config.writeInput(1, numerator,"Numerator")
  113. config.writeInput(1, denominator,"Denominator")
  114. config.writeReference(1, resultValue,"DivisionValue")
  115. config.writeReferenceS16(1, resultShift,"DivisionShift")
  116. config.setOverwrite(False)
  117. vals,ref=initLogValues(format)
  118. config.writeInput(1, vals,"LogInput")
  119. config.writeReference(1, ref,"Log")
  120. config.setOverwrite(False)
  121. # Testing of ATAN2
  122. angles=np.linspace(0.0,2*math.pi,1000,endpoint=True)
  123. angles=np.hstack([angles,np.array([math.pi/4.0])])
  124. if format == Tools.Q31 or format == Tools.Q15:
  125. radius=[1.0]
  126. else:
  127. radius=np.linspace(0.1,0.9,10,endpoint=True)
  128. combinations = cartesian(radius,angles)
  129. res=[]
  130. yx = []
  131. for r,angle in combinations:
  132. x = r*np.cos(angle)
  133. y = r*np.sin(angle)
  134. res.append(np.arctan2(y,x))
  135. yx.append(y)
  136. yx.append(x)
  137. config.writeInput(1, np.array(yx).flatten(),"Atan2Input")
  138. # Q2.29 or Q2.13 to represent PI in the output
  139. if format == Tools.Q31 or format == Tools.Q15:
  140. config.writeReference(1, np.array(res)/4.0,"Atan2Ref")
  141. else:
  142. config.writeReference(1, np.array(res),"Atan2Ref")
  143. config.setOverwrite(False)
  144. def writeTestsFloat(config,format):
  145. writeTests(config,format)
  146. data1 = np.random.randn(20)
  147. data1 = np.abs(data1)
  148. data1 = data1 + 1e-3 # To avoid zero values
  149. data1 = Tools.normalize(data1)
  150. samples=np.concatenate((np.array([0.0,1.0]),np.linspace(-0.4,0.4)))
  151. config.writeInput(1, samples,"ExpInput")
  152. v = np.exp(samples)
  153. config.writeReference(1, v,"Exp")
  154. # For benchmarks and other tests
  155. samples=np.random.randn(NBSAMPLES)
  156. samples = np.abs(Tools.normalize(samples))
  157. config.writeInput(1, samples,"Samples")
  158. v = 1.0 / samples
  159. config.writeReference(1, v,"Inverse")
  160. def generatePatterns():
  161. PATTERNDIR = os.path.join("Patterns","DSP","FastMath","FastMath")
  162. PARAMDIR = os.path.join("Parameters","DSP","FastMath","FastMath")
  163. configf64=Tools.Config(PATTERNDIR,PARAMDIR,"f64")
  164. configf32=Tools.Config(PATTERNDIR,PARAMDIR,"f32")
  165. configf16=Tools.Config(PATTERNDIR,PARAMDIR,"f16")
  166. configq31=Tools.Config(PATTERNDIR,PARAMDIR,"q31")
  167. configq15=Tools.Config(PATTERNDIR,PARAMDIR,"q15")
  168. configf64.setOverwrite(False)
  169. configf32.setOverwrite(False)
  170. configf16.setOverwrite(False)
  171. configq31.setOverwrite(False)
  172. configq15.setOverwrite(False)
  173. writeTestsFloat(configf64,Tools.F64)
  174. writeTestsFloat(configf32,0)
  175. writeTestsFloat(configf16,16)
  176. writeTests(configq31,31)
  177. writeTests(configq15,15)
  178. if __name__ == '__main__':
  179. generatePatterns()