// Copyright 2009 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "rkcommon/math/LinearSpace.ih"
#include "rkcommon/math/box.ih"

OSPRAY_BEGIN_ISPC_NAMESPACE

struct Data1D
{
  uint8 *addr;
  int64 byteStride;
  uint32 numItems;
  bool huge; // 64bit address calculation necessary
};

inline void Data1D_Constructor(Data1D *uniform self)
{
  self->addr = NULL;
  self->byteStride = 0;
  self->numItems = 0;
  self->huge = false;
}

// ok to pass-by-value, will be inlined and optimized
inline uniform bool valid(const uniform Data1D data)
{
  return data.addr != NULL;
}

// special 64-bit safe code:
#define BITS 20

template <typename T>
inline varying T get(const uniform Data1D &data, const varying int index)
{
  return *((const uniform T *)(data.addr + data.byteStride * index));
}

// ok to pass-by-value, will be inlined and optimized
#ifdef OSPRAY_TARGET_SYCL
// SYCL execution in a thread is scalar
// Old macro version (kept for compatibility with older code)
#define __define_get(T)                                                        \
  inline uniform T get_##T(const uniform Data1D data, const uniform int index) \
  {                                                                            \
    return *((const T *)(data.addr + data.byteStride * index));                \
  }
#else
#define __define_get(T)                                                        \
  inline T get_##T(const uniform Data1D data, const varying int index)         \
  {                                                                            \
    if (data.huge) {                                                           \
      T v;                                                                     \
      const int index_lo = index & ((1 << BITS) - 1);                          \
      const int index_hi = index - index_lo;                                   \
      const int scaledIndexLo = index_lo * data.byteStride;                    \
      foreach_unique (hi in index_hi) {                                        \
        /* uniform offset for upper bits */                                    \
        const uniform uint64 scaledIndexHi = (uint64)(hi)*data.byteStride;     \
        /* properly shifted base address (shifted by 64-bits) */               \
        const uint8 *uniform base_hi = data.addr + scaledIndexHi;              \
        v = *((const T *)(base_hi + scaledIndexLo));                           \
      }                                                                        \
      return v;                                                                \
    } else                                                                     \
      return *((const T *)(data.addr + data.byteStride * index));              \
  }                                                                            \
  inline uniform T get_##T(const uniform Data1D data, const uniform int index) \
  {                                                                            \
    return *((const T *)(data.addr + data.byteStride * index));                \
  }
#endif

__define_get(int32);
__define_get(vec2i);
__define_get(vec3i);
__define_get(vec4i);

__define_get(uint8);
__define_get(uint32);
__define_get(vec2ui);
__define_get(vec3ui);
__define_get(vec4ui);

__define_get(float);
__define_get(vec2f);
__define_get(vec3f);
__define_get(vec4f);

__define_get(box1f);
__define_get(box2f);
__define_get(box3f);
__define_get(box4f);

__define_get(linear3f);

#undef __define_get
#undef BITS

OSPRAY_END_ISPC_NAMESPACE
