Fixed the code style in *.c files to two spaces indentation and add a linter named funstx to Fun. (0.39.0)
This commit is contained in:
parent
33001a7ad2
commit
03b2532474
237 changed files with 17550 additions and 13911 deletions
|
|
@ -10,38 +10,39 @@
|
|||
*/
|
||||
|
||||
/**
|
||||
* @file isqrt.c
|
||||
* @file isqrt.c
|
||||
* @brief Implements the OP_ISQRT opcode for integer square root (floor).
|
||||
*/
|
||||
|
||||
case OP_ISQRT: {
|
||||
Value v = pop_value(vm);
|
||||
if (!((v.type == VAL_INT) || (v.type == VAL_FLOAT))) {
|
||||
fprintf(stderr, "Runtime type error: ISQRT expects number, got %s\n", value_type_name(v.type));
|
||||
exit(1);
|
||||
}
|
||||
int64_t a = (v.type == VAL_INT) ? v.i : (int64_t)v.d;
|
||||
if (a <= 0) {
|
||||
free_value(v);
|
||||
push_value(vm, make_int(0));
|
||||
break;
|
||||
}
|
||||
/* Binary isqrt inline */
|
||||
uint64_t n = (uint64_t)a;
|
||||
uint64_t x = 0;
|
||||
uint64_t bit = (uint64_t)1 << 62; /* highest even bit set */
|
||||
while (bit > n) bit >>= 2;
|
||||
while (bit != 0) {
|
||||
if (n >= x + bit) {
|
||||
n -= x + bit;
|
||||
x = (x >> 1) + bit;
|
||||
} else {
|
||||
x >>= 1;
|
||||
}
|
||||
bit >>= 2;
|
||||
}
|
||||
int64_t r = (int64_t)x;
|
||||
Value v = pop_value(vm);
|
||||
if (!((v.type == VAL_INT) || (v.type == VAL_FLOAT))) {
|
||||
fprintf(stderr, "Runtime type error: ISQRT expects number, got %s\n", value_type_name(v.type));
|
||||
exit(1);
|
||||
}
|
||||
int64_t a = (v.type == VAL_INT) ? v.i : (int64_t)v.d;
|
||||
if (a <= 0) {
|
||||
free_value(v);
|
||||
push_value(vm, make_int(r));
|
||||
push_value(vm, make_int(0));
|
||||
break;
|
||||
}
|
||||
/* Binary isqrt inline */
|
||||
uint64_t n = (uint64_t)a;
|
||||
uint64_t x = 0;
|
||||
uint64_t bit = (uint64_t)1 << 62; /* highest even bit set */
|
||||
while (bit > n)
|
||||
bit >>= 2;
|
||||
while (bit != 0) {
|
||||
if (n >= x + bit) {
|
||||
n -= x + bit;
|
||||
x = (x >> 1) + bit;
|
||||
} else {
|
||||
x >>= 1;
|
||||
}
|
||||
bit >>= 2;
|
||||
}
|
||||
int64_t r = (int64_t)x;
|
||||
free_value(v);
|
||||
push_value(vm, make_int(r));
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue