github-actions[bot] commented on code in PR #25386: URL: https://github.com/apache/doris/pull/25386#discussion_r1364289279
########## be/src/vec/core/decomposed_float.h: ########## @@ -0,0 +1,201 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// This file is copied from +// https://github.com/ClickHouse/ClickHouse/blob/master/base/base/DecomposedFloat.h +// and modified by Doris +#pragma once + +#include <cstddef> +#include <cstdint> +#include <cstring> + +#include "extended_types.h" + +/// Allows to check the internals of IEEE-754 floating point number. + +template <typename T> +struct FloatTraits; + +template <> +struct FloatTraits<float> { + using UInt = uint32_t; + static constexpr size_t bits = 32; + static constexpr size_t exponent_bits = 8; + static constexpr size_t mantissa_bits = bits - exponent_bits - 1; +}; + +template <> +struct FloatTraits<double> { + using UInt = uint64_t; + static constexpr size_t bits = 64; + static constexpr size_t exponent_bits = 11; + static constexpr size_t mantissa_bits = bits - exponent_bits - 1; +}; + +/// x = sign * (2 ^ normalized_exponent) * (1 + mantissa * 2 ^ -mantissa_bits) +/// x = sign * (2 ^ normalized_exponent + mantissa * 2 ^ (normalized_exponent - mantissa_bits)) +template <typename T> +struct DecomposedFloat { + using Traits = FloatTraits<T>; + + explicit DecomposedFloat(T x) { memcpy(&x_uint, &x, sizeof(x)); } + + typename Traits::UInt x_uint; + + bool isNegative() const { return x_uint >> (Traits::bits - 1); } + + /// Returns 0 for both +0. and -0. + int sign() const { return (exponent() == 0 && mantissa() == 0) ? 0 : (isNegative() ? -1 : 1); } + + uint16_t exponent() const { + return (x_uint >> (Traits::mantissa_bits)) & + (((1ull << (Traits::exponent_bits + 1)) - 1) >> 1); + } + + int16_t normalizedExponent() const { + return int16_t(exponent()) - ((1ull << (Traits::exponent_bits - 1)) - 1); + } + + uint64_t mantissa() const { return x_uint & ((1ull << Traits::mantissa_bits) - 1); } + + int64_t mantissaWithSign() const { return isNegative() ? -mantissa() : mantissa(); } + + /// NOTE Probably floating point instructions can be better. + bool isIntegerInRepresentableRange() const { + return x_uint == 0 || + (normalizedExponent() >= 0 /// The number is not less than one + /// The number is inside the range where every integer has exact representation in float + && normalizedExponent() <= static_cast<int16_t>(Traits::mantissa_bits) + /// After multiplying by 2^exp, the fractional part becomes zero, means the number is integer + && ((mantissa() & ((1ULL << (Traits::mantissa_bits - normalizedExponent())) - 1)) == + 0)); + } + + /// Compare float with integer of arbitrary width (both signed and unsigned are supported). Assuming two's complement arithmetic. + /// This function is generic, big integers (128, 256 bit) are supported as well. + /// Infinities are compared correctly. NaNs are treat similarly to infinities, so they can be less than all numbers. + /// (note that we need total order) + /// Returns -1, 0 or 1. + template <typename Int> + int compare(Int rhs) const { + if (rhs == 0) return sign(); + + /// Different signs + if (isNegative() && rhs > 0) return -1; + if (!isNegative() && rhs < 0) return 1; + + /// Fractional number with magnitude less than one + if (normalizedExponent() < 0) { + if (!isNegative()) + return rhs > 0 ? -1 : 1; + else + return rhs >= 0 ? -1 : 1; + } + + /// The case of the most negative integer + if constexpr (is_signed_v<Int>) { + if (rhs == std::numeric_limits<Int>::lowest()) { + assert(isNegative()); + + if (normalizedExponent() < static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) + return 1; + if (normalizedExponent() > static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) + return -1; + + if (mantissa() == 0) + return 0; + else + return -1; + } + } + + /// Too large number: abs(float) > abs(rhs). Also the case with infinities and NaN. + if (normalizedExponent() >= static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) + return isNegative() ? -1 : 1; + + using UInt = std::conditional_t<(sizeof(Int) > sizeof(typename Traits::UInt)), + make_unsigned_t<Int>, typename Traits::UInt>; + UInt uint_rhs = rhs < 0 ? -rhs : rhs; + + /// Smaller octave: abs(rhs) < abs(float) + /// FYI, TIL: octave is also called "binade", https://en.wikipedia.org/wiki/Binade + if (uint_rhs < (static_cast<UInt>(1) << normalizedExponent())) return isNegative() ? -1 : 1; Review Comment: warning: statement should be inside braces [readability-braces-around-statements] ```suggestion if (uint_rhs < (static_cast<UInt>(1) << normalizedExponent())) { return isNegative() ? -1 : 1; } ``` ########## be/src/vec/core/decomposed_float.h: ########## @@ -0,0 +1,201 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// This file is copied from +// https://github.com/ClickHouse/ClickHouse/blob/master/base/base/DecomposedFloat.h +// and modified by Doris +#pragma once + +#include <cstddef> +#include <cstdint> +#include <cstring> + +#include "extended_types.h" + +/// Allows to check the internals of IEEE-754 floating point number. + +template <typename T> +struct FloatTraits; + +template <> +struct FloatTraits<float> { + using UInt = uint32_t; + static constexpr size_t bits = 32; + static constexpr size_t exponent_bits = 8; + static constexpr size_t mantissa_bits = bits - exponent_bits - 1; +}; + +template <> +struct FloatTraits<double> { + using UInt = uint64_t; + static constexpr size_t bits = 64; + static constexpr size_t exponent_bits = 11; + static constexpr size_t mantissa_bits = bits - exponent_bits - 1; +}; + +/// x = sign * (2 ^ normalized_exponent) * (1 + mantissa * 2 ^ -mantissa_bits) +/// x = sign * (2 ^ normalized_exponent + mantissa * 2 ^ (normalized_exponent - mantissa_bits)) +template <typename T> +struct DecomposedFloat { + using Traits = FloatTraits<T>; + + explicit DecomposedFloat(T x) { memcpy(&x_uint, &x, sizeof(x)); } + + typename Traits::UInt x_uint; + + bool isNegative() const { return x_uint >> (Traits::bits - 1); } + + /// Returns 0 for both +0. and -0. + int sign() const { return (exponent() == 0 && mantissa() == 0) ? 0 : (isNegative() ? -1 : 1); } + + uint16_t exponent() const { + return (x_uint >> (Traits::mantissa_bits)) & + (((1ull << (Traits::exponent_bits + 1)) - 1) >> 1); + } + + int16_t normalizedExponent() const { + return int16_t(exponent()) - ((1ull << (Traits::exponent_bits - 1)) - 1); + } + + uint64_t mantissa() const { return x_uint & ((1ull << Traits::mantissa_bits) - 1); } + + int64_t mantissaWithSign() const { return isNegative() ? -mantissa() : mantissa(); } + + /// NOTE Probably floating point instructions can be better. + bool isIntegerInRepresentableRange() const { + return x_uint == 0 || + (normalizedExponent() >= 0 /// The number is not less than one + /// The number is inside the range where every integer has exact representation in float + && normalizedExponent() <= static_cast<int16_t>(Traits::mantissa_bits) + /// After multiplying by 2^exp, the fractional part becomes zero, means the number is integer + && ((mantissa() & ((1ULL << (Traits::mantissa_bits - normalizedExponent())) - 1)) == + 0)); + } + + /// Compare float with integer of arbitrary width (both signed and unsigned are supported). Assuming two's complement arithmetic. + /// This function is generic, big integers (128, 256 bit) are supported as well. + /// Infinities are compared correctly. NaNs are treat similarly to infinities, so they can be less than all numbers. + /// (note that we need total order) + /// Returns -1, 0 or 1. + template <typename Int> + int compare(Int rhs) const { + if (rhs == 0) return sign(); + + /// Different signs + if (isNegative() && rhs > 0) return -1; + if (!isNegative() && rhs < 0) return 1; + + /// Fractional number with magnitude less than one + if (normalizedExponent() < 0) { + if (!isNegative()) + return rhs > 0 ? -1 : 1; + else + return rhs >= 0 ? -1 : 1; + } + + /// The case of the most negative integer + if constexpr (is_signed_v<Int>) { + if (rhs == std::numeric_limits<Int>::lowest()) { + assert(isNegative()); + + if (normalizedExponent() < static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) + return 1; + if (normalizedExponent() > static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) + return -1; + + if (mantissa() == 0) + return 0; + else + return -1; + } + } + + /// Too large number: abs(float) > abs(rhs). Also the case with infinities and NaN. + if (normalizedExponent() >= static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) Review Comment: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers] ```cpp if (normalizedExponent() >= static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) ^ ``` ########## be/src/vec/core/decomposed_float.h: ########## @@ -0,0 +1,201 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// This file is copied from +// https://github.com/ClickHouse/ClickHouse/blob/master/base/base/DecomposedFloat.h +// and modified by Doris +#pragma once + +#include <cstddef> +#include <cstdint> +#include <cstring> + +#include "extended_types.h" + +/// Allows to check the internals of IEEE-754 floating point number. + +template <typename T> +struct FloatTraits; + +template <> +struct FloatTraits<float> { + using UInt = uint32_t; + static constexpr size_t bits = 32; + static constexpr size_t exponent_bits = 8; + static constexpr size_t mantissa_bits = bits - exponent_bits - 1; +}; + +template <> +struct FloatTraits<double> { + using UInt = uint64_t; + static constexpr size_t bits = 64; + static constexpr size_t exponent_bits = 11; + static constexpr size_t mantissa_bits = bits - exponent_bits - 1; +}; + +/// x = sign * (2 ^ normalized_exponent) * (1 + mantissa * 2 ^ -mantissa_bits) +/// x = sign * (2 ^ normalized_exponent + mantissa * 2 ^ (normalized_exponent - mantissa_bits)) +template <typename T> +struct DecomposedFloat { + using Traits = FloatTraits<T>; + + explicit DecomposedFloat(T x) { memcpy(&x_uint, &x, sizeof(x)); } + + typename Traits::UInt x_uint; + + bool isNegative() const { return x_uint >> (Traits::bits - 1); } + + /// Returns 0 for both +0. and -0. + int sign() const { return (exponent() == 0 && mantissa() == 0) ? 0 : (isNegative() ? -1 : 1); } + + uint16_t exponent() const { + return (x_uint >> (Traits::mantissa_bits)) & + (((1ull << (Traits::exponent_bits + 1)) - 1) >> 1); + } + + int16_t normalizedExponent() const { + return int16_t(exponent()) - ((1ull << (Traits::exponent_bits - 1)) - 1); + } + + uint64_t mantissa() const { return x_uint & ((1ull << Traits::mantissa_bits) - 1); } + + int64_t mantissaWithSign() const { return isNegative() ? -mantissa() : mantissa(); } + + /// NOTE Probably floating point instructions can be better. + bool isIntegerInRepresentableRange() const { + return x_uint == 0 || + (normalizedExponent() >= 0 /// The number is not less than one + /// The number is inside the range where every integer has exact representation in float + && normalizedExponent() <= static_cast<int16_t>(Traits::mantissa_bits) + /// After multiplying by 2^exp, the fractional part becomes zero, means the number is integer + && ((mantissa() & ((1ULL << (Traits::mantissa_bits - normalizedExponent())) - 1)) == + 0)); + } + + /// Compare float with integer of arbitrary width (both signed and unsigned are supported). Assuming two's complement arithmetic. + /// This function is generic, big integers (128, 256 bit) are supported as well. + /// Infinities are compared correctly. NaNs are treat similarly to infinities, so they can be less than all numbers. + /// (note that we need total order) + /// Returns -1, 0 or 1. + template <typename Int> + int compare(Int rhs) const { + if (rhs == 0) return sign(); + + /// Different signs + if (isNegative() && rhs > 0) return -1; + if (!isNegative() && rhs < 0) return 1; + + /// Fractional number with magnitude less than one + if (normalizedExponent() < 0) { + if (!isNegative()) + return rhs > 0 ? -1 : 1; + else + return rhs >= 0 ? -1 : 1; + } + + /// The case of the most negative integer + if constexpr (is_signed_v<Int>) { + if (rhs == std::numeric_limits<Int>::lowest()) { + assert(isNegative()); + + if (normalizedExponent() < static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) + return 1; + if (normalizedExponent() > static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) + return -1; + + if (mantissa() == 0) + return 0; + else + return -1; + } + } + + /// Too large number: abs(float) > abs(rhs). Also the case with infinities and NaN. + if (normalizedExponent() >= static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) + return isNegative() ? -1 : 1; Review Comment: warning: statement should be inside braces [readability-braces-around-statements] ```suggestion if (normalizedExponent() >= static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) { return isNegative() ? -1 : 1; } ``` ########## be/src/vec/core/decomposed_float.h: ########## @@ -0,0 +1,201 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// This file is copied from +// https://github.com/ClickHouse/ClickHouse/blob/master/base/base/DecomposedFloat.h +// and modified by Doris +#pragma once + +#include <cstddef> +#include <cstdint> +#include <cstring> + +#include "extended_types.h" + +/// Allows to check the internals of IEEE-754 floating point number. + +template <typename T> +struct FloatTraits; + +template <> +struct FloatTraits<float> { + using UInt = uint32_t; + static constexpr size_t bits = 32; + static constexpr size_t exponent_bits = 8; + static constexpr size_t mantissa_bits = bits - exponent_bits - 1; +}; + +template <> +struct FloatTraits<double> { + using UInt = uint64_t; + static constexpr size_t bits = 64; + static constexpr size_t exponent_bits = 11; + static constexpr size_t mantissa_bits = bits - exponent_bits - 1; +}; + +/// x = sign * (2 ^ normalized_exponent) * (1 + mantissa * 2 ^ -mantissa_bits) +/// x = sign * (2 ^ normalized_exponent + mantissa * 2 ^ (normalized_exponent - mantissa_bits)) +template <typename T> +struct DecomposedFloat { + using Traits = FloatTraits<T>; + + explicit DecomposedFloat(T x) { memcpy(&x_uint, &x, sizeof(x)); } + + typename Traits::UInt x_uint; + + bool isNegative() const { return x_uint >> (Traits::bits - 1); } + + /// Returns 0 for both +0. and -0. + int sign() const { return (exponent() == 0 && mantissa() == 0) ? 0 : (isNegative() ? -1 : 1); } + + uint16_t exponent() const { + return (x_uint >> (Traits::mantissa_bits)) & + (((1ull << (Traits::exponent_bits + 1)) - 1) >> 1); + } + + int16_t normalizedExponent() const { + return int16_t(exponent()) - ((1ull << (Traits::exponent_bits - 1)) - 1); + } + + uint64_t mantissa() const { return x_uint & ((1ull << Traits::mantissa_bits) - 1); } + + int64_t mantissaWithSign() const { return isNegative() ? -mantissa() : mantissa(); } + + /// NOTE Probably floating point instructions can be better. + bool isIntegerInRepresentableRange() const { + return x_uint == 0 || + (normalizedExponent() >= 0 /// The number is not less than one + /// The number is inside the range where every integer has exact representation in float + && normalizedExponent() <= static_cast<int16_t>(Traits::mantissa_bits) + /// After multiplying by 2^exp, the fractional part becomes zero, means the number is integer + && ((mantissa() & ((1ULL << (Traits::mantissa_bits - normalizedExponent())) - 1)) == + 0)); + } + + /// Compare float with integer of arbitrary width (both signed and unsigned are supported). Assuming two's complement arithmetic. + /// This function is generic, big integers (128, 256 bit) are supported as well. + /// Infinities are compared correctly. NaNs are treat similarly to infinities, so they can be less than all numbers. + /// (note that we need total order) + /// Returns -1, 0 or 1. + template <typename Int> + int compare(Int rhs) const { + if (rhs == 0) return sign(); + + /// Different signs + if (isNegative() && rhs > 0) return -1; + if (!isNegative() && rhs < 0) return 1; + + /// Fractional number with magnitude less than one + if (normalizedExponent() < 0) { + if (!isNegative()) + return rhs > 0 ? -1 : 1; + else + return rhs >= 0 ? -1 : 1; + } + + /// The case of the most negative integer + if constexpr (is_signed_v<Int>) { + if (rhs == std::numeric_limits<Int>::lowest()) { + assert(isNegative()); + + if (normalizedExponent() < static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) + return 1; + if (normalizedExponent() > static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) + return -1; + + if (mantissa() == 0) + return 0; + else + return -1; + } + } + + /// Too large number: abs(float) > abs(rhs). Also the case with infinities and NaN. + if (normalizedExponent() >= static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) + return isNegative() ? -1 : 1; + + using UInt = std::conditional_t<(sizeof(Int) > sizeof(typename Traits::UInt)), + make_unsigned_t<Int>, typename Traits::UInt>; + UInt uint_rhs = rhs < 0 ? -rhs : rhs; + + /// Smaller octave: abs(rhs) < abs(float) + /// FYI, TIL: octave is also called "binade", https://en.wikipedia.org/wiki/Binade + if (uint_rhs < (static_cast<UInt>(1) << normalizedExponent())) return isNegative() ? -1 : 1; + + /// Larger octave: abs(rhs) > abs(float) + if (normalizedExponent() + 1 < static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>) && + uint_rhs >= (static_cast<UInt>(1) << (normalizedExponent() + 1))) + return isNegative() ? 1 : -1; + + /// The same octave + /// uint_rhs == 2 ^ normalizedExponent + mantissa * 2 ^ (normalizedExponent - mantissa_bits) + + bool large_and_always_integer = + normalizedExponent() >= static_cast<int16_t>(Traits::mantissa_bits); + + UInt a = large_and_always_integer + ? static_cast<UInt>(mantissa()) + << (normalizedExponent() - Traits::mantissa_bits) + : static_cast<UInt>(mantissa()) >> + (Traits::mantissa_bits - normalizedExponent()); + + UInt b = uint_rhs - (static_cast<UInt>(1) << normalizedExponent()); + + if (a < b) return isNegative() ? 1 : -1; Review Comment: warning: statement should be inside braces [readability-braces-around-statements] ```suggestion if (a < b) { return isNegative() ? 1 : -1; } ``` ########## be/src/vec/core/decomposed_float.h: ########## @@ -0,0 +1,201 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// This file is copied from +// https://github.com/ClickHouse/ClickHouse/blob/master/base/base/DecomposedFloat.h +// and modified by Doris +#pragma once + +#include <cstddef> +#include <cstdint> +#include <cstring> + +#include "extended_types.h" + +/// Allows to check the internals of IEEE-754 floating point number. + +template <typename T> +struct FloatTraits; + +template <> +struct FloatTraits<float> { + using UInt = uint32_t; + static constexpr size_t bits = 32; + static constexpr size_t exponent_bits = 8; + static constexpr size_t mantissa_bits = bits - exponent_bits - 1; +}; + +template <> +struct FloatTraits<double> { + using UInt = uint64_t; + static constexpr size_t bits = 64; + static constexpr size_t exponent_bits = 11; + static constexpr size_t mantissa_bits = bits - exponent_bits - 1; +}; + +/// x = sign * (2 ^ normalized_exponent) * (1 + mantissa * 2 ^ -mantissa_bits) +/// x = sign * (2 ^ normalized_exponent + mantissa * 2 ^ (normalized_exponent - mantissa_bits)) +template <typename T> +struct DecomposedFloat { + using Traits = FloatTraits<T>; + + explicit DecomposedFloat(T x) { memcpy(&x_uint, &x, sizeof(x)); } + + typename Traits::UInt x_uint; + + bool isNegative() const { return x_uint >> (Traits::bits - 1); } + + /// Returns 0 for both +0. and -0. + int sign() const { return (exponent() == 0 && mantissa() == 0) ? 0 : (isNegative() ? -1 : 1); } + + uint16_t exponent() const { + return (x_uint >> (Traits::mantissa_bits)) & + (((1ull << (Traits::exponent_bits + 1)) - 1) >> 1); + } + + int16_t normalizedExponent() const { + return int16_t(exponent()) - ((1ull << (Traits::exponent_bits - 1)) - 1); + } + + uint64_t mantissa() const { return x_uint & ((1ull << Traits::mantissa_bits) - 1); } + + int64_t mantissaWithSign() const { return isNegative() ? -mantissa() : mantissa(); } + + /// NOTE Probably floating point instructions can be better. + bool isIntegerInRepresentableRange() const { + return x_uint == 0 || + (normalizedExponent() >= 0 /// The number is not less than one + /// The number is inside the range where every integer has exact representation in float + && normalizedExponent() <= static_cast<int16_t>(Traits::mantissa_bits) + /// After multiplying by 2^exp, the fractional part becomes zero, means the number is integer + && ((mantissa() & ((1ULL << (Traits::mantissa_bits - normalizedExponent())) - 1)) == + 0)); + } + + /// Compare float with integer of arbitrary width (both signed and unsigned are supported). Assuming two's complement arithmetic. + /// This function is generic, big integers (128, 256 bit) are supported as well. + /// Infinities are compared correctly. NaNs are treat similarly to infinities, so they can be less than all numbers. + /// (note that we need total order) + /// Returns -1, 0 or 1. + template <typename Int> + int compare(Int rhs) const { + if (rhs == 0) return sign(); + + /// Different signs + if (isNegative() && rhs > 0) return -1; + if (!isNegative() && rhs < 0) return 1; + + /// Fractional number with magnitude less than one + if (normalizedExponent() < 0) { + if (!isNegative()) + return rhs > 0 ? -1 : 1; + else + return rhs >= 0 ? -1 : 1; + } + + /// The case of the most negative integer + if constexpr (is_signed_v<Int>) { + if (rhs == std::numeric_limits<Int>::lowest()) { + assert(isNegative()); + + if (normalizedExponent() < static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) + return 1; + if (normalizedExponent() > static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) + return -1; + + if (mantissa() == 0) + return 0; + else + return -1; + } + } + + /// Too large number: abs(float) > abs(rhs). Also the case with infinities and NaN. + if (normalizedExponent() >= static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) + return isNegative() ? -1 : 1; + + using UInt = std::conditional_t<(sizeof(Int) > sizeof(typename Traits::UInt)), + make_unsigned_t<Int>, typename Traits::UInt>; + UInt uint_rhs = rhs < 0 ? -rhs : rhs; + + /// Smaller octave: abs(rhs) < abs(float) + /// FYI, TIL: octave is also called "binade", https://en.wikipedia.org/wiki/Binade + if (uint_rhs < (static_cast<UInt>(1) << normalizedExponent())) return isNegative() ? -1 : 1; + + /// Larger octave: abs(rhs) > abs(float) + if (normalizedExponent() + 1 < static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>) && + uint_rhs >= (static_cast<UInt>(1) << (normalizedExponent() + 1))) + return isNegative() ? 1 : -1; Review Comment: warning: statement should be inside braces [readability-braces-around-statements] ```suggestion uint_rhs >= (static_cast<UInt>(1) << (normalizedExponent() + 1))) { return isNegative() ? 1 : -1; } ``` ########## be/src/vec/core/decomposed_float.h: ########## @@ -0,0 +1,201 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// This file is copied from +// https://github.com/ClickHouse/ClickHouse/blob/master/base/base/DecomposedFloat.h +// and modified by Doris +#pragma once + +#include <cstddef> +#include <cstdint> +#include <cstring> + +#include "extended_types.h" + +/// Allows to check the internals of IEEE-754 floating point number. + +template <typename T> +struct FloatTraits; + +template <> +struct FloatTraits<float> { + using UInt = uint32_t; + static constexpr size_t bits = 32; + static constexpr size_t exponent_bits = 8; + static constexpr size_t mantissa_bits = bits - exponent_bits - 1; +}; + +template <> +struct FloatTraits<double> { + using UInt = uint64_t; + static constexpr size_t bits = 64; + static constexpr size_t exponent_bits = 11; + static constexpr size_t mantissa_bits = bits - exponent_bits - 1; +}; + +/// x = sign * (2 ^ normalized_exponent) * (1 + mantissa * 2 ^ -mantissa_bits) +/// x = sign * (2 ^ normalized_exponent + mantissa * 2 ^ (normalized_exponent - mantissa_bits)) +template <typename T> +struct DecomposedFloat { + using Traits = FloatTraits<T>; + + explicit DecomposedFloat(T x) { memcpy(&x_uint, &x, sizeof(x)); } + + typename Traits::UInt x_uint; + + bool isNegative() const { return x_uint >> (Traits::bits - 1); } + + /// Returns 0 for both +0. and -0. + int sign() const { return (exponent() == 0 && mantissa() == 0) ? 0 : (isNegative() ? -1 : 1); } + + uint16_t exponent() const { + return (x_uint >> (Traits::mantissa_bits)) & + (((1ull << (Traits::exponent_bits + 1)) - 1) >> 1); + } + + int16_t normalizedExponent() const { + return int16_t(exponent()) - ((1ull << (Traits::exponent_bits - 1)) - 1); + } + + uint64_t mantissa() const { return x_uint & ((1ull << Traits::mantissa_bits) - 1); } + + int64_t mantissaWithSign() const { return isNegative() ? -mantissa() : mantissa(); } + + /// NOTE Probably floating point instructions can be better. + bool isIntegerInRepresentableRange() const { + return x_uint == 0 || + (normalizedExponent() >= 0 /// The number is not less than one + /// The number is inside the range where every integer has exact representation in float + && normalizedExponent() <= static_cast<int16_t>(Traits::mantissa_bits) + /// After multiplying by 2^exp, the fractional part becomes zero, means the number is integer + && ((mantissa() & ((1ULL << (Traits::mantissa_bits - normalizedExponent())) - 1)) == + 0)); + } + + /// Compare float with integer of arbitrary width (both signed and unsigned are supported). Assuming two's complement arithmetic. + /// This function is generic, big integers (128, 256 bit) are supported as well. + /// Infinities are compared correctly. NaNs are treat similarly to infinities, so they can be less than all numbers. + /// (note that we need total order) + /// Returns -1, 0 or 1. + template <typename Int> + int compare(Int rhs) const { + if (rhs == 0) return sign(); + + /// Different signs + if (isNegative() && rhs > 0) return -1; + if (!isNegative() && rhs < 0) return 1; + + /// Fractional number with magnitude less than one + if (normalizedExponent() < 0) { + if (!isNegative()) + return rhs > 0 ? -1 : 1; + else + return rhs >= 0 ? -1 : 1; + } + + /// The case of the most negative integer + if constexpr (is_signed_v<Int>) { + if (rhs == std::numeric_limits<Int>::lowest()) { + assert(isNegative()); + + if (normalizedExponent() < static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) + return 1; + if (normalizedExponent() > static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) + return -1; + + if (mantissa() == 0) + return 0; + else + return -1; + } + } + + /// Too large number: abs(float) > abs(rhs). Also the case with infinities and NaN. + if (normalizedExponent() >= static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) + return isNegative() ? -1 : 1; + + using UInt = std::conditional_t<(sizeof(Int) > sizeof(typename Traits::UInt)), + make_unsigned_t<Int>, typename Traits::UInt>; + UInt uint_rhs = rhs < 0 ? -rhs : rhs; + + /// Smaller octave: abs(rhs) < abs(float) + /// FYI, TIL: octave is also called "binade", https://en.wikipedia.org/wiki/Binade + if (uint_rhs < (static_cast<UInt>(1) << normalizedExponent())) return isNegative() ? -1 : 1; + + /// Larger octave: abs(rhs) > abs(float) + if (normalizedExponent() + 1 < static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>) && + uint_rhs >= (static_cast<UInt>(1) << (normalizedExponent() + 1))) + return isNegative() ? 1 : -1; + + /// The same octave + /// uint_rhs == 2 ^ normalizedExponent + mantissa * 2 ^ (normalizedExponent - mantissa_bits) + + bool large_and_always_integer = + normalizedExponent() >= static_cast<int16_t>(Traits::mantissa_bits); + + UInt a = large_and_always_integer + ? static_cast<UInt>(mantissa()) + << (normalizedExponent() - Traits::mantissa_bits) + : static_cast<UInt>(mantissa()) >> + (Traits::mantissa_bits - normalizedExponent()); + + UInt b = uint_rhs - (static_cast<UInt>(1) << normalizedExponent()); + + if (a < b) return isNegative() ? 1 : -1; + if (a > b) return isNegative() ? -1 : 1; + + /// Float has no fractional part means that the numbers are equal. + if (large_and_always_integer || + (mantissa() & ((1ULL << (Traits::mantissa_bits - normalizedExponent())) - 1)) == 0) Review Comment: warning: statement should be inside braces [readability-braces-around-statements] ```suggestion (mantissa() & ((1ULL << (Traits::mantissa_bits - normalizedExponent())) - 1)) == 0) { ``` be/src/vec/core/decomposed_float.h:163: ```diff - else + } else ``` ########## be/src/vec/core/decomposed_float.h: ########## @@ -0,0 +1,201 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// This file is copied from +// https://github.com/ClickHouse/ClickHouse/blob/master/base/base/DecomposedFloat.h +// and modified by Doris +#pragma once + +#include <cstddef> +#include <cstdint> +#include <cstring> + +#include "extended_types.h" + +/// Allows to check the internals of IEEE-754 floating point number. + +template <typename T> +struct FloatTraits; + +template <> +struct FloatTraits<float> { + using UInt = uint32_t; + static constexpr size_t bits = 32; + static constexpr size_t exponent_bits = 8; + static constexpr size_t mantissa_bits = bits - exponent_bits - 1; +}; + +template <> +struct FloatTraits<double> { + using UInt = uint64_t; + static constexpr size_t bits = 64; + static constexpr size_t exponent_bits = 11; + static constexpr size_t mantissa_bits = bits - exponent_bits - 1; +}; + +/// x = sign * (2 ^ normalized_exponent) * (1 + mantissa * 2 ^ -mantissa_bits) +/// x = sign * (2 ^ normalized_exponent + mantissa * 2 ^ (normalized_exponent - mantissa_bits)) +template <typename T> +struct DecomposedFloat { + using Traits = FloatTraits<T>; + + explicit DecomposedFloat(T x) { memcpy(&x_uint, &x, sizeof(x)); } + + typename Traits::UInt x_uint; + + bool isNegative() const { return x_uint >> (Traits::bits - 1); } + + /// Returns 0 for both +0. and -0. + int sign() const { return (exponent() == 0 && mantissa() == 0) ? 0 : (isNegative() ? -1 : 1); } + + uint16_t exponent() const { + return (x_uint >> (Traits::mantissa_bits)) & + (((1ull << (Traits::exponent_bits + 1)) - 1) >> 1); + } + + int16_t normalizedExponent() const { + return int16_t(exponent()) - ((1ull << (Traits::exponent_bits - 1)) - 1); + } + + uint64_t mantissa() const { return x_uint & ((1ull << Traits::mantissa_bits) - 1); } + + int64_t mantissaWithSign() const { return isNegative() ? -mantissa() : mantissa(); } + + /// NOTE Probably floating point instructions can be better. + bool isIntegerInRepresentableRange() const { + return x_uint == 0 || + (normalizedExponent() >= 0 /// The number is not less than one + /// The number is inside the range where every integer has exact representation in float + && normalizedExponent() <= static_cast<int16_t>(Traits::mantissa_bits) + /// After multiplying by 2^exp, the fractional part becomes zero, means the number is integer + && ((mantissa() & ((1ULL << (Traits::mantissa_bits - normalizedExponent())) - 1)) == + 0)); + } + + /// Compare float with integer of arbitrary width (both signed and unsigned are supported). Assuming two's complement arithmetic. + /// This function is generic, big integers (128, 256 bit) are supported as well. + /// Infinities are compared correctly. NaNs are treat similarly to infinities, so they can be less than all numbers. + /// (note that we need total order) + /// Returns -1, 0 or 1. + template <typename Int> + int compare(Int rhs) const { + if (rhs == 0) return sign(); + + /// Different signs + if (isNegative() && rhs > 0) return -1; + if (!isNegative() && rhs < 0) return 1; + + /// Fractional number with magnitude less than one + if (normalizedExponent() < 0) { + if (!isNegative()) + return rhs > 0 ? -1 : 1; + else + return rhs >= 0 ? -1 : 1; + } + + /// The case of the most negative integer + if constexpr (is_signed_v<Int>) { + if (rhs == std::numeric_limits<Int>::lowest()) { + assert(isNegative()); + + if (normalizedExponent() < static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) + return 1; + if (normalizedExponent() > static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) + return -1; + + if (mantissa() == 0) + return 0; + else + return -1; + } + } + + /// Too large number: abs(float) > abs(rhs). Also the case with infinities and NaN. + if (normalizedExponent() >= static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) + return isNegative() ? -1 : 1; + + using UInt = std::conditional_t<(sizeof(Int) > sizeof(typename Traits::UInt)), + make_unsigned_t<Int>, typename Traits::UInt>; + UInt uint_rhs = rhs < 0 ? -rhs : rhs; + + /// Smaller octave: abs(rhs) < abs(float) + /// FYI, TIL: octave is also called "binade", https://en.wikipedia.org/wiki/Binade + if (uint_rhs < (static_cast<UInt>(1) << normalizedExponent())) return isNegative() ? -1 : 1; + + /// Larger octave: abs(rhs) > abs(float) + if (normalizedExponent() + 1 < static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>) && + uint_rhs >= (static_cast<UInt>(1) << (normalizedExponent() + 1))) + return isNegative() ? 1 : -1; + + /// The same octave + /// uint_rhs == 2 ^ normalizedExponent + mantissa * 2 ^ (normalizedExponent - mantissa_bits) + + bool large_and_always_integer = + normalizedExponent() >= static_cast<int16_t>(Traits::mantissa_bits); + + UInt a = large_and_always_integer + ? static_cast<UInt>(mantissa()) + << (normalizedExponent() - Traits::mantissa_bits) + : static_cast<UInt>(mantissa()) >> + (Traits::mantissa_bits - normalizedExponent()); + + UInt b = uint_rhs - (static_cast<UInt>(1) << normalizedExponent()); + + if (a < b) return isNegative() ? 1 : -1; + if (a > b) return isNegative() ? -1 : 1; + + /// Float has no fractional part means that the numbers are equal. + if (large_and_always_integer || + (mantissa() & ((1ULL << (Traits::mantissa_bits - normalizedExponent())) - 1)) == 0) + return 0; + else + /// Float has fractional part means its abs value is larger. + return isNegative() ? -1 : 1; Review Comment: warning: do not use 'else' after 'return' [readability-else-after-return] ```suggestion /// Float has fractional part means its abs value is larger. return isNegative() ? -1 : 1; ``` ########## be/src/vec/core/decomposed_float.h: ########## @@ -0,0 +1,201 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// This file is copied from +// https://github.com/ClickHouse/ClickHouse/blob/master/base/base/DecomposedFloat.h +// and modified by Doris +#pragma once + +#include <cstddef> +#include <cstdint> +#include <cstring> + +#include "extended_types.h" + +/// Allows to check the internals of IEEE-754 floating point number. + +template <typename T> +struct FloatTraits; + +template <> +struct FloatTraits<float> { + using UInt = uint32_t; + static constexpr size_t bits = 32; + static constexpr size_t exponent_bits = 8; + static constexpr size_t mantissa_bits = bits - exponent_bits - 1; +}; + +template <> +struct FloatTraits<double> { + using UInt = uint64_t; + static constexpr size_t bits = 64; + static constexpr size_t exponent_bits = 11; + static constexpr size_t mantissa_bits = bits - exponent_bits - 1; +}; + +/// x = sign * (2 ^ normalized_exponent) * (1 + mantissa * 2 ^ -mantissa_bits) +/// x = sign * (2 ^ normalized_exponent + mantissa * 2 ^ (normalized_exponent - mantissa_bits)) +template <typename T> +struct DecomposedFloat { + using Traits = FloatTraits<T>; + + explicit DecomposedFloat(T x) { memcpy(&x_uint, &x, sizeof(x)); } + + typename Traits::UInt x_uint; + + bool isNegative() const { return x_uint >> (Traits::bits - 1); } + + /// Returns 0 for both +0. and -0. + int sign() const { return (exponent() == 0 && mantissa() == 0) ? 0 : (isNegative() ? -1 : 1); } + + uint16_t exponent() const { + return (x_uint >> (Traits::mantissa_bits)) & + (((1ull << (Traits::exponent_bits + 1)) - 1) >> 1); + } + + int16_t normalizedExponent() const { + return int16_t(exponent()) - ((1ull << (Traits::exponent_bits - 1)) - 1); + } + + uint64_t mantissa() const { return x_uint & ((1ull << Traits::mantissa_bits) - 1); } + + int64_t mantissaWithSign() const { return isNegative() ? -mantissa() : mantissa(); } + + /// NOTE Probably floating point instructions can be better. + bool isIntegerInRepresentableRange() const { + return x_uint == 0 || + (normalizedExponent() >= 0 /// The number is not less than one + /// The number is inside the range where every integer has exact representation in float + && normalizedExponent() <= static_cast<int16_t>(Traits::mantissa_bits) + /// After multiplying by 2^exp, the fractional part becomes zero, means the number is integer + && ((mantissa() & ((1ULL << (Traits::mantissa_bits - normalizedExponent())) - 1)) == + 0)); + } + + /// Compare float with integer of arbitrary width (both signed and unsigned are supported). Assuming two's complement arithmetic. + /// This function is generic, big integers (128, 256 bit) are supported as well. + /// Infinities are compared correctly. NaNs are treat similarly to infinities, so they can be less than all numbers. + /// (note that we need total order) + /// Returns -1, 0 or 1. + template <typename Int> + int compare(Int rhs) const { + if (rhs == 0) return sign(); + + /// Different signs + if (isNegative() && rhs > 0) return -1; + if (!isNegative() && rhs < 0) return 1; + + /// Fractional number with magnitude less than one + if (normalizedExponent() < 0) { + if (!isNegative()) + return rhs > 0 ? -1 : 1; + else + return rhs >= 0 ? -1 : 1; + } + + /// The case of the most negative integer + if constexpr (is_signed_v<Int>) { + if (rhs == std::numeric_limits<Int>::lowest()) { + assert(isNegative()); + + if (normalizedExponent() < static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) + return 1; + if (normalizedExponent() > static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) + return -1; + + if (mantissa() == 0) + return 0; + else + return -1; + } + } + + /// Too large number: abs(float) > abs(rhs). Also the case with infinities and NaN. + if (normalizedExponent() >= static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) + return isNegative() ? -1 : 1; + + using UInt = std::conditional_t<(sizeof(Int) > sizeof(typename Traits::UInt)), + make_unsigned_t<Int>, typename Traits::UInt>; + UInt uint_rhs = rhs < 0 ? -rhs : rhs; + + /// Smaller octave: abs(rhs) < abs(float) + /// FYI, TIL: octave is also called "binade", https://en.wikipedia.org/wiki/Binade + if (uint_rhs < (static_cast<UInt>(1) << normalizedExponent())) return isNegative() ? -1 : 1; + + /// Larger octave: abs(rhs) > abs(float) + if (normalizedExponent() + 1 < static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>) && + uint_rhs >= (static_cast<UInt>(1) << (normalizedExponent() + 1))) + return isNegative() ? 1 : -1; + + /// The same octave + /// uint_rhs == 2 ^ normalizedExponent + mantissa * 2 ^ (normalizedExponent - mantissa_bits) + + bool large_and_always_integer = + normalizedExponent() >= static_cast<int16_t>(Traits::mantissa_bits); + + UInt a = large_and_always_integer + ? static_cast<UInt>(mantissa()) + << (normalizedExponent() - Traits::mantissa_bits) + : static_cast<UInt>(mantissa()) >> + (Traits::mantissa_bits - normalizedExponent()); + + UInt b = uint_rhs - (static_cast<UInt>(1) << normalizedExponent()); + + if (a < b) return isNegative() ? 1 : -1; + if (a > b) return isNegative() ? -1 : 1; + + /// Float has no fractional part means that the numbers are equal. + if (large_and_always_integer || + (mantissa() & ((1ULL << (Traits::mantissa_bits - normalizedExponent())) - 1)) == 0) + return 0; + else Review Comment: warning: statement should be inside braces [readability-braces-around-statements] ```cpp else ^ ``` this fix will not be applied because it overlaps with another fix ########## be/src/vec/core/types.h: ########## @@ -425,14 +465,20 @@ struct Decimal { whole_part = abs_value / decimal_scale_multiplier<T>(scale); frac_part = abs_value % decimal_scale_multiplier<T>(scale); } - auto end = fmt::format_to(str.data() + pos, "{}", whole_part); - pos = end - str.data(); + if constexpr (std::is_same_v<T, Int256>) { + std::string num_str {wide::to_string(whole_part)}; + auto end = fmt::format_to(str.data() + pos, "{}", num_str); + pos = end - str.data(); + } else { + auto end = fmt::format_to(str.data() + pos, "{}", whole_part); + pos = end - str.data(); + } if (scale) { str[pos++] = '.'; for (auto end_pos = pos + scale - 1; end_pos >= pos && frac_part > 0; --end_pos, frac_part /= 10) { - str[end_pos] += frac_part % 10; + str[end_pos] += (int)(frac_part % 10); Review Comment: warning: 10 is a magic number; consider replacing it with a named constant [readability-magic-numbers] ```cpp str[end_pos] += (int)(frac_part % 10); ^ ``` ########## be/src/vec/core/decomposed_float.h: ########## @@ -0,0 +1,201 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// This file is copied from +// https://github.com/ClickHouse/ClickHouse/blob/master/base/base/DecomposedFloat.h +// and modified by Doris +#pragma once + +#include <cstddef> +#include <cstdint> +#include <cstring> + +#include "extended_types.h" + +/// Allows to check the internals of IEEE-754 floating point number. + +template <typename T> +struct FloatTraits; + +template <> +struct FloatTraits<float> { + using UInt = uint32_t; + static constexpr size_t bits = 32; + static constexpr size_t exponent_bits = 8; + static constexpr size_t mantissa_bits = bits - exponent_bits - 1; +}; + +template <> +struct FloatTraits<double> { + using UInt = uint64_t; + static constexpr size_t bits = 64; + static constexpr size_t exponent_bits = 11; + static constexpr size_t mantissa_bits = bits - exponent_bits - 1; +}; + +/// x = sign * (2 ^ normalized_exponent) * (1 + mantissa * 2 ^ -mantissa_bits) +/// x = sign * (2 ^ normalized_exponent + mantissa * 2 ^ (normalized_exponent - mantissa_bits)) +template <typename T> +struct DecomposedFloat { + using Traits = FloatTraits<T>; + + explicit DecomposedFloat(T x) { memcpy(&x_uint, &x, sizeof(x)); } + + typename Traits::UInt x_uint; + + bool isNegative() const { return x_uint >> (Traits::bits - 1); } + + /// Returns 0 for both +0. and -0. + int sign() const { return (exponent() == 0 && mantissa() == 0) ? 0 : (isNegative() ? -1 : 1); } + + uint16_t exponent() const { + return (x_uint >> (Traits::mantissa_bits)) & + (((1ull << (Traits::exponent_bits + 1)) - 1) >> 1); + } + + int16_t normalizedExponent() const { + return int16_t(exponent()) - ((1ull << (Traits::exponent_bits - 1)) - 1); + } + + uint64_t mantissa() const { return x_uint & ((1ull << Traits::mantissa_bits) - 1); } + + int64_t mantissaWithSign() const { return isNegative() ? -mantissa() : mantissa(); } + + /// NOTE Probably floating point instructions can be better. + bool isIntegerInRepresentableRange() const { + return x_uint == 0 || + (normalizedExponent() >= 0 /// The number is not less than one + /// The number is inside the range where every integer has exact representation in float + && normalizedExponent() <= static_cast<int16_t>(Traits::mantissa_bits) + /// After multiplying by 2^exp, the fractional part becomes zero, means the number is integer + && ((mantissa() & ((1ULL << (Traits::mantissa_bits - normalizedExponent())) - 1)) == + 0)); + } + + /// Compare float with integer of arbitrary width (both signed and unsigned are supported). Assuming two's complement arithmetic. + /// This function is generic, big integers (128, 256 bit) are supported as well. + /// Infinities are compared correctly. NaNs are treat similarly to infinities, so they can be less than all numbers. + /// (note that we need total order) + /// Returns -1, 0 or 1. + template <typename Int> + int compare(Int rhs) const { + if (rhs == 0) return sign(); + + /// Different signs + if (isNegative() && rhs > 0) return -1; + if (!isNegative() && rhs < 0) return 1; + + /// Fractional number with magnitude less than one + if (normalizedExponent() < 0) { + if (!isNegative()) + return rhs > 0 ? -1 : 1; + else + return rhs >= 0 ? -1 : 1; + } + + /// The case of the most negative integer + if constexpr (is_signed_v<Int>) { + if (rhs == std::numeric_limits<Int>::lowest()) { + assert(isNegative()); + + if (normalizedExponent() < static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) + return 1; + if (normalizedExponent() > static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) + return -1; + + if (mantissa() == 0) + return 0; + else + return -1; + } + } + + /// Too large number: abs(float) > abs(rhs). Also the case with infinities and NaN. + if (normalizedExponent() >= static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) + return isNegative() ? -1 : 1; + + using UInt = std::conditional_t<(sizeof(Int) > sizeof(typename Traits::UInt)), + make_unsigned_t<Int>, typename Traits::UInt>; + UInt uint_rhs = rhs < 0 ? -rhs : rhs; + + /// Smaller octave: abs(rhs) < abs(float) + /// FYI, TIL: octave is also called "binade", https://en.wikipedia.org/wiki/Binade + if (uint_rhs < (static_cast<UInt>(1) << normalizedExponent())) return isNegative() ? -1 : 1; + + /// Larger octave: abs(rhs) > abs(float) + if (normalizedExponent() + 1 < static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>) && + uint_rhs >= (static_cast<UInt>(1) << (normalizedExponent() + 1))) + return isNegative() ? 1 : -1; + + /// The same octave + /// uint_rhs == 2 ^ normalizedExponent + mantissa * 2 ^ (normalizedExponent - mantissa_bits) + + bool large_and_always_integer = + normalizedExponent() >= static_cast<int16_t>(Traits::mantissa_bits); + + UInt a = large_and_always_integer + ? static_cast<UInt>(mantissa()) + << (normalizedExponent() - Traits::mantissa_bits) + : static_cast<UInt>(mantissa()) >> + (Traits::mantissa_bits - normalizedExponent()); + + UInt b = uint_rhs - (static_cast<UInt>(1) << normalizedExponent()); + + if (a < b) return isNegative() ? 1 : -1; + if (a > b) return isNegative() ? -1 : 1; Review Comment: warning: statement should be inside braces [readability-braces-around-statements] ```suggestion if (a > b) { return isNegative() ? -1 : 1; } ``` ########## be/src/vec/core/types.h: ########## @@ -522,9 +588,291 @@ struct Decimal128I : public Decimal<Int128> { } }; +template <> +struct Decimal<Int256> { + using T = Int256; + using NativeType = Int256; + + Decimal() = default; + Decimal(Decimal<T>&&) = default; + Decimal(const Decimal<T>&) = default; + +#define DECLARE_NUMERIC_CTOR(TYPE) \ + explicit Decimal(const TYPE& value_) : value(value_) {} + + DECLARE_NUMERIC_CTOR(Int256) + DECLARE_NUMERIC_CTOR(Int128) + DECLARE_NUMERIC_CTOR(Int32) + DECLARE_NUMERIC_CTOR(Int64) + DECLARE_NUMERIC_CTOR(UInt32) + DECLARE_NUMERIC_CTOR(UInt64) + +#undef DECLARE_NUMERIC_CTOR + + explicit Decimal(const Float32& value_) : value(value_) { + if constexpr (std::is_integral<T>::value) { + value = round(value_); + } + } + explicit Decimal(const Float64& value_) : value(value_) { + if constexpr (std::is_integral<T>::value) { + value = round(value_); + } + } + + static Decimal double_to_decimal(double value_) { + DecimalV2Value decimal_value; + decimal_value.assign_from_double(value_); + return Decimal(binary_cast<DecimalV2Value, T>(decimal_value)); + } + + template <typename U> + explicit Decimal(const Decimal<U>& x) { + value = x.value; + } + + constexpr Decimal<T>& operator=(Decimal<T>&&) = default; + constexpr Decimal<T>& operator=(const Decimal<T>&) = default; + + operator T() const { return value; } + + operator Int128() const { return (Int128)value.items[0] + ((Int128)(value.items[1]) << 64); } + + const Decimal<T>& operator++() { + value++; + return *this; + } + const Decimal<T>& operator--() { Review Comment: warning: method 'operator--' can be made const [readability-make-member-function-const] ```suggestion const Decimal<T>& operator--() const { ``` ########## be/src/vec/core/types.h: ########## @@ -522,9 +588,291 @@ struct Decimal128I : public Decimal<Int128> { } }; +template <> +struct Decimal<Int256> { + using T = Int256; + using NativeType = Int256; + + Decimal() = default; + Decimal(Decimal<T>&&) = default; + Decimal(const Decimal<T>&) = default; + +#define DECLARE_NUMERIC_CTOR(TYPE) \ + explicit Decimal(const TYPE& value_) : value(value_) {} + + DECLARE_NUMERIC_CTOR(Int256) + DECLARE_NUMERIC_CTOR(Int128) + DECLARE_NUMERIC_CTOR(Int32) + DECLARE_NUMERIC_CTOR(Int64) + DECLARE_NUMERIC_CTOR(UInt32) + DECLARE_NUMERIC_CTOR(UInt64) + +#undef DECLARE_NUMERIC_CTOR + + explicit Decimal(const Float32& value_) : value(value_) { + if constexpr (std::is_integral<T>::value) { + value = round(value_); + } + } + explicit Decimal(const Float64& value_) : value(value_) { + if constexpr (std::is_integral<T>::value) { + value = round(value_); + } + } + + static Decimal double_to_decimal(double value_) { + DecimalV2Value decimal_value; + decimal_value.assign_from_double(value_); + return Decimal(binary_cast<DecimalV2Value, T>(decimal_value)); + } + + template <typename U> + explicit Decimal(const Decimal<U>& x) { + value = x.value; + } + + constexpr Decimal<T>& operator=(Decimal<T>&&) = default; + constexpr Decimal<T>& operator=(const Decimal<T>&) = default; + + operator T() const { return value; } + + operator Int128() const { return (Int128)value.items[0] + ((Int128)(value.items[1]) << 64); } + + const Decimal<T>& operator++() { + value++; + return *this; + } + const Decimal<T>& operator--() { + value--; + return *this; + } + + const Decimal<T>& operator+=(const T& x) { + value += x; + return *this; + } + const Decimal<T>& operator-=(const T& x) { Review Comment: warning: method 'operator-=' can be made const [readability-make-member-function-const] ```suggestion const Decimal<T>& operator-=(const T& x) const { ``` ########## be/src/vec/core/types.h: ########## @@ -522,9 +588,291 @@ struct Decimal128I : public Decimal<Int128> { } }; +template <> +struct Decimal<Int256> { + using T = Int256; + using NativeType = Int256; + + Decimal() = default; + Decimal(Decimal<T>&&) = default; + Decimal(const Decimal<T>&) = default; + +#define DECLARE_NUMERIC_CTOR(TYPE) \ + explicit Decimal(const TYPE& value_) : value(value_) {} + + DECLARE_NUMERIC_CTOR(Int256) + DECLARE_NUMERIC_CTOR(Int128) + DECLARE_NUMERIC_CTOR(Int32) + DECLARE_NUMERIC_CTOR(Int64) + DECLARE_NUMERIC_CTOR(UInt32) + DECLARE_NUMERIC_CTOR(UInt64) + +#undef DECLARE_NUMERIC_CTOR + + explicit Decimal(const Float32& value_) : value(value_) { + if constexpr (std::is_integral<T>::value) { + value = round(value_); + } + } + explicit Decimal(const Float64& value_) : value(value_) { + if constexpr (std::is_integral<T>::value) { + value = round(value_); + } + } + + static Decimal double_to_decimal(double value_) { + DecimalV2Value decimal_value; + decimal_value.assign_from_double(value_); + return Decimal(binary_cast<DecimalV2Value, T>(decimal_value)); + } + + template <typename U> + explicit Decimal(const Decimal<U>& x) { + value = x.value; + } + + constexpr Decimal<T>& operator=(Decimal<T>&&) = default; + constexpr Decimal<T>& operator=(const Decimal<T>&) = default; + + operator T() const { return value; } + + operator Int128() const { return (Int128)value.items[0] + ((Int128)(value.items[1]) << 64); } + + const Decimal<T>& operator++() { + value++; + return *this; + } + const Decimal<T>& operator--() { + value--; + return *this; + } + + const Decimal<T>& operator+=(const T& x) { + value += x; + return *this; + } + const Decimal<T>& operator-=(const T& x) { + value -= x; + return *this; + } + const Decimal<T>& operator*=(const T& x) { + value *= x; + return *this; + } + const Decimal<T>& operator/=(const T& x) { Review Comment: warning: method 'operator/=' can be made const [readability-make-member-function-const] ```suggestion const Decimal<T>& operator/=(const T& x) const { ``` ########## be/src/vec/core/types.h: ########## @@ -522,9 +588,291 @@ struct Decimal128I : public Decimal<Int128> { } }; +template <> +struct Decimal<Int256> { + using T = Int256; + using NativeType = Int256; + + Decimal() = default; + Decimal(Decimal<T>&&) = default; + Decimal(const Decimal<T>&) = default; + +#define DECLARE_NUMERIC_CTOR(TYPE) \ + explicit Decimal(const TYPE& value_) : value(value_) {} + + DECLARE_NUMERIC_CTOR(Int256) + DECLARE_NUMERIC_CTOR(Int128) + DECLARE_NUMERIC_CTOR(Int32) + DECLARE_NUMERIC_CTOR(Int64) + DECLARE_NUMERIC_CTOR(UInt32) + DECLARE_NUMERIC_CTOR(UInt64) + +#undef DECLARE_NUMERIC_CTOR + + explicit Decimal(const Float32& value_) : value(value_) { + if constexpr (std::is_integral<T>::value) { + value = round(value_); + } + } + explicit Decimal(const Float64& value_) : value(value_) { + if constexpr (std::is_integral<T>::value) { + value = round(value_); + } + } + + static Decimal double_to_decimal(double value_) { + DecimalV2Value decimal_value; + decimal_value.assign_from_double(value_); + return Decimal(binary_cast<DecimalV2Value, T>(decimal_value)); + } + + template <typename U> + explicit Decimal(const Decimal<U>& x) { + value = x.value; + } + + constexpr Decimal<T>& operator=(Decimal<T>&&) = default; + constexpr Decimal<T>& operator=(const Decimal<T>&) = default; + + operator T() const { return value; } + + operator Int128() const { return (Int128)value.items[0] + ((Int128)(value.items[1]) << 64); } + + const Decimal<T>& operator++() { + value++; + return *this; + } + const Decimal<T>& operator--() { + value--; + return *this; + } + + const Decimal<T>& operator+=(const T& x) { Review Comment: warning: method 'operator+=' can be made const [readability-make-member-function-const] ```suggestion const Decimal<T>& operator+=(const T& x) const { ``` ########## be/src/vec/core/types.h: ########## @@ -522,9 +588,291 @@ struct Decimal128I : public Decimal<Int128> { } }; +template <> +struct Decimal<Int256> { + using T = Int256; + using NativeType = Int256; + + Decimal() = default; + Decimal(Decimal<T>&&) = default; + Decimal(const Decimal<T>&) = default; + +#define DECLARE_NUMERIC_CTOR(TYPE) \ + explicit Decimal(const TYPE& value_) : value(value_) {} + + DECLARE_NUMERIC_CTOR(Int256) + DECLARE_NUMERIC_CTOR(Int128) + DECLARE_NUMERIC_CTOR(Int32) + DECLARE_NUMERIC_CTOR(Int64) + DECLARE_NUMERIC_CTOR(UInt32) + DECLARE_NUMERIC_CTOR(UInt64) + +#undef DECLARE_NUMERIC_CTOR + + explicit Decimal(const Float32& value_) : value(value_) { + if constexpr (std::is_integral<T>::value) { + value = round(value_); + } + } + explicit Decimal(const Float64& value_) : value(value_) { + if constexpr (std::is_integral<T>::value) { + value = round(value_); + } + } + + static Decimal double_to_decimal(double value_) { + DecimalV2Value decimal_value; + decimal_value.assign_from_double(value_); + return Decimal(binary_cast<DecimalV2Value, T>(decimal_value)); + } + + template <typename U> + explicit Decimal(const Decimal<U>& x) { + value = x.value; + } + + constexpr Decimal<T>& operator=(Decimal<T>&&) = default; + constexpr Decimal<T>& operator=(const Decimal<T>&) = default; + + operator T() const { return value; } + + operator Int128() const { return (Int128)value.items[0] + ((Int128)(value.items[1]) << 64); } + + const Decimal<T>& operator++() { + value++; + return *this; + } + const Decimal<T>& operator--() { + value--; + return *this; + } + + const Decimal<T>& operator+=(const T& x) { + value += x; + return *this; + } + const Decimal<T>& operator-=(const T& x) { + value -= x; + return *this; + } + const Decimal<T>& operator*=(const T& x) { + value *= x; + return *this; + } + const Decimal<T>& operator/=(const T& x) { + value /= x; + return *this; + } + const Decimal<T>& operator%=(const T& x) { + value %= x; + return *this; + } + + auto operator<=>(const Decimal<T>& x) const { return value <=> x.value; } + + static constexpr int max_string_length() { + constexpr auto precision = + std::is_same_v<T, Int32> + ? BeConsts::MAX_DECIMAL32_PRECISION + : (std::is_same_v<T, Int64> + ? BeConsts::MAX_DECIMAL64_PRECISION + : (std::is_same_v<T, Int128> + ? BeConsts::MAX_DECIMAL128_PRECISION + : BeConsts::MAX_DECIMAL256_PRECISION)); + return precision + 1 // Add a space for decimal place + + 1 // Add a space for leading 0 + + 1; // Add a space for negative sign + } + + std::string to_string(UInt32 scale) const { + if (value == std::numeric_limits<T>::min()) { + if constexpr (std::is_same_v<T, Int256>) { + std::string res {wide::to_string(value)}; + res.insert(res.size() - scale, "."); + return res; + } else { + fmt::memory_buffer buffer; + fmt::format_to(buffer, "{}", value); + std::string res {buffer.data(), buffer.size()}; + res.insert(res.size() - scale, "."); + return res; + } + } + + static constexpr auto precision = + std::is_same_v<T, Int32> Review Comment: warning: multiple declarations in a single statement reduces readability [readability-isolate-declaration] ```suggestion static constexpr auto precision = std::is_same_v<T; static constexpr auto Int32> ? BeConsts::MAX_DECIMAL32_PRECISION : (std::is_same_v<T; ``` ########## be/src/vec/core/types.h: ########## @@ -522,9 +588,291 @@ struct Decimal128I : public Decimal<Int128> { } }; +template <> +struct Decimal<Int256> { + using T = Int256; + using NativeType = Int256; + + Decimal() = default; + Decimal(Decimal<T>&&) = default; + Decimal(const Decimal<T>&) = default; + +#define DECLARE_NUMERIC_CTOR(TYPE) \ + explicit Decimal(const TYPE& value_) : value(value_) {} + + DECLARE_NUMERIC_CTOR(Int256) + DECLARE_NUMERIC_CTOR(Int128) + DECLARE_NUMERIC_CTOR(Int32) + DECLARE_NUMERIC_CTOR(Int64) + DECLARE_NUMERIC_CTOR(UInt32) + DECLARE_NUMERIC_CTOR(UInt64) + +#undef DECLARE_NUMERIC_CTOR + + explicit Decimal(const Float32& value_) : value(value_) { + if constexpr (std::is_integral<T>::value) { + value = round(value_); + } + } + explicit Decimal(const Float64& value_) : value(value_) { + if constexpr (std::is_integral<T>::value) { + value = round(value_); + } + } + + static Decimal double_to_decimal(double value_) { + DecimalV2Value decimal_value; + decimal_value.assign_from_double(value_); + return Decimal(binary_cast<DecimalV2Value, T>(decimal_value)); + } + + template <typename U> + explicit Decimal(const Decimal<U>& x) { + value = x.value; + } + + constexpr Decimal<T>& operator=(Decimal<T>&&) = default; + constexpr Decimal<T>& operator=(const Decimal<T>&) = default; + + operator T() const { return value; } + + operator Int128() const { return (Int128)value.items[0] + ((Int128)(value.items[1]) << 64); } + + const Decimal<T>& operator++() { Review Comment: warning: method 'operator++' can be made const [readability-make-member-function-const] ```suggestion const Decimal<T>& operator++() const { ``` ########## be/src/vec/core/types.h: ########## @@ -522,9 +588,291 @@ struct Decimal128I : public Decimal<Int128> { } }; +template <> +struct Decimal<Int256> { + using T = Int256; + using NativeType = Int256; + + Decimal() = default; + Decimal(Decimal<T>&&) = default; + Decimal(const Decimal<T>&) = default; + +#define DECLARE_NUMERIC_CTOR(TYPE) \ + explicit Decimal(const TYPE& value_) : value(value_) {} + + DECLARE_NUMERIC_CTOR(Int256) + DECLARE_NUMERIC_CTOR(Int128) + DECLARE_NUMERIC_CTOR(Int32) + DECLARE_NUMERIC_CTOR(Int64) + DECLARE_NUMERIC_CTOR(UInt32) + DECLARE_NUMERIC_CTOR(UInt64) + +#undef DECLARE_NUMERIC_CTOR + + explicit Decimal(const Float32& value_) : value(value_) { + if constexpr (std::is_integral<T>::value) { + value = round(value_); + } + } + explicit Decimal(const Float64& value_) : value(value_) { + if constexpr (std::is_integral<T>::value) { + value = round(value_); + } + } + + static Decimal double_to_decimal(double value_) { + DecimalV2Value decimal_value; + decimal_value.assign_from_double(value_); + return Decimal(binary_cast<DecimalV2Value, T>(decimal_value)); + } + + template <typename U> + explicit Decimal(const Decimal<U>& x) { + value = x.value; + } + + constexpr Decimal<T>& operator=(Decimal<T>&&) = default; + constexpr Decimal<T>& operator=(const Decimal<T>&) = default; + + operator T() const { return value; } + + operator Int128() const { return (Int128)value.items[0] + ((Int128)(value.items[1]) << 64); } + + const Decimal<T>& operator++() { + value++; + return *this; + } + const Decimal<T>& operator--() { + value--; + return *this; + } + + const Decimal<T>& operator+=(const T& x) { + value += x; + return *this; + } + const Decimal<T>& operator-=(const T& x) { + value -= x; + return *this; + } + const Decimal<T>& operator*=(const T& x) { Review Comment: warning: method 'operator*=' can be made const [readability-make-member-function-const] ```suggestion const Decimal<T>& operator*=(const T& x) const { ``` ########## be/src/vec/core/types.h: ########## @@ -522,9 +588,291 @@ struct Decimal128I : public Decimal<Int128> { } }; +template <> +struct Decimal<Int256> { + using T = Int256; + using NativeType = Int256; + + Decimal() = default; + Decimal(Decimal<T>&&) = default; + Decimal(const Decimal<T>&) = default; + +#define DECLARE_NUMERIC_CTOR(TYPE) \ + explicit Decimal(const TYPE& value_) : value(value_) {} + + DECLARE_NUMERIC_CTOR(Int256) + DECLARE_NUMERIC_CTOR(Int128) + DECLARE_NUMERIC_CTOR(Int32) + DECLARE_NUMERIC_CTOR(Int64) + DECLARE_NUMERIC_CTOR(UInt32) + DECLARE_NUMERIC_CTOR(UInt64) + +#undef DECLARE_NUMERIC_CTOR + + explicit Decimal(const Float32& value_) : value(value_) { + if constexpr (std::is_integral<T>::value) { + value = round(value_); + } + } + explicit Decimal(const Float64& value_) : value(value_) { + if constexpr (std::is_integral<T>::value) { + value = round(value_); + } + } + + static Decimal double_to_decimal(double value_) { + DecimalV2Value decimal_value; + decimal_value.assign_from_double(value_); + return Decimal(binary_cast<DecimalV2Value, T>(decimal_value)); + } + + template <typename U> + explicit Decimal(const Decimal<U>& x) { + value = x.value; + } + + constexpr Decimal<T>& operator=(Decimal<T>&&) = default; + constexpr Decimal<T>& operator=(const Decimal<T>&) = default; + + operator T() const { return value; } + + operator Int128() const { return (Int128)value.items[0] + ((Int128)(value.items[1]) << 64); } + + const Decimal<T>& operator++() { + value++; + return *this; + } + const Decimal<T>& operator--() { + value--; + return *this; + } + + const Decimal<T>& operator+=(const T& x) { + value += x; + return *this; + } + const Decimal<T>& operator-=(const T& x) { + value -= x; + return *this; + } + const Decimal<T>& operator*=(const T& x) { + value *= x; + return *this; + } + const Decimal<T>& operator/=(const T& x) { + value /= x; + return *this; + } + const Decimal<T>& operator%=(const T& x) { + value %= x; + return *this; + } + + auto operator<=>(const Decimal<T>& x) const { return value <=> x.value; } + + static constexpr int max_string_length() { + constexpr auto precision = + std::is_same_v<T, Int32> Review Comment: warning: multiple declarations in a single statement reduces readability [readability-isolate-declaration] ```suggestion constexpr auto precision = std::is_same_v<T; constexpr auto Int32> ? BeConsts::MAX_DECIMAL32_PRECISION : (std::is_same_v<T; ``` ########## be/src/vec/core/types.h: ########## @@ -522,9 +588,291 @@ struct Decimal128I : public Decimal<Int128> { } }; +template <> +struct Decimal<Int256> { + using T = Int256; + using NativeType = Int256; + + Decimal() = default; + Decimal(Decimal<T>&&) = default; + Decimal(const Decimal<T>&) = default; + +#define DECLARE_NUMERIC_CTOR(TYPE) \ + explicit Decimal(const TYPE& value_) : value(value_) {} + + DECLARE_NUMERIC_CTOR(Int256) + DECLARE_NUMERIC_CTOR(Int128) + DECLARE_NUMERIC_CTOR(Int32) + DECLARE_NUMERIC_CTOR(Int64) + DECLARE_NUMERIC_CTOR(UInt32) + DECLARE_NUMERIC_CTOR(UInt64) + +#undef DECLARE_NUMERIC_CTOR + + explicit Decimal(const Float32& value_) : value(value_) { + if constexpr (std::is_integral<T>::value) { + value = round(value_); + } + } + explicit Decimal(const Float64& value_) : value(value_) { + if constexpr (std::is_integral<T>::value) { + value = round(value_); + } + } + + static Decimal double_to_decimal(double value_) { + DecimalV2Value decimal_value; + decimal_value.assign_from_double(value_); + return Decimal(binary_cast<DecimalV2Value, T>(decimal_value)); + } + + template <typename U> + explicit Decimal(const Decimal<U>& x) { + value = x.value; + } + + constexpr Decimal<T>& operator=(Decimal<T>&&) = default; + constexpr Decimal<T>& operator=(const Decimal<T>&) = default; + + operator T() const { return value; } + + operator Int128() const { return (Int128)value.items[0] + ((Int128)(value.items[1]) << 64); } + + const Decimal<T>& operator++() { + value++; + return *this; + } + const Decimal<T>& operator--() { + value--; + return *this; + } + + const Decimal<T>& operator+=(const T& x) { + value += x; + return *this; + } + const Decimal<T>& operator-=(const T& x) { + value -= x; + return *this; + } + const Decimal<T>& operator*=(const T& x) { + value *= x; + return *this; + } + const Decimal<T>& operator/=(const T& x) { + value /= x; + return *this; + } + const Decimal<T>& operator%=(const T& x) { + value %= x; + return *this; + } + + auto operator<=>(const Decimal<T>& x) const { return value <=> x.value; } + + static constexpr int max_string_length() { + constexpr auto precision = + std::is_same_v<T, Int32> + ? BeConsts::MAX_DECIMAL32_PRECISION + : (std::is_same_v<T, Int64> + ? BeConsts::MAX_DECIMAL64_PRECISION + : (std::is_same_v<T, Int128> + ? BeConsts::MAX_DECIMAL128_PRECISION + : BeConsts::MAX_DECIMAL256_PRECISION)); + return precision + 1 // Add a space for decimal place + + 1 // Add a space for leading 0 + + 1; // Add a space for negative sign + } + + std::string to_string(UInt32 scale) const { + if (value == std::numeric_limits<T>::min()) { + if constexpr (std::is_same_v<T, Int256>) { + std::string res {wide::to_string(value)}; + res.insert(res.size() - scale, "."); + return res; + } else { + fmt::memory_buffer buffer; + fmt::format_to(buffer, "{}", value); + std::string res {buffer.data(), buffer.size()}; + res.insert(res.size() - scale, "."); + return res; + } + } + + static constexpr auto precision = + std::is_same_v<T, Int32> + ? BeConsts::MAX_DECIMAL32_PRECISION + : (std::is_same_v<T, Int64> + ? BeConsts::MAX_DECIMAL64_PRECISION + : (std::is_same_v<T, Int128> + ? BeConsts::MAX_DECIMAL128_PRECISION + : BeConsts::MAX_DECIMAL256_PRECISION)); + bool is_nagetive = value < 0; + int max_result_length = precision + (scale > 0) // Add a space for decimal place + + (scale == precision) // Add a space for leading 0 + + (is_nagetive); // Add a space for negative sign + std::string str = std::string(max_result_length, '0'); + + T abs_value = value; + int pos = 0; + + if (is_nagetive) { + abs_value = -value; + str[pos++] = '-'; + } + + T whole_part = abs_value; + T frac_part; + if (scale) { + whole_part = abs_value / decimal_scale_multiplier<T>(scale); + frac_part = abs_value % decimal_scale_multiplier<T>(scale); + } + if constexpr (std::is_same_v<T, Int256>) { + std::string num_str {wide::to_string(whole_part)}; + auto end = fmt::format_to(str.data() + pos, "{}", num_str); + pos = end - str.data(); + } else { + auto end = fmt::format_to(str.data() + pos, "{}", whole_part); + pos = end - str.data(); + } + + if (scale) { + str[pos++] = '.'; + for (auto end_pos = pos + scale - 1; end_pos >= pos && frac_part > 0; + --end_pos, frac_part /= 10) { + str[end_pos] += (int)(frac_part % 10); Review Comment: warning: 10 is a magic number; consider replacing it with a named constant [readability-magic-numbers] ```cpp str[end_pos] += (int)(frac_part % 10); ^ ``` ########## be/src/vec/core/types.h: ########## @@ -522,9 +588,291 @@ struct Decimal128I : public Decimal<Int128> { } }; +template <> +struct Decimal<Int256> { + using T = Int256; + using NativeType = Int256; + + Decimal() = default; + Decimal(Decimal<T>&&) = default; + Decimal(const Decimal<T>&) = default; + +#define DECLARE_NUMERIC_CTOR(TYPE) \ + explicit Decimal(const TYPE& value_) : value(value_) {} + + DECLARE_NUMERIC_CTOR(Int256) + DECLARE_NUMERIC_CTOR(Int128) + DECLARE_NUMERIC_CTOR(Int32) + DECLARE_NUMERIC_CTOR(Int64) + DECLARE_NUMERIC_CTOR(UInt32) + DECLARE_NUMERIC_CTOR(UInt64) + +#undef DECLARE_NUMERIC_CTOR + + explicit Decimal(const Float32& value_) : value(value_) { + if constexpr (std::is_integral<T>::value) { + value = round(value_); + } + } + explicit Decimal(const Float64& value_) : value(value_) { + if constexpr (std::is_integral<T>::value) { + value = round(value_); + } + } + + static Decimal double_to_decimal(double value_) { + DecimalV2Value decimal_value; + decimal_value.assign_from_double(value_); + return Decimal(binary_cast<DecimalV2Value, T>(decimal_value)); + } + + template <typename U> + explicit Decimal(const Decimal<U>& x) { + value = x.value; + } + + constexpr Decimal<T>& operator=(Decimal<T>&&) = default; + constexpr Decimal<T>& operator=(const Decimal<T>&) = default; + + operator T() const { return value; } + + operator Int128() const { return (Int128)value.items[0] + ((Int128)(value.items[1]) << 64); } + + const Decimal<T>& operator++() { + value++; + return *this; + } + const Decimal<T>& operator--() { + value--; + return *this; + } + + const Decimal<T>& operator+=(const T& x) { + value += x; + return *this; + } + const Decimal<T>& operator-=(const T& x) { + value -= x; + return *this; + } + const Decimal<T>& operator*=(const T& x) { + value *= x; + return *this; + } + const Decimal<T>& operator/=(const T& x) { + value /= x; + return *this; + } + const Decimal<T>& operator%=(const T& x) { + value %= x; + return *this; + } + + auto operator<=>(const Decimal<T>& x) const { return value <=> x.value; } + + static constexpr int max_string_length() { + constexpr auto precision = + std::is_same_v<T, Int32> + ? BeConsts::MAX_DECIMAL32_PRECISION + : (std::is_same_v<T, Int64> + ? BeConsts::MAX_DECIMAL64_PRECISION + : (std::is_same_v<T, Int128> + ? BeConsts::MAX_DECIMAL128_PRECISION + : BeConsts::MAX_DECIMAL256_PRECISION)); + return precision + 1 // Add a space for decimal place + + 1 // Add a space for leading 0 + + 1; // Add a space for negative sign + } + + std::string to_string(UInt32 scale) const { + if (value == std::numeric_limits<T>::min()) { + if constexpr (std::is_same_v<T, Int256>) { + std::string res {wide::to_string(value)}; + res.insert(res.size() - scale, "."); + return res; + } else { + fmt::memory_buffer buffer; + fmt::format_to(buffer, "{}", value); + std::string res {buffer.data(), buffer.size()}; + res.insert(res.size() - scale, "."); + return res; + } + } + + static constexpr auto precision = + std::is_same_v<T, Int32> + ? BeConsts::MAX_DECIMAL32_PRECISION + : (std::is_same_v<T, Int64> + ? BeConsts::MAX_DECIMAL64_PRECISION + : (std::is_same_v<T, Int128> + ? BeConsts::MAX_DECIMAL128_PRECISION + : BeConsts::MAX_DECIMAL256_PRECISION)); + bool is_nagetive = value < 0; + int max_result_length = precision + (scale > 0) // Add a space for decimal place + + (scale == precision) // Add a space for leading 0 + + (is_nagetive); // Add a space for negative sign + std::string str = std::string(max_result_length, '0'); + + T abs_value = value; + int pos = 0; + + if (is_nagetive) { + abs_value = -value; + str[pos++] = '-'; + } + + T whole_part = abs_value; + T frac_part; + if (scale) { + whole_part = abs_value / decimal_scale_multiplier<T>(scale); + frac_part = abs_value % decimal_scale_multiplier<T>(scale); + } + if constexpr (std::is_same_v<T, Int256>) { + std::string num_str {wide::to_string(whole_part)}; + auto end = fmt::format_to(str.data() + pos, "{}", num_str); + pos = end - str.data(); + } else { + auto end = fmt::format_to(str.data() + pos, "{}", whole_part); + pos = end - str.data(); + } + + if (scale) { + str[pos++] = '.'; + for (auto end_pos = pos + scale - 1; end_pos >= pos && frac_part > 0; + --end_pos, frac_part /= 10) { + str[end_pos] += (int)(frac_part % 10); + } + } + + str.resize(pos + scale); + return str; + } + + /** + * Got the string representation of a decimal. + * @param dst Store the result, should be pre-allocated. + * @param scale Decimal's scale. + * @param scale_multiplier Decimal's scale multiplier. + * @return The length of string. + */ + __attribute__((always_inline)) size_t to_string(char* dst, UInt32 scale, + const T& scale_multiplier) const { Review Comment: warning: method 'to_string' can be made static [readability-convert-member-functions-to-static] ```suggestion static __attribute__((always_inline)) size_t to_string(char* dst, UInt32 scale, const T& scale_multiplier) { ``` ########## be/src/vec/core/types.h: ########## @@ -522,9 +588,291 @@ struct Decimal128I : public Decimal<Int128> { } }; +template <> +struct Decimal<Int256> { + using T = Int256; + using NativeType = Int256; + + Decimal() = default; + Decimal(Decimal<T>&&) = default; + Decimal(const Decimal<T>&) = default; + +#define DECLARE_NUMERIC_CTOR(TYPE) \ + explicit Decimal(const TYPE& value_) : value(value_) {} + + DECLARE_NUMERIC_CTOR(Int256) + DECLARE_NUMERIC_CTOR(Int128) + DECLARE_NUMERIC_CTOR(Int32) + DECLARE_NUMERIC_CTOR(Int64) + DECLARE_NUMERIC_CTOR(UInt32) + DECLARE_NUMERIC_CTOR(UInt64) + +#undef DECLARE_NUMERIC_CTOR + + explicit Decimal(const Float32& value_) : value(value_) { + if constexpr (std::is_integral<T>::value) { + value = round(value_); + } + } + explicit Decimal(const Float64& value_) : value(value_) { + if constexpr (std::is_integral<T>::value) { + value = round(value_); + } + } + + static Decimal double_to_decimal(double value_) { + DecimalV2Value decimal_value; + decimal_value.assign_from_double(value_); + return Decimal(binary_cast<DecimalV2Value, T>(decimal_value)); + } + + template <typename U> + explicit Decimal(const Decimal<U>& x) { + value = x.value; + } + + constexpr Decimal<T>& operator=(Decimal<T>&&) = default; + constexpr Decimal<T>& operator=(const Decimal<T>&) = default; + + operator T() const { return value; } + + operator Int128() const { return (Int128)value.items[0] + ((Int128)(value.items[1]) << 64); } + + const Decimal<T>& operator++() { + value++; + return *this; + } + const Decimal<T>& operator--() { + value--; + return *this; + } + + const Decimal<T>& operator+=(const T& x) { + value += x; + return *this; + } + const Decimal<T>& operator-=(const T& x) { + value -= x; + return *this; + } + const Decimal<T>& operator*=(const T& x) { + value *= x; + return *this; + } + const Decimal<T>& operator/=(const T& x) { + value /= x; + return *this; + } + const Decimal<T>& operator%=(const T& x) { + value %= x; + return *this; + } + + auto operator<=>(const Decimal<T>& x) const { return value <=> x.value; } + + static constexpr int max_string_length() { + constexpr auto precision = + std::is_same_v<T, Int32> + ? BeConsts::MAX_DECIMAL32_PRECISION + : (std::is_same_v<T, Int64> + ? BeConsts::MAX_DECIMAL64_PRECISION + : (std::is_same_v<T, Int128> + ? BeConsts::MAX_DECIMAL128_PRECISION + : BeConsts::MAX_DECIMAL256_PRECISION)); + return precision + 1 // Add a space for decimal place + + 1 // Add a space for leading 0 + + 1; // Add a space for negative sign + } + + std::string to_string(UInt32 scale) const { + if (value == std::numeric_limits<T>::min()) { + if constexpr (std::is_same_v<T, Int256>) { + std::string res {wide::to_string(value)}; + res.insert(res.size() - scale, "."); + return res; + } else { + fmt::memory_buffer buffer; + fmt::format_to(buffer, "{}", value); + std::string res {buffer.data(), buffer.size()}; + res.insert(res.size() - scale, "."); + return res; + } + } + + static constexpr auto precision = + std::is_same_v<T, Int32> + ? BeConsts::MAX_DECIMAL32_PRECISION + : (std::is_same_v<T, Int64> + ? BeConsts::MAX_DECIMAL64_PRECISION + : (std::is_same_v<T, Int128> + ? BeConsts::MAX_DECIMAL128_PRECISION + : BeConsts::MAX_DECIMAL256_PRECISION)); + bool is_nagetive = value < 0; + int max_result_length = precision + (scale > 0) // Add a space for decimal place + + (scale == precision) // Add a space for leading 0 + + (is_nagetive); // Add a space for negative sign + std::string str = std::string(max_result_length, '0'); + + T abs_value = value; + int pos = 0; + + if (is_nagetive) { + abs_value = -value; + str[pos++] = '-'; + } + + T whole_part = abs_value; + T frac_part; + if (scale) { + whole_part = abs_value / decimal_scale_multiplier<T>(scale); + frac_part = abs_value % decimal_scale_multiplier<T>(scale); + } + if constexpr (std::is_same_v<T, Int256>) { + std::string num_str {wide::to_string(whole_part)}; + auto end = fmt::format_to(str.data() + pos, "{}", num_str); + pos = end - str.data(); + } else { + auto end = fmt::format_to(str.data() + pos, "{}", whole_part); + pos = end - str.data(); + } + + if (scale) { + str[pos++] = '.'; + for (auto end_pos = pos + scale - 1; end_pos >= pos && frac_part > 0; + --end_pos, frac_part /= 10) { Review Comment: warning: 10 is a magic number; consider replacing it with a named constant [readability-magic-numbers] ```cpp --end_pos, frac_part /= 10) { ^ ``` ########## be/src/vec/core/types.h: ########## @@ -522,9 +588,291 @@ struct Decimal128I : public Decimal<Int128> { } }; +template <> +struct Decimal<Int256> { + using T = Int256; + using NativeType = Int256; + + Decimal() = default; + Decimal(Decimal<T>&&) = default; + Decimal(const Decimal<T>&) = default; + +#define DECLARE_NUMERIC_CTOR(TYPE) \ + explicit Decimal(const TYPE& value_) : value(value_) {} + + DECLARE_NUMERIC_CTOR(Int256) + DECLARE_NUMERIC_CTOR(Int128) + DECLARE_NUMERIC_CTOR(Int32) + DECLARE_NUMERIC_CTOR(Int64) + DECLARE_NUMERIC_CTOR(UInt32) + DECLARE_NUMERIC_CTOR(UInt64) + +#undef DECLARE_NUMERIC_CTOR + + explicit Decimal(const Float32& value_) : value(value_) { + if constexpr (std::is_integral<T>::value) { + value = round(value_); + } + } + explicit Decimal(const Float64& value_) : value(value_) { + if constexpr (std::is_integral<T>::value) { + value = round(value_); + } + } + + static Decimal double_to_decimal(double value_) { + DecimalV2Value decimal_value; + decimal_value.assign_from_double(value_); + return Decimal(binary_cast<DecimalV2Value, T>(decimal_value)); + } + + template <typename U> + explicit Decimal(const Decimal<U>& x) { + value = x.value; + } + + constexpr Decimal<T>& operator=(Decimal<T>&&) = default; + constexpr Decimal<T>& operator=(const Decimal<T>&) = default; + + operator T() const { return value; } + + operator Int128() const { return (Int128)value.items[0] + ((Int128)(value.items[1]) << 64); } + + const Decimal<T>& operator++() { + value++; + return *this; + } + const Decimal<T>& operator--() { + value--; + return *this; + } + + const Decimal<T>& operator+=(const T& x) { + value += x; + return *this; + } + const Decimal<T>& operator-=(const T& x) { + value -= x; + return *this; + } + const Decimal<T>& operator*=(const T& x) { + value *= x; + return *this; + } + const Decimal<T>& operator/=(const T& x) { + value /= x; + return *this; + } + const Decimal<T>& operator%=(const T& x) { Review Comment: warning: method 'operator%=' can be made const [readability-make-member-function-const] ```suggestion const Decimal<T>& operator%=(const T& x) const { ``` ########## be/src/vec/core/wide_integer.h: ########## @@ -0,0 +1,302 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +/////////////////////////////////////////////////////////////// +// Distributed under the Boost Software License, Version 1.0. +// (See at http://www.boost.org/LICENSE_1_0.txt) +/////////////////////////////////////////////////////////////// + +/* Divide and multiply + * + * + * Copyright (c) 2008 + * Evan Teran + * + * Permission to use, copy, modify, and distribute this software and its + * documentation for any purpose and without fee is hereby granted, provided + * that the above copyright notice appears in all copies and that both the + * copyright notice and this permission notice appear in supporting + * documentation, and that the same name not be used in advertising or + * publicity pertaining to distribution of the software without specific, + * written prior permission. We make no representations about the + * suitability this software for any purpose. It is provided "as is" + * without express or implied warranty. + */ +// This file is copied from +// https://github.com/ClickHouse/ClickHouse/blob/master/base/base/wide_integer.h +// and modified by Doris +#pragma once + +#include <cstdint> +#include <initializer_list> +#include <limits> +#include <type_traits> + +// NOLINTBEGIN(*) + +namespace wide { +template <size_t Bits, typename Signed> Review Comment: warning: unknown type name 'size_t'; did you mean 'std::size_t'? [clang-diagnostic-error] ```suggestion template <std::size_t Bits, typename Signed> ``` <details> <summary>Additional context</summary> **/usr/include/x86_64-linux-gnu/c++/11/bits/c++config.h:279:** 'std::size_t' declared here ```cpp typedef __SIZE_TYPE__ size_t; ^ ``` </details> ########## be/src/vec/core/wide_integer.h: ########## @@ -0,0 +1,302 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +/////////////////////////////////////////////////////////////// +// Distributed under the Boost Software License, Version 1.0. +// (See at http://www.boost.org/LICENSE_1_0.txt) +/////////////////////////////////////////////////////////////// + +/* Divide and multiply + * + * + * Copyright (c) 2008 + * Evan Teran + * + * Permission to use, copy, modify, and distribute this software and its + * documentation for any purpose and without fee is hereby granted, provided + * that the above copyright notice appears in all copies and that both the + * copyright notice and this permission notice appear in supporting + * documentation, and that the same name not be used in advertising or + * publicity pertaining to distribution of the software without specific, + * written prior permission. We make no representations about the + * suitability this software for any purpose. It is provided "as is" + * without express or implied warranty. + */ +// This file is copied from +// https://github.com/ClickHouse/ClickHouse/blob/master/base/base/wide_integer.h +// and modified by Doris +#pragma once + +#include <cstdint> +#include <initializer_list> +#include <limits> +#include <type_traits> + +// NOLINTBEGIN(*) + +namespace wide { +template <size_t Bits, typename Signed> +class integer; +} + +namespace std { + +template <size_t Bits, typename Signed, size_t Bits2, typename Signed2> +struct common_type<wide::integer<Bits, Signed>, wide::integer<Bits2, Signed2>>; + +template <size_t Bits, typename Signed, typename Arithmetic> +struct common_type<wide::integer<Bits, Signed>, Arithmetic>; + +template <typename Arithmetic, size_t Bits, typename Signed> +struct common_type<Arithmetic, wide::integer<Bits, Signed>>; + +} // namespace std + +namespace wide { + +template <size_t Bits, typename Signed> Review Comment: warning: unknown type name 'size_t'; did you mean 'std::size_t'? [clang-diagnostic-error] ```suggestion template <std::size_t Bits, typename Signed> ``` <details> <summary>Additional context</summary> **/usr/include/x86_64-linux-gnu/c++/11/bits/c++config.h:279:** 'std::size_t' declared here ```cpp typedef __SIZE_TYPE__ size_t; ^ ``` </details> ########## be/src/vec/core/decomposed_float.h: ########## @@ -0,0 +1,201 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// This file is copied from +// https://github.com/ClickHouse/ClickHouse/blob/master/base/base/DecomposedFloat.h +// and modified by Doris +#pragma once + +#include <cstddef> +#include <cstdint> +#include <cstring> + +#include "extended_types.h" + +/// Allows to check the internals of IEEE-754 floating point number. + +template <typename T> +struct FloatTraits; + +template <> +struct FloatTraits<float> { + using UInt = uint32_t; + static constexpr size_t bits = 32; + static constexpr size_t exponent_bits = 8; + static constexpr size_t mantissa_bits = bits - exponent_bits - 1; +}; + +template <> +struct FloatTraits<double> { + using UInt = uint64_t; + static constexpr size_t bits = 64; + static constexpr size_t exponent_bits = 11; + static constexpr size_t mantissa_bits = bits - exponent_bits - 1; +}; + +/// x = sign * (2 ^ normalized_exponent) * (1 + mantissa * 2 ^ -mantissa_bits) +/// x = sign * (2 ^ normalized_exponent + mantissa * 2 ^ (normalized_exponent - mantissa_bits)) +template <typename T> +struct DecomposedFloat { + using Traits = FloatTraits<T>; + + explicit DecomposedFloat(T x) { memcpy(&x_uint, &x, sizeof(x)); } + + typename Traits::UInt x_uint; + + bool isNegative() const { return x_uint >> (Traits::bits - 1); } + + /// Returns 0 for both +0. and -0. + int sign() const { return (exponent() == 0 && mantissa() == 0) ? 0 : (isNegative() ? -1 : 1); } + + uint16_t exponent() const { + return (x_uint >> (Traits::mantissa_bits)) & + (((1ull << (Traits::exponent_bits + 1)) - 1) >> 1); + } + + int16_t normalizedExponent() const { + return int16_t(exponent()) - ((1ull << (Traits::exponent_bits - 1)) - 1); + } + + uint64_t mantissa() const { return x_uint & ((1ull << Traits::mantissa_bits) - 1); } + + int64_t mantissaWithSign() const { return isNegative() ? -mantissa() : mantissa(); } + + /// NOTE Probably floating point instructions can be better. + bool isIntegerInRepresentableRange() const { + return x_uint == 0 || + (normalizedExponent() >= 0 /// The number is not less than one + /// The number is inside the range where every integer has exact representation in float + && normalizedExponent() <= static_cast<int16_t>(Traits::mantissa_bits) + /// After multiplying by 2^exp, the fractional part becomes zero, means the number is integer + && ((mantissa() & ((1ULL << (Traits::mantissa_bits - normalizedExponent())) - 1)) == + 0)); + } + + /// Compare float with integer of arbitrary width (both signed and unsigned are supported). Assuming two's complement arithmetic. + /// This function is generic, big integers (128, 256 bit) are supported as well. + /// Infinities are compared correctly. NaNs are treat similarly to infinities, so they can be less than all numbers. + /// (note that we need total order) + /// Returns -1, 0 or 1. + template <typename Int> + int compare(Int rhs) const { + if (rhs == 0) return sign(); + + /// Different signs + if (isNegative() && rhs > 0) return -1; + if (!isNegative() && rhs < 0) return 1; + + /// Fractional number with magnitude less than one + if (normalizedExponent() < 0) { + if (!isNegative()) + return rhs > 0 ? -1 : 1; + else + return rhs >= 0 ? -1 : 1; + } + + /// The case of the most negative integer + if constexpr (is_signed_v<Int>) { + if (rhs == std::numeric_limits<Int>::lowest()) { + assert(isNegative()); + + if (normalizedExponent() < static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) + return 1; + if (normalizedExponent() > static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) + return -1; + + if (mantissa() == 0) + return 0; + else + return -1; + } + } + + /// Too large number: abs(float) > abs(rhs). Also the case with infinities and NaN. + if (normalizedExponent() >= static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>)) + return isNegative() ? -1 : 1; + + using UInt = std::conditional_t<(sizeof(Int) > sizeof(typename Traits::UInt)), + make_unsigned_t<Int>, typename Traits::UInt>; + UInt uint_rhs = rhs < 0 ? -rhs : rhs; + + /// Smaller octave: abs(rhs) < abs(float) + /// FYI, TIL: octave is also called "binade", https://en.wikipedia.org/wiki/Binade + if (uint_rhs < (static_cast<UInt>(1) << normalizedExponent())) return isNegative() ? -1 : 1; + + /// Larger octave: abs(rhs) > abs(float) + if (normalizedExponent() + 1 < static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>) && Review Comment: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers] ```cpp if (normalizedExponent() + 1 < static_cast<int16_t>(8 * sizeof(Int) - is_signed_v<Int>) && ^ ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org