From acee8e7ec292b1567214b13757031d77b4d2a493 Mon Sep 17 00:00:00 2001 From: Ivan Radanov Ivanov Date: Thu, 22 Feb 2024 08:46:50 +0900 Subject: [PATCH] Make header work in C --- enzyme/Enzyme/Runtime/EnzymeMPFR.cpp | 36 -------------- enzyme/Enzyme/Runtime/EnzymeMPFR.h | 72 ++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 36 deletions(-) delete mode 100644 enzyme/Enzyme/Runtime/EnzymeMPFR.cpp create mode 100644 enzyme/Enzyme/Runtime/EnzymeMPFR.h diff --git a/enzyme/Enzyme/Runtime/EnzymeMPFR.cpp b/enzyme/Enzyme/Runtime/EnzymeMPFR.cpp deleted file mode 100644 index 170167269d95..000000000000 --- a/enzyme/Enzyme/Runtime/EnzymeMPFR.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include -#include - -extern "C" { - -#define BINOP(LLVM_OP_NAME, MPFR_FUNC_NAME, FROM_TYPE, RET, MPFR_GET, ARG1, \ - MPFR_SET_ARG1, ARG2, MPFR_SET_ARG2, ROUNDING_MODE) \ - __attribute__((weak)) RET __enzyme_mpfr_##FROM_TYPE_binop_##LLVM_OP_NAME( \ - ARG1 a, ARG2 b, int64_t exponent, int64_t significand) { \ - mpfr_t ma, mb, mc; \ - mpfr_init2(ma, significand); \ - mpfr_init2(mb, significand); \ - mpfr_init2(mc, significand); \ - mpfr_set_##MPFR_SET_ARG1(ma, a, ROUNDING_MODE); \ - mpfr_set_##MPFR_SET_ARG1(mb, b, ROUNDING_MODE); \ - mpfr_##MPFR_FUNC_NAME(mc, ma, mb, ROUNDING_MODE); \ - RET c = mpfr_get_##MPFR_GET(mc, ROUNDING_MODE); \ - mpfr_clear(ma); \ - mpfr_clear(mb); \ - mpfr_clear(mc); \ - return c; \ - } - -#define DEFAULT_ROUNDING_MODE GMP_RNDN -#define DBL_MANGLE 64_52 -#define DOUBLE_BINOP(LLVM_OP_NAME, MPFR_FUNC_NAME, ROUNDING_MODE) \ - BINOP(LLVM_OP_NAME, MPFR_FUNC_NAME, DBL_MANGLE, double, d, double, d, \ - double, d, ROUNDING_MODE) -#define DOUBLE_BINOP_DEFAULT_ROUNDING(LLVM_OP_NAME, MPFR_FUNC_NAME) \ - DOUBLE_BINOP(LLVM_OP_NAME, MPFR_FUNC_NAME, DEFAULT_ROUNDING_MODE) - -// BINOP(fmul, mul, 64_52, double, d, double, d, double, d, GMP_RNDN) -DOUBLE_BINOP_DEFAULT_ROUNDING(fmul, mul) -DOUBLE_BINOP_DEFAULT_ROUNDING(fadd, add) -DOUBLE_BINOP_DEFAULT_ROUNDING(fdiv, div) -} diff --git a/enzyme/Enzyme/Runtime/EnzymeMPFR.h b/enzyme/Enzyme/Runtime/EnzymeMPFR.h new file mode 100644 index 000000000000..175ea317bef3 --- /dev/null +++ b/enzyme/Enzyme/Runtime/EnzymeMPFR.h @@ -0,0 +1,72 @@ +//===- EnzymeMPFR.h - Implementation of forward and reverse pass generation==// +// +// Enzyme Project +// +// Part of the Enzyme Project, under the Apache License v2.0 with LLVM +// Exceptions. See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// If using this code in an academic setting, please cite the following: +// @incollection{enzymeNeurips, +// title = {Instead of Rewriting Foreign Code for Machine Learning, +// Automatically Synthesize Fast Gradients}, +// author = {Moses, William S. and Churavy, Valentin}, +// booktitle = {Advances in Neural Information Processing Systems 33}, +// year = {2020}, +// note = {To appear in}, +// } +// +//===----------------------------------------------------------------------===// +// +// This file contains easy to use wrappers around MPFR functions. +// +//===----------------------------------------------------------------------===// +#ifndef __ENZYME_RUNTIME_ENZYME_MPFR__ +#define __ENZYME_RUNTIME_ENZYME_MPFR__ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define __ENZYME_MPFR_BINOP(LLVM_OP_NAME, MPFR_FUNC_NAME, FROM_TYPE, RET, \ + MPFR_GET, ARG1, MPFR_SET_ARG1, ARG2, \ + MPFR_SET_ARG2, ROUNDING_MODE) \ + __attribute__((weak)) RET __enzyme_mpfr_##FROM_TYPE_binop_##LLVM_OP_NAME( \ + ARG1 a, ARG2 b, int64_t exponent, int64_t significand) { \ + mpfr_t ma, mb, mc; \ + mpfr_init2(ma, significand); \ + mpfr_init2(mb, significand); \ + mpfr_init2(mc, significand); \ + mpfr_set_##MPFR_SET_ARG1(ma, a, ROUNDING_MODE); \ + mpfr_set_##MPFR_SET_ARG1(mb, b, ROUNDING_MODE); \ + mpfr_##MPFR_FUNC_NAME(mc, ma, mb, ROUNDING_MODE); \ + RET c = mpfr_get_##MPFR_GET(mc, ROUNDING_MODE); \ + mpfr_clear(ma); \ + mpfr_clear(mb); \ + mpfr_clear(mc); \ + return c; \ + } + +#define __ENZYME_MPFR_DEFAULT_ROUNDING_MODE GMP_RNDN +#define __ENZYME_MPFR_DBL_MANGLE 64_52 +#define __ENZYME_MPFR_DOUBLE_BINOP(LLVM_OP_NAME, MPFR_FUNC_NAME, \ + ROUNDING_MODE) \ + __ENZYME_MPFR_BINOP(LLVM_OP_NAME, MPFR_FUNC_NAME, __ENZYME_MPFR_DBL_MANGLE, \ + double, d, double, d, double, d, ROUNDING_MODE) +#define __ENZYME_MPFR_DOUBLE_BINOP_DEFAULT_ROUNDING(LLVM_OP_NAME, \ + MPFR_FUNC_NAME) \ + __ENZYME_MPFR_DOUBLE_BINOP(LLVM_OP_NAME, MPFR_FUNC_NAME, \ + __ENZYME_MPFR_DEFAULT_ROUNDING_MODE) + +__ENZYME_MPFR_DOUBLE_BINOP_DEFAULT_ROUNDING(fmul, mul) +__ENZYME_MPFR_DOUBLE_BINOP_DEFAULT_ROUNDING(fadd, add) +__ENZYME_MPFR_DOUBLE_BINOP_DEFAULT_ROUNDING(fdiv, div) + +#ifdef __cplusplus +} +#endif + +#endif // #ifndef __ENZYME_RUNTIME_ENZYME_MPFR__