2015-04-11 07:44:37 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011-2014, Wind River Systems, Inc.
|
|
|
|
*
|
2015-10-07 00:00:37 +08:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2015-04-11 07:44:37 +08:00
|
|
|
*
|
2015-10-07 00:00:37 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-04-11 07:44:37 +08:00
|
|
|
*
|
2015-10-07 00:00:37 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2015-04-11 07:44:37 +08:00
|
|
|
*/
|
|
|
|
|
2015-12-04 23:09:39 +08:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Misc utilities
|
|
|
|
*
|
2015-10-21 00:42:33 +08:00
|
|
|
* Misc utilities usable by nanokernel, microkernel, and application code.
|
2015-07-02 05:22:39 +08:00
|
|
|
*/
|
2015-04-11 07:44:37 +08:00
|
|
|
|
|
|
|
#ifndef _UTIL__H_
|
|
|
|
#define _UTIL__H_
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2015-08-12 22:48:18 +08:00
|
|
|
#ifndef _ASMLANGUAGE
|
|
|
|
|
2016-06-28 19:20:46 +08:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2015-09-10 21:03:23 +08:00
|
|
|
/* Helper to pass a int as a pointer or vice-versa.
|
2015-10-21 00:42:33 +08:00
|
|
|
* Those are available for 32 bits architectures:
|
|
|
|
*/
|
2015-09-10 21:03:23 +08:00
|
|
|
#define POINTER_TO_UINT(x) ((uint32_t) (x))
|
|
|
|
#define UINT_TO_POINTER(x) ((void *) (x))
|
|
|
|
#define POINTER_TO_INT(x) ((int32_t) (x))
|
|
|
|
#define INT_TO_POINTER(x) ((void *) (x))
|
|
|
|
|
2015-08-06 01:04:55 +08:00
|
|
|
#define ARRAY_SIZE(array) ((unsigned long)(sizeof(array) / sizeof((array)[0])))
|
2015-06-15 00:07:34 +08:00
|
|
|
#define CONTAINER_OF(ptr, type, field) \
|
|
|
|
((type *)(((char *)(ptr)) - offsetof(type, field)))
|
2015-05-12 16:15:11 +08:00
|
|
|
|
2015-04-11 07:44:37 +08:00
|
|
|
/* round "x" up/down to next multiple of "align" (which must be a power of 2) */
|
|
|
|
#define ROUND_UP(x, align) \
|
|
|
|
(((unsigned long)(x) + ((unsigned long)align - 1)) & \
|
|
|
|
~((unsigned long)align - 1))
|
|
|
|
#define ROUND_DOWN(x, align) ((unsigned long)(x) & ~((unsigned long)align - 1))
|
|
|
|
|
|
|
|
#ifdef INLINED
|
|
|
|
#define INLINE inline
|
|
|
|
#else
|
|
|
|
#define INLINE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef max
|
|
|
|
#define max(a, b) (((a) > (b)) ? (a) : (b))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef min
|
|
|
|
#define min(a, b) (((a) < (b)) ? (a) : (b))
|
|
|
|
#endif
|
|
|
|
|
2015-08-21 01:56:10 +08:00
|
|
|
static inline int is_power_of_two(unsigned int x)
|
2015-04-11 07:44:37 +08:00
|
|
|
{
|
|
|
|
return (x != 0) && !(x & (x - 1));
|
|
|
|
}
|
2016-06-28 19:20:46 +08:00
|
|
|
|
|
|
|
static inline int64_t arithmetic_shift_right(int64_t value, uint8_t shift)
|
|
|
|
{
|
|
|
|
int64_t sign_ext;
|
|
|
|
|
|
|
|
if (shift == 0) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* extract sign bit */
|
|
|
|
sign_ext = (value >> 63) & 1;
|
|
|
|
|
|
|
|
/* make all bits of sign_ext be the same as the value's sign bit */
|
|
|
|
sign_ext = -sign_ext;
|
|
|
|
|
|
|
|
/* shift value and fill opened bit positions with sign bit */
|
|
|
|
return (value >> shift) | (sign_ext << (64 - shift));
|
|
|
|
}
|
|
|
|
|
2015-04-11 07:44:37 +08:00
|
|
|
#endif /* !_ASMLANGUAGE */
|
|
|
|
|
|
|
|
/* KB, MB, GB */
|
|
|
|
#define KB(x) ((x) << 10)
|
|
|
|
#define MB(x) (KB(x) << 10)
|
|
|
|
#define GB(x) (MB(x) << 10)
|
|
|
|
|
|
|
|
/* KHZ, MHZ */
|
|
|
|
#define KHZ(x) ((x) * 1000)
|
|
|
|
#define MHZ(x) (KHZ(x) * 1000)
|
|
|
|
|
2016-07-08 17:23:41 +08:00
|
|
|
#ifndef BIT
|
2015-10-27 07:38:13 +08:00
|
|
|
#define BIT(n) (1UL << (n))
|
2016-07-08 17:23:41 +08:00
|
|
|
#endif
|
2015-10-27 07:38:13 +08:00
|
|
|
|
2016-03-22 20:53:41 +08:00
|
|
|
#define BIT_MASK(n) (BIT(n) - 1)
|
|
|
|
|
2015-04-11 07:44:37 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* _UTIL__H_ */
|