Skip to content
This repository was archived by the owner on Apr 2, 2021. It is now read-only.

Commit cb3f40b

Browse files
committed
Enforce distance2 index bounding
1 parent d3874cb commit cb3f40b

3 files changed

Lines changed: 17 additions & 3 deletions

File tree

FastNoiseSIMD/FastNoiseSIMD.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "FastNoiseSIMD.h"
3030
#include <assert.h>
3131
#include <stdlib.h>
32+
#include <algorithm>
3233
#include <cstdint>
3334

3435
#ifdef FN_COMPILE_NO_SIMD_FALLBACK
@@ -524,6 +525,15 @@ float FastNoiseSIMD::CalculateFractalBounding(int octaves, float gain)
524525
return 1.0f / ampFractal;
525526
}
526527

528+
void FastNoiseSIMD::SetCellularDistance2Indicies(int cellularDistanceIndex0, int cellularDistanceIndex1)
529+
{
530+
m_cellularDistanceIndex0 = std::min(cellularDistanceIndex0, cellularDistanceIndex1);
531+
m_cellularDistanceIndex1 = std::max(cellularDistanceIndex0, cellularDistanceIndex1);
532+
533+
m_cellularDistanceIndex0 = std::min(std::max(m_cellularDistanceIndex0, 0), FN_CELLULAR_INDEX_MAX);
534+
m_cellularDistanceIndex1 = std::min(std::max(m_cellularDistanceIndex1, 0), FN_CELLULAR_INDEX_MAX);
535+
}
536+
527537
void FastNoiseVectorSet::Free()
528538
{
529539
size = -1;

FastNoiseSIMD/FastNoiseSIMD.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,9 @@ class FastNoiseSIMD
198198

199199
// Sets the 2 distance indicies used for distance2 return types
200200
// Default: 0, 1
201-
void SetCellularDistance2Indicies(int cellularDistanceIndex0, int cellularDistanceIndex1) { m_cellularDistanceIndex0 = cellularDistanceIndex0; m_cellularDistanceIndex1 = cellularDistanceIndex1; }
201+
// Note: index0 should be lower than index1
202+
// index1 must be < 4, both indicies must be >= 0
203+
void SetCellularDistance2Indicies(int cellularDistanceIndex0, int cellularDistanceIndex1);
202204

203205

204206
// Enables position perturbing for all noise types
@@ -343,6 +345,8 @@ struct FastNoiseVectorSet
343345
void SetSize(int _size);
344346
};
345347

348+
#define FN_CELLULAR_INDEX_MAX 3
349+
346350
#define FN_NO_SIMD_FALLBACK 0
347351
#define FN_SSE2 1
348352
#define FN_SSE41 2

FastNoiseSIMD/FastNoiseSIMD_internal.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,8 +1888,8 @@ static SIMDf VECTORCALL FUNC(Cellular##returnFunc##distanceFunc##Single)(SIMDi s
18881888
#define CELLULAR_DISTANCE2CAVE_SINGLE(distanceFunc)\
18891889
static SIMDf VECTORCALL FUNC(CellularDistance2Cave##distanceFunc##Single)(SIMDi seed, SIMDf x, SIMDf y, SIMDf z, int index0, int index1)\
18901890
{\
1891-
SIMDf distance0[4] = {SIMDf_NUM(999999),SIMDf_NUM(999999),SIMDf_NUM(999999),SIMDf_NUM(999999)};\
1892-
SIMDf distance1[4] = {SIMDf_NUM(999999),SIMDf_NUM(999999),SIMDf_NUM(999999),SIMDf_NUM(999999)};\
1891+
SIMDf distance0[FN_CELLULAR_INDEX_MAX+1] = {SIMDf_NUM(999999),SIMDf_NUM(999999),SIMDf_NUM(999999),SIMDf_NUM(999999)};\
1892+
SIMDf distance1[FN_CELLULAR_INDEX_MAX+1] = {SIMDf_NUM(999999),SIMDf_NUM(999999),SIMDf_NUM(999999),SIMDf_NUM(999999)};\
18931893
\
18941894
SIMDi xc = SIMDi_SUB(SIMDi_CONVERT_TO_INT(x), SIMDi_NUM(1));\
18951895
SIMDi ycBase = SIMDi_SUB(SIMDi_CONVERT_TO_INT(y), SIMDi_NUM(1));\

0 commit comments

Comments
 (0)