Skip to content

Commit

Permalink
Merge pull request #432 from bluescarni/pr/rel_bool_ops
Browse files Browse the repository at this point in the history
Relational/logical operations
  • Loading branch information
bluescarni authored Jun 25, 2024
2 parents 31c363a + 766b585 commit 3465aab
Show file tree
Hide file tree
Showing 21 changed files with 2,960 additions and 34 deletions.
7 changes: 5 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if(NOT CMAKE_BUILD_TYPE)
FORCE)
endif()

project(heyoka VERSION 5.0.0 LANGUAGES CXX C)
project(heyoka VERSION 5.1.0 LANGUAGES CXX C)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" "${CMAKE_CURRENT_SOURCE_DIR}/cmake/yacma")

Expand Down Expand Up @@ -313,6 +313,9 @@ set(HEYOKA_SRC_FILES
"${CMAKE_CURRENT_SOURCE_DIR}/src/math/prod.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/math/constants.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/math/dfun.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/math/relational.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/math/logical.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/math/select.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/detail/string_conv.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/detail/logging_impl.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/step_callback.cpp"
Expand All @@ -329,7 +332,7 @@ if(HEYOKA_WITH_SLEEF)
endif()

# Setup the heyoka ABI version number.
set(HEYOKA_ABI_VERSION 28)
set(HEYOKA_ABI_VERSION 29)

if(HEYOKA_BUILD_STATIC_LIBRARY)
# Setup of the heyoka static library.
Expand Down
11 changes: 11 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
Changelog
=========

5.1.0 (unreleased)
------------------

New
~~~

- Add the ``select()`` primitive to the expression system
(`#432 <https://github.com/bluescarni/heyoka/pull/432>`__).
- Add relational and logical operators to the expression system
(`#432 <https://github.com/bluescarni/heyoka/pull/432>`__).

5.0.0 (2024-06-13)
------------------

Expand Down
2 changes: 2 additions & 0 deletions include/heyoka/detail/llvm_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ HEYOKA_DLL_PUBLIC llvm::Value *llvm_fcmp_ole(llvm_state &, llvm::Value *, llvm::
HEYOKA_DLL_PUBLIC llvm::Value *llvm_fcmp_olt(llvm_state &, llvm::Value *, llvm::Value *);
HEYOKA_DLL_PUBLIC llvm::Value *llvm_fcmp_ogt(llvm_state &, llvm::Value *, llvm::Value *);
HEYOKA_DLL_PUBLIC llvm::Value *llvm_fcmp_oeq(llvm_state &, llvm::Value *, llvm::Value *);
HEYOKA_DLL_PUBLIC llvm::Value *llvm_fcmp_one(llvm_state &, llvm::Value *, llvm::Value *);
HEYOKA_DLL_PUBLIC llvm::Value *llvm_fnz(llvm_state &, llvm::Value *);
HEYOKA_DLL_PUBLIC llvm::Value *llvm_min(llvm_state &, llvm::Value *, llvm::Value *);
HEYOKA_DLL_PUBLIC llvm::Value *llvm_max(llvm_state &, llvm::Value *, llvm::Value *);
HEYOKA_DLL_PUBLIC llvm::Value *llvm_min_nan(llvm_state &, llvm::Value *, llvm::Value *);
Expand Down
2 changes: 2 additions & 0 deletions include/heyoka/detail/real_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ llvm::Value *llvm_real_fcmp_ole(llvm_state &, llvm::Value *, llvm::Value *);
llvm::Value *llvm_real_fcmp_olt(llvm_state &, llvm::Value *, llvm::Value *);
llvm::Value *llvm_real_fcmp_ogt(llvm_state &, llvm::Value *, llvm::Value *);
llvm::Value *llvm_real_fcmp_oeq(llvm_state &, llvm::Value *, llvm::Value *);
llvm::Value *llvm_real_fcmp_one(llvm_state &, llvm::Value *, llvm::Value *);
llvm::Value *llvm_real_fnz(llvm_state &, llvm::Value *);
llvm::Value *llvm_real_ui_to_fp(llvm_state &, llvm::Value *, llvm::Type *);
llvm::Value *llvm_real_sgn(llvm_state &, llvm::Value *);

Expand Down
3 changes: 3 additions & 0 deletions include/heyoka/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@
#include <heyoka/math/kepE.hpp>
#include <heyoka/math/kepF.hpp>
#include <heyoka/math/log.hpp>
#include <heyoka/math/logical.hpp>
#include <heyoka/math/pow.hpp>
#include <heyoka/math/prod.hpp>
#include <heyoka/math/relational.hpp>
#include <heyoka/math/relu.hpp>
#include <heyoka/math/select.hpp>
#include <heyoka/math/sigmoid.hpp>
#include <heyoka/math/sin.hpp>
#include <heyoka/math/sinh.hpp>
Expand Down
93 changes: 93 additions & 0 deletions include/heyoka/math/logical.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2020, 2021, 2022, 2023, 2024 Francesco Biscani ([email protected]), Dario Izzo ([email protected])
//
// This file is part of the heyoka library.
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef HEYOKA_MATH_LOGICAL_HPP
#define HEYOKA_MATH_LOGICAL_HPP

#include <cstdint>
#include <vector>

#include <heyoka/config.hpp>
#include <heyoka/detail/fwd_decl.hpp>
#include <heyoka/detail/llvm_fwd.hpp>
#include <heyoka/detail/visibility.hpp>
#include <heyoka/func.hpp>
#include <heyoka/s11n.hpp>

HEYOKA_BEGIN_NAMESPACE

namespace detail
{

class HEYOKA_DLL_PUBLIC logical_and_impl : public func_base
{
friend class boost::serialization::access;
template <typename Archive>
void serialize(Archive &ar, unsigned)
{
ar &boost::serialization::base_object<func_base>(*this);
}

public:
logical_and_impl();
explicit logical_and_impl(std::vector<expression>);

[[nodiscard]] std::vector<expression> gradient() const;

[[nodiscard]] llvm::Value *llvm_eval(llvm_state &, llvm::Type *, const std::vector<llvm::Value *> &, llvm::Value *,
llvm::Value *, llvm::Value *, std::uint32_t, bool) const;

[[nodiscard]] llvm::Function *llvm_c_eval_func(llvm_state &, llvm::Type *, std::uint32_t, bool) const;

[[nodiscard]] llvm::Value *taylor_diff(llvm_state &, llvm::Type *, const std::vector<std::uint32_t> &,
const std::vector<llvm::Value *> &, llvm::Value *, llvm::Value *,
std::uint32_t, std::uint32_t, std::uint32_t, std::uint32_t, bool) const;

[[nodiscard]] llvm::Function *taylor_c_diff_func(llvm_state &, llvm::Type *, std::uint32_t, std::uint32_t,
bool) const;
};

class HEYOKA_DLL_PUBLIC logical_or_impl : public func_base
{
friend class boost::serialization::access;
template <typename Archive>
void serialize(Archive &ar, unsigned)
{
ar &boost::serialization::base_object<func_base>(*this);
}

public:
logical_or_impl();
explicit logical_or_impl(std::vector<expression>);

[[nodiscard]] std::vector<expression> gradient() const;

[[nodiscard]] llvm::Value *llvm_eval(llvm_state &, llvm::Type *, const std::vector<llvm::Value *> &, llvm::Value *,
llvm::Value *, llvm::Value *, std::uint32_t, bool) const;

[[nodiscard]] llvm::Function *llvm_c_eval_func(llvm_state &, llvm::Type *, std::uint32_t, bool) const;

[[nodiscard]] llvm::Value *taylor_diff(llvm_state &, llvm::Type *, const std::vector<std::uint32_t> &,
const std::vector<llvm::Value *> &, llvm::Value *, llvm::Value *,
std::uint32_t, std::uint32_t, std::uint32_t, std::uint32_t, bool) const;

[[nodiscard]] llvm::Function *taylor_c_diff_func(llvm_state &, llvm::Type *, std::uint32_t, std::uint32_t,
bool) const;
};

} // namespace detail

HEYOKA_DLL_PUBLIC expression logical_and(std::vector<expression>);
HEYOKA_DLL_PUBLIC expression logical_or(std::vector<expression>);

HEYOKA_END_NAMESPACE

HEYOKA_S11N_FUNC_EXPORT_KEY(heyoka::detail::logical_and_impl)
HEYOKA_S11N_FUNC_EXPORT_KEY(heyoka::detail::logical_or_impl)

#endif
123 changes: 123 additions & 0 deletions include/heyoka/math/relational.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright 2020, 2021, 2022, 2023, 2024 Francesco Biscani ([email protected]), Dario Izzo ([email protected])
//
// This file is part of the heyoka library.
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef HEYOKA_MATH_RELATIONAL_HPP
#define HEYOKA_MATH_RELATIONAL_HPP

#include <heyoka/config.hpp>

#include <cstdint>
#include <sstream>
#include <vector>

#if defined(HEYOKA_HAVE_REAL128)

#include <mp++/real128.hpp>

#endif

#if defined(HEYOKA_HAVE_REAL)

#include <mp++/real.hpp>

#endif

#include <heyoka/detail/fwd_decl.hpp>
#include <heyoka/detail/llvm_fwd.hpp>
#include <heyoka/detail/visibility.hpp>
#include <heyoka/func.hpp>
#include <heyoka/s11n.hpp>

HEYOKA_BEGIN_NAMESPACE

namespace detail
{

enum class rel_op { eq, neq, lt, gt, lte, gte };

class HEYOKA_DLL_PUBLIC rel_impl : public func_base
{
rel_op m_op = rel_op::eq;

friend class boost::serialization::access;
template <typename Archive>
void serialize(Archive &ar, unsigned)
{
ar &boost::serialization::base_object<func_base>(*this);
ar & m_op;
}

public:
rel_impl();
explicit rel_impl(rel_op, expression, expression);

[[nodiscard]] rel_op get_op() const noexcept;

void to_stream(std::ostringstream &) const;

[[nodiscard]] std::vector<expression> gradient() const;

[[nodiscard]] llvm::Value *llvm_eval(llvm_state &, llvm::Type *, const std::vector<llvm::Value *> &, llvm::Value *,
llvm::Value *, llvm::Value *, std::uint32_t, bool) const;

[[nodiscard]] llvm::Function *llvm_c_eval_func(llvm_state &, llvm::Type *, std::uint32_t, bool) const;

[[nodiscard]] llvm::Value *taylor_diff(llvm_state &, llvm::Type *, const std::vector<std::uint32_t> &,
const std::vector<llvm::Value *> &, llvm::Value *, llvm::Value *,
std::uint32_t, std::uint32_t, std::uint32_t, std::uint32_t, bool) const;

[[nodiscard]] llvm::Function *taylor_c_diff_func(llvm_state &, llvm::Type *, std::uint32_t, std::uint32_t,
bool) const;
};

} // namespace detail

HEYOKA_DLL_PUBLIC expression eq(expression, expression);
HEYOKA_DLL_PUBLIC expression neq(expression, expression);
HEYOKA_DLL_PUBLIC expression lt(expression, expression);
HEYOKA_DLL_PUBLIC expression gt(expression, expression);
HEYOKA_DLL_PUBLIC expression lte(expression, expression);
HEYOKA_DLL_PUBLIC expression gte(expression, expression);

#define HEYOKA_DECLARE_REL_OVERLOADS(type) \
HEYOKA_DLL_PUBLIC expression eq(expression, type); \
HEYOKA_DLL_PUBLIC expression eq(type, expression); \
HEYOKA_DLL_PUBLIC expression neq(expression, type); \
HEYOKA_DLL_PUBLIC expression neq(type, expression); \
HEYOKA_DLL_PUBLIC expression lt(expression, type); \
HEYOKA_DLL_PUBLIC expression lt(type, expression); \
HEYOKA_DLL_PUBLIC expression gt(expression, type); \
HEYOKA_DLL_PUBLIC expression gt(type, expression); \
HEYOKA_DLL_PUBLIC expression lte(expression, type); \
HEYOKA_DLL_PUBLIC expression lte(type, expression); \
HEYOKA_DLL_PUBLIC expression gte(expression, type); \
HEYOKA_DLL_PUBLIC expression gte(type, expression);

HEYOKA_DECLARE_REL_OVERLOADS(float);
HEYOKA_DECLARE_REL_OVERLOADS(double);
HEYOKA_DECLARE_REL_OVERLOADS(long double);

#if defined(HEYOKA_HAVE_REAL128)

HEYOKA_DECLARE_REL_OVERLOADS(mppp::real128);

#endif

#if defined(HEYOKA_HAVE_REAL)

HEYOKA_DECLARE_REL_OVERLOADS(mppp::real);

#endif

#undef HEYOKA_DECLARE_REL_OVERLOADS

HEYOKA_END_NAMESPACE

HEYOKA_S11N_FUNC_EXPORT_KEY(heyoka::detail::rel_impl)

#endif
102 changes: 102 additions & 0 deletions include/heyoka/math/select.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright 2020, 2021, 2022, 2023, 2024 Francesco Biscani ([email protected]), Dario Izzo ([email protected])
//
// This file is part of the heyoka library.
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef HEYOKA_MATH_SELECT_HPP
#define HEYOKA_MATH_SELECT_HPP

#include <heyoka/config.hpp>

#include <cstdint>
#include <vector>

#if defined(HEYOKA_HAVE_REAL128)

#include <mp++/real128.hpp>

#endif

#if defined(HEYOKA_HAVE_REAL)

#include <mp++/real.hpp>

#endif

#include <heyoka/detail/fwd_decl.hpp>
#include <heyoka/detail/llvm_fwd.hpp>
#include <heyoka/detail/visibility.hpp>
#include <heyoka/func.hpp>
#include <heyoka/s11n.hpp>

HEYOKA_BEGIN_NAMESPACE

namespace detail
{

class HEYOKA_DLL_PUBLIC select_impl : public func_base
{
friend class boost::serialization::access;
template <typename Archive>
void serialize(Archive &ar, unsigned)
{
ar &boost::serialization::base_object<func_base>(*this);
}

public:
select_impl();
explicit select_impl(expression, expression, expression);

[[nodiscard]] std::vector<expression> gradient() const;

[[nodiscard]] llvm::Value *llvm_eval(llvm_state &, llvm::Type *, const std::vector<llvm::Value *> &, llvm::Value *,
llvm::Value *, llvm::Value *, std::uint32_t, bool) const;

[[nodiscard]] llvm::Function *llvm_c_eval_func(llvm_state &, llvm::Type *, std::uint32_t, bool) const;

[[nodiscard]] llvm::Value *taylor_diff(llvm_state &, llvm::Type *, const std::vector<std::uint32_t> &,
const std::vector<llvm::Value *> &, llvm::Value *, llvm::Value *,
std::uint32_t, std::uint32_t, std::uint32_t, std::uint32_t, bool) const;

[[nodiscard]] llvm::Function *taylor_c_diff_func(llvm_state &, llvm::Type *, std::uint32_t, std::uint32_t,
bool) const;
};

} // namespace detail

HEYOKA_DLL_PUBLIC expression select(expression, expression, expression);

#define HEYOKA_DECLARE_SELECT_OVERLOADS(type) \
HEYOKA_DLL_PUBLIC expression select(expression, type, type); \
HEYOKA_DLL_PUBLIC expression select(type, expression, type); \
HEYOKA_DLL_PUBLIC expression select(type, type, expression); \
HEYOKA_DLL_PUBLIC expression select(expression, expression, type); \
HEYOKA_DLL_PUBLIC expression select(expression, type, expression); \
HEYOKA_DLL_PUBLIC expression select(type, expression, expression)

HEYOKA_DECLARE_SELECT_OVERLOADS(float);
HEYOKA_DECLARE_SELECT_OVERLOADS(double);
HEYOKA_DECLARE_SELECT_OVERLOADS(long double);

#if defined(HEYOKA_HAVE_REAL128)

HEYOKA_DECLARE_SELECT_OVERLOADS(mppp::real128);

#endif

#if defined(HEYOKA_HAVE_REAL)

HEYOKA_DECLARE_SELECT_OVERLOADS(mppp::real);

#endif

#undef HEYOKA_DECLARE_SELECT_OVERLOADS

HEYOKA_END_NAMESPACE

HEYOKA_S11N_FUNC_EXPORT_KEY(heyoka::detail::select_impl)

#endif
Loading

0 comments on commit 3465aab

Please sign in to comment.