aboutsummaryrefslogtreecommitdiffstats
path: root/ydb/core/formats/func_gcd.h
blob: 9d4b72e4b0059ac52bfe042e1ec33ea577a54f1a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#pragma once
#include <contrib/libs/apache/arrow/cpp/src/arrow/array/array_base.h>
#include <contrib/libs/apache/arrow/cpp/src/arrow/array/builder_base.h>
#include <contrib/libs/apache/arrow/cpp/src/arrow/compute/kernel.h>
#include <contrib/libs/apache/arrow/cpp/src/arrow/datum.h>
#include <contrib/libs/apache/arrow/cpp/src/arrow/type_traits.h>
#include "func_common.h"
#include <cstdlib>
#include <type_traits>

namespace NKikimr::NArrow {

template<typename T>
__attribute__((always_inline)) void FastIntSwap(T& lhs, T& rhs) {
    lhs ^= rhs;
    rhs ^= lhs;
    lhs ^= rhs;
 }

struct TGreatestCommonDivisor {

    static constexpr const char * Name = "gcd";

    template <typename TRes, typename TArg0, typename TArg1>
    static constexpr TRes Call(arrow::compute::KernelContext*, TArg0 lhs, TArg1 rhs, arrow::Status*) {
        static_assert(std::is_integral_v<TRes>, "");
        static_assert(std::is_integral_v<TArg0>, "");
        static_assert(std::is_integral_v<TArg1>, "");
        while (rhs != 0) {
            lhs %= rhs;
            FastIntSwap(lhs, rhs);
        }
        return lhs;
    }
};

}