From 78cd387e394747fcbfdb0ffac54a0b9a8c8bec37 Mon Sep 17 00:00:00 2001 From: arphaman Date: Fri, 12 Jul 2013 18:41:54 +0100 Subject: added complex number functions --- include/Numerical/Complex.h | 56 ++++++++++++++++++++++++++ include/Strings/Character.h | 2 +- lib/Numerical/CMakeLists.txt | 3 +- lib/Numerical/Complex.cpp | 93 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 include/Numerical/Complex.h create mode 100644 lib/Numerical/Complex.cpp diff --git a/include/Numerical/Complex.h b/include/Numerical/Complex.h new file mode 100644 index 0000000..2706bd2 --- /dev/null +++ b/include/Numerical/Complex.h @@ -0,0 +1,56 @@ +//===--- Complex.h - Complex numbers 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_COMPLEX_H +#define LLVM_LIBFLANG_NUMERICAL_COMPLEX_H + +#include "Libflang.h" + +struct Complex4 { + float Re, Im; +}; + +struct Complex8 { + double Re, Im; +}; + +LIBFLANG_ABI Complex8 libflang_cpow(double LHSRe, double LHSIm, + double RHSRe, double RHSIm); +LIBFLANG_ABI Complex4 libflang_cpowf(float LHSRe, float LHSIm, + float RHSRe, float RHSIm); + +LIBFLANG_ABI Complex8 libflang_cpowi(double Re, double Im, + int32_t power); +LIBFLANG_ABI Complex4 libflang_cpowif(float Re, float Im, + int32_t power); + +// intrinsics + +LIBFLANG_ABI double libflang_cabs(double Re, double Im); +LIBFLANG_ABI float libflang_cabsf(float Re, float Im); + +LIBFLANG_ABI Complex8 libflang_csqrt(double Re, double Im); +LIBFLANG_ABI Complex4 libflang_csqrtf(float Re, float Im); + +LIBFLANG_ABI Complex8 libflang_cexp(double Re, double Im); +LIBFLANG_ABI Complex4 libflang_cexpf(float Re, float Im); + +LIBFLANG_ABI Complex8 libflang_clog(double Re, double Im); +LIBFLANG_ABI Complex4 libflang_clogf(float Re, float Im); + +LIBFLANG_ABI Complex8 libflang_csin(double Re, double Im); +LIBFLANG_ABI Complex4 libflang_csinf(float Re, float Im); + +LIBFLANG_ABI Complex8 libflang_ccos(double Re, double Im); +LIBFLANG_ABI Complex4 libflang_ccosf(float Re, float Im); + +LIBFLANG_ABI Complex8 libflang_ctan(double Re, double Im); +LIBFLANG_ABI Complex4 libflang_ctanf(float Re, float Im); + +#endif diff --git a/include/Strings/Character.h b/include/Strings/Character.h index 7c67577..134d901 100644 --- a/include/Strings/Character.h +++ b/include/Strings/Character.h @@ -1,4 +1,4 @@ -//===--- Core.h - Core library --------------------------------*- C++ -*-===// +//===--- Character.h - Character library ------------------------*- C++ -*-===// // // The LLVM Compiler Infrastructure // diff --git a/lib/Numerical/CMakeLists.txt b/lib/Numerical/CMakeLists.txt index 8b13789..780074a 100644 --- a/lib/Numerical/CMakeLists.txt +++ b/lib/Numerical/CMakeLists.txt @@ -1 +1,2 @@ - +add_libflang_library(libflangNumerical + Complex.cpp) diff --git a/lib/Numerical/Complex.cpp b/lib/Numerical/Complex.cpp new file mode 100644 index 0000000..ccc9dc6 --- /dev/null +++ b/lib/Numerical/Complex.cpp @@ -0,0 +1,93 @@ +#include +#include "Numerical/Complex.h" + +using namespace std; + +static inline Complex8 cmplx(complex Value) { + Complex8 Result = { Value.real(), Value.imag() }; + return Result; +} + +static inline Complex4 cmplx(complex Value) { + Complex4 Result = { Value.real(), Value.imag() }; + return Result; +} + +static inline complex generic(double Re, double Im) { + return complex(Re, Im); +} + +static inline complex generic(float Re, float Im) { + return complex(Re, Im); +} + +LIBFLANG_ABI Complex8 libflang_cpow(double LHSRe, double LHSIm, + double RHSRe, double RHSIm) { + return cmplx(pow(generic(LHSRe, LHSIm), + generic(RHSRe, RHSIm))); +} +LIBFLANG_ABI Complex4 libflang_cpowf(float LHSRe, float LHSIm, + float RHSRe, float RHSIm) { + return cmplx(pow(generic(LHSRe, LHSIm), + generic(RHSRe, RHSIm))); +} + +LIBFLANG_ABI Complex8 libflang_cpowi(double Re, double Im, + int32_t power) { + return cmplx(pow(generic(Re, Im), power)); +} +LIBFLANG_ABI Complex4 libflang_cpowif(float Re, float Im, + int32_t power) { + return cmplx(complex(pow(generic(Re, Im), power))); +} + +// intrinsics + +LIBFLANG_ABI double libflang_cabs(double Re, double Im) { + return abs(generic(Re, Im)); +} +LIBFLANG_ABI float libflang_cabsf(float Re, float Im) { + return abs(generic(Re, Im)); +} + +LIBFLANG_ABI Complex8 libflang_csqrt(double Re, double Im) { + return cmplx(sqrt(generic(Re, Im))); +} +LIBFLANG_ABI Complex4 libflang_csqrtf(float Re, float Im) { + return cmplx(sqrt(generic(Re, Im))); +} + +LIBFLANG_ABI Complex8 libflang_cexp(double Re, double Im) { + return cmplx(exp(generic(Re, Im))); +} +LIBFLANG_ABI Complex4 libflang_cexpf(float Re, float Im) { + return cmplx(exp(generic(Re, Im))); +} + +LIBFLANG_ABI Complex8 libflang_clog(double Re, double Im) { + return cmplx(log(generic(Re, Im))); +} +LIBFLANG_ABI Complex4 libflang_clogf(float Re, float Im) { + return cmplx(log(generic(Re, Im))); +} + +LIBFLANG_ABI Complex8 libflang_csin(double Re, double Im) { + return cmplx(sin(generic(Re, Im))); +} +LIBFLANG_ABI Complex4 libflang_csinf(float Re, float Im) { + return cmplx(sin(generic(Re, Im))); +} + +LIBFLANG_ABI Complex8 libflang_ccos(double Re, double Im) { + return cmplx(cos(generic(Re, Im))); +} +LIBFLANG_ABI Complex4 libflang_ccosf(float Re, float Im) { + return cmplx(cos(generic(Re, Im))); +} + +LIBFLANG_ABI Complex8 libflang_ctan(double Re, double Im) { + return cmplx(tan(generic(Re, Im))); +} +LIBFLANG_ABI Complex4 libflang_ctanf(float Re, float Im) { + return cmplx(tan(generic(Re, Im))); +} -- cgit v1.2.1