1
0
Fork 0
mirror of https://github.com/ganelson/inform.git synced 2024-07-08 10:04:21 +03:00
inform7/inter/final-module/Chapter 5/C Arithmetic.w

377 lines
12 KiB
OpenEdge ABL
Raw Normal View History

2021-08-12 21:07:06 +03:00
[CArithmetic::] C Arithmetic.
Integer and floating-point calculations translated to C.
@ Integer arithmetic is handled by the standard operators in C, so this is very
2021-10-31 12:38:25 +02:00
easy, except that we want to fail gracefully in the case of division by zero:
so if we're compiling a function, we do this with the functions implementing the
opcodes |@div_r| and |@mod_r| instead.
2021-08-12 21:07:06 +03:00
=
2021-09-25 20:21:49 +03:00
int CArithmetic::invoke_primitive(code_generation *gen, inter_ti bip, inter_tree_node *P) {
2021-08-12 21:07:06 +03:00
text_stream *OUT = CodeGen::current(gen);
switch (bip) {
2021-10-31 12:38:25 +02:00
case BITWISEAND_BIP:
WRITE("(("); VNODE_1C; WRITE(")&("); VNODE_2C; WRITE("))"); break;
case BITWISEOR_BIP:
WRITE("(("); VNODE_1C; WRITE(")|("); VNODE_2C; WRITE("))"); break;
case BITWISENOT_BIP:
WRITE("(~("); VNODE_1C; WRITE("))"); break;
case PLUS_BIP:
WRITE("("); VNODE_1C; WRITE(" + "); VNODE_2C; WRITE(")"); break;
case MINUS_BIP:
WRITE("("); VNODE_1C; WRITE(" - "); VNODE_2C; WRITE(")"); break;
case UNARYMINUS_BIP:
WRITE("(-("); VNODE_1C; WRITE("))"); break;
case TIMES_BIP:
WRITE("("); VNODE_1C; WRITE(" * "); VNODE_2C; WRITE(")"); break;
case DIVIDE_BIP:
if (CFunctionModel::inside_function(gen)) {
WRITE("i7_div(proc, "); VNODE_1C; WRITE(", "); VNODE_2C; WRITE(")");
} else {
WRITE("("); VNODE_1C; WRITE(" / "); VNODE_2C; WRITE(")"); break;
}
break;
case MODULO_BIP:
if (CFunctionModel::inside_function(gen)) {
WRITE("i7_mod(proc, "); VNODE_1C; WRITE(", "); VNODE_2C; WRITE(")");
} else {
WRITE("("); VNODE_1C; WRITE(" %% "); VNODE_2C; WRITE(")");
}
break;
case SEQUENTIAL_BIP:
WRITE("("); VNODE_1C; WRITE(","); VNODE_2C; WRITE(")"); break;
case TERNARYSEQUENTIAL_BIP:
WRITE("("); VNODE_1C; WRITE(", "); VNODE_2C; WRITE(", ");
VNODE_3C; WRITE(")"); break;
case RANDOM_BIP:
2021-11-07 01:30:31 +02:00
WRITE("i7_random(proc, "); VNODE_1C; WRITE(")"); break;
2021-10-31 12:38:25 +02:00
default: return NOT_APPLICABLE;
2021-08-12 21:07:06 +03:00
}
return FALSE;
}
2021-10-31 12:38:25 +02:00
@h add, sub, neg, mul, div, mod.
Also the functions |i7_div| and |i7_mod|, which are just wrappers for their
opcodes but return values rather than copying to pointers.
2021-08-13 01:31:23 +03:00
2021-10-31 12:38:25 +02:00
The implementations of |@div| and |@mod| are borrowed from the glulxe
reference code, to be sure that we have the right sign conventions.
2021-08-12 21:07:06 +03:00
= (text to inform7_clib.h)
2021-10-30 15:04:16 +03:00
void i7_opcode_add(i7process_t *proc, i7word_t x, i7word_t y, i7word_t *z);
void i7_opcode_sub(i7process_t *proc, i7word_t x, i7word_t y, i7word_t *z);
void i7_opcode_neg(i7process_t *proc, i7word_t x, i7word_t *y);
void i7_opcode_mul(i7process_t *proc, i7word_t x, i7word_t y, i7word_t *z);
void i7_opcode_div(i7process_t *proc, i7word_t x, i7word_t y, i7word_t *z);
void i7_opcode_mod(i7process_t *proc, i7word_t x, i7word_t y, i7word_t *z);
2021-10-31 12:38:25 +02:00
i7word_t i7_div(i7process_t *proc, i7word_t x, i7word_t y);
i7word_t i7_mod(i7process_t *proc, i7word_t x, i7word_t y);
2021-09-07 01:12:33 +03:00
=
= (text to inform7_clib.c)
2021-10-30 15:04:16 +03:00
void i7_opcode_add(i7process_t *proc, i7word_t x, i7word_t y, i7word_t *z) {
2021-08-16 01:41:42 +03:00
if (z) *z = x + y;
2021-08-12 21:07:06 +03:00
}
2021-10-30 15:04:16 +03:00
void i7_opcode_sub(i7process_t *proc, i7word_t x, i7word_t y, i7word_t *z) {
2021-08-16 01:41:42 +03:00
if (z) *z = x - y;
2021-08-12 21:07:06 +03:00
}
2021-10-30 15:04:16 +03:00
void i7_opcode_neg(i7process_t *proc, i7word_t x, i7word_t *y) {
2021-08-16 01:41:42 +03:00
if (y) *y = -x;
2021-08-12 21:07:06 +03:00
}
2021-10-30 15:04:16 +03:00
void i7_opcode_mul(i7process_t *proc, i7word_t x, i7word_t y, i7word_t *z) {
2021-08-16 01:41:42 +03:00
if (z) *z = x * y;
2021-08-12 21:07:06 +03:00
}
2021-10-30 15:04:16 +03:00
void i7_opcode_div(i7process_t *proc, i7word_t x, i7word_t y, i7word_t *z) {
if (y == 0) { printf("Division of %d by 0\n", x); i7_fatal_exit(proc); return; }
2021-08-16 01:41:42 +03:00
int result, ax, ay;
if (x < 0) {
ax = (-x);
if (y < 0) {
ay = (-y);
result = ax / ay;
} else {
ay = y;
result = -(ax / ay);
}
} else {
ax = x;
if (y < 0) {
ay = (-y);
result = -(ax / ay);
} else {
ay = y;
result = ax / ay;
}
}
if (z) *z = result;
}
2021-10-30 15:04:16 +03:00
void i7_opcode_mod(i7process_t *proc, i7word_t x, i7word_t y, i7word_t *z) {
if (y == 0) { printf("Division of %d by 0\n", x); i7_fatal_exit(proc); return; }
2021-10-31 12:38:25 +02:00
int result, ax, ay = (y < 0)?(-y):y;
2021-08-16 01:41:42 +03:00
if (x < 0) {
ax = (-x);
result = -(ax % ay);
} else {
ax = x;
result = ax % ay;
}
if (z) *z = result;
}
2021-10-31 12:38:25 +02:00
i7word_t i7_div(i7process_t *proc, i7word_t x, i7word_t y) {
2021-09-21 01:26:22 +03:00
i7word_t z;
2021-10-31 12:38:25 +02:00
i7_opcode_div(proc, x, y, &z);
2021-08-16 01:41:42 +03:00
return z;
2021-08-12 21:07:06 +03:00
}
2021-10-31 12:38:25 +02:00
i7word_t i7_mod(i7process_t *proc, i7word_t x, i7word_t y) {
i7word_t z;
i7_opcode_mod(proc, x, y, &z);
return z;
2021-08-16 01:41:42 +03:00
}
2021-10-31 12:38:25 +02:00
=
2021-08-16 01:41:42 +03:00
2021-10-31 12:38:25 +02:00
@h fadd, fsub, fmul, fdiv, fmod.
The remaining opcodes are for floating-point arithmetic, and it all seems
straightforward, since we just want to use standard C |float| arithmetic
throughout, but the devil is in the details. The code below is heavily
indebted to Andrew Plotkin.
2021-08-16 01:41:42 +03:00
2021-10-31 12:38:25 +02:00
= (text to inform7_clib.h)
void i7_opcode_fadd(i7process_t *proc, i7word_t x, i7word_t y, i7word_t *z);
void i7_opcode_fsub(i7process_t *proc, i7word_t x, i7word_t y, i7word_t *z);
void i7_opcode_fmul(i7process_t *proc, i7word_t x, i7word_t y, i7word_t *z);
void i7_opcode_fdiv(i7process_t *proc, i7word_t x, i7word_t y, i7word_t *z);
void i7_opcode_fmod(i7process_t *proc, i7word_t x, i7word_t y, i7word_t *z, i7word_t *w);
=
2021-08-12 21:07:06 +03:00
2021-10-31 12:38:25 +02:00
= (text to inform7_clib.c)
2021-10-30 15:04:16 +03:00
void i7_opcode_fadd(i7process_t *proc, i7word_t x, i7word_t y, i7word_t *z) {
2021-10-31 12:38:25 +02:00
*z = i7_encode_float(i7_decode_float(x) + i7_decode_float(y));
2021-08-12 21:07:06 +03:00
}
2021-10-31 12:38:25 +02:00
void i7_opcode_fsub(i7process_t *proc, i7word_t x, i7word_t y, i7word_t *z) {
*z = i7_encode_float(i7_decode_float(x) - i7_decode_float(y));
2021-08-12 21:07:06 +03:00
}
2021-10-31 12:38:25 +02:00
void i7_opcode_fmul(i7process_t *proc, i7word_t x, i7word_t y, i7word_t *z) {
*z = i7_encode_float(i7_decode_float(x) * i7_decode_float(y));
}
void i7_opcode_fdiv(i7process_t *proc, i7word_t x, i7word_t y, i7word_t *z) {
*z = i7_encode_float(i7_decode_float(x) / i7_decode_float(y));
2021-08-16 01:41:42 +03:00
}
2021-10-30 15:04:16 +03:00
void i7_opcode_fmod(i7process_t *proc, i7word_t x, i7word_t y, i7word_t *z, i7word_t *w) {
2021-10-31 12:38:25 +02:00
float fx = i7_decode_float(x), fy = i7_decode_float(y);
2021-08-16 01:41:42 +03:00
float fquot = fmodf(fx, fy);
2021-10-31 12:38:25 +02:00
i7word_t quot = i7_encode_float(fquot);
i7word_t rem = i7_encode_float((fx-fquot) / fy);
2021-08-16 01:41:42 +03:00
if (rem == 0x0 || rem == 0x80000000) {
/* When the quotient is zero, the sign has been lost in the
2021-10-31 12:38:25 +02:00
shuffle. We'll set that by hand, based on the original arguments. */
2021-08-16 01:41:42 +03:00
rem = (x ^ y) & 0x80000000;
}
if (z) *z = quot;
if (w) *w = rem;
}
2021-10-31 12:38:25 +02:00
@h floor, ceil, ftonumn, ftonumz, numtof.
All of which are conversions between integer and floating-point values.
2021-08-16 01:41:42 +03:00
2021-10-31 12:38:25 +02:00
= (text to inform7_clib.h)
void i7_opcode_floor(i7process_t *proc, i7word_t x, i7word_t *y);
void i7_opcode_ceil(i7process_t *proc, i7word_t x, i7word_t *y);
void i7_opcode_ftonumn(i7process_t *proc, i7word_t x, i7word_t *y);
void i7_opcode_ftonumz(i7process_t *proc, i7word_t x, i7word_t *y);
void i7_opcode_numtof(i7process_t *proc, i7word_t x, i7word_t *y);
=
= (text to inform7_clib.c)
void i7_opcode_floor(i7process_t *proc, i7word_t x, i7word_t *y) {
*y = i7_encode_float(floorf(i7_decode_float(x)));
}
void i7_opcode_ceil(i7process_t *proc, i7word_t x, i7word_t *y) {
*y = i7_encode_float(ceilf(i7_decode_float(x)));
2021-08-16 01:41:42 +03:00
}
2021-10-30 15:04:16 +03:00
void i7_opcode_ftonumn(i7process_t *proc, i7word_t x, i7word_t *y) {
2021-10-31 12:38:25 +02:00
float fx = i7_decode_float(x);
2021-09-21 01:26:22 +03:00
i7word_t result;
2021-08-16 01:41:42 +03:00
if (!signbit(fx)) {
if (isnan(fx) || isinf(fx) || (fx > 2147483647.0))
result = 0x7FFFFFFF;
else
2021-09-21 01:26:22 +03:00
result = (i7word_t) (roundf(fx));
2021-08-16 01:41:42 +03:00
}
else {
if (isnan(fx) || isinf(fx) || (fx < -2147483647.0))
result = 0x80000000;
else
2021-09-21 01:26:22 +03:00
result = (i7word_t) (roundf(fx));
2021-08-16 01:41:42 +03:00
}
*y = result;
}
2021-10-30 15:04:16 +03:00
void i7_opcode_ftonumz(i7process_t *proc, i7word_t x, i7word_t *y) {
2021-10-31 12:38:25 +02:00
float fx = i7_decode_float(x);
2021-09-21 01:26:22 +03:00
i7word_t result;
2021-08-16 01:41:42 +03:00
if (!signbit(fx)) {
if (isnan(fx) || isinf(fx) || (fx > 2147483647.0))
result = 0x7FFFFFFF;
else
2021-09-21 01:26:22 +03:00
result = (i7word_t) (truncf(fx));
2021-08-16 01:41:42 +03:00
}
else {
if (isnan(fx) || isinf(fx) || (fx < -2147483647.0))
result = 0x80000000;
else
2021-09-21 01:26:22 +03:00
result = (i7word_t) (truncf(fx));
2021-08-16 01:41:42 +03:00
}
*y = result;
}
2021-10-30 15:04:16 +03:00
void i7_opcode_numtof(i7process_t *proc, i7word_t x, i7word_t *y) {
2021-10-31 12:38:25 +02:00
*y = i7_encode_float((float) x);
}
=
@h exp, log, pow, sqrt.
= (text to inform7_clib.h)
void i7_opcode_exp(i7process_t *proc, i7word_t x, i7word_t *y);
void i7_opcode_log(i7process_t *proc, i7word_t x, i7word_t *y);
void i7_opcode_pow(i7process_t *proc, i7word_t x, i7word_t y, i7word_t *z);
void i7_opcode_sqrt(i7process_t *proc, i7word_t x, i7word_t *y);
=
= (text to inform7_clib.c)
void i7_opcode_exp(i7process_t *proc, i7word_t x, i7word_t *y) {
*y = i7_encode_float(expf(i7_decode_float(x)));
}
void i7_opcode_log(i7process_t *proc, i7word_t x, i7word_t *y) {
*y = i7_encode_float(logf(i7_decode_float(x)));
}
void i7_opcode_pow(i7process_t *proc, i7word_t x, i7word_t y, i7word_t *z) {
if (i7_decode_float(x) == 1.0f)
*z = i7_encode_float(1.0f);
else if ((i7_decode_float(y) == 0.0f) || (i7_decode_float(y) == -0.0f))
*z = i7_encode_float(1.0f);
else if ((i7_decode_float(x) == -1.0f) && isinf(i7_decode_float(y)))
*z = i7_encode_float(1.0f);
else
*z = i7_encode_float(powf(i7_decode_float(x), i7_decode_float(y)));
}
void i7_opcode_sqrt(i7process_t *proc, i7word_t x, i7word_t *y) {
*y = i7_encode_float(sqrtf(i7_decode_float(x)));
}
=
@h sin, cos, tan, asin, acos, atan.
= (text to inform7_clib.h)
void i7_opcode_sin(i7process_t *proc, i7word_t x, i7word_t *y);
void i7_opcode_cos(i7process_t *proc, i7word_t x, i7word_t *y);
void i7_opcode_tan(i7process_t *proc, i7word_t x, i7word_t *y);
void i7_opcode_asin(i7process_t *proc, i7word_t x, i7word_t *y);
void i7_opcode_acos(i7process_t *proc, i7word_t x, i7word_t *y);
void i7_opcode_atan(i7process_t *proc, i7word_t x, i7word_t *y);
=
= (text to inform7_clib.c)
void i7_opcode_sin(i7process_t *proc, i7word_t x, i7word_t *y) {
*y = i7_encode_float(sinf(i7_decode_float(x)));
}
void i7_opcode_cos(i7process_t *proc, i7word_t x, i7word_t *y) {
*y = i7_encode_float(cosf(i7_decode_float(x)));
}
void i7_opcode_tan(i7process_t *proc, i7word_t x, i7word_t *y) {
*y = i7_encode_float(tanf(i7_decode_float(x)));
2021-08-12 21:07:06 +03:00
}
2021-10-31 12:38:25 +02:00
void i7_opcode_asin(i7process_t *proc, i7word_t x, i7word_t *y) {
*y = i7_encode_float(asinf(i7_decode_float(x)));
}
void i7_opcode_acos(i7process_t *proc, i7word_t x, i7word_t *y) {
*y = i7_encode_float(acosf(i7_decode_float(x)));
}
void i7_opcode_atan(i7process_t *proc, i7word_t x, i7word_t *y) {
*y = i7_encode_float(atanf(i7_decode_float(x)));
}
=
@h jfeq. jfne, jfge, jflt, jisinf, jisnan.
These are branch instructions of the kind which spook anybody who's never
looked at how floating-point arithmetic is actually done. Once you stop
thinking of a |float| as a number and start thinking of it as a sort of fuzzy
uncertainty range all of this becomes more explicable, but still.
= (text to inform7_clib.h)
int i7_opcode_jfeq(i7process_t *proc, i7word_t x, i7word_t y, i7word_t z);
int i7_opcode_jfne(i7process_t *proc, i7word_t x, i7word_t y, i7word_t z);
int i7_opcode_jfge(i7process_t *proc, i7word_t x, i7word_t y);
int i7_opcode_jflt(i7process_t *proc, i7word_t x, i7word_t y);
int i7_opcode_jisinf(i7process_t *proc, i7word_t x);
int i7_opcode_jisnan(i7process_t *proc, i7word_t x);
=
= (text to inform7_clib.c)
2021-10-30 15:04:16 +03:00
int i7_opcode_jfeq(i7process_t *proc, i7word_t x, i7word_t y, i7word_t z) {
2021-08-16 01:41:42 +03:00
int result;
if ((z & 0x7F800000) == 0x7F800000 && (z & 0x007FFFFF) != 0) {
/* The delta is NaN, which can never match. */
result = 0;
} else if ((x == 0x7F800000 || x == 0xFF800000)
&& (y == 0x7F800000 || y == 0xFF800000)) {
/* Both are infinite. Opposite infinities are never equal,
even if the difference is infinite, so this is easy. */
result = (x == y);
} else {
2021-10-31 12:38:25 +02:00
float fx = i7_decode_float(y) - i7_decode_float(x);
float fy = fabs(i7_decode_float(z));
2021-08-16 01:41:42 +03:00
result = (fx <= fy && fx >= -fy);
}
2021-08-17 17:55:25 +03:00
if (result) return 1;
2021-08-12 21:07:06 +03:00
return 0;
}
2021-10-30 15:04:16 +03:00
int i7_opcode_jfne(i7process_t *proc, i7word_t x, i7word_t y, i7word_t z) {
2021-08-16 01:41:42 +03:00
int result;
if ((z & 0x7F800000) == 0x7F800000 && (z & 0x007FFFFF) != 0) {
/* The delta is NaN, which can never match. */
result = 0;
} else if ((x == 0x7F800000 || x == 0xFF800000)
&& (y == 0x7F800000 || y == 0xFF800000)) {
/* Both are infinite. Opposite infinities are never equal,
even if the difference is infinite, so this is easy. */
result = (x == y);
} else {
2021-10-31 12:38:25 +02:00
float fx = i7_decode_float(y) - i7_decode_float(x);
float fy = fabs(i7_decode_float(z));
2021-08-16 01:41:42 +03:00
result = (fx <= fy && fx >= -fy);
}
if (!result) return 1;
return 0;
2021-08-16 01:41:42 +03:00
}
2021-10-30 15:04:16 +03:00
int i7_opcode_jfge(i7process_t *proc, i7word_t x, i7word_t y) {
2021-10-31 12:38:25 +02:00
if (i7_decode_float(x) >= i7_decode_float(y)) return 1;
2021-08-12 21:07:06 +03:00
return 0;
}
2021-10-30 15:04:16 +03:00
int i7_opcode_jflt(i7process_t *proc, i7word_t x, i7word_t y) {
2021-10-31 12:38:25 +02:00
if (i7_decode_float(x) < i7_decode_float(y)) return 1;
2021-08-12 21:07:06 +03:00
return 0;
}
2021-10-30 15:04:16 +03:00
int i7_opcode_jisinf(i7process_t *proc, i7word_t x) {
2021-08-17 17:55:25 +03:00
if (x == 0x7F800000 || x == 0xFF800000) return 1;
2021-08-12 21:07:06 +03:00
return 0;
}
2021-10-30 15:04:16 +03:00
int i7_opcode_jisnan(i7process_t *proc, i7word_t x) {
2021-08-17 17:55:25 +03:00
if ((x & 0x7F800000) == 0x7F800000 && (x & 0x007FFFFF) != 0) return 1;
2021-08-12 21:07:06 +03:00
return 0;
}
=