// SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute // SPDX-License-Identifier: GPL-3.0-only # pragma once #include #include "JFJochException.h" constexpr int64_t bias = 512; inline uint64_t encode(int64_t v) { const int64_t tmp = v + bias; if (tmp < 0 || tmp >= 2 * bias) throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, "Value out of range"); return static_cast(tmp); } inline uint64_t hkl_key(const int64_t h, const int64_t k, const int64_t l) { const uint64_t uh = encode(h); const uint64_t uk = encode(k); const uint64_t ul = encode(l); return uh | (uk << 11) | (ul << 22); } inline int64_t decode(uint64_t v) { if (v >= 2 * bias) throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, "Value out of range"); return static_cast(v) - bias; } inline void hkl_from_key(uint64_t key, int64_t& h, int64_t& k, int64_t& l) { constexpr uint64_t mask = (1ULL << 11) - 1; const uint64_t uh = (key >> 0) & mask; const uint64_t uk = (key >> 11) & mask; const uint64_t ul = (key >> 22) & mask; h = decode(uh); k = decode(uk); l = decode(ul); }