FastMath.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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.setOverwrite(True)
  94. config.writeInput(1, vals,"SqrtInput")
  95. config.writeReference(1, sqrtvals,"Sqrt")
  96. config.setOverwrite(False)
  97. config.writeReference(1, refcos,"Cos")
  98. config.writeReference(1, refsin,"Sin")
  99. # For benchmarks
  100. samples=np.random.randn(NBSAMPLES)
  101. samples = np.abs(Tools.normalize(samples))
  102. config.writeInput(1, samples,"Samples")
  103. numerator=np.linspace(-0.9,0.9)
  104. denominator=np.linspace(-0.9,0.9)
  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.writeInput(1, numerator,"Numerator")
  112. config.writeInput(1, denominator,"Denominator")
  113. config.writeReference(1, resultValue,"DivisionValue")
  114. config.writeReferenceS16(1, resultShift,"DivisionShift")
  115. vals,ref=initLogValues(format)
  116. config.writeInput(1, vals,"LogInput")
  117. config.writeReference(1, ref,"Log")
  118. def writeTestsFloat(config,format):
  119. writeTests(config,format)
  120. data1 = np.random.randn(20)
  121. data1 = np.abs(data1)
  122. data1 = data1 + 1e-3 # To avoid zero values
  123. data1 = Tools.normalize(data1)
  124. samples=np.concatenate((np.array([0.0,1.0]),np.linspace(-0.4,0.4)))
  125. config.writeInput(1, samples,"ExpInput")
  126. v = np.exp(samples)
  127. config.writeReference(1, v,"Exp")
  128. # For benchmarks and other tests
  129. samples=np.random.randn(NBSAMPLES)
  130. samples = np.abs(Tools.normalize(samples))
  131. config.writeInput(1, samples,"Samples")
  132. v = 1.0 / samples
  133. config.writeReference(1, v,"Inverse")
  134. def generatePatterns():
  135. PATTERNDIR = os.path.join("Patterns","DSP","FastMath","FastMath")
  136. PARAMDIR = os.path.join("Parameters","DSP","FastMath","FastMath")
  137. configf64=Tools.Config(PATTERNDIR,PARAMDIR,"f64")
  138. configf32=Tools.Config(PATTERNDIR,PARAMDIR,"f32")
  139. configf16=Tools.Config(PATTERNDIR,PARAMDIR,"f16")
  140. configq31=Tools.Config(PATTERNDIR,PARAMDIR,"q31")
  141. configq15=Tools.Config(PATTERNDIR,PARAMDIR,"q15")
  142. configf64.setOverwrite(False)
  143. configf32.setOverwrite(False)
  144. configf16.setOverwrite(False)
  145. configq31.setOverwrite(False)
  146. configq15.setOverwrite(False)
  147. writeTestsFloat(configf64,Tools.F64)
  148. writeTestsFloat(configf32,0)
  149. writeTestsFloat(configf16,16)
  150. writeTests(configq31,31)
  151. writeTests(configq15,15)
  152. if __name__ == '__main__':
  153. generatePatterns()