Replaced clamping macros with functions to avoid problems with evaluating expressions twice