1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright(c) 2016 Intel Corporation. All rights reserved.
4  *
5  * Author: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
6  *         Liam Girdwood <liam.r.girdwood@linux.intel.com>
7  *         Keyon Jie <yang.jie@linux.intel.com>
8  */
9 
10 #ifndef __SOF_MATH_NUMBERS_H__
11 #define __SOF_MATH_NUMBERS_H__
12 
13 #include <stdint.h>
14 
15 #ifndef __ZEPHYR__
16 /* Unsafe and portable macros for consistency with Zephyr.
17  * See SEI CERT-C PRE31-C
18  */
19 #define MIN(a, b) ((a) < (b) ? (a) : (b))
20 #define MAX(a, b) ((a) < (b) ? (b) : (a))
21 
22 #define ROUND_DOWN(size, alignment) ({			\
23 	typeof(size) __size = (size);			\
24 	typeof(alignment) __alignment = (alignment);	\
25 	__size - (__size % __alignment);		\
26 })
27 #endif /* ! __ZEPHYR__ */
28 
29 #define ABS(a) ({		\
30 	typeof(a) __a = (a);	\
31 	__a < 0 ? -__a : __a;	\
32 })
33 #define SGN(a) ({		\
34 	typeof(a) __a = (a);	\
35 	__a < 0 ? -1 :		\
36 	__a > 0 ? 1 : 0;	\
37 })
38 
39 int gcd(int a, int b); /* Calculate greatest common divisor for a and b */
40 
41 /* This is a divide function that returns ceil of the quotient.
42  * E.g. ceil_divide(9, 3) returns 3, ceil_divide(10, 3) returns 4.
43  */
ceil_divide(int a,int b)44 static inline int ceil_divide(int a, int b)
45 {
46 	int c;
47 
48 	c = a / b;
49 
50 	/* First, we check whether the signs of the params are different.
51 	 * If they are, we already know the result is going to be negative and
52 	 * therefore, is going to be already rounded up (truncated).
53 	 *
54 	 * If the signs are the same, we check if there was any remainder in
55 	 * the division by multiplying the number back.
56 	 */
57 	if (!((a ^ b) & (1U << ((sizeof(int) * 8) - 1))) && c * b != a)
58 		c++;
59 
60 	return c;
61 }
62 
63 /**
64  * \brief Cross product function
65  *
66  * Calculate cross product for vectors AB(a, b, c) and AC(d, e, f), where A, B, and C
67  * are points of a triangle in 3D space. Cross product is used in computational
68  * geometry. Cross product AB x AC is (b * f - c * e, c * d - a * f, a * e - b * d)
69  *
70  * \param[out]	px	x-axis component of cross product vector
71  * \param[out]	py	y-axis component of cross product vector
72  * \param[out]	pz	z-axis component of cross product vector
73  * \param[in]	a	x-axis component of vector AB
74  * \param[in]	b	y-axis component of vector AB
75  * \param[in]	c	z-axis component of vector AB
76  * \param[in]	d	x-axis component of vector AC
77  * \param[in]	e	y-axis component of vector AC
78  * \param[in]	f	z-axis component of vector AC
79  */
cross_product_s16(int32_t * px,int32_t * py,int32_t * pz,int16_t a,int16_t b,int16_t c,int16_t d,int16_t e,int16_t f)80 static inline void cross_product_s16(int32_t *px, int32_t *py, int32_t *pz,
81 				     int16_t a, int16_t b, int16_t c,
82 				     int16_t d, int16_t e, int16_t f)
83 {
84 	*px = (int32_t)b * f - (int32_t)c * e;
85 	*py = (int32_t)c * d - (int32_t)a * f;
86 	*pz = (int32_t)a * e - (int32_t)b * d;
87 }
88 
89 /* Find indices of equal values in a vector of integer values */
90 int find_equal_int16(int16_t idx[], int16_t vec[], int n, int vec_length,
91 	int max_results);
92 
93 /* Return the smallest value found in a vector */
94 int16_t find_min_int16(int16_t vec[], int vec_length);
95 
96 /* Return the largest absolute value found in a vector */
97 int32_t find_max_abs_int32(int32_t vec[], int vec_length);
98 
99 /* Count the left shift amount to normalize a 32 bit signed integer value
100  * without causing overflow. Input value 0 will result to 31.
101  */
102 int norm_int32(int32_t val);
103 
104 uint32_t crc32(uint32_t base, const void *data, uint32_t bytes);
105 
106 /* merges two 16-bit values into a single 32-bit value */
107 #define merge_16b16b(high, low) (((uint32_t)(high) << 16) | \
108 				 ((low) & 0xFFFF))
109 
110 /* merges two 4-bit values into a single 8-bit value */
111 #define merge_4b4b(high, low) (((uint8_t)(high) << 4) | \
112 			       ((low) & 0xF))
113 
114 /* Get max and min signed integer values for N bits word length */
115 #define INT_MAX_FOR_NUMBER_OF_BITS(N)	((int64_t)((1ULL << ((N) - 1)) - 1))
116 #define INT_MIN_FOR_NUMBER_OF_BITS(N)	((int64_t)(-((1ULL << ((N) - 1)) - 1) - 1))
117 
118 /* Speed of sound (m/s) in 20 C temperature at standard atmospheric pressure */
119 #define SPEED_OF_SOUND		343
120 
121 #endif /* __SOF_MATH_NUMBERS_H__ */
122