Skip to content

Commit

Permalink
cleanup: include/: move misc/math_extras.h to sys/math_extras.h
Browse files Browse the repository at this point in the history
move misc/math_extras.h to sys/math_extras.h and
create a shim for backward-compatibility.

No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.

Related to #16539

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
  • Loading branch information
nashif committed Jun 28, 2019
1 parent 68cb66d commit 6ecadb0
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 70 deletions.
67 changes: 5 additions & 62 deletions include/misc/math_extras.h
Original file line number Diff line number Diff line change
@@ -1,72 +1,15 @@
/*
* Copyright (c) 2019 Facebook.
* Copyright (c) 2019 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @file
* @brief Extra arithmetic and bitmanipulation functions.
*
* @details This header file provides portable wrapper functions for a number of
* arithmetic and bit-counting functions that are often provided by compiler
* builtins. If the compiler does not have an appropriate builtin, a portable C
* implementation is used instead.
*/

#ifndef ZEPHYR_INCLUDE_MISC_MATH_EXTRAS_H_
#define ZEPHYR_INCLUDE_MISC_MATH_EXTRAS_H_

#include <zephyr/types.h>
#include <stdbool.h>
#include <stddef.h>

/**
* @name Unsigned integer addition with overflow detection.
*
* These functions compute `a + b` and store the result in `*result`, returning
* true if the operation overflowed.
*/
/**@{*/
static bool u32_add_overflow(u32_t a, u32_t b, u32_t *result);
static bool u64_add_overflow(u64_t a, u64_t b, u64_t *result);
static bool size_add_overflow(size_t a, size_t b, size_t *result);
/**@}*/

/**
* @name Unsigned integer multiplication with overflow detection.
*
* These functions compute `a * b` and store the result in `*result`, returning
* true if the operation overflowed.
*/
/**@{*/
static bool u32_mul_overflow(u32_t a, u32_t b, u32_t *result);
static bool u64_mul_overflow(u64_t a, u64_t b, u64_t *result);
static bool size_mul_overflow(size_t a, size_t b, size_t *result);
/**@}*/

/**
* @name Count leading zeros.
*
* Count the number of leading zero bits in the bitwise representation of `x`.
* When `x = 0`, this is the size of `x` in bits.
*/
/**@{*/
static int u32_count_leading_zeros(u32_t x);
static int u64_count_leading_zeros(u64_t x);
/**@}*/

/**
* @name Count trailing zeros.
*
* Count the number of trailing zero bits in the bitwise representation of `x`.
* When `x = 0`, this is the size of `x` in bits.
*/
/**@{*/
static int u32_count_trailing_zeros(u32_t x);
static int u64_count_trailing_zeros(u64_t x);
/**@}*/
#ifndef CONFIG_COMPAT_INCLUDES
#warning "This header file has moved, include <sys/math_extras.h> instead."
#endif

#include <misc/math_extras_impl.h>
#include <sys/math_extras.h>

#endif /* ZEPHYR_INCLUDE_MISC_MATH_EXTRAS_H_ */
72 changes: 72 additions & 0 deletions include/sys/math_extras.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2019 Facebook.
*
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @file
* @brief Extra arithmetic and bitmanipulation functions.
*
* @details This header file provides portable wrapper functions for a number of
* arithmetic and bit-counting functions that are often provided by compiler
* builtins. If the compiler does not have an appropriate builtin, a portable C
* implementation is used instead.
*/

#ifndef ZEPHYR_INCLUDE_SYS_MATH_EXTRAS_H_
#define ZEPHYR_INCLUDE_SYS_MATH_EXTRAS_H_

#include <zephyr/types.h>
#include <stdbool.h>
#include <stddef.h>

/**
* @name Unsigned integer addition with overflow detection.
*
* These functions compute `a + b` and store the result in `*result`, returning
* true if the operation overflowed.
*/
/**@{*/
static bool u32_add_overflow(u32_t a, u32_t b, u32_t *result);
static bool u64_add_overflow(u64_t a, u64_t b, u64_t *result);
static bool size_add_overflow(size_t a, size_t b, size_t *result);
/**@}*/

/**
* @name Unsigned integer multiplication with overflow detection.
*
* These functions compute `a * b` and store the result in `*result`, returning
* true if the operation overflowed.
*/
/**@{*/
static bool u32_mul_overflow(u32_t a, u32_t b, u32_t *result);
static bool u64_mul_overflow(u64_t a, u64_t b, u64_t *result);
static bool size_mul_overflow(size_t a, size_t b, size_t *result);
/**@}*/

/**
* @name Count leading zeros.
*
* Count the number of leading zero bits in the bitwise representation of `x`.
* When `x = 0`, this is the size of `x` in bits.
*/
/**@{*/
static int u32_count_leading_zeros(u32_t x);
static int u64_count_leading_zeros(u64_t x);
/**@}*/

/**
* @name Count trailing zeros.
*
* Count the number of trailing zero bits in the bitwise representation of `x`.
* When `x = 0`, this is the size of `x` in bits.
*/
/**@{*/
static int u32_count_trailing_zeros(u32_t x);
static int u64_count_trailing_zeros(u64_t x);
/**@}*/

#include <misc/math_extras_impl.h>

#endif /* ZEPHYR_INCLUDE_SYS_MATH_EXTRAS_H_ */
2 changes: 1 addition & 1 deletion kernel/include/syscall_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#ifndef _ASMLANGUAGE
#include <kernel.h>
#include <misc/printk.h>
#include <misc/math_extras.h>
#include <sys/math_extras.h>
#include <kernel_internal.h>
#include <stdbool.h>

Expand Down
2 changes: 1 addition & 1 deletion kernel/mempool.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <init.h>
#include <string.h>
#include <sys/__assert.h>
#include <misc/math_extras.h>
#include <sys/math_extras.h>
#include <stdbool.h>

static struct k_spinlock lock;
Expand Down
2 changes: 1 addition & 1 deletion kernel/msg_q.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <string.h>
#include <wait_q.h>
#include <sys/dlist.h>
#include <misc/math_extras.h>
#include <sys/math_extras.h>
#include <init.h>
#include <syscall_handler.h>
#include <kernel_internal.h>
Expand Down
2 changes: 1 addition & 1 deletion kernel/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <spinlock.h>
#include <kernel_structs.h>
#include <misc/printk.h>
#include <misc/math_extras.h>
#include <sys/math_extras.h>
#include <sys_clock.h>
#include <drivers/timer/system_timer.h>
#include <ksched.h>
Expand Down
2 changes: 1 addition & 1 deletion kernel/userspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include <kernel.h>
#include <string.h>
#include <misc/math_extras.h>
#include <sys/math_extras.h>
#include <misc/printk.h>
#include <misc/rb.h>
#include <kernel_structs.h>
Expand Down
2 changes: 1 addition & 1 deletion lib/libc/minimal/source/stdlib/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <zephyr.h>
#include <init.h>
#include <errno.h>
#include <misc/math_extras.h>
#include <sys/math_extras.h>
#include <misc/mempool.h>
#include <string.h>
#include <app_memory/app_memdomain.h>
Expand Down
2 changes: 1 addition & 1 deletion subsys/net/lib/sockets/sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ LOG_MODULE_REGISTER(net_sock, CONFIG_NET_SOCKETS_LOG_LEVEL);
#include <net/socket.h>
#include <syscall_handler.h>
#include <sys/fdtable.h>
#include <misc/math_extras.h>
#include <sys/math_extras.h>

#include "sockets_internal.h"

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/math_extras/tests.inc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

#include <ztest.h>
#include <misc/math_extras.h>
#include <sys/math_extras.h>
#include <inttypes.h>

static void VNAME(u32_add)(void)
Expand Down

0 comments on commit 6ecadb0

Please sign in to comment.