Skip to content

Commit

Permalink
Add mod implementation to runtime library that is different from rem
Browse files Browse the repository at this point in the history
  • Loading branch information
rbanerjee20 committed Oct 4, 2024
1 parent 483b585 commit 9737224
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion runtime/libcn/include/cn-executable/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,18 @@ cn_bool *cn_pointer_gt(cn_pointer *i1, cn_pointer *i2);
return CNTYPE##_gt(i1, i2) ? i1 : i2;\
}

/* TODO: Add case for negative numbers. For non-negative numbers mod and rem are the same */
/* TODO: Account for UB: https://stackoverflow.com/a/20638659 */
#define CN_GEN_MOD(CTYPE, CNTYPE)\
static inline CNTYPE *CNTYPE##_mod(CNTYPE *i1, CNTYPE *i2) {\
CNTYPE *res = (CNTYPE *) alloc(sizeof(CNTYPE));\
res->val = i1->val % i2->val;\
if (res->val < 0) {\
res->val = (i2->val < 0) ? res->val - i2->val : res->val + i2->val;\
}\
return res;\
}


#define CN_GEN_REM(CTYPE, CNTYPE)\
static inline CNTYPE *CNTYPE##_rem(CNTYPE *i1, CNTYPE *i2) {\
CNTYPE *res = (CNTYPE *) alloc(sizeof(CNTYPE));\
Expand Down

0 comments on commit 9737224

Please sign in to comment.