bw_common

A common header to make sure that a bunch of basic definitions are available and consistent for all modules.

API

Module type: Foundation

BW_INCLUDE_WITH_QUOTES

Normally, modules include other such modules using angle brackets.

If you would rather prefer that to happen using double quotes, you can define BW_INCLUDE_WITH_QUOTES.

BW_NO_CXX

If BW_NO_CXX is defined, the C++ APIs and implementations in modules are not included.

BW_CXX_NO_EXTERN_C

Normally, the C APIs and implementations in modules are included in extern "C" blocks when using a C++ compiler, even if BW_NO_CXX is defined.

If you don't want to have them included in such blocks, you can define BW_CXX_NO_EXTERN_C.

BW_CXX_NO_ARRAY

C++ APIs in modules typically include overloaded methods that use std::array arguments, and thus require the <array> header file.

If this is not wanted, defining BW_CXX_NO_ARRAY suppresses such methods and the inclusion of said header file.

Basic definitions

Modules require definitions of:

  • NULL (C only) and size_t, normally supplied by stddef.h;
  • (u)int{8,16,32,64}_t, INT{8,16,32,64}_{MIN,MAX}, and UINT{8,16,32,64}_MAX, normally supplied by stdint.h;
  • INFINITY, normally supplied by math.h.

You can control whether those definitions are taken from said headers or you can supply them yourselves. In particular:

  • if BW_NO_STDLIB or BW_NO_STDDEF_H is defined, then stddef.h is not #included;
  • if BW_NO_STDLIB or BW_NO_STDINT_H is defined, then stdint.h is not #included;
  • if BW_NO_STDLIB or BW_NO_MATH_H is defined, then math.h is not #included.

If not already defined, a BW_NULL macro is defined whose value is either NULL (C or C++ disabled) or nullptr (C++).

BW_RESTRICT

BW_RESTRICT is a preprocessor definition that wraps the restrict keyword.

If it is not defined already, then it gets defined as restrict if C99 is supported, otherwise it has no effect (typically when compiling C++ code).

BW_ASSERT and BW_ASSERT_DEEP

Both macros are assert()-like. BW_ASSERT is meant to perform rapid/basic validity checks (e.g., input pointer is not NULL, input float number is finite), while BW_ASSERT_DEEP should be used for deeper checks (e.g., validate data structures and internal states).

If BW_NO_DEBUG is defined then "calls" to both BW_ASSERT and BW_ASSERT_DEEP will be stripped away by the preprocessor.

Whether BW_NO_DEBUG is defined or not, "calls" to BW_ASSERT_DEEP will still be stripped anyway if BW_DEBUG_DEEP is not defined.

Otherwise, BW_ASSERT and BW_ASSERT_DEEP can either be provided by you, otherwise BW_ASSERT is defined as assert (assert.h is #included and BW_NO_STDLIB must not be defined — please mind that assert.h is still influenced by NDEBUG), and BW_ASSERT_DEEP is defined as BW_ASSERT.

bw_is_inf()

static inline char bw_is_inf(
	float x);

Returns non-0 if x is positive or negative infinity, 0 otherwise.

bw_is_nan()

static inline char bw_is_nan(
	float x);

Returns non-0 if x is NaN, 0 otherwise.

bw_is_finite()

static inline char bw_is_finite(
	float x);

Returns non-0 if x is finite (neither NaN nor positive or negative infinity), 0 otherwise.

bw_has_inf()

static inline char bw_has_inf(
	const float * BW_RESTRICT x,
	size_t                    n_elems);

Scans the fist n_elems in buffer x and returns non-0 if it contains at least one positive or negative inifinity value, 0 otherwise.

bw_has_nan()

static inline char bw_has_nan(
	const float * BW_RESTRICT x,
	size_t                    n_elems);

Scans the fist n_elems in buffer x and returns non-0 if it contains at least one NaN value, 0 otherwise.

bw_has_only_finite()

static inline char bw_has_only_finite(
	const float * BW_RESTRICT x,
	size_t                    n_elems);

Scans the fist n_elems in buffer x and returns non-0 if it only finds finite values (neither NaN nor positive or negative infinity), 0 otherwise.

bw_hash_sdbm()

static inline uint32_t bw_hash_sdbm(
	const char * BW_RESTRICT string);

Returns the sdbm hash of the given string.

Changelog

  • Version 1.2.0:
    • Added BW_INCLUDE_WITH_QUOTES, BW_NO_CXX, and BW_CXX_NO_EXTERN_C.
    • Allowed custom definitions of BW_NULL.
    • Accomodate MSVC reporting incorrect C++ standard support.
    • Removed "Brickworks" references in the documentation.
  • Version 1.1.0:
    • Added BW_NULL and relaxed NULL definition requirement in C++.
    • Added BW_CXX_NO_ARRAY.
    • Fixed sign-related warning in bw_hash_sdbm().
    • Now checking that C++ compilers support C++11 or later.
  • Version 1.0.0:
    • Removed BW_SIZE_T and went for size_t instead.
    • Made control over inclusion of standard library headers more explicit via BW_NO_STDLIB, BW_NO_STDDEF_H, BW_NO_STDINT_H, and BW_NO_MATH_H.
    • Introduced BW_NO_DEBUG to replace NDEBUG and prevented useless inclusion of assert.h.
    • Added BW_RESTRICT specifiers to input arguments.
    • Removed usage of reserved identifiers.
    • Added extern "C" to functions.
  • Version 0.6.0:
    • Added BW_ASSERT, bw_is_inf(), bw_is_nan(), bw_is_finite(), bw_has_inf(), bw_has_nan(), bw_has_only_finite(), and bw_hash_sdbm().
    • Removed dependency on bw_config.
  • Version 0.5.0:
    • Fixed typo in the documentation of BW_SIZE_T.
  • Version 0.4.0:
    • Added BW_SIZE_T.
  • Version 0.2.0:
    • Removed BW_MALLOC, BW_REALLOC, and BW_FREE.
    • Added BW_RESTRICT.
  • Version 0.1.0:
    • First release.