1import os.path
2import numpy as np
3import itertools
4
5# This python module is containing definitions useful for
6# generating test patterns.
7import Tools
8
9
10# This function is generating patterns for an addition
11def writeTests(config,format):
12    # In this test, we can use the same inputs for several tests.
13    # For instance, we could test an addition on 3 samples and an addition
14    # on 9 samples to be sure that the vectorized code with tail is
15    # working.
16    # So, we generate two long patterns and in the different tests we may load
17    # only a subset of the samples.
18    NBSAMPLES=256
19
20    # Two random arrays with gaussian distribution
21    data1=np.random.randn(NBSAMPLES)
22    data2=np.random.randn(NBSAMPLES)
23
24    # We normalize the data to ensure that the q31, q15 and q7 patterns won't
25    # be already saturated.
26    data1 = Tools.normalize(data1)
27    data2 = Tools.normalize(data2)
28
29    # The input patterns are written. The writeInput function of the config object is
30    # doing a lot:
31    # It is converting the float data to the right format (float, q31, q15 or q7)
32    # depending on the config object.
33    # It is generating a text file with the right format as recognized by the test framework
34    # It is naming the file using the PATTERNDIR (defined below), the id (1 or 2 in this example)
35    # and using "Input".
36    #
37    # So first file is named "Input1_f32.txt"
38    config.writeInput(1, data1)
39    config.writeInput(2, data2)
40
41    # We compute the reference pattern
42    ref = data1 + data2
43
44    # Write reference is similar to writeInput.
45    # The created file will be named "Reference1_f32.txt"
46    config.writeReference(1, ref)
47
48# This function is generating patterns for all the types
49def generatePatterns():
50
51    # We define the path to the patterns.
52    # This path must be compatible with the folder directives used in the desc.txt
53    # test description file.
54    # By default, the root folder for pattern is Patterns and the root one for
55    # Parameters is Parameters.
56    # So both path defines in desc.txt are relative to those root folders.
57    #
58    # The last folder will be completed with the type.
59    # So for instance we will get ExampleCategoryF32, ExampleCategoryQ31 ...
60    PATTERNDIR = os.path.join("Patterns","Example","ExampleCategory","ExampleCategory")
61    PARAMDIR = os.path.join("Parameters","Example","ExampleCategory","ExampleCategory")
62
63    # config object for each type are created
64    configf32=Tools.Config(PATTERNDIR,PARAMDIR,"f32")
65    configq31=Tools.Config(PATTERNDIR,PARAMDIR,"q31")
66    configq15=Tools.Config(PATTERNDIR,PARAMDIR,"q15")
67    configq7=Tools.Config(PATTERNDIR,PARAMDIR,"q7")
68
69
70    # Test patterns for each config are generated.
71    # Second argument may be used to vary the content fo files
72    # depending on the type.
73    #
74    # For instance, in Tools there is Tools.loopnb which can be used
75    # like Tools.loopnb(format,Tools.TAILONLY)
76    # It is giving a number of iterations corresponding to the case (Tail only, body only, body and tail)
77    # Since the number of lanes depends on the type, testing vectorized code is requiring the use of
78    # different lengths according to the type.
79    writeTests(configf32,0)
80    writeTests(configq31,31)
81    writeTests(configq15,15)
82    writeTests(configq7,7)
83
84# Useful to be able to use this file as a script or to import it from another script
85# and use the generatePatterns function
86if __name__ == '__main__':
87  generatePatterns()