From 32dfcc109c6bb10465015a77b51f06856c2a7d95 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Sat, 28 Oct 2023 18:16:50 +0100 Subject: Added macro to do safe subtractions on a word Default C just lets overflows occur for subtraction, so this macro will default to 0 if the subtraction causes an overflow. --- lib/base.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/base.h b/lib/base.h index dbeec80..f21e4a8 100644 --- a/lib/base.h +++ b/lib/base.h @@ -49,9 +49,10 @@ typedef enum DATA_TYPE_WORD, } data_type_t; -#define DBYTE(BYTE) ((data_t){.as_byte = (BYTE)}) -#define DHWORD(HWORD) ((data_t){.as_hword = (HWORD)}) -#define DWORD(WORD) ((data_t){.as_word = (WORD)}) +#define DBYTE(BYTE) ((data_t){.as_byte = (BYTE)}) +#define DHWORD(HWORD) ((data_t){.as_hword = (HWORD)}) +#define DWORD(WORD) ((data_t){.as_word = (WORD)}) +#define WORD_SAFE_SUB(W, SUB) ((W) > (SUB) ? ((W) - (SUB)) : 0) #define HWORD_SIZE sizeof(hword) #define WORD_SIZE sizeof(word) -- cgit v1.2.3-13-gbd6f