Basic Arithmetic

See src/vm_core/utility/arithmetic.ts

Since the JavaScript only have number type, which is an abstraction of float/int. We use functions in arithmetic.ts to perform basic arithmetic operations like +, -, * or /.

export function c_add(x: DataView, y: DataView): DataView;	// x + y
export function c_sub(x: DataView, y: DataView): DataView;	// x - y
export function c_mul(x: DataView, y: DataView, Issue_Handler: MessageEmitter): DataView;	// x * y
export function c_div(x: DataView, y: DataView): DataView;	// x / y
export function c_rem(x: DataView, y: DataView): DataView;  // x % y
export function c_lsh(x: DataView, y: DataView): DataView;	// x << y
export function c_rsh(x: DataView, y: DataView): DataView;	// x >> y

export function c_and(x: DataView, y: DataView): DataView; 	// x & y
export function c_or(x: DataView, y: DataView): DataView;   // x | y
export function c_xor(x: DataView, y: DataView): DataView;  // x ^ y

These functions will try its best to mimic the overflow behavior of C program.

What is Issue_Handler in c_mul's header'?

These functions are not perfect. A specific example is shown below:

cmul(2147483647, 25165823) = 2122317824 (JS)
2147483647 * 25165823 = 2122317825 (C)

This is due to the precision lost problem of float. When such precision lost is detected, C0VM.ts will NOT interrupt the execution. However, we do want the user to realize such imprecise calculation. Therefore, we will use IssueHandler to emit a warning to user. The IssueHandler is an object that follows the MessageEmitter interface.

By passing in different implementation of MessageEmitter, we may have one of the following behaviors: 1) log a warning in the console, 2) popup a warning in the GUI, etc.

Last updated