Skip to content

Commit

Permalink
Make header work in C
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanradanov committed Feb 21, 2024
1 parent 8a03779 commit 8f7606d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 36 deletions.
36 changes: 0 additions & 36 deletions enzyme/Enzyme/Runtime/EnzymeMPFR.cpp

This file was deleted.

72 changes: 72 additions & 0 deletions enzyme/Enzyme/Runtime/EnzymeMPFR.h
Original file line number Diff line number Diff line change
@@ -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 <mpfr.h>
#include <stdint.h>

#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__

0 comments on commit 8f7606d

Please sign in to comment.