[riot-notifications] [RIOT-OS/RIOT] sys: add units.h for helper macros (#13530)

Kaspar Schleiser notifications at github.com
Tue May 12 10:34:51 CEST 2020


@kaspar030 commented on this pull request.



> + * @brief       Unit helper macros
+ *
+ * @author      Benjamin Valentin <benjamin.valentin at ml-pa.com>
+ */
+
+#ifndef UNITS_H
+#define UNITS_H
+
+#ifdef __cplusplus
+ extern "C" {
+#endif
+
+/* KiB, MiB, GiB */
+#define KiB(x) ((unsigned long)(x) << 10)
+#define MiB(x) (KiB(x) << 10)
+#define GiB(x) (MiB(x) << 10)

Just to confirm - this only works for up to 3g, right? (because `4 = 0b100`, and `0b100 << 30 = 0b1[32 zeros]`, which overflows unsigned int, which KiB has been force-casted to.)

If we define `KiB()` as `unsigned long long`, will the optimizer do the right thing?

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/RIOT-OS/RIOT/pull/13530#pullrequestreview-409819023
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.riot-os.org/pipermail/notifications/attachments/20200512/7ea78aed/attachment.htm>


More information about the notifications mailing list