summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorarphaman <arphaman@gmail.com>2013-09-04 12:47:44 +0100
committerarphaman <arphaman@gmail.com>2013-09-04 12:47:44 +0100
commitb0930584ad6c1f8590c306bc8d92f923b4da8f2c (patch)
tree6d4663d95e2b5c1c7ac04fbaf5c4885169e9b399
parent1e6e1abbcdd3fada164c5f3fa8b6bbabacce4766 (diff)
downloadlibflangrt-b0930584ad6c1f8590c306bc8d92f923b4da8f2c.tar.gz
added integer power functions
-rw-r--r--CMakeLists.txt10
-rw-r--r--include/Numerical/Integer.h21
-rw-r--r--lib/Numerical/CMakeLists.txt3
-rw-r--r--lib/Numerical/Integer.cpp29
-rw-r--r--test/CMakeLists.txt1
-rw-r--r--test/Unit/CMakeLists.txt1
-rw-r--r--test/Unit/ipow.cpp38
7 files changed, 102 insertions, 1 deletions
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 <stdint.h>
+
+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<typename T>
+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 <assert.h>
+#include <iostream>
+#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;
+}