Sound Open Firmware
Loading...
Searching...
No Matches
numbers.h
Go to the documentation of this file.
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
16#ifndef __SOF_MATH_NUMBERS_H__
17#define __SOF_MATH_NUMBERS_H__
18
19#include <stdint.h>
20
21#ifndef __ZEPHYR__
22/* Unsafe and portable macros for consistency with Zephyr.
23 * See SEI CERT-C PRE31-C
24 */
25#define MIN(a, b) ((a) < (b) ? (a) : (b))
26#define MAX(a, b) ((a) < (b) ? (b) : (a))
27
28#define ROUND_DOWN(size, alignment) ({ \
29 __typeof__(size) __size = (size); \
30 __typeof__(alignment) __alignment = (alignment); \
31 __size - (__size % __alignment); \
32})
33
34#define ROUND_UP(size, alignment) ({ \
35 __typeof__(size) __size = (size); \
36 __typeof__(alignment) __alignment = (alignment); \
37 ((__size + __alignment - 1) / __alignment) * __alignment; \
38})
39#endif /* ! __ZEPHYR__ */
40
41#define ABS(a) ({ \
42 __typeof__(a) __a = (a); \
43 __a < 0 ? -__a : __a; \
44})
45#define SGN(a) ({ \
46 __typeof__(a) __a = (a); \
47 __a < 0 ? -1 : \
48 __a > 0 ? 1 : 0; \
49})
50
51/* Zephyr added gcd() in 2025/Nov to sys/util.h */
52#ifndef gcd
53#define USE_SOF_GCD 1
54int gcd(int a, int b); /* Calculate greatest common divisor for a and b */
55#endif
56
57/* This is a divide function that returns ceil of the quotient.
58 * E.g. ceil_divide(9, 3) returns 3, ceil_divide(10, 3) returns 4.
59 */
60static inline int ceil_divide(int a, int b)
61{
62 int c;
63
64 c = a / b;
65
66 /* First, we check whether the signs of the params are different.
67 * If they are, we already know the result is going to be negative and
68 * therefore, is going to be already rounded up (truncated).
69 *
70 * If the signs are the same, we check if there was any remainder in
71 * the division by multiplying the number back.
72 */
73 if (!((a ^ b) & (1U << ((sizeof(int) * 8) - 1))) && c * b != a)
74 c++;
75
76 return c;
77}
78
96static inline void cross_product_s16(int32_t *px, int32_t *py, int32_t *pz,
97 int16_t a, int16_t b, int16_t c,
98 int16_t d, int16_t e, int16_t f)
99{
100 *px = (int32_t)b * f - (int32_t)c * e;
101 *py = (int32_t)c * d - (int32_t)a * f;
102 *pz = (int32_t)a * e - (int32_t)b * d;
103}
104
105/* Find indices of equal values in a vector of integer values */
106int find_equal_int16(int16_t idx[], int16_t vec[], int n, int vec_length,
107 int max_results);
108
109/* Return the smallest value found in a vector */
110int16_t find_min_int16(int16_t vec[], int vec_length);
111
112/* Return the largest absolute value found in a vector */
113int32_t find_max_abs_int32(int32_t vec[], int vec_length);
114
115/* Count the left shift amount to normalize a 32 bit signed integer value
116 * without causing overflow. Input value 0 will result to 31.
117 */
118int norm_int32(int32_t val);
119
120uint32_t crc32(uint32_t base, const void *data, uint32_t bytes);
121
122/* merges two 16-bit values into a single 32-bit value */
123#define merge_16b16b(high, low) (((uint32_t)(high) << 16) | \
124 ((low) & 0xFFFF))
125
126/* merges two 4-bit values into a single 8-bit value */
127#define merge_4b4b(high, low) (((uint8_t)(high) << 4) | \
128 ((low) & 0xF))
129
130/* Get max and min signed integer values for N bits word length */
131#define INT_MAX_FOR_NUMBER_OF_BITS(N) ((int64_t)((1ULL << ((N) - 1)) - 1))
132#define INT_MIN_FOR_NUMBER_OF_BITS(N) ((int64_t)(-((1ULL << ((N) - 1)) - 1) - 1))
133
134/* Speed of sound (m/s) in 20 C temperature at standard atmospheric pressure */
135#define SPEED_OF_SOUND 343
136#define RECIPROCAL_SPEED_OF_SOUND_Q31 6260885 /* Q1.31 */
137
138#endif /* __SOF_MATH_NUMBERS_H__ */
uint8_t data[0]
Binary byte payload for raw controls.
Definition header.h:5
int16_t find_min_int16(int16_t vec[], int vec_length)
int norm_int32(int32_t val)
static void 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)
Cross product function.
Definition numbers.h:96
int32_t find_max_abs_int32(int32_t vec[], int vec_length)
static int ceil_divide(int a, int b)
Definition numbers.h:60
uint32_t crc32(uint32_t base, const void *data, uint32_t bytes)
int gcd(int a, int b)
int find_equal_int16(int16_t idx[], int16_t vec[], int n, int vec_length, int max_results)