1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/core/include/macros/units.h
Benjamin Valentin 0862a3c512 core/include: Don't use 64 bit for MHZ & MiB macros
Those macros are all about convenience. However, always using 64 bit makes casts
nececcary that goes against the idea of having a convenience macro.

E.g. when printing a frequency in KHZ one might want to do

	printf("freq: %lu kHz\n", freq / KHZ(1));

leads to an error

> error: format '%lu' expects argument of type 'long unsigned int', but argument 2 has type 'long long unsigned int'

Now we would have to cast - `%llu` is not available with newlib-nano and wholly
uneccecary.

Only use 64 bit artithmetic where necessary (GHZ, GiB), not for smaller units.
2020-06-17 12:50:50 +02:00

62 lines
1.1 KiB
C

/*
* Copyright (C) 2020 ML!PA Consulting GmbH
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup core_macros
* @{
*
* @file
* @brief Unit helper macros
*
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
*/
#ifndef MACROS_UNITS_H
#define MACROS_UNITS_H
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief A macro to return the bytes in x KiB
*/
#define KiB(x) ((unsigned long)(x) << 10)
/**
* @brief A macro to return the bytes in x MiB
*/
#define MiB(x) (KiB(x) << 10)
/**
* @brief A macro to return the bytes in x GiB
*/
#define GiB(x) ((unsigned long long)MiB(x) << 10)
/**
* @brief A macro to return the Hz in x kHz
*/
#define KHZ(x) ((x) * 1000UL)
/**
* @brief A macro to return the Hz in x MHz
*/
#define MHZ(x) (KHZ(x) * 1000UL)
/**
* @brief A macro to return the Hz in x GHz
*/
#define GHZ(x) (MHZ(x) * 1000ULL)
#ifdef __cplusplus
}
#endif
#endif /* MACROS_UNITS_H */
/** @} */