From 4c8b661bdc89f0b3b432f8c14fe13bc35459cf53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Sved=C3=A4ng?= Date: Tue, 6 Mar 2018 16:44:07 +0100 Subject: [PATCH] Moved safe int functions into their own file that's not automatically loaded. --- core/Int.carp | 7 +------ core/SafeInt.carp | 7 +++++++ core/carp_int.h | 6 ------ core/carp_safe_int.h | 6 ++++++ test/safe_artihmetic.carp | 1 + 5 files changed, 15 insertions(+), 12 deletions(-) create mode 100644 core/SafeInt.carp create mode 100644 core/carp_safe_int.h diff --git a/core/Int.carp b/core/Int.carp index e8561b1a..7e592a8c 100644 --- a/core/Int.carp +++ b/core/Int.carp @@ -18,12 +18,7 @@ (register bit-not (λ [Int] Int)) (register inc (λ [Int] Int)) (register dec (λ [Int] Int)) - (register copy (λ [&Int] Int)) ;; TODO: Should not be needed when refs to value types are auto-converted to non-refs. - - (not-on-windows - (register safe-add (λ [Int Int (Ref Int)] Bool)) - (register safe-sub (λ [Int Int (Ref Int)] Bool)) - (register safe-mul (λ [Int Int (Ref Int)] Bool))) + (register copy (λ [&Int] Int)) (register abs (λ [Int] Int)) diff --git a/core/SafeInt.carp b/core/SafeInt.carp new file mode 100644 index 00000000..dbda6bc5 --- /dev/null +++ b/core/SafeInt.carp @@ -0,0 +1,7 @@ +(system-include "carp_safe_int.h") + +(defmodule Int + (register safe-add (λ [Int Int (Ref Int)] Bool)) + (register safe-sub (λ [Int Int (Ref Int)] Bool)) + (register safe-mul (λ [Int Int (Ref Int)] Bool)) + ) diff --git a/core/carp_int.h b/core/carp_int.h index c2990af5..dc3b0f97 100644 --- a/core/carp_int.h +++ b/core/carp_int.h @@ -5,11 +5,6 @@ int Int__PLUS_(int x, int y) { return x + y; } int Int__MINUS_(int x, int y) { return x - y; } int Int__MUL_(int x, int y) { return x * y; } int Int__DIV_(int x, int y) { return x / y; } -#ifndef _WIN32 -bool Int_safe_MINUS_add(int x, int y, int* res) { return __builtin_sadd_overflow(x, y, res); } -bool Int_safe_MINUS_sub(int x, int y, int* res) { return __builtin_ssub_overflow(x, y, res); } -bool Int_safe_MINUS_mul(int x, int y, int* res) { return __builtin_smul_overflow(x, y, res); } -#endif bool Int__EQ_(int x, int y) { return x == y; } bool Int__DIV__EQ_(int x, int y) { return x != y; } bool Int__LT_(int x, int y) { return x < y; } @@ -34,4 +29,3 @@ int Int_mod(int x, int divider) { bool Int_mask(int a, int b) { return a & b; } - diff --git a/core/carp_safe_int.h b/core/carp_safe_int.h new file mode 100644 index 00000000..d7337917 --- /dev/null +++ b/core/carp_safe_int.h @@ -0,0 +1,6 @@ +#include +#include + +bool Int_safe_MINUS_add(int x, int y, int* res) { return __builtin_sadd_overflow(x, y, res); } +bool Int_safe_MINUS_sub(int x, int y, int* res) { return __builtin_ssub_overflow(x, y, res); } +bool Int_safe_MINUS_mul(int x, int y, int* res) { return __builtin_smul_overflow(x, y, res); } diff --git a/test/safe_artihmetic.carp b/test/safe_artihmetic.carp index 921f8b0b..0254a531 100644 --- a/test/safe_artihmetic.carp +++ b/test/safe_artihmetic.carp @@ -1,4 +1,5 @@ (load "Test.carp") +(load "SafeInt.carp") (use-all Bool Int Long Test)