Skip to content

Commit

Permalink
Separate arithmetic_operation from arithmetic_files (#3020)
Browse files Browse the repository at this point in the history
  • Loading branch information
SeaRise authored Sep 9, 2021
1 parent 228db6b commit 92e6681
Show file tree
Hide file tree
Showing 84 changed files with 2,042 additions and 2,152 deletions.
44 changes: 44 additions & 0 deletions dbms/src/Functions/DivisionUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once

#include <Common/Exception.h>
#include <common/likely.h>

namespace DB
{
namespace ErrorCodes
{
extern const int ILLEGAL_DIVISION;
}

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-compare"

template <typename A, typename B>
inline void throwIfDivisionLeadsToFPE(A a, B b)
{
/// Is it better to use siglongjmp instead of checks?

if (unlikely(b == 0))
throw Exception("Division by zero", ErrorCodes::ILLEGAL_DIVISION);

/// http://avva.livejournal.com/2548306.html
if (unlikely(std::is_signed_v<A> && std::is_signed_v<B> && a == std::numeric_limits<A>::min() && b == -1))
throw Exception("Division of minimal signed number by minus one", ErrorCodes::ILLEGAL_DIVISION);
}

template <typename A, typename B>
inline bool divisionLeadsToFPE(A a, B b)
{
if (unlikely(b == 0))
return true;

if (unlikely(std::is_signed_v<A> && std::is_signed_v<B> && a == std::numeric_limits<A>::min() && b == -1))
return true;

return false;
}


#pragma GCC diagnostic pop

} // namespace DB
Loading

0 comments on commit 92e6681

Please sign in to comment.