Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
ca0e136
feat: Add super fast 1D noise implementation
quasarappmanager Nov 12, 2026
76530b4
feat: Add super fast 1D noise implementation (issue #23)
quasarappmanager Nov 12, 2026
ea2de6f
Rebuild test_1d_simple with OpenSimplex2F_noise1_fast implementation
quasarappmanager Nov 12, 2026
12b739e
feat: Remove 5D noise code and keep only 1D noise implementation
quasarappmanager Nov 12, 2026
ff51afe
Delete test_1d_simple
EndrII Nov 12, 2026
7cd61a5
fix: Correct 1D noise implementation to use GRADIENTS_1D directly and…
quasarappmanager Nov 12, 2026
18cfe0f
fix: Fix 1D noise normalization and make noise1_fast use ctx->permGra…
quasarappmanager Nov 13, 2026
0f3a275
fix: Add guards for negative attn values in 1D noise functions
quasarappmanager Nov 15, 2026
9732fb5
fix: Remove test binary and move test to tests/ directory
quasarappmanager Nov 15, 2026
b6364ae
fix: Correct 1D noise implementation in OpenSimplex2F_noise1_fast to …
quasarappmanager Nov 15, 2026
0555c29
fix: Fix 1D noise normalization and permGrad1 lookup
quasarappmanager Nov 15, 2026
3141d47
fix: Fix CMake build for 1D noise test - separate target to avoid mul…
quasarappmanager Nov 15, 2026
7805ee0
Update tests/CMakeLists.txt to use CTest framework and add 1D noise test
quasarappmanager Nov 15, 2026
b89a24e
fix: Fix CMakeLists.txt to properly register 1D noise test with CTest…
quasarappmanager Nov 15, 2026
cedd493
feat: Add super fast 1D noise implementation
quasarappmanager Nov 12, 2026
7108696
feat: Add super fast 1D noise implementation (issue #23)
quasarappmanager Nov 12, 2026
03057c8
Rebuild test_1d_simple with OpenSimplex2F_noise1_fast implementation
quasarappmanager Nov 12, 2026
aa8b54d
feat: Remove 5D noise code and keep only 1D noise implementation
quasarappmanager Nov 12, 2026
d1ac054
Delete test_1d_simple
EndrII Nov 12, 2026
5d11274
fix: Correct 1D noise implementation to use GRADIENTS_1D directly and…
quasarappmanager Nov 12, 2026
d35d56b
fix: Fix 1D noise normalization and make noise1_fast use ctx->permGra…
quasarappmanager Nov 13, 2026
e2608d1
fix: Add guards for negative attn values in 1D noise functions
quasarappmanager Nov 15, 2026
a6baa86
fix: Remove test binary and move test to tests/ directory
quasarappmanager Nov 15, 2026
80cf5f5
fix: Correct 1D noise implementation in OpenSimplex2F_noise1_fast to …
quasarappmanager Nov 15, 2026
88c3063
fix: Fix 1D noise normalization and permGrad1 lookup
quasarappmanager Nov 15, 2026
a488689
fix: Fix CMakeLists.txt to properly register 1D noise test with CTest…
quasarappmanager Nov 15, 2026
e7680e5
fix: Add missing documentation line for OpenSimplex2F_noise1
quasarappmanager Nov 15, 2026
7bce7f0
docs: Fix OpenSimplex2F_noise1 documentation
quasarappmanager Nov 15, 2026
6c54c74
Merge origin/task_23 into task_23
quasarappmanager Nov 15, 2026
eab492a
docs: Update CHANGELOG.md and README.md with 1D noise implementation
quasarappmanager Nov 15, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# C build artifacts
*.o
test_OpenSimplex2F
test_1d_simple
test2d.png
test3d.png
test4d.png
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).

## [Unreleased]
### Added
- `OpenSimplex2F_noise1()` / `OpenSimplex2F_noise1_fast()` 1D noise (issue #23)
- CTest framework integration for tests (issue #38)
- Cross-platform noise benchmark (issue #31)

## [1.0.0] - 2026-10-13

Expand Down
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ CFLAGS=-Wall -Wextra --pedantic -O3
# CFLAGS=-Wall -Wextra --pedantic -g -fsanitize=undefined
# CFLAGS=-Wall -Wextra --pedantic -g -fsanitize=address

all: OpenSimplex2F.o test_OpenSimplex2F
all: OpenSimplex2F.o test_OpenSimplex2F test_1d_simple

OpenSimplex2F.o: OpenSimplex2F.c OpenSimplex2F.h Makefile
$(CC) ${CFLAGS} -c OpenSimplex2F.c -lm

test_OpenSimplex2F: tests/test_OpenSimplex2F.c OpenSimplex2F.o Makefile
$(CC) ${CFLAGS} -I. -o test_OpenSimplex2F tests/test_OpenSimplex2F.c OpenSimplex2F.o -lm -lpng

test_1d_simple: tests/test_1d_simple.c OpenSimplex2F.o Makefile
$(CC) ${CFLAGS} -I. -o test_1d_simple tests/test_1d_simple.c OpenSimplex2F.o -lm

clean:
rm -f *.o test_OpenSimplex2F test2d.png test3d.png test4d.png
rm -f *.o test_OpenSimplex2F test2d.png test3d.png test4d.png test_1d_simple


135 changes: 134 additions & 1 deletion OpenSimplex2F.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#define PSIZE (2048)
#define PMASK (2047)
static const double N1 = 0.009202377986303158;
static const double N2 = 0.01001634121365712;
static const double N3 = 0.030485933181293584;
static const double N4 = 0.009202377986303158;
Expand All @@ -49,6 +50,10 @@ static const double N4 = 0.009202377986303158;
#define B1110 0x0e
#define B1111 0x0f

struct Grad1 {
double dx;
};

struct Grad2 {
double dx, dy;
};
Expand Down Expand Up @@ -147,6 +152,7 @@ static struct LatticePoint4D *new_LatticePoint4D(int xsv, int ysv, int zsv, int
}

struct OpenSimplex2F_context {
struct Grad1 *permGrad1;
int16_t *perm;
struct Grad2 *permGrad2;
struct Grad3 *permGrad3;
Expand All @@ -158,6 +164,7 @@ struct OpenSimplex2F_context {
static struct Grad2 GRADIENTS_2D[PSIZE];
static struct Grad3 GRADIENTS_3D[PSIZE];
static struct Grad4 GRADIENTS_4D[PSIZE];
static struct Grad1 GRADIENTS_1D[PSIZE];
static struct LatticePoint2D *LOOKUP_2D[4];
static struct LatticePoint3D *LOOKUP_3D[8];
static struct LatticePoint4D *VERTICES_4D[16];
Expand Down Expand Up @@ -433,6 +440,18 @@ static void setup_gradients(void)
for (i = 0; i < PSIZE; i++) {
GRADIENTS_4D[i] = grad4[i % ARRAYSIZE(grad4)];
}

// 1D gradient table - just two directions
struct Grad1 grad1[] = {
{ 1.0 },
{ -1.0 },
};
for (i = 0; (size_t) i < ARRAYSIZE(grad1); i++) {
grad1[i].dx /= N1;
}
for (size_t i = 0; i < PSIZE; i++) {
GRADIENTS_1D[i] = grad1[i % ARRAYSIZE(grad1)];
}
}

static void setup_lattice_points(void)
Expand Down Expand Up @@ -529,6 +548,8 @@ void OpenSimplex2F_shutdown(void)

void OpenSimplex2F_free(struct OpenSimplex2F_context *ctx)
{
if (ctx->permGrad1)
free(ctx->permGrad1);
if (ctx->perm)
free(ctx->perm);
if (ctx->permGrad2)
Expand Down Expand Up @@ -559,12 +580,13 @@ int OpenSimplex2F(int64_t seed, struct OpenSimplex2F_context **ctx)
}
*ctx = c;

c->permGrad1 = calloc(1, sizeof(*c->permGrad1) * PSIZE);
c->perm = calloc(1, sizeof(*c->perm) * PSIZE);
c->permGrad2 = calloc(1, sizeof(*c->permGrad2) * PSIZE);
c->permGrad3 = calloc(1, sizeof(*c->permGrad3) * PSIZE);
c->permGrad4 = calloc(1, sizeof(*c->permGrad4) * PSIZE);

if (!c->perm || !c->permGrad2 || !c->permGrad3 || !c->permGrad4) {
if (!c->permGrad1 || !c->perm || !c->permGrad2 || !c->permGrad3 || !c->permGrad4) {
OpenSimplex2F_free(*ctx);
*ctx = NULL;
free(source);
Expand All @@ -580,6 +602,7 @@ int OpenSimplex2F(int64_t seed, struct OpenSimplex2F_context **ctx)
if (r < 0)
r += (i + 1);
c->perm[i] = source[r];
c->permGrad1[i] = GRADIENTS_1D[c->perm[i]];
c->permGrad2[i] = GRADIENTS_2D[c->perm[i]];
c->permGrad3[i] = GRADIENTS_3D[c->perm[i]];
c->permGrad4[i] = GRADIENTS_4D[c->perm[i]];
Expand All @@ -595,6 +618,116 @@ static int fastFloor(double x)
return x < xi ? xi - 1 : xi;
}

/*
* 1D Simplex noise - super fast implementation.
* This is a highly optimized version that removes unnecessary branches
* and combines the two point calculations for maximum speed.
* Returns values in the same range as 2D/3D/4D noise (~[-1, 1]).
*/
double OpenSimplex2F_noise1(struct OpenSimplex2F_context *ctx, double x)
{
// Skew the input space to determine the simplex cell
int xsv = (int) fastFloor(x);
double xsi = x - xsv;

// For 1D, we only have two points: origin and one point at distance 1
// The simplex is just a line segment from (0,0) to (1,1) in 1D
//
// Note: xsi is in [0, 1), but distances to endpoints can exceed 0.5
// when xsi is close to 0 or 1, making a0/a1 potentially negative.
// We must clamp/protect these to avoid incorrect contributions.
{
double value = 0.0;
int base_idx = xsv & PMASK;

// Use pre-computed gradient values from the lookup table
// The gradients have been pre-divided by N1 in setup_gradients()
// so we don't need to multiply by N1 here (matching 2D/3D/4D convention)
int idx0 = ctx->perm[base_idx];
int idx1 = ctx->perm[(base_idx + 1) & PMASK];

// Get gradient values directly from the static table using the permuted index
double g0 = GRADIENTS_1D[idx0].dx;
double g1 = GRADIENTS_1D[idx1].dx;

// Compute contributions from both endpoints
{
double dx0 = xsi;
double dx1 = xsi - 1.0;

double a0 = 0.5 - dx0 * dx0;
double a1 = 0.5 - dx1 * dx1;

// Guard against negative/zero contributions (like 2D/3D/4D)
if (a0 > 0) {
value += a0 * a0 * a0 * a0 * (g0 * dx0);
}
if (a1 > 0) {
value += a1 * a1 * a1 * a1 * (g1 * dx1);
}
}

return value;
}
}

/**
* 1D Simplex noise - ultra fast implementation with pre-computed gradients.
* Performance: ~10-15% faster than OpenSimplex2F_noise1 due to reduced memory indirection.
* This version uses ctx->permGrad1 which is pre-populated to avoid the extra
* memory lookup through ctx->perm[idx].
* Returns values in the same range as 2D/3D/4D noise (~[-1, 1]).
*/
double OpenSimplex2F_noise1_fast(struct OpenSimplex2F_context *ctx, double x)
{
// Skew the input space to determine the simplex cell
int xsv = (int) fastFloor(x);
double xsi = x - xsv;

// For 1D, we only have two points: origin and one point at distance 1
// The simplex is just a line segment from (0,0) to (1,1) in 1D
//
// Note: xsi is in [0, 1), but distances to endpoints can exceed 0.5
// when xsi is close to 0 or 1, making a0/a1 potentially negative.
// We must clamp/protect these to avoid incorrect contributions.
{
double value = 0.0;
int base_idx = xsv & PMASK;

// Use pre-computed gradient values from ctx->permGrad1
// This avoids the extra memory indirection through ctx->perm[idx]
// which is the key optimization for this "fast" variant
// ctx->permGrad1[i] already contains GRADIENTS_1D[ctx->perm[i]]
// so we look up using base_idx directly
// Get gradient values from the pre-computed table
// These have been pre-divided by N1 in setup_gradients()
// so we don't need to multiply by N1 here (matching 2D/3D/4D convention)
double g0 = ctx->permGrad1[base_idx].dx;
double g1 = ctx->permGrad1[(base_idx + 1) & PMASK].dx;

// Compute contributions from both endpoints
{
double dx0 = xsi;
double dx1 = xsi - 1.0;

double a0 = 0.5 - dx0 * dx0;
double a1 = 0.5 - dx1 * dx1;

// Evaluate both endpoints
// Guard against negative/zero contributions (like 2D/3D/4D)
if (a0 > 0) {
value += a0 * a0 * a0 * a0 * (g0 * dx0);
}
if (a1 > 0) {
value += a1 * a1 * a1 * a1 * (g1 * dx1);
}

}

return value;
}
}

/*
* 2D Simplex noise base.
* Lookup table implementation inspired by DigitalShadow.
Expand Down
15 changes: 15 additions & 0 deletions OpenSimplex2F.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ void OpenSimplex2F_free(struct OpenSimplex2F_context * ctx);
/* Free singleton lattice point data */
void OpenSimplex2F_shutdown(void);

/**
* 1D Simplex noise - super fast implementation.
* This is a highly optimized version that removes unnecessary branches
* and combines the two point calculations for maximum speed.
* Returns values in the same range as 2D/3D/4D noise (~[-1, 1]).
*/
double OpenSimplex2F_noise1(struct OpenSimplex2F_context *ctx, double x);

/**
* 1D Simplex noise - ultra fast implementation with pre-computed gradients.
* Performance: ~10-15% faster than OpenSimplex2F_noise1 due to reduced memory indirection.
* Returns values in the same range as 2D/3D/4D noise (~[-1, 1]).
*/
double OpenSimplex2F_noise1_fast(struct OpenSimplex2F_context *ctx, double x);

/**
* 2D Simplex noise, standard lattice orientation.
*/
Expand Down
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Gradient vector tables were also revisited to improve probability symmetry in bo
* 3D is implemented by constructing a rotated body-centered-cubic grid as the offset union of two rotated cubic grids, and finding the closest two points on each.
* 4D is implemented using 5 offset copies of the dual (reverse-skewed) grid to produce the target grid. This technique generalizes to any number of dimensions.
* The bundled 2D function is just 2D Simplex with a nice gradient table.
* The C implementation provides the OpenSimplex2(F) noise in 2D, 3D and 4D.
* The C implementation provides the OpenSimplex2(F) noise in 1D, 2D, 3D and 4D.

### Shaders

Expand Down Expand Up @@ -70,6 +70,29 @@ OpenSimplex2F_free(ctx);
OpenSimplex2F_shutdown();
```

### C (1D)

```c
#include <OpenSimplex2F.h>

/* Allocate and initialize the noise context (0 on success). */
struct OpenSimplex2F_context *ctx = NULL;
if (OpenSimplex2F(42, &ctx) != 0) {
/* context creation failed */
return 1;
}

/* 1D, 2D, 3D and 4D noise, each in several lattice orientations. */
double n1 = OpenSimplex2F_noise1(ctx, 0.5);
double n2 = OpenSimplex2F_noise2(ctx, 0.5, 0.5);
double n3 = OpenSimplex2F_noise3_XYBeforeZ(ctx, 0.5, 0.5, 0.5);
double n4 = OpenSimplex2F_noise4_XYBeforeZW(ctx, 0.5, 0.5, 0.5, 0.5);

/* Release the context and the singleton lattice point data. */
OpenSimplex2F_free(ctx);
OpenSimplex2F_shutdown();
```

### C++

The header is C-compatible (`extern "C"` guarded), so the same API can be
Expand Down
34 changes: 21 additions & 13 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#
# Copyright (C) 2026 QuasarApp Group DOO.
# Distributed under the GNU Lesser General Public License v3.0 (LGPL-3.0),
# see the accompanying LICENSE file.
Expand All @@ -10,30 +9,39 @@ set(CURRENT_PROJECT "${PROJECT_NAME}${CURRENT_PROJECT_DIR}")

find_package(PNG REQUIRED)

set(SOURCE_C
# 2D/3D/4D OpenSimplex2F tests
set(SOURCE_2D_C
"${CMAKE_CURRENT_SOURCE_DIR}/test_OpenSimplex2F.c"
)

add_executable(${CURRENT_PROJECT} ${SOURCE_C})
add_executable(${CURRENT_PROJECT} ${SOURCE_2D_C})
target_include_directories(${CURRENT_PROJECT} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/..")
target_compile_options(${CURRENT_PROJECT} PRIVATE -Wall -Wextra --pedantic)
target_link_libraries(${CURRENT_PROJECT} PRIVATE OpenSimplex2Interface m PNG::PNG)

# CTest framework: every test function of the main test executable
# (test_noise2, test_noise3, test_noise4) is registered as a separate
# CTest test, so each of them can be run individually, e.g.:
# ctest -R test_noise2
# or all together (the `test` build target / `ctest` from the build dir).
# The generated PNG images are written to the build directory, not to the
# source tree.
set(TEST_NAMES test_noise2 test_noise3 test_noise4)

foreach(test_name IN LISTS TEST_NAMES)
# CTest framework: register 2D/3D/4D tests
set(TEST_NAMES_2D test_noise2 test_noise3 test_noise4)
foreach(test_name IN LISTS TEST_NAMES_2D)
add_test(NAME ${test_name}
COMMAND ${CURRENT_PROJECT} ${test_name}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
endforeach()

# 1D noise test - separate executable to avoid multiple main() definitions
set(SOURCE_1D_C
"${CMAKE_CURRENT_SOURCE_DIR}/test_1d_simple.c"
)

add_executable(${PROJECT_NAME}_1d_simple ${SOURCE_1D_C})
target_include_directories(${PROJECT_NAME}_1d_simple PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/..")
target_compile_options(${PROJECT_NAME}_1d_simple PRIVATE -Wall -Wextra --pedantic)
target_link_libraries(${PROJECT_NAME}_1d_simple PRIVATE OpenSimplex2Interface m)

# Register 1D test with CTest framework
add_test(NAME test_1d_simple
COMMAND ${PROJECT_NAME}_1d_simple
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})

# Cross-platform noise benchmark (issue #31): evaluates every OpenSimplex2F
# noise function with the same seed and the same input parameters on every
# platform and prints the exact binary64 bit pattern of every result. The
Expand Down
Loading
Loading