Skip to content

Commit

Permalink
libmorton
Browse files Browse the repository at this point in the history
  • Loading branch information
Reputeless committed Aug 28, 2023
1 parent cb273f6 commit ca2cdb8
Show file tree
Hide file tree
Showing 17 changed files with 1,568 additions and 26 deletions.
1 change: 1 addition & 0 deletions Linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ set(SIV3D_INTERNAL_SOURCES
../Siv3D/src/Siv3D/Model/SivModel.cpp
../Siv3D/src/Siv3D/ModelObject/SivModelObject.cpp
../Siv3D/src/Siv3D/Monitor/SivMonitor.cpp
../Siv3D/src/Siv3D/Morton/SivMorton.cpp
../Siv3D/src/Siv3D/Mouse/MouseFactory.cpp
../Siv3D/src/Siv3D/Mouse/SivMouse.cpp
../Siv3D/src/Siv3D/MSRenderTexture/SivMSRenderTexture.cpp
Expand Down
3 changes: 3 additions & 0 deletions Siv3D/include/Siv3D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@
// Disjoint-set (Union-find) | Disjoint-set (Union–find)
# include <Siv3D/DisjointSet.hpp>

// モートン順序 | Morton order
# include <Siv3D/Morton.hpp>

//////////////////////////////////////////////////
//
// 並列・並行処理 | Parallel and Concurrent
Expand Down
64 changes: 64 additions & 0 deletions Siv3D/include/Siv3D/Morton.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//-----------------------------------------------
//
// This file is part of the Siv3D Engine.
//
// Copyright (c) 2008-2023 Ryo Suzuki
// Copyright (c) 2016-2023 OpenSiv3D Project
//
// Licensed under the MIT License.
//
//-----------------------------------------------

# pragma once
# include "Common.hpp"
# include "PointVector.hpp"

namespace s3d
{
/// @brief 32-bit にエンコードされたモートン順序
using Morton32 = uint32;

/// @brief 64-bit にエンコードされたモートン順序
using Morton64 = uint64;

namespace Morton
{
/// @brief 2D 座標を 32-bit のモートン順序にエンコードして返します。
/// @param x X 座標
/// @param y Y 座標
/// @return モートン順序
[[nodiscard]]
Morton32 Encode2D32(uint16 x, uint16 y) noexcept;

/// @brief 2D 座標を 64-bit のモートン順序にエンコードして返します。
/// @param x X 座標
/// @param y Y 座標
/// @return モートン順序
[[nodiscard]]
Morton64 Encode2D64(uint32 x, uint32 y) noexcept;

/// @brief 2D 座標を 32-bit のモートン順序にエンコードして返します。
/// @param pos 座標
/// @return モートン順序
[[nodiscard]]
Morton32 Encode2D32(Point pos) noexcept;

/// @brief 2D 座標を 64-bit のモートン順序にエンコードして返します。
/// @param pos 座標
/// @return モートン順序
[[nodiscard]]
Morton64 Encode2D64(Point pos) noexcept;

/// @brief 32-bit のモートン順序を 2D 座標にデコードして返します。
/// @param morton モートン順序
/// @return 2D 座標
[[nodiscard]]
Point Decode2D32(Morton32 morton) noexcept;

/// @brief 64-bit のモートン順序を 2D 座標にデコードして返します。
/// @param morton モートン順序
/// @return 2D 座標
[[nodiscard]]
Point Decode2D64(Morton64 morton) noexcept;
}
}
76 changes: 50 additions & 26 deletions Siv3D/src/Siv3D/LicenseManager/LicenseList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,30 @@ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.)-"
},

{
UR"-(libmorton)-",

UR"-(Copyright (c) 2016 Jeroen Baert)-",

UR"-(Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.)-"
},

{
UR"-(libogg)-",

Expand Down Expand Up @@ -930,6 +954,32 @@ this source code in a product, acknowledgment is not required but would
be appreciated.)-"
},

{
UR"-(libtiff)-",

UR"-(Copyright (c) 1988-1997 Sam Leffler
Copyright (c) 1991-1997 Silicon Graphics, Inc.)-",

UR"-(Permission to use, copy, modify, distribute, and sell this software and
its documentation for any purpose is hereby granted without fee, provided
that (i) the above copyright notices and this permission notice appear in
all copies of the software and related documentation, and (ii) the names of
Sam Leffler and Silicon Graphics may not be used in any advertising or
publicity relating to the software without the specific, prior written
permission of Sam Leffler and Silicon Graphics.
THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
OF THIS SOFTWARE.)-"
},

{
UR"-(libvorbis)-",

Expand Down Expand Up @@ -963,32 +1013,6 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.)-"
},

{
UR"-(libtiff)-",

UR"-(Copyright (c) 1988-1997 Sam Leffler
Copyright (c) 1991-1997 Silicon Graphics, Inc.)-",

UR"-(Permission to use, copy, modify, distribute, and sell this software and
its documentation for any purpose is hereby granted without fee, provided
that (i) the above copyright notices and this permission notice appear in
all copies of the software and related documentation, and (ii) the names of
Sam Leffler and Silicon Graphics may not be used in any advertising or
publicity relating to the software without the specific, prior written
permission of Sam Leffler and Silicon Graphics.
THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
OF THIS SOFTWARE.)-"
},

{
UR"-(libwebp)-",

Expand Down
53 changes: 53 additions & 0 deletions Siv3D/src/Siv3D/Morton/SivMorton.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//-----------------------------------------------
//
// This file is part of the Siv3D Engine.
//
// Copyright (c) 2008-2023 Ryo Suzuki
// Copyright (c) 2016-2023 OpenSiv3D Project
//
// Licensed under the MIT License.
//
//-----------------------------------------------

# include <ThirdParty/libmorton/morton.h>
# include <Siv3D/Morton.hpp>

namespace s3d
{
namespace Morton
{
Morton32 Encode2D32(const uint16 x, const uint16 y) noexcept
{
return libmorton::morton2D_32_encode(x, y);
}

Morton64 Encode2D64(const uint32 x, const uint32 y) noexcept
{
return libmorton::morton2D_64_encode(x, y);
}

Morton32 Encode2D32(const Point pos) noexcept
{
return libmorton::morton2D_32_encode(static_cast<std::uint_fast16_t>(pos.x), static_cast<std::uint_fast16_t>(pos.y));
}

Morton64 Encode2D64(const Point pos) noexcept
{
return libmorton::morton2D_64_encode(static_cast<std::uint_fast32_t>(pos.x), static_cast<std::uint_fast32_t>(pos.y));
}

Point Decode2D32(const Morton32 morton) noexcept
{
std::uint_fast16_t x, y;
libmorton::morton2D_32_decode(morton, x, y);
return{ x, y };
}

Point Decode2D64(const Morton64 morton) noexcept
{
std::uint_fast32_t x, y;
libmorton::morton2D_64_decode(morton, x, y);
return{ x, y };
}
}
}
106 changes: 106 additions & 0 deletions Siv3D/src/ThirdParty/libmorton/morton.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#pragma once

// This file will always contain inline functions which point to the fastest Morton encoding/decoding implementation
// IF you just want to use the fastest method to encode/decode morton codes, include this header.

// If you want to experiment with alternative methods (which might be slower, all depending on hardware / your data set)
// check the individual headers below.

#include "morton2D.h"
#include "morton3D.h"

#if defined(__BMI2__) || defined(__AVX2__)
#include "morton_BMI.h"
#elif defined(__AVX512BITALG__)
#include "morton_AVX512BITALG.h"
#endif

namespace libmorton {
// Functions under this are stubs which will always point to fastest implementation at the moment
//-----------------------------------------------------------------------------------------------

// ENCODING
#if defined(__BMI2__) || defined(__AVX2__)
inline uint_fast32_t morton2D_32_encode(const uint_fast16_t x, const uint_fast16_t y) {
return m2D_e_BMI<uint_fast32_t, uint_fast16_t>(x, y);
}
inline uint_fast64_t morton2D_64_encode(const uint_fast32_t x, const uint_fast32_t y) {
return m2D_e_BMI<uint_fast64_t, uint_fast32_t>(x, y);
}
inline uint_fast32_t morton3D_32_encode(const uint_fast16_t x, const uint_fast16_t y, const uint_fast16_t z) {
return m3D_e_BMI<uint_fast32_t, uint_fast16_t>(x, y, z);
}
inline uint_fast64_t morton3D_64_encode(const uint_fast32_t x, const uint_fast32_t y, const uint_fast32_t z) {
return m3D_e_BMI<uint_fast64_t, uint_fast32_t>(x, y, z);
}
#elif defined(__AVX512BITALG__)
inline uint_fast32_t morton2D_32_encode(const uint_fast16_t x, const uint_fast16_t y) {
return m2D_e_BITALG<uint_fast32_t, uint_fast16_t>(x, y);
}
inline uint_fast64_t morton2D_64_encode(const uint_fast32_t x, const uint_fast32_t y) {
return m2D_e_BITALG<uint_fast64_t, uint_fast32_t>(x, y);
}
inline uint_fast32_t morton3D_32_encode(const uint_fast16_t x, const uint_fast16_t y, const uint_fast16_t z) {
return m3D_e_BITALG<uint_fast32_t, uint_fast16_t>(x, y, z);
}
inline uint_fast64_t morton3D_64_encode(const uint_fast32_t x, const uint_fast32_t y, const uint_fast32_t z) {
return m3D_e_BITALG<uint_fast64_t, uint_fast32_t>(x, y, z);
}
#else
inline uint_fast32_t morton2D_32_encode(const uint_fast16_t x, const uint_fast16_t y) {
return m2D_e_magicbits_combined(x, y);
}
inline uint_fast64_t morton2D_64_encode(const uint_fast32_t x, const uint_fast32_t y) {
return m2D_e_sLUT<uint_fast64_t, uint_fast32_t>(x, y);
}
inline uint_fast32_t morton3D_32_encode(const uint_fast16_t x, const uint_fast16_t y, const uint_fast16_t z) {
return m3D_e_sLUT<uint_fast32_t, uint_fast16_t>(x, y, z);
}
inline uint_fast64_t morton3D_64_encode(const uint_fast32_t x, const uint_fast32_t y, const uint_fast32_t z) {
return m3D_e_sLUT<uint_fast64_t, uint_fast32_t>(x, y, z);
}
#endif

// DECODING

#if defined(__BMI2__) || defined(__AVX2__)
inline void morton2D_32_decode(const uint_fast32_t morton, uint_fast16_t& x, uint_fast16_t& y) {
m2D_d_BMI<uint_fast32_t, uint_fast16_t>(morton, x, y);
}
inline void morton2D_64_decode(const uint_fast64_t morton, uint_fast32_t& x, uint_fast32_t& y) {
m2D_d_BMI<uint_fast64_t, uint_fast32_t>(morton, x, y);
}
inline void morton3D_32_decode(const uint_fast32_t morton, uint_fast16_t& x, uint_fast16_t& y, uint_fast16_t& z) {
m3D_d_BMI<uint_fast32_t, uint_fast16_t>(morton, x, y, z);
}
inline void morton3D_64_decode(const uint_fast64_t morton, uint_fast32_t& x, uint_fast32_t& y, uint_fast32_t& z) {
m3D_d_BMI<uint_fast64_t, uint_fast32_t>(morton, x, y, z);
}
#elif defined(__AVX512BITALG__)
inline void morton2D_32_decode(const uint_fast32_t morton, uint_fast16_t& x, uint_fast16_t& y) {
m2D_d_BITALG<uint_fast32_t, uint_fast16_t>(morton, x, y);
}
inline void morton2D_64_decode(const uint_fast64_t morton, uint_fast32_t& x, uint_fast32_t& y) {
m2D_d_BITALG<uint_fast64_t, uint_fast32_t>(morton, x, y);
}
inline void morton3D_32_decode(const uint_fast32_t morton, uint_fast16_t& x, uint_fast16_t& y, uint_fast16_t& z) {
m3D_d_BITALG<uint_fast32_t, uint_fast16_t>(morton, x, y, z);
}
inline void morton3D_64_decode(const uint_fast64_t morton, uint_fast32_t& x, uint_fast32_t& y, uint_fast32_t& z) {
m3D_d_BITALG<uint_fast64_t, uint_fast32_t>(morton, x, y, z);
}
#else
inline void morton2D_32_decode(const uint_fast32_t morton, uint_fast16_t& x, uint_fast16_t& y) {
m2D_d_magicbits_combined(morton, x, y);
}
inline void morton2D_64_decode(const uint_fast64_t morton, uint_fast32_t& x, uint_fast32_t& y) {
m2D_d_sLUT<uint_fast64_t, uint_fast32_t>(morton, x, y);
}
inline void morton3D_32_decode(const uint_fast32_t morton, uint_fast16_t& x, uint_fast16_t& y, uint_fast16_t& z) {
m3D_d_sLUT<uint_fast32_t, uint_fast16_t>(morton, x, y, z);
}
inline void morton3D_64_decode(const uint_fast64_t morton, uint_fast32_t& x, uint_fast32_t& y, uint_fast32_t& z) {
m3D_d_sLUT<uint_fast64_t, uint_fast32_t>(morton, x, y, z);
}
#endif
}
Loading

0 comments on commit ca2cdb8

Please sign in to comment.