| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | #ifndef FLOAT_H |
| | #define FLOAT_H |
| |
|
| | #include <math.h> |
| |
|
| | |
| | #ifndef M_PI |
| | #define M_PI 3.14159265358979323846 |
| | #endif |
| |
|
| | |
| | #define RADIANS_PER_DEGREE 0.0174532925199432957692 |
| |
|
| | |
| | #if defined(WIN32) && !defined(NAN) |
| | static const uint32 nan[2] = {0xffffffff, 0x7fffffff}; |
| |
|
| | #define NAN (*(const float8 *) nan) |
| | #endif |
| |
|
| | extern PGDLLIMPORT int extra_float_digits; |
| |
|
| | |
| | |
| | |
| | extern void float_overflow_error(void) pg_attribute_noreturn(); |
| | extern void float_underflow_error(void) pg_attribute_noreturn(); |
| | extern void float_zero_divide_error(void) pg_attribute_noreturn(); |
| | extern int is_infinite(float8 val); |
| | extern float8 float8in_internal(char *num, char **endptr_p, |
| | const char *type_name, const char *orig_string, |
| | struct Node *escontext); |
| | extern float4 float4in_internal(char *num, char **endptr_p, |
| | const char *type_name, const char *orig_string, |
| | struct Node *escontext); |
| | extern char *float8out_internal(float8 num); |
| | extern int float4_cmp_internal(float4 a, float4 b); |
| | extern int float8_cmp_internal(float8 a, float8 b); |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | #ifdef _MSC_VER |
| | #pragma warning(disable:4756) |
| | #endif |
| | static inline float4 |
| | get_float4_infinity(void) |
| | { |
| | #ifdef INFINITY |
| | |
| | return (float4) INFINITY; |
| | #else |
| | #ifdef _MSC_VER |
| | #pragma warning(default:4756) |
| | #endif |
| |
|
| | |
| | |
| | |
| | |
| | |
| | return (float4) (HUGE_VAL * HUGE_VAL); |
| | #endif |
| | } |
| |
|
| | static inline float8 |
| | get_float8_infinity(void) |
| | { |
| | #ifdef INFINITY |
| | |
| | return (float8) INFINITY; |
| | #else |
| |
|
| | |
| | |
| | |
| | |
| | |
| | return (float8) (HUGE_VAL * HUGE_VAL); |
| | #endif |
| | } |
| |
|
| | static inline float4 |
| | get_float4_nan(void) |
| | { |
| | #ifdef NAN |
| | |
| | return (float4) NAN; |
| | #else |
| | |
| | return (float4) (0.0 / 0.0); |
| | #endif |
| | } |
| |
|
| | static inline float8 |
| | get_float8_nan(void) |
| | { |
| | |
| | #if defined(NAN) && !(defined(__NetBSD__) && defined(__mips__)) |
| | |
| | return (float8) NAN; |
| | #else |
| | |
| | return (float8) (0.0 / 0.0); |
| | #endif |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | static inline float4 |
| | float4_pl(const float4 val1, const float4 val2) |
| | { |
| | float4 result; |
| |
|
| | result = val1 + val2; |
| | if (unlikely(isinf(result)) && !isinf(val1) && !isinf(val2)) |
| | float_overflow_error(); |
| |
|
| | return result; |
| | } |
| |
|
| | static inline float8 |
| | float8_pl(const float8 val1, const float8 val2) |
| | { |
| | float8 result; |
| |
|
| | result = val1 + val2; |
| | if (unlikely(isinf(result)) && !isinf(val1) && !isinf(val2)) |
| | float_overflow_error(); |
| |
|
| | return result; |
| | } |
| |
|
| | static inline float4 |
| | float4_mi(const float4 val1, const float4 val2) |
| | { |
| | float4 result; |
| |
|
| | result = val1 - val2; |
| | if (unlikely(isinf(result)) && !isinf(val1) && !isinf(val2)) |
| | float_overflow_error(); |
| |
|
| | return result; |
| | } |
| |
|
| | static inline float8 |
| | float8_mi(const float8 val1, const float8 val2) |
| | { |
| | float8 result; |
| |
|
| | result = val1 - val2; |
| | if (unlikely(isinf(result)) && !isinf(val1) && !isinf(val2)) |
| | float_overflow_error(); |
| |
|
| | return result; |
| | } |
| |
|
| | static inline float4 |
| | float4_mul(const float4 val1, const float4 val2) |
| | { |
| | float4 result; |
| |
|
| | result = val1 * val2; |
| | if (unlikely(isinf(result)) && !isinf(val1) && !isinf(val2)) |
| | float_overflow_error(); |
| | if (unlikely(result == 0.0f) && val1 != 0.0f && val2 != 0.0f) |
| | float_underflow_error(); |
| |
|
| | return result; |
| | } |
| |
|
| | static inline float8 |
| | float8_mul(const float8 val1, const float8 val2) |
| | { |
| | float8 result; |
| |
|
| | result = val1 * val2; |
| | if (unlikely(isinf(result)) && !isinf(val1) && !isinf(val2)) |
| | float_overflow_error(); |
| | if (unlikely(result == 0.0) && val1 != 0.0 && val2 != 0.0) |
| | float_underflow_error(); |
| |
|
| | return result; |
| | } |
| |
|
| | static inline float4 |
| | float4_div(const float4 val1, const float4 val2) |
| | { |
| | float4 result; |
| |
|
| | if (unlikely(val2 == 0.0f) && !isnan(val1)) |
| | float_zero_divide_error(); |
| | result = val1 / val2; |
| | if (unlikely(isinf(result)) && !isinf(val1)) |
| | float_overflow_error(); |
| | if (unlikely(result == 0.0f) && val1 != 0.0f && !isinf(val2)) |
| | float_underflow_error(); |
| |
|
| | return result; |
| | } |
| |
|
| | static inline float8 |
| | float8_div(const float8 val1, const float8 val2) |
| | { |
| | float8 result; |
| |
|
| | if (unlikely(val2 == 0.0) && !isnan(val1)) |
| | float_zero_divide_error(); |
| | result = val1 / val2; |
| | if (unlikely(isinf(result)) && !isinf(val1)) |
| | float_overflow_error(); |
| | if (unlikely(result == 0.0) && val1 != 0.0 && !isinf(val2)) |
| | float_underflow_error(); |
| |
|
| | return result; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | static inline bool |
| | float4_eq(const float4 val1, const float4 val2) |
| | { |
| | return isnan(val1) ? isnan(val2) : !isnan(val2) && val1 == val2; |
| | } |
| |
|
| | static inline bool |
| | float8_eq(const float8 val1, const float8 val2) |
| | { |
| | return isnan(val1) ? isnan(val2) : !isnan(val2) && val1 == val2; |
| | } |
| |
|
| | static inline bool |
| | float4_ne(const float4 val1, const float4 val2) |
| | { |
| | return isnan(val1) ? !isnan(val2) : isnan(val2) || val1 != val2; |
| | } |
| |
|
| | static inline bool |
| | float8_ne(const float8 val1, const float8 val2) |
| | { |
| | return isnan(val1) ? !isnan(val2) : isnan(val2) || val1 != val2; |
| | } |
| |
|
| | static inline bool |
| | float4_lt(const float4 val1, const float4 val2) |
| | { |
| | return !isnan(val1) && (isnan(val2) || val1 < val2); |
| | } |
| |
|
| | static inline bool |
| | float8_lt(const float8 val1, const float8 val2) |
| | { |
| | return !isnan(val1) && (isnan(val2) || val1 < val2); |
| | } |
| |
|
| | static inline bool |
| | float4_le(const float4 val1, const float4 val2) |
| | { |
| | return isnan(val2) || (!isnan(val1) && val1 <= val2); |
| | } |
| |
|
| | static inline bool |
| | float8_le(const float8 val1, const float8 val2) |
| | { |
| | return isnan(val2) || (!isnan(val1) && val1 <= val2); |
| | } |
| |
|
| | static inline bool |
| | float4_gt(const float4 val1, const float4 val2) |
| | { |
| | return !isnan(val2) && (isnan(val1) || val1 > val2); |
| | } |
| |
|
| | static inline bool |
| | float8_gt(const float8 val1, const float8 val2) |
| | { |
| | return !isnan(val2) && (isnan(val1) || val1 > val2); |
| | } |
| |
|
| | static inline bool |
| | float4_ge(const float4 val1, const float4 val2) |
| | { |
| | return isnan(val1) || (!isnan(val2) && val1 >= val2); |
| | } |
| |
|
| | static inline bool |
| | float8_ge(const float8 val1, const float8 val2) |
| | { |
| | return isnan(val1) || (!isnan(val2) && val1 >= val2); |
| | } |
| |
|
| | static inline float4 |
| | float4_min(const float4 val1, const float4 val2) |
| | { |
| | return float4_lt(val1, val2) ? val1 : val2; |
| | } |
| |
|
| | static inline float8 |
| | float8_min(const float8 val1, const float8 val2) |
| | { |
| | return float8_lt(val1, val2) ? val1 : val2; |
| | } |
| |
|
| | static inline float4 |
| | float4_max(const float4 val1, const float4 val2) |
| | { |
| | return float4_gt(val1, val2) ? val1 : val2; |
| | } |
| |
|
| | static inline float8 |
| | float8_max(const float8 val1, const float8 val2) |
| | { |
| | return float8_gt(val1, val2) ? val1 : val2; |
| | } |
| |
|
| | #endif |
| |
|