From b0930584ad6c1f8590c306bc8d92f923b4da8f2c Mon Sep 17 00:00:00 2001 From: arphaman Date: Wed, 4 Sep 2013 12:47:44 +0100 Subject: added integer power functions --- CMakeLists.txt | 10 ++++++++++ include/Numerical/Integer.h | 21 +++++++++++++++++++++ lib/Numerical/CMakeLists.txt | 3 ++- lib/Numerical/Integer.cpp | 29 +++++++++++++++++++++++++++++ test/CMakeLists.txt | 1 + test/Unit/CMakeLists.txt | 1 + test/Unit/ipow.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 include/Numerical/Integer.h create mode 100644 lib/Numerical/Integer.cpp create mode 100644 test/CMakeLists.txt create mode 100644 test/Unit/CMakeLists.txt create mode 100644 test/Unit/ipow.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 3f59e89..29eee3e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,11 +38,21 @@ endif() add_library(libflang ${libkind} lib/Libflang.cpp lib/Core/Core.cpp lib/Numerical/Complex.cpp + lib/Numerical/Integer.cpp lib/Strings/Character.cpp lib/IO/Write.cpp lib/System/System.cpp ) +# Tests + +macro(add_libflang_test name) + add_executable( ${name} ${ARGN} ) + target_link_libraries( ${name} libflang ) +endmacro(add_libflang_test) + +add_subdirectory(test) + set(BUG_REPORT_URL "http://llvm.org/bugs/" CACHE STRING "Default URL where bug reports are to be submitted.") diff --git a/include/Numerical/Integer.h b/include/Numerical/Integer.h new file mode 100644 index 0000000..e41b4f6 --- /dev/null +++ b/include/Numerical/Integer.h @@ -0,0 +1,21 @@ +//===--- Integer.h - Integer operations library -----------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBFLANG_NUMERICAL_INTEGER_H +#define LLVM_LIBFLANG_NUMERICAL_INTEGER_H + +#include "Libflang.h" +#include + +LIBFLANG_ABI int8_t libflang_pow_i1_i1(int8_t x, int8_t y); +LIBFLANG_ABI int16_t libflang_pow_i2_i2(int16_t x, int16_t y); +LIBFLANG_ABI int32_t libflang_pow_i4_i4(int32_t x, int32_t y); +LIBFLANG_ABI int64_t libflang_pow_i8_i8(int64_t x, int64_t y); + +#endif diff --git a/lib/Numerical/CMakeLists.txt b/lib/Numerical/CMakeLists.txt index 780074a..5311879 100644 --- a/lib/Numerical/CMakeLists.txt +++ b/lib/Numerical/CMakeLists.txt @@ -1,2 +1,3 @@ add_libflang_library(libflangNumerical - Complex.cpp) + Complex.cpp + Integer.cpp) diff --git a/lib/Numerical/Integer.cpp b/lib/Numerical/Integer.cpp new file mode 100644 index 0000000..fd3060e --- /dev/null +++ b/lib/Numerical/Integer.cpp @@ -0,0 +1,29 @@ +#include "Numerical/Integer.h" + +template +static T ipow(T x, T y) { + if(y >= 0) { + T result = 1; + for(; y != 0; --y) + result *= x; + return result; + } + return 0; +} + +LIBFLANG_ABI int8_t libflang_pow_i1_i1(int8_t x, int8_t y) { + return ipow(x, y); +} + +LIBFLANG_ABI int16_t libflang_pow_i2_i2(int16_t x, int16_t y) { + return ipow(x, y); +} + +LIBFLANG_ABI int32_t libflang_pow_i4_i4(int32_t x, int32_t y) { + return ipow(x, y); +} + +LIBFLANG_ABI int64_t libflang_pow_i8_i8(int64_t x, int64_t y) { + return ipow(x, y); +} + diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..57e285f --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(Unit) diff --git a/test/Unit/CMakeLists.txt b/test/Unit/CMakeLists.txt new file mode 100644 index 0000000..a5b4a2c --- /dev/null +++ b/test/Unit/CMakeLists.txt @@ -0,0 +1 @@ +add_libflang_test(ipow ipow.cpp) diff --git a/test/Unit/ipow.cpp b/test/Unit/ipow.cpp new file mode 100644 index 0000000..0956bb2 --- /dev/null +++ b/test/Unit/ipow.cpp @@ -0,0 +1,38 @@ +//===-- ipow.cpp - Test libflang_pow_i*_i* --------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include +#include +#include "Numerical/Integer.h" + +bool testPowI4(int32_t x, int32_t y, int32_t expected) { + x = libflang_pow_i4_i4(x, y); + if(x != expected) + std::cout << "Error in libflang_pow_i4_i4 - expected " << expected + << ", got " << x << std::endl; + return x != expected; +} + +int main() { + if(testPowI4(2,2,4)) + return 1; + if(testPowI4(3,3,27)) + return 1; + if(testPowI4(4,1,4)) + return 1; + if(testPowI4(5,0,1)) + return 1; + if(testPowI4(6,-1,0)) + return 1; + if(testPowI4(-1,2,1)) + return 1; + if(testPowI4(-1,-1,0)) + return 1; + return 0; +} -- cgit v1.2.1