Skip to content

File powr.h

File List > code_source > templat > include > TempLat > util > powr.h

Go to the documentation of this file

#ifndef TEMPLAT_UTIL_POWR_H
#define TEMPLAT_UTIL_POWR_H

/* This file is part of TempLat, available at https://cosmolattice.github.io/templat .
   Copyright 2021-2026 The TempLat authors, see AUTHORS.md.
   Released under the MIT license, see LICENSE.md. */

// File info: Main contributor(s): Franz R. Sattler, Year: 2025

#include "TempLat/parallel/device.h"

namespace TempLat
{
  template <int n, typename NumberType>
    requires requires(NumberType x) {
      x * x;
      static_cast<NumberType>(1) / x;
    }
  constexpr DEVICE_INLINE_FUNCTION NumberType powr(const NumberType x)
  {
    if constexpr (n == 0)
      return static_cast<NumberType>(1);
    else if constexpr (n < 0)
      return static_cast<NumberType>(1) / powr<-n, NumberType>(x);
    else if constexpr (n == 1)
      return x;
    else if constexpr (n % 2 == 0)
      return powr<n / 2>(x) * powr<n / 2>(x);
    else
      return powr<n / 2>(x) * powr<n / 2>(x) * x;
  }
} // namespace TempLat

#endif