xref: /dpdk/lib/sched/rte_sched_common.h (revision 719834a6849e1daf4a70ff7742bbcc3ae7e25607)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 
5 #ifndef __INCLUDE_RTE_SCHED_COMMON_H__
6 #define __INCLUDE_RTE_SCHED_COMMON_H__
7 
8 #include <stdint.h>
9 #include <sys/types.h>
10 
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 
15 #if 0
16 static inline uint32_t
17 rte_min_pos_4_u16(uint16_t *x)
18 {
19 	uint32_t pos0, pos1;
20 
21 	pos0 = (x[0] <= x[1])? 0 : 1;
22 	pos1 = (x[2] <= x[3])? 2 : 3;
23 
24 	return (x[pos0] <= x[pos1])? pos0 : pos1;
25 }
26 
27 #else
28 
29 /* simplified version to remove branches with CMOV instruction */
30 static inline uint32_t
31 rte_min_pos_4_u16(uint16_t *x)
32 {
33 	uint32_t pos0 = 0;
34 	uint32_t pos1 = 2;
35 
36 	if (x[1] <= x[0]) pos0 = 1;
37 	if (x[3] <= x[2]) pos1 = 3;
38 	if (x[pos1] <= x[pos0]) pos0 = pos1;
39 
40 	return pos0;
41 }
42 
43 #endif
44 
45 /*
46  * Compute the Greatest Common Divisor (GCD) of two numbers.
47  * This implementation uses Euclid's algorithm:
48  *    gcd(a, 0) = a
49  *    gcd(a, b) = gcd(b, a mod b)
50  */
51 static inline uint64_t
52 rte_get_gcd64(uint64_t a, uint64_t b)
53 {
54 	uint64_t c;
55 
56 	if (a == 0)
57 		return b;
58 	if (b == 0)
59 		return a;
60 
61 	if (a < b) {
62 		c = a;
63 		a = b;
64 		b = c;
65 	}
66 
67 	while (b != 0) {
68 		c = a % b;
69 		a = b;
70 		b = c;
71 	}
72 
73 	return a;
74 }
75 
76 /*
77  * 32-bit version of Greatest Common Divisor (GCD).
78  */
79 static inline uint32_t
80 rte_get_gcd(uint32_t a, uint32_t b)
81 {
82 	return rte_get_gcd64(a, b);
83 }
84 
85 /*
86  * Compute the Lowest Common Denominator (LCD) of two numbers.
87  * This implementation computes GCD first:
88  *    LCD(a, b) = (a * b) / GCD(a, b)
89  */
90 static inline uint32_t
91 rte_get_lcd(uint32_t a, uint32_t b)
92 {
93 	return (a * b) / rte_get_gcd(a, b);
94 }
95 
96 #ifdef __cplusplus
97 }
98 #endif
99 
100 #endif /* __INCLUDE_RTE_SCHED_COMMON_H__ */
101