FastMath.py 5.3 KB

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