Summary

Topics: Normal arithmetic - Wrapping arithmetic - Saturating arithmetic and Checked arithmetic

Once again, interesting things happened only on issue/e8113638/math-on-bits and nothing was pushed to devel. Also once again, these "interesting things" only concern arithmetic.

Breadth and depth of the topic surprised me. There are at least three kinds of arithmetic: the "normal" one known from school where 8 + 8 equals 16, the "wrapping" one where 8 + 8 equals 6, and the "saturating" one where 8 + 8 equals 9.

There is also a fourth kind of arithmetic which is fixed-width but instead of wrapping or saturating it throws exceptions when results go out of range - the "checked" one.


Normal arithmetic

Expected behaviour (assuming range is 0 to 9): 8 + 8 = 16.

Viua will provide a family of arithmetic instructions with "expand" prefix implementing this behaviour, e.g. expandadd and expandmul.

Arithmetic that provides unsurprising, mathematically satisfying results. Bit width of the numbers is expanded as needed and the programmer does not need to concern themselves with overflows.

This arithmetic would be most useful when implementing software dealing with "casual math"; calculations requiring the same results that would be produced if you computed them yourself using pen and paper (assuming you made no mistake).

What is more, all other type of arithmetic can be implemented in terms of this arithmetic using bitslength instruction (which fetches length of a bit string), and a combination of ifs, throws, division, etc. Due to this fact, this kind of arithmetic will be implemented and made available in a development release first, and then additional kinds of arithmetic will be added to the VM.

Wrapping arithmetic

Expected behaviour (assuming range is 0 to 9): 8 + 8 = 6.

Viua will provide a family of arithmetic instructions with "wrap" prefix implementing this behaviour, e.g. wrapadd and wrapmul.

The arithmetic we all know and love (or hate). This is the behaviour (oversimplification warning!) you get when you program in C++. Sometimes it leads to bugs, but I think that it will not if the behaviour is explicitly requested.

Saturating arithmetic

Expected behaviour (assuming range is 0 to 9): 8 + 8 = 9.

Viua will provide a family of arithmetic instructions with "saturating" prefix implementing this behaviour, e.g. saturatingadd and saturatingmul.

This kind of arithmetic is used (from what I read) for example in audio processing, to avoid accidentally overflowing values and having the sound distorted. It should be generally useful when working with values that are "capped" and should not exceed a specified range.

Checked arithmetic

Expected behaviour (assuming range is 0 to 9): 8 + 8 throws an overflow exception.

Viua will provide a family of arithmetic instructions with "checked" prefix implementing this behaviour, e.g. checkedadd and checkedmul.

This kind of arithmetic is (is it?) used to keep value in a specified range, and warn the programmer when the value somehow goes out-or-range. Prevents bugs.