xref: /dpdk/lib/sched/rte_sched_common.h (revision 719834a6849e1daf4a70ff7742bbcc3ae7e25607)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2010-2014 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
599a2dd95SBruce Richardson #ifndef __INCLUDE_RTE_SCHED_COMMON_H__
699a2dd95SBruce Richardson #define __INCLUDE_RTE_SCHED_COMMON_H__
799a2dd95SBruce Richardson 
8*719834a6SMattias Rönnblom #include <stdint.h>
9*719834a6SMattias Rönnblom #include <sys/types.h>
10*719834a6SMattias Rönnblom 
1199a2dd95SBruce Richardson #ifdef __cplusplus
1299a2dd95SBruce Richardson extern "C" {
1399a2dd95SBruce Richardson #endif
1499a2dd95SBruce Richardson 
1599a2dd95SBruce Richardson #if 0
1699a2dd95SBruce Richardson static inline uint32_t
1799a2dd95SBruce Richardson rte_min_pos_4_u16(uint16_t *x)
1899a2dd95SBruce Richardson {
1999a2dd95SBruce Richardson 	uint32_t pos0, pos1;
2099a2dd95SBruce Richardson 
2199a2dd95SBruce Richardson 	pos0 = (x[0] <= x[1])? 0 : 1;
2299a2dd95SBruce Richardson 	pos1 = (x[2] <= x[3])? 2 : 3;
2399a2dd95SBruce Richardson 
2499a2dd95SBruce Richardson 	return (x[pos0] <= x[pos1])? pos0 : pos1;
2599a2dd95SBruce Richardson }
2699a2dd95SBruce Richardson 
2799a2dd95SBruce Richardson #else
2899a2dd95SBruce Richardson 
2999a2dd95SBruce Richardson /* simplified version to remove branches with CMOV instruction */
3099a2dd95SBruce Richardson static inline uint32_t
3199a2dd95SBruce Richardson rte_min_pos_4_u16(uint16_t *x)
3299a2dd95SBruce Richardson {
3399a2dd95SBruce Richardson 	uint32_t pos0 = 0;
3499a2dd95SBruce Richardson 	uint32_t pos1 = 2;
3599a2dd95SBruce Richardson 
3699a2dd95SBruce Richardson 	if (x[1] <= x[0]) pos0 = 1;
3799a2dd95SBruce Richardson 	if (x[3] <= x[2]) pos1 = 3;
3899a2dd95SBruce Richardson 	if (x[pos1] <= x[pos0]) pos0 = pos1;
3999a2dd95SBruce Richardson 
4099a2dd95SBruce Richardson 	return pos0;
4199a2dd95SBruce Richardson }
4299a2dd95SBruce Richardson 
4399a2dd95SBruce Richardson #endif
4499a2dd95SBruce Richardson 
4599a2dd95SBruce Richardson /*
4699a2dd95SBruce Richardson  * Compute the Greatest Common Divisor (GCD) of two numbers.
4799a2dd95SBruce Richardson  * This implementation uses Euclid's algorithm:
4899a2dd95SBruce Richardson  *    gcd(a, 0) = a
4999a2dd95SBruce Richardson  *    gcd(a, b) = gcd(b, a mod b)
5099a2dd95SBruce Richardson  */
51eb5636e8SXueming Li static inline uint64_t
52eb5636e8SXueming Li rte_get_gcd64(uint64_t a, uint64_t b)
5399a2dd95SBruce Richardson {
54eb5636e8SXueming Li 	uint64_t c;
5599a2dd95SBruce Richardson 
5699a2dd95SBruce Richardson 	if (a == 0)
5799a2dd95SBruce Richardson 		return b;
5899a2dd95SBruce Richardson 	if (b == 0)
5999a2dd95SBruce Richardson 		return a;
6099a2dd95SBruce Richardson 
6199a2dd95SBruce Richardson 	if (a < b) {
6299a2dd95SBruce Richardson 		c = a;
6399a2dd95SBruce Richardson 		a = b;
6499a2dd95SBruce Richardson 		b = c;
6599a2dd95SBruce Richardson 	}
6699a2dd95SBruce Richardson 
6799a2dd95SBruce Richardson 	while (b != 0) {
6899a2dd95SBruce Richardson 		c = a % b;
6999a2dd95SBruce Richardson 		a = b;
7099a2dd95SBruce Richardson 		b = c;
7199a2dd95SBruce Richardson 	}
7299a2dd95SBruce Richardson 
7399a2dd95SBruce Richardson 	return a;
7499a2dd95SBruce Richardson }
7599a2dd95SBruce Richardson 
7699a2dd95SBruce Richardson /*
77eb5636e8SXueming Li  * 32-bit version of Greatest Common Divisor (GCD).
78eb5636e8SXueming Li  */
79eb5636e8SXueming Li static inline uint32_t
80eb5636e8SXueming Li rte_get_gcd(uint32_t a, uint32_t b)
81eb5636e8SXueming Li {
82eb5636e8SXueming Li 	return rte_get_gcd64(a, b);
83eb5636e8SXueming Li }
84eb5636e8SXueming Li 
85eb5636e8SXueming Li /*
8699a2dd95SBruce Richardson  * Compute the Lowest Common Denominator (LCD) of two numbers.
8799a2dd95SBruce Richardson  * This implementation computes GCD first:
8899a2dd95SBruce Richardson  *    LCD(a, b) = (a * b) / GCD(a, b)
8999a2dd95SBruce Richardson  */
9099a2dd95SBruce Richardson static inline uint32_t
9199a2dd95SBruce Richardson rte_get_lcd(uint32_t a, uint32_t b)
9299a2dd95SBruce Richardson {
9399a2dd95SBruce Richardson 	return (a * b) / rte_get_gcd(a, b);
9499a2dd95SBruce Richardson }
9599a2dd95SBruce Richardson 
9699a2dd95SBruce Richardson #ifdef __cplusplus
9799a2dd95SBruce Richardson }
9899a2dd95SBruce Richardson #endif
9999a2dd95SBruce Richardson 
10099a2dd95SBruce Richardson #endif /* __INCLUDE_RTE_SCHED_COMMON_H__ */
101