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