forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumbers.c
More file actions
133 lines (112 loc) · 2.81 KB
/
numbers.c
File metadata and controls
133 lines (112 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2016 Intel Corporation. All rights reserved.
//
// Author: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
// Liam Girdwood <liam.r.girdwood@linux.intel.com>
// Keyon Jie <yang.jie@linux.intel.com>
/* Euclidean algorithm for greatest common denominator from
* pseudocode in
* https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations
*/
#include <sof/audio/format.h>
#include <sof/math/numbers.h>
#include <stdint.h>
int gcd(int a, int b)
{
int t;
while (b != 0) {
t = b;
b = a % b;
a = t;
}
return a;
}
/* This function searches from vec[] (of length vec_length) integer values
* of n. The indexes to equal values is returned in idx[]. The function
* returns the number of found matches. The max_results should be set to
* 0 (or negative) or vec_length get all the matches. The max_result can be set
* to 1 to receive only the first match in ascending order. It avoids need
* for an array for idx.
*/
int find_equal_int16(int16_t idx[], int16_t vec[], int n, int vec_length,
int max_results)
{
int nresults = 0;
int i;
for (i = 0; i < vec_length; i++) {
if (vec[i] == n) {
idx[nresults++] = i;
if (nresults == max_results)
break;
}
}
return nresults;
}
/* Return the smallest value found in the vector */
int16_t find_min_int16(int16_t vec[], int vec_length)
{
int i;
int min = vec[0];
for (i = 1; i < vec_length; i++)
min = (vec[i] < min) ? vec[i] : min;
return min;
}
/* Return the largest absolute value found in the vector. Note that
* smallest negative value need to be saturated to preset as int32_t.
*/
int32_t find_max_abs_int32(int32_t vec[], int vec_length)
{
int i;
int64_t amax = (vec[0] > 0) ? vec[0] : -vec[0];
for (i = 1; i < vec_length; i++) {
amax = (vec[i] > amax) ? vec[i] : amax;
amax = (-vec[i] > amax) ? -vec[i] : amax;
}
return SATP_INT32(amax); /* Amax is always a positive value */
}
/* Count the left shift amount to normalize a 32 bit signed integer value
* without causing overflow. Input value 0 will result to 31.
*/
int norm_int32(int32_t val)
{
int s;
int32_t n;
if (!val)
return 31;
if (val > 0) {
n = val << 1;
s = 0;
while (n > 0) {
n = n << 1;
s++;
}
} else {
n = val << 1;
s = 0;
while (n < 0) {
n = n << 1;
s++;
}
}
return s;
}
/**
* Basic CRC-32 implementation, based on pseudo-code from
* https://en.wikipedia.org/wiki/Cyclic_redundancy_check#CRC-32_algorithm
* 0xEDB88320 is the reversed polynomial representation
*/
uint32_t crc32(const void *data, uint32_t bytes)
{
uint32_t crc = 0xFFFFFFFF;
uint32_t cur;
int i;
int j;
for (i = 0; i < bytes; ++i) {
cur = (crc ^ ((const uint8_t *)data)[i]) & 0xFF;
for (j = 0; j < 8; ++j)
cur = cur & 1 ? (cur >> 1) ^ 0xEDB88320 : cur >> 1;
crc = cur ^ (crc >> 8);
}
return ~crc;
}