FastMath.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import os.path
  2. import numpy as np
  3. import itertools
  4. import Tools
  5. import math
  6. # Those patterns are used for tests and benchmarks.
  7. # For tests, there is the need to add tests for saturation
  8. # For benchmarks
  9. NBSAMPLES=256
  10. def cartesian(*somelists):
  11. r=[]
  12. for element in itertools.product(*somelists):
  13. r.append(element)
  14. return(r)
  15. # Fixed point division should not be called with a denominator of zero.
  16. # But if it is, it should return a saturated result.
  17. def divide(f,r):
  18. e = 0
  19. if f == Tools.Q31:
  20. e = 1.0 / (1<<31)
  21. if f == Tools.Q15:
  22. e = 1.0 / (1<<15)
  23. if f == Tools.Q7:
  24. e = 1.0 / (1<<7)
  25. a,b=r
  26. if b == 0.0:
  27. if a >= 0.0:
  28. return(1.0,0)
  29. else:
  30. return(-1.0,0)
  31. k = 0
  32. while abs(a) > abs(b):
  33. a = a / 2.0
  34. k = k + 1
  35. # In C code we don't saturate but instead generate the right value
  36. # with a shift of 1.
  37. # So this test is to ease the comparison between the Python reference
  38. # and the output of the division algorithm in C
  39. if abs(a/b) > 1 - e:
  40. a = a / 2.0
  41. k = k + 1
  42. return(a/b,k)
  43. def initLogValues(format):
  44. if format == Tools.Q15:
  45. exps = -np.linspace(0,15,num=125)
  46. else:
  47. exps = -np.linspace(0,31,num=125)
  48. basis=2.0*np.ones(exps.size)
  49. vals=np.float_power(basis,exps)
  50. ref=np.log(vals)
  51. if format==Tools.Q31 :
  52. # Format must be Q5.26
  53. ref = ref / 32.0
  54. if format == Tools.Q15:
  55. # Format must be Q4.11
  56. ref = ref / 16.0
  57. return(vals,ref)
  58. def writeTests(config,format):
  59. config.setOverwrite(False)
  60. 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])
  61. 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])
  62. a3 = a1 + 2*math.pi
  63. angles=np.concatenate((a1,a2,a3))
  64. refcos = np.cos(angles)
  65. refsin = np.sin(angles)
  66. vals=np.array([0.0, 0.0, 0.1,1.0,2.0,3.0,3.5,3.6])
  67. sqrtvals=np.sqrt(vals)
  68. # Negative values in CMSIS are giving 0
  69. vals[0] = -0.4
  70. sqrtvals[0] = 0.0
  71. if format != 0 and format != 16:
  72. angles=np.concatenate((a1,a2,a1))
  73. angles = angles / (2*math.pi)
  74. config.writeInput(1, angles,"Angles")
  75. config.writeInput(1, vals,"SqrtInput")
  76. config.writeReference(1, refcos,"Cos")
  77. config.writeReference(1, refsin,"Sin")
  78. config.writeReference(1, sqrtvals,"Sqrt")
  79. # For benchmarks
  80. samples=np.random.randn(NBSAMPLES)
  81. samples = np.abs(Tools.normalize(samples))
  82. config.writeInput(1, samples,"Samples")
  83. numerator=np.linspace(-0.9,0.9)
  84. denominator=np.linspace(-0.9,0.9)
  85. samples=cartesian(numerator,denominator)
  86. numerator=[x[0] for x in samples]
  87. denominator=[x[1] for x in samples]
  88. result=[divide(format,x) for x in samples]
  89. resultValue=[x[0] for x in result]
  90. resultShift=[x[1] for x in result]
  91. config.writeInput(1, numerator,"Numerator")
  92. config.writeInput(1, denominator,"Denominator")
  93. config.writeReference(1, resultValue,"DivisionValue")
  94. config.writeReferenceS16(1, resultShift,"DivisionShift")
  95. config.setOverwrite(True)
  96. vals,ref=initLogValues(format)
  97. config.writeInput(1, vals,"LogInput")
  98. config.writeReference(1, ref,"Log")
  99. config.setOverwrite(False)
  100. def writeTestsFloat(config,format):
  101. config.setOverwrite(False)
  102. writeTests(config,format)
  103. data1 = np.random.randn(20)
  104. data1 = np.abs(data1)
  105. data1 = data1 + 1e-3 # To avoid zero values
  106. data1 = Tools.normalize(data1)
  107. config.setOverwrite(True)
  108. samples,v=initLogValues(format)
  109. config.writeInput(1, samples,"LogInput")
  110. config.writeReference(1, v,"Log")
  111. config.setOverwrite(False)
  112. samples=np.concatenate((np.array([0.0,1.0]),np.linspace(-0.4,0.4)))
  113. config.writeInput(1, samples,"ExpInput")
  114. v = np.exp(samples)
  115. config.writeReference(1, v,"Exp")
  116. # For benchmarks and other tests
  117. samples=np.random.randn(NBSAMPLES)
  118. samples = np.abs(Tools.normalize(samples))
  119. config.writeInput(1, samples,"Samples")
  120. v = 1.0 / samples
  121. config.writeReference(1, v,"Inverse")
  122. config.setOverwrite(False)
  123. def generatePatterns():
  124. PATTERNDIR = os.path.join("Patterns","DSP","FastMath","FastMath")
  125. PARAMDIR = os.path.join("Parameters","DSP","FastMath","FastMath")
  126. configf32=Tools.Config(PATTERNDIR,PARAMDIR,"f32")
  127. configf16=Tools.Config(PATTERNDIR,PARAMDIR,"f16")
  128. configq31=Tools.Config(PATTERNDIR,PARAMDIR,"q31")
  129. configq15=Tools.Config(PATTERNDIR,PARAMDIR,"q15")
  130. writeTestsFloat(configf32,0)
  131. writeTestsFloat(configf16,16)
  132. writeTests(configq31,31)
  133. writeTests(configq15,15)
  134. if __name__ == '__main__':
  135. generatePatterns()