improve value_bit_xxx

This commit is contained in:
lixianjing 2022-09-27 11:38:55 +08:00
parent 8e3060d7c6
commit 92e2a0564b
5 changed files with 91 additions and 20 deletions

View File

@ -53,7 +53,6 @@ static ret_t func_rshift(fscript_t* fscript, fscript_args_t* args, value_t* resu
static ret_t func_bit_set(fscript_t* fscript, fscript_args_t* args, value_t* result) {
uint32_t n = 0;
value_t* value = NULL;
ret_t ret = RET_OK;
result->type = VALUE_TYPE_INVALID;
FSCRIPT_FUNC_CHECK(args->size == 2, RET_BAD_PARAMS);
@ -66,7 +65,6 @@ static ret_t func_bit_set(fscript_t* fscript, fscript_args_t* args, value_t* res
static ret_t func_bit_clear(fscript_t* fscript, fscript_args_t* args, value_t* result) {
uint32_t n = 0;
value_t* value = NULL;
ret_t ret = RET_OK;
result->type = VALUE_TYPE_INVALID;
FSCRIPT_FUNC_CHECK(args->size == 2, RET_BAD_PARAMS);
@ -79,7 +77,6 @@ static ret_t func_bit_clear(fscript_t* fscript, fscript_args_t* args, value_t* r
static ret_t func_bit_toggle(fscript_t* fscript, fscript_args_t* args, value_t* result) {
uint32_t n = 0;
value_t* value = NULL;
ret_t ret = RET_OK;
result->type = VALUE_TYPE_INVALID;
FSCRIPT_FUNC_CHECK(args->size == 2, RET_BAD_PARAMS);
@ -92,7 +89,6 @@ static ret_t func_bit_toggle(fscript_t* fscript, fscript_args_t* args, value_t*
static ret_t func_bit_get(fscript_t* fscript, fscript_args_t* args, value_t* result) {
uint32_t n = 0;
value_t* value = NULL;
ret_t ret = RET_OK;
result->type = VALUE_TYPE_INVALID;
FSCRIPT_FUNC_CHECK(args->size == 2, RET_BAD_PARAMS);
@ -113,10 +109,10 @@ static ret_t func_bit_or(fscript_t* fscript, fscript_args_t* args, value_t* resu
return value_bit_or(args->args, args->args + 1, result);
}
static ret_t func_bit_nor(fscript_t* fscript, fscript_args_t* args, value_t* result) {
static ret_t func_bit_xor(fscript_t* fscript, fscript_args_t* args, value_t* result) {
FSCRIPT_FUNC_CHECK(args->size == 2, RET_BAD_PARAMS);
return value_bit_nor(args->args, args->args + 1, result);
return value_bit_xor(args->args, args->args + 1, result);
}
static ret_t func_bit_not(fscript_t* fscript, fscript_args_t* args, value_t* result) {
@ -127,7 +123,7 @@ static ret_t func_bit_not(fscript_t* fscript, fscript_args_t* args, value_t* res
FACTORY_TABLE_BEGIN(s_ext_bits)
FACTORY_TABLE_ENTRY("&", func_bit_and)
FACTORY_TABLE_ENTRY("^", func_bit_nor)
FACTORY_TABLE_ENTRY("^", func_bit_xor)
FACTORY_TABLE_ENTRY("~", func_bit_not)
FACTORY_TABLE_ENTRY("|", func_bit_or)
FACTORY_TABLE_ENTRY("<<", func_lshift)

View File

@ -1409,6 +1409,11 @@ ret_t value_bit_not(value_t* v, value_t* result) {
return_value_if_fail(v != NULL && result != NULL, RET_BAD_PARAMS);
switch (v->type) {
case VALUE_TYPE_BOOL: {
bool_t vv = value_bool(v);
value_set_bool(result, !vv);
break;
}
case VALUE_TYPE_INT8: {
int8_t vv = value_int8(v);
value_set_int8(result, ~vv);
@ -1466,6 +1471,11 @@ ret_t value_bit_or(value_t* v, value_t* other, value_t* result) {
type = tk_max_int((int)(v->type), (int)(other->type));
switch (type) {
case VALUE_TYPE_BOOL: {
bool_t vv = value_bool(v) | value_bool(other);
value_set_bool(result, vv);
break;
}
case VALUE_TYPE_INT8: {
int8_t vv = value_int8(v) | value_int8(other);
value_set_int8(result, vv);
@ -1523,6 +1533,11 @@ ret_t value_bit_and(value_t* v, value_t* other, value_t* result) {
type = tk_max_int((int)(v->type), (int)(other->type));
switch (type) {
case VALUE_TYPE_BOOL: {
bool_t vv = value_bool(v) & value_bool(other);
value_set_bool(result, vv);
break;
}
case VALUE_TYPE_INT8: {
int8_t vv = value_int8(v) & value_int8(other);
value_set_int8(result, vv);
@ -1572,7 +1587,7 @@ ret_t value_bit_and(value_t* v, value_t* other, value_t* result) {
return ret;
}
ret_t value_bit_nor(value_t* v, value_t* other, value_t* result) {
ret_t value_bit_xor(value_t* v, value_t* other, value_t* result) {
ret_t ret = RET_OK;
uint32_t type = 0;
return_value_if_fail(result != NULL, RET_BAD_PARAMS);
@ -1580,6 +1595,11 @@ ret_t value_bit_nor(value_t* v, value_t* other, value_t* result) {
type = tk_max_int((int)(v->type), (int)(other->type));
switch (type) {
case VALUE_TYPE_BOOL: {
bool_t vv = value_bool(v) ^ value_bool(other);
value_set_bool(result, vv);
break;
}
case VALUE_TYPE_INT8: {
int8_t vv = value_int8(v) ^ value_int8(other);
value_set_int8(result, vv);

View File

@ -1037,7 +1037,7 @@ ret_t value_bit_or(value_t* v, value_t* other, value_t* result);
ret_t value_bit_and(value_t* v, value_t* other, value_t* result);
/**
* @method value_bit_nor
* @method value_bit_xor
* v和other按位取异或result对象
* @param {value_t*} v value对象()
* @param {value_t*} other value对象()
@ -1045,7 +1045,7 @@ ret_t value_bit_and(value_t* v, value_t* other, value_t* result);
*
* @return {ret_t} RET_OK表示成功
*/
ret_t value_bit_nor(value_t* v, value_t* other, value_t* result);
ret_t value_bit_xor(value_t* v, value_t* other, value_t* result);
END_C_DECLS

View File

@ -429,7 +429,7 @@ TEST(FScript, bit_and) {
TK_OBJECT_UNREF(obj);
}
TEST(FScript, bit_nor) {
TEST(FScript, bit_xor) {
value_t v;
tk_object_t* obj = object_default_create();

View File

@ -898,9 +898,16 @@ TEST(value, bits) {
ASSERT_EQ(value_bit_not(&v, &r), RET_OK);
ASSERT_EQ(value_int8(&r), ~value_int8(&v));
value_set_bool(&v, TRUE);
ASSERT_EQ(value_bit_not(&v, &r), RET_OK);
ASSERT_EQ(value_bool(&r), FALSE);
value_set_bool(&v, FALSE);
ASSERT_EQ(value_bit_not(&v, &r), RET_OK);
ASSERT_EQ(value_bool(&r), TRUE);
}
TEST(value, bit_and_or_nor) {
TEST(value, bit_and_or_xor) {
value_t v1;
value_t v2;
value_t r;
@ -915,7 +922,7 @@ TEST(value, bit_and_or_nor) {
ASSERT_EQ(r.type, VALUE_TYPE_INT8);
ASSERT_EQ(value_int8(&r), (int8_t)0xff);
ASSERT_EQ(value_bit_nor(&v1, &v2, &r), RET_OK);
ASSERT_EQ(value_bit_xor(&v1, &v2, &r), RET_OK);
ASSERT_EQ(r.type, VALUE_TYPE_INT8);
ASSERT_EQ(value_int8(&r), (int8_t)0xff);
@ -929,7 +936,7 @@ TEST(value, bit_and_or_nor) {
ASSERT_EQ(r.type, VALUE_TYPE_UINT8);
ASSERT_EQ(value_uint8(&r), (uint8_t)0xff);
ASSERT_EQ(value_bit_nor(&v1, &v2, &r), RET_OK);
ASSERT_EQ(value_bit_xor(&v1, &v2, &r), RET_OK);
ASSERT_EQ(r.type, VALUE_TYPE_UINT8);
ASSERT_EQ(value_uint8(&r), (uint8_t)0xff);
@ -944,7 +951,7 @@ TEST(value, bit_and_or_nor) {
ASSERT_EQ(r.type, VALUE_TYPE_INT16);
ASSERT_EQ(value_int16(&r), (int16_t)0xff);
ASSERT_EQ(value_bit_nor(&v1, &v2, &r), RET_OK);
ASSERT_EQ(value_bit_xor(&v1, &v2, &r), RET_OK);
ASSERT_EQ(r.type, VALUE_TYPE_INT16);
ASSERT_EQ(value_int16(&r), (int16_t)0xff);
@ -958,7 +965,7 @@ TEST(value, bit_and_or_nor) {
ASSERT_EQ(r.type, VALUE_TYPE_UINT16);
ASSERT_EQ(value_uint16(&r), (uint16_t)0xff);
ASSERT_EQ(value_bit_nor(&v1, &v2, &r), RET_OK);
ASSERT_EQ(value_bit_xor(&v1, &v2, &r), RET_OK);
ASSERT_EQ(r.type, VALUE_TYPE_UINT16);
ASSERT_EQ(value_uint16(&r), (uint16_t)0xff);
@ -972,7 +979,7 @@ TEST(value, bit_and_or_nor) {
ASSERT_EQ(r.type, VALUE_TYPE_INT32);
ASSERT_EQ(value_int32(&r), (int32_t)0xff);
ASSERT_EQ(value_bit_nor(&v1, &v2, &r), RET_OK);
ASSERT_EQ(value_bit_xor(&v1, &v2, &r), RET_OK);
ASSERT_EQ(r.type, VALUE_TYPE_INT32);
ASSERT_EQ(value_int32(&r), (int32_t)0xff);
@ -986,7 +993,7 @@ TEST(value, bit_and_or_nor) {
ASSERT_EQ(r.type, VALUE_TYPE_UINT32);
ASSERT_EQ(value_uint32(&r), (uint32_t)0xff);
ASSERT_EQ(value_bit_nor(&v1, &v2, &r), RET_OK);
ASSERT_EQ(value_bit_xor(&v1, &v2, &r), RET_OK);
ASSERT_EQ(r.type, VALUE_TYPE_UINT32);
ASSERT_EQ(value_uint32(&r), (uint32_t)0xff);
@ -1000,7 +1007,7 @@ TEST(value, bit_and_or_nor) {
ASSERT_EQ(r.type, VALUE_TYPE_INT64);
ASSERT_EQ(value_int64(&r), (int64_t)0xff);
ASSERT_EQ(value_bit_nor(&v1, &v2, &r), RET_OK);
ASSERT_EQ(value_bit_xor(&v1, &v2, &r), RET_OK);
ASSERT_EQ(r.type, VALUE_TYPE_INT64);
ASSERT_EQ(value_int64(&r), (int64_t)0xff);
@ -1014,7 +1021,7 @@ TEST(value, bit_and_or_nor) {
ASSERT_EQ(r.type, VALUE_TYPE_UINT64);
ASSERT_EQ(value_uint64(&r), (uint64_t)0xff);
ASSERT_EQ(value_bit_nor(&v1, &v2, &r), RET_OK);
ASSERT_EQ(value_bit_xor(&v1, &v2, &r), RET_OK);
ASSERT_EQ(r.type, VALUE_TYPE_UINT64);
ASSERT_EQ(value_uint64(&r), (uint64_t)0xff);
@ -1029,5 +1036,53 @@ TEST(value, bit_and_or_nor) {
ASSERT_EQ(value_bit_and(&v1, &v2, &r), RET_OK);
ASSERT_EQ(r.type, VALUE_TYPE_UINT32);
ASSERT_EQ(value_uint64(&r), 0);
value_set_bool(&v1, TRUE);
value_set_bool(&v2, TRUE);
ASSERT_EQ(value_bit_and(&v1, &v2, &r), RET_OK);
ASSERT_EQ(r.type, VALUE_TYPE_BOOL);
ASSERT_EQ(value_bool(&r), TRUE);
value_set_bool(&v1, TRUE);
value_set_bool(&v2, TRUE);
ASSERT_EQ(value_bit_or(&v1, &v2, &r), RET_OK);
ASSERT_EQ(r.type, VALUE_TYPE_BOOL);
ASSERT_EQ(value_bool(&r), TRUE);
value_set_bool(&v1, TRUE);
value_set_bool(&v2, TRUE);
ASSERT_EQ(value_bit_xor(&v1, &v2, &r), RET_OK);
ASSERT_EQ(r.type, VALUE_TYPE_BOOL);
ASSERT_EQ(value_bool(&r), FALSE);
value_set_bool(&v1, TRUE);
value_set_bool(&v2, FALSE);
ASSERT_EQ(value_bit_and(&v1, &v2, &r), RET_OK);
ASSERT_EQ(r.type, VALUE_TYPE_BOOL);
ASSERT_EQ(value_bool(&r), FALSE);
value_set_bool(&v1, TRUE);
value_set_bool(&v2, FALSE);
ASSERT_EQ(value_bit_or(&v1, &v2, &r), RET_OK);
ASSERT_EQ(r.type, VALUE_TYPE_BOOL);
ASSERT_EQ(value_bool(&r), TRUE);
value_set_bool(&v1, TRUE);
value_set_bool(&v2, FALSE);
ASSERT_EQ(value_bit_xor(&v1, &v2, &r), RET_OK);
ASSERT_EQ(r.type, VALUE_TYPE_BOOL);
ASSERT_EQ(value_bool(&r), TRUE);
value_set_bool(&v1, FALSE);
value_set_bool(&v2, FALSE);
ASSERT_EQ(value_bit_or(&v1, &v2, &r), RET_OK);
ASSERT_EQ(r.type, VALUE_TYPE_BOOL);
ASSERT_EQ(value_bool(&r), FALSE);
value_set_bool(&v1, FALSE);
value_set_bool(&v2, FALSE);
ASSERT_EQ(value_bit_xor(&v1, &v2, &r), RET_OK);
ASSERT_EQ(r.type, VALUE_TYPE_BOOL);
ASSERT_EQ(value_bool(&r), FALSE);
}