/* Copyright (C) 2003-2013 Runtime Revolution Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see . */
#include
extern "C" MC_DLLEXPORT void MCBitwiseEvalBitwiseAnd(integer_t p_left, integer_t p_right, integer_t& r_output)
{
r_output = p_left & p_right;
}
extern "C" MC_DLLEXPORT void MCBitwiseEvalBitwiseOr(integer_t p_left, integer_t p_right, integer_t& r_output)
{
r_output = p_left | p_right;
}
extern "C" MC_DLLEXPORT void MCBitwiseEvalBitwiseXor(integer_t p_left, integer_t p_right, integer_t& r_output)
{
r_output = p_left ^ p_right;
}
extern "C" MC_DLLEXPORT void MCBitwiseEvalBitwiseNot(integer_t p_operand, integer_t& r_output)
{
r_output = ~p_operand;
}
/* Compute the maximum shift possible in C for a given operand. */
template
static inline void
MCBitwiseEvalBitwiseShiftCount (T p_operand, uinteger_t & p_shift)
{
/* Maximum shift count for which the C operator is defined */
uinteger_t t_max_shift = (sizeof(T) << 3) - 1;
p_shift = MCMin (p_shift, t_max_shift);
}
extern "C" MC_DLLEXPORT void
MCBitwiseEvalBitwiseShiftRight (integer_t p_operand,
uinteger_t p_shift,
integer_t & r_output)
{
MCBitwiseEvalBitwiseShiftCount (p_operand, p_shift);
r_output = p_operand >> p_shift;
}
extern "C" MC_DLLEXPORT void
MCBitwiseEvalBitwiseShiftLeft (integer_t p_operand,
uinteger_t p_shift,
integer_t& r_output)
{
MCBitwiseEvalBitwiseShiftCount (p_operand, p_shift);
integer_t t_shifted = p_operand << p_shift;
/* Overflow check */
if (p_operand != t_shifted >> p_shift)
{
MCErrorCreateAndThrow (kMCGenericErrorTypeInfo, "reason",
MCSTR("overflow in bitwise operation"), nil);
return;
}
r_output = t_shifted;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
#ifdef _TEST
void MCBitwiseRunTests()
{
}
#endif