<commit_before>// This file is distributed under the MIT license.
// See the LICENSE file for details.

#pragma once

#ifndef VSNRAY_SAMPLER_COMMON_H
#define VSNRAY_SAMPLER_COMMON_H 1


#include <visionaray/detail/macros.h>
#include <visionaray/math/math.h>

#include "../forward.h"


namespace visionaray
{
namespace detail
{

//-------------------------------------------------------------------------------------------------
// Remap tex coord based on address mode
//

template <typename F, typename I>
F map_tex_coord(F const& coord, I const& texsize, tex_address_mode mode)
{
    F N = convert_to_float(texsize);

    switch (mode)
    {

    case Mirror:
        return select(
            (convert_to_int(floor(coord)) & I(1)) == 1, // if is odd
            F(texsize - 1) / F(texsize) - (coord - floor(coord)),
            coord - floor(coord)
            );

    case Wrap:
        return coord - floor(coord);

    case Clamp:
        // fall-through
    default:
        return clamp( coord, F(0.0), F(1.0) - F(1.0) / N );
    }
}

template <typename F, typename I>
F map_tex_coord(F const& coord, I const& texsize, std::array<tex_address_mode, 1> const& mode)
{
    return map_tex_coord(coord, texsize, mode[0]);
}

template <size_t Dim, typename F, typename I>
vector<Dim, F> map_tex_coord(
        vector<Dim, F> const&                       coord,
        vector<Dim, I> const&                       texsize,
        std::array<tex_address_mode, Dim> const&    mode
        )
{
    vector<Dim, F> result;

    for (size_t d = 0; d < Dim; ++d)
    {
        result[d] = map_tex_coord( coord[d], texsize[d], mode[d] );
    }

    return result;
}


//-------------------------------------------------------------------------------------------------
// Array access functions for scalar and SIMD types
// Return type as 3rd value for overload resolution!
//

template <typename RT, typename T>
inline RT point(T const* tex, ptrdiff_t idx, RT = RT())
{
    return tex[idx];
}


// SIMD: if multi-channel texture, assume AoS

template <typename T>
inline simd::float4 point(T const* tex, simd::int4 idx, simd::float4 /* result type */)
{

    VSNRAY_ALIGN(16) int indices[4];
    store(&indices[0], idx);
    return simd::float4(
            tex[indices[0]],
            tex[indices[1]],
            tex[indices[2]],
            tex[indices[3]]
            );

}

#if VSNRAY_SIMD_ISA >= VSNRAY_SIMD_ISA_AVX

template <typename T>
inline simd::float8 point(T const* tex, simd::int8 idx, simd::float8 /* result type */)
{
    VSNRAY_ALIGN(32) int indices[8];
    store(&indices[0], idx);
    return simd::float8(
            tex[indices[0]],
            tex[indices[1]],
            tex[indices[2]],
            tex[indices[3]],
            tex[indices[4]],
            tex[indices[5]],
            tex[indices[6]],
            tex[indices[7]]
            );
}

#endif // VSNRAY_SIMD_ISA >= VSNRAY_SIMD_ISA_AVX

inline vector<3, simd::float4> point(
        vector<3, unorm<8>> const*  tex,
        simd::int4                  idx,
        vector<3, simd::float4>     /* result type */
        )
{
    VSNRAY_ALIGN(16) int indices[4];
    store(&indices[0], idx);
    return vector<3, simd::float4>(
            simd::float4(tex[indices[0]].x, tex[indices[1]].x, tex[indices[2]].x, tex[indices[3]].x),
            simd::float4(tex[indices[0]].y, tex[indices[1]].y, tex[indices[2]].y, tex[indices[3]].y),
            simd::float4(tex[indices[0]].z, tex[indices[1]].z, tex[indices[2]].z, tex[indices[3]].z)
            );
}

inline vector<4, simd::float4> point(
        vector<4, float> const*     tex,
        simd::int4                  idx,
        vector<4, simd::float4>     /* result type */
        )
{

    // Special case: colors are AoS. Those can be obtained
    // without a context switch to GP registers by transposing
    // to SoA after memory lookup.

    simd::float4 iidx( idx * 4 );
    VSNRAY_ALIGN(16) int indices[4];
    store(&indices[0], iidx);

    float const* tmp = reinterpret_cast<float const*>(tex);

    vector<4, simd::float4> colors(
            &tmp[0] + indices[0],
            &tmp[0] + indices[1],
            &tmp[0] + indices[2],
            &tmp[0] + indices[3]
            );

    colors = transpose(colors);
    return colors;

}<commit_msg>Make casts more explicit<commit_after>// This file is distributed under the MIT license.
// See the LICENSE file for details.

#pragma once

#ifndef VSNRAY_SAMPLER_COMMON_H
#define VSNRAY_SAMPLER_COMMON_H 1


#include <visionaray/detail/macros.h>
#include <visionaray/math/math.h>

#include "../forward.h"


namespace visionaray
{
namespace detail
{

//-------------------------------------------------------------------------------------------------
// Remap tex coord based on address mode
//

template <typename F, typename I>
F map_tex_coord(F const& coord, I const& texsize, tex_address_mode mode)
{
    F N = convert_to_float(texsize);

    switch (mode)
    {

    case Mirror:
        return select(
            (convert_to_int(floor(coord)) & I(1)) == 1, // if is odd
            F(texsize - 1) / F(texsize) - (coord - floor(coord)),
            coord - floor(coord)
            );

    case Wrap:
        return coord - floor(coord);

    case Clamp:
        // fall-through
    default:
        return clamp( coord, F(0.0), F(1.0) - F(1.0) / N );
    }
}

template <typename F, typename I>
F map_tex_coord(F const& coord, I const& texsize, std::array<tex_address_mode, 1> const& mode)
{
    return map_tex_coord(coord, texsize, mode[0]);
}

template <size_t Dim, typename F, typename I>
vector<Dim, F> map_tex_coord(
        vector<Dim, F> const&                       coord,
        vector<Dim, I> const&                       texsize,
        std::array<tex_address_mode, Dim> const&    mode
        )
{
    vector<Dim, F> result;

    for (size_t d = 0; d < Dim; ++d)
    {
        result[d] = map_tex_coord( coord[d], texsize[d], mode[d] );
    }

    return result;
}


//-------------------------------------------------------------------------------------------------
// Array access functions for scalar and SIMD types
// Return type as 3rd value for overload resolution!
//

template <typename RT, typename T>
inline RT point(T const* tex, ptrdiff_t idx, RT = RT())
{
    return tex[idx];
}


// SIMD: if multi-channel texture, assume AoS

template <typename T>
inline simd::float4 point(T const* tex, simd::int4 idx, simd::float4 /* result type */)
{

    VSNRAY_ALIGN(16) int indices[4];
    store(&indices[0], idx);
    return simd::float4(
            tex[indices[0]],
            tex[indices[1]],
            tex[indices[2]],
            tex[indices[3]]
            );

}

#if VSNRAY_SIMD_ISA >= VSNRAY_SIMD_ISA_AVX

template <typename T>
inline simd::float8 point(T const* tex, simd::int8 idx, simd::float8 /* result type */)
{
    VSNRAY_ALIGN(32) int indices[8];
    store(&indices[0], idx);
    return simd::float8(
            tex[indices[0]],
            tex[indices[1]],
            tex[indices[2]],
            tex[indices[3]],
            tex[indices[4]],
            tex[indices[5]],
            tex[indices[6]],
            tex[indices[7]]
            );
}

#endif // VSNRAY_SIMD_ISA >= VSNRAY_SIMD_ISA_AVX

inline vector<3, simd::float4> point(
        vector<3, unorm<8>> const*  tex,
        simd::int4                  idx,
        vector<3, simd::float4>     /* result type */
        )
{
    VSNRAY_ALIGN(16) int indices[4];
    store(&indices[0], idx);
    return vector<3, simd::float4>(
            simd::float4(tex[indices[0]].x, tex[indices[1]].x, tex[indices[2]].x, tex[indices[3]].x),
            simd::float4(tex[indices[0]].y, tex[indices[1]].y, tex[indices[2]].y, tex[indices[3]].y),
            simd::float4(tex[indices[0]].z, tex[indices[1]].z, tex[indices[2]].z, tex[indices[3]].z)
            );
}

inline vector<4, simd::float4> point(
        vector<4, float> const*     tex,
        simd::int4                  idx,
        vector<4, simd::float4>     /* result type */
        )
{

    // Special case: colors are AoS. Those can be obtained
    // without a context switch to GP registers by transposing
    // to SoA after memory lookup.

    simd::float4 iidx( idx * 4 );
    VSNRAY_ALIGN(16) float indices[4];
    store(&indices[0], iidx);

    float const* tmp = reinterpret_cast<float const*>(tex);

    vector<4, simd::float4> colors(
            &tmp[0] + static_cast<int>(indices[0]),
            &tmp[0] + static_cast<int>(indices[1]),
            &tmp[0] + static_cast<int>(indices[2]),
            &tmp[0] + static_cast<int>(indices[3])
            );

    colors = transpose(colors);
    return colors;

}
{'fraction_non_alphanumeric': 0.18456569385165533, 'fraction_numerical': 0.021225054792940363, 'mean_word_length': 2.039971949509116, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2007060', 'n_tokens_mistral': 2931, 'n_tokens_neox': 2842, 'n_words': 695}