MLIR 22.0.0git
APFloatWrappers.cpp
Go to the documentation of this file.
1//===- APFloatWrappers.cpp - Software Implementation of FP Arithmetics --- ===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file exposes the APFloat infrastructure to MLIR programs as a runtime
10// library. APFloat is a software implementation of floating point arithmetics.
11//
12// On the MLIR side, floating-point values must be bitcasted to 64-bit integers
13// before calling a runtime function. If a floating-point type has less than
14// 64 bits, it must be zero-extended to 64 bits after bitcasting it to an
15// integer.
16//
17// Runtime functions receive the floating-point operands of the arithmeic
18// operation in the form of 64-bit integers, along with the APFloat semantics
19// in the form of a 32-bit integer, which will be interpreted as an
20// APFloatBase::Semantics enum value.
21//
22#include "llvm/ADT/APFloat.h"
23
24#ifdef _WIN32
25#ifndef MLIR_APFLOAT_WRAPPERS_EXPORT
26#ifdef mlir_apfloat_wrappers_EXPORTS
27// We are building this library
28#define MLIR_APFLOAT_WRAPPERS_EXPORT __declspec(dllexport)
29#else
30// We are using this library
31#define MLIR_APFLOAT_WRAPPERS_EXPORT __declspec(dllimport)
32#endif // mlir_apfloat_wrappers_EXPORTS
33#endif // MLIR_APFLOAT_WRAPPERS_EXPORT
34#else
35// Non-windows: use visibility attributes.
36#define MLIR_APFLOAT_WRAPPERS_EXPORT __attribute__((visibility("default")))
37#endif // _WIN32
38
39/// Binary operations without rounding mode.
40#define APFLOAT_BINARY_OP(OP) \
41 MLIR_APFLOAT_WRAPPERS_EXPORT int64_t _mlir_apfloat_##OP( \
42 int32_t semantics, uint64_t a, uint64_t b) { \
43 const llvm::fltSemantics &sem = llvm::APFloatBase::EnumToSemantics( \
44 static_cast<llvm::APFloatBase::Semantics>(semantics)); \
45 unsigned bitWidth = llvm::APFloatBase::semanticsSizeInBits(sem); \
46 llvm::APFloat lhs(sem, llvm::APInt(bitWidth, a)); \
47 llvm::APFloat rhs(sem, llvm::APInt(bitWidth, b)); \
48 lhs.OP(rhs); \
49 return lhs.bitcastToAPInt().getZExtValue(); \
50 }
51
52/// Binary operations with rounding mode.
53#define APFLOAT_BINARY_OP_ROUNDING_MODE(OP, ROUNDING_MODE) \
54 MLIR_APFLOAT_WRAPPERS_EXPORT int64_t _mlir_apfloat_##OP( \
55 int32_t semantics, uint64_t a, uint64_t b) { \
56 const llvm::fltSemantics &sem = llvm::APFloatBase::EnumToSemantics( \
57 static_cast<llvm::APFloatBase::Semantics>(semantics)); \
58 unsigned bitWidth = llvm::APFloatBase::semanticsSizeInBits(sem); \
59 llvm::APFloat lhs(sem, llvm::APInt(bitWidth, a)); \
60 llvm::APFloat rhs(sem, llvm::APInt(bitWidth, b)); \
61 lhs.OP(rhs, ROUNDING_MODE); \
62 return lhs.bitcastToAPInt().getZExtValue(); \
63 }
64
65extern "C" {
66
67#define BIN_OPS_WITH_ROUNDING(X) \
68 X(add, llvm::RoundingMode::NearestTiesToEven) \
69 X(subtract, llvm::RoundingMode::NearestTiesToEven) \
70 X(multiply, llvm::RoundingMode::NearestTiesToEven) \
71 X(divide, llvm::RoundingMode::NearestTiesToEven)
72
74#undef BIN_OPS_WITH_ROUNDING
75#undef APFLOAT_BINARY_OP_ROUNDING_MODE
76
77APFLOAT_BINARY_OP(remainder)
78
79#undef APFLOAT_BINARY_OP
80
81MLIR_APFLOAT_WRAPPERS_EXPORT void printApFloat(int32_t semantics, uint64_t a) {
82 const llvm::fltSemantics &sem = llvm::APFloatBase::EnumToSemantics(
83 static_cast<llvm::APFloatBase::Semantics>(semantics));
84 unsigned bitWidth = llvm::APFloatBase::semanticsSizeInBits(sem);
85 llvm::APFloat x(sem, llvm::APInt(bitWidth, a));
86 double d = x.convertToDouble();
87 fprintf(stdout, "%lg", d);
88}
89}
MLIR_APFLOAT_WRAPPERS_EXPORT void printApFloat(int32_t semantics, uint64_t a)
#define BIN_OPS_WITH_ROUNDING(X)
#define APFLOAT_BINARY_OP(OP)
Binary operations without rounding mode.
#define APFLOAT_BINARY_OP_ROUNDING_MODE(OP, ROUNDING_MODE)
Binary operations with rounding mode.
#define MLIR_APFLOAT_WRAPPERS_EXPORT