empirical-soft / empirical-lang

A language for time-series analysis
https://www.empirical-soft.com/
Other
168 stars 13 forks source link

Integer overflow detection #107

Open chrisaycock opened 3 years ago

chrisaycock commented 3 years ago

Suggested by @llllllllll, use the compiler-optimized routines for safe integer arithmetic, setting the failed result to nil. For example:

x = ...;
y = ...;
if (__builtin_add_overflow(x, y, &z)) {
  z = nil_value;
}

This would require a significant change to the interpreter's math routines. The current set-up is portable, generic, and fast. We would need this to operate across Clang, GCC, and Visual Studio.

There could be a "debug" mode for Empirical in the future, which would abort when overflow occurs rather than return a nil.