OpenWareLaboratory
IntArray.h
Go to the documentation of this file.
1 #ifndef __IntArray_h__
2 #define __IntArray_h__
3 
4 #include <stdint.h>
5 #include "SimpleArray.h"
6 #include "FloatArray.h"
7 #include <limits.h>
8 
9 class IntArray : public SimpleArray<int32_t> {
10 public:
12  IntArray(int32_t* data, size_t size) :
13  SimpleArray(data, size) {}
14 
15  void setAll(int32_t value){
17  #ifdef ARM_CORTEX
18  arm_fill_q31(value, data, size);
19  #else
20  for(size_t n=0; n<size; n++){
21  data[n]=value;
22  }
23  #endif /* ARM_CORTEX */
24  }
25 
30  void clear(){
31  setAll(0);
32  }
33 
40  void add(IntArray operand2, IntArray destination){ //allows in-place
41  //ASSERT(operand2.size >= size && destination.size<=size, "Arrays must be matching size");
43  #ifdef ARM_CORTEX
44  arm_add_q31(data, operand2.data, destination.data, size);
45  #else
46  for(size_t n=0; n<size; n++){
47  destination[n]=data[n]+operand2[n];
48  }
49  #endif /* ARM_CORTEX */
50 }
51 
57  void add(IntArray operand2){ //in-place
59  add(operand2, *this);
60  } //in-place
61 
67  void shift(int shiftValue){
68 #ifdef ARM_CORTEX
69  arm_shift_q31(data, shiftValue, data, size);
70 #else
71  if(shiftValue >= 0){
72  for(size_t n=0; n<size; n++)
73  data[n] = data[n] << shiftValue;
74  }else{
75  shiftValue = -shiftValue;
76  for(size_t n=0; n<size; n++)
77  data[n] = data[n] >> shiftValue;
78  }
79 #endif
80  }
81 
93  IntArray subArray(int offset, size_t length){
94  ASSERT(size >= offset+length, "Array too small");
95  return IntArray(data+offset, length);
96  }
97 
98 
104  void toFloat(FloatArray destination){
105  ASSERT(destination.getSize() == size, "Size does not match");
106 #ifdef ARM_CORTEX
108  arm_q31_to_float(data, destination.getData(), size);
109 #else
110  for(size_t n = 0; n < size; ++n)
111  destination[n] = getFloatValue(n);
112 #endif
113  }
114 
120  void fromFloat(FloatArray source){
121  ASSERT(source.getSize() == size, "Size does not match");
122 #ifdef ARM_CORTEX
124  arm_float_to_q31(source.getData(), data, size);
125 #else
126  for(size_t n = 0; n < size; ++n){
127  setFloatValue(n, source[n]);
128  }
129 #endif
130  }
131 
138  void setFloatValue(uint32_t n, float value){
139  data[n] = (int32_t)(value * -(float)INT_MIN);
140  }
141 
148  float getFloatValue(uint32_t n){
149  return data[n] / -(float)INT_MIN;
150  }
151 
159  static IntArray create(int size){
160  IntArray fa(new int32_t[size], size);
161  fa.clear();
162  return fa;
163  }
164 
171  static void destroy(IntArray array){
172  delete[] array.data;
173  }
174 };
175 
176 #endif // __IntArray_h__
This class contains useful methods for manipulating arrays of floats.
Definition: FloatArray.h:12
IntArray()
Definition: IntArray.h:11
void setFloatValue(uint32_t n, float value)
Converts a float to int16 and stores it.
Definition: IntArray.h:138
void setAll(int32_t value)
Definition: IntArray.h:15
IntArray subArray(int offset, size_t length)
A subset of the array.
Definition: IntArray.h:93
void clear()
Clear the array.
Definition: IntArray.h:30
static IntArray create(int size)
Creates a new IntArray.
Definition: IntArray.h:159
void shift(int shiftValue)
Bitshift the array values, saturating.
Definition: IntArray.h:67
float getFloatValue(uint32_t n)
Returns an element of the array converted to float.
Definition: IntArray.h:148
void add(IntArray operand2)
Element-wise sum between arrays.
Definition: IntArray.h:57
void add(IntArray operand2, IntArray destination)
Element-wise sum between arrays.
Definition: IntArray.h:40
void toFloat(FloatArray destination)
Copies the content of the array to a FloatArray, interpreting the content of the IntArray as 1....
Definition: IntArray.h:104
void fromFloat(FloatArray source)
Copies the content of a FloatArray into a IntArray, converting the float elements to fixed-point 1....
Definition: IntArray.h:120
IntArray(int32_t *data, size_t size)
Definition: IntArray.h:12
static void destroy(IntArray array)
Destroys a IntArray created with the create() method.
Definition: IntArray.h:171
SimpleArray holds a pointer to an array and the array size, and is designed to be passed by value.
Definition: SimpleArray.h:14
size_t getSize() const
Definition: SimpleArray.h:31
T * getData()
Get the data stored in the Array.
Definition: SimpleArray.h:27
#define ASSERT(cond, msg)
Definition: message.h:16