FastMath.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. elif format == Tools.F16:
  47. exps = -np.linspace(0,10,num=125)
  48. else:
  49. exps = -np.linspace(0,31,num=125)
  50. basis=2.0*np.ones(exps.size)
  51. vals=np.float_power(basis,exps)
  52. ref=np.log(vals)
  53. if format==Tools.Q31 :
  54. # Format must be Q5.26
  55. ref = ref / 32.0
  56. if format == Tools.Q15:
  57. # Format must be Q4.11
  58. ref = ref / 16.0
  59. return(vals,ref)
  60. def writeTests(config,format):
  61. config.setOverwrite(False)
  62. 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])
  63. 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])
  64. a3 = a1 + 2*math.pi
  65. angles=np.concatenate((a1,a2,a3))
  66. refcos = np.cos(angles)
  67. refsin = np.sin(angles)
  68. vals=np.array([0.0, 0.0, 0.1,1.0,2.0,3.0,3.5,3.6])
  69. sqrtvals=np.sqrt(vals)
  70. # Negative values in CMSIS are giving 0
  71. vals[0] = -0.4
  72. sqrtvals[0] = 0.0
  73. if format != 0 and format != 16:
  74. angles=np.concatenate((a1,a2,a1))
  75. angles = angles / (2*math.pi)
  76. config.writeInput(1, angles,"Angles")
  77. config.writeInput(1, vals,"SqrtInput")
  78. config.writeReference(1, refcos,"Cos")
  79. config.writeReference(1, refsin,"Sin")
  80. config.writeReference(1, sqrtvals,"Sqrt")
  81. # For benchmarks
  82. samples=np.random.randn(NBSAMPLES)
  83. samples = np.abs(Tools.normalize(samples))
  84. config.writeInput(1, samples,"Samples")
  85. numerator=np.linspace(-0.9,0.9)
  86. denominator=np.linspace(-0.9,0.9)
  87. samples=cartesian(numerator,denominator)
  88. numerator=[x[0] for x in samples]
  89. denominator=[x[1] for x in samples]
  90. result=[divide(format,x) for x in samples]
  91. resultValue=[x[0] for x in result]
  92. resultShift=[x[1] for x in result]
  93. config.writeInput(1, numerator,"Numerator")
  94. config.writeInput(1, denominator,"Denominator")
  95. config.writeReference(1, resultValue,"DivisionValue")
  96. config.writeReferenceS16(1, resultShift,"DivisionShift")
  97. config.setOverwrite(True)
  98. vals,ref=initLogValues(format)
  99. config.writeInput(1, vals,"LogInput")
  100. config.writeReference(1, ref,"Log")
  101. config.setOverwrite(False)
  102. def writeTestsFloat(config,format):
  103. config.setOverwrite(False)
  104. writeTests(config,format)
  105. data1 = np.random.randn(20)
  106. data1 = np.abs(data1)
  107. data1 = data1 + 1e-3 # To avoid zero values
  108. data1 = Tools.normalize(data1)
  109. config.setOverwrite(True)
  110. samples,v=initLogValues(format)
  111. config.writeInput(1, samples,"LogInput")
  112. config.writeReference(1, v,"Log")
  113. config.setOverwrite(False)
  114. samples=np.concatenate((np.array([0.0,1.0]),np.linspace(-0.4,0.4)))
  115. config.writeInput(1, samples,"ExpInput")
  116. v = np.exp(samples)
  117. config.writeReference(1, v,"Exp")
  118. # For benchmarks and other tests
  119. samples=np.random.randn(NBSAMPLES)
  120. samples = np.abs(Tools.normalize(samples))
  121. config.writeInput(1, samples,"Samples")
  122. v = 1.0 / samples
  123. config.writeReference(1, v,"Inverse")
  124. config.setOverwrite(False)
  125. def generatePatterns():
  126. PATTERNDIR = os.path.join("Patterns","DSP","FastMath","FastMath")
  127. PARAMDIR = os.path.join("Parameters","DSP","FastMath","FastMath")
  128. configf32=Tools.Config(PATTERNDIR,PARAMDIR,"f32")
  129. configf16=Tools.Config(PATTERNDIR,PARAMDIR,"f16")
  130. configq31=Tools.Config(PATTERNDIR,PARAMDIR,"q31")
  131. configq15=Tools.Config(PATTERNDIR,PARAMDIR,"q15")
  132. writeTestsFloat(configf32,0)
  133. writeTestsFloat(configf16,16)
  134. writeTests(configq31,31)
  135. writeTests(configq15,15)
  136. if __name__ == '__main__':
  137. generatePatterns()