Skip to content

Comments

Fix simplex noise using mod(i,255) instead of iand(i,255)#1218

Open
sbryngelson wants to merge 1 commit intoMFlowCode:masterfrom
sbryngelson:fix/simplex-noise-mod-range
Open

Fix simplex noise using mod(i,255) instead of iand(i,255)#1218
sbryngelson wants to merge 1 commit intoMFlowCode:masterfrom
sbryngelson:fix/simplex-noise-mod-range

Conversation

@sbryngelson
Copy link
Member

@sbryngelson sbryngelson commented Feb 21, 2026

User description

Summary

  • Fix 2D simplex noise in m_simplex_noise.fpp using mod(i, 255) which produces range 0-254, skipping index 255 of the permutation table.

Bug Details

The simplex noise implementation hashes grid coordinates into the permutation table using:

ii = mod(i, 255)    ! range 0..254
jj = mod(j, 255)    ! range 0..254

The permutation table p_vec has 256 entries (indices 0-255). Using mod(i, 255) gives range 0-254, so p_vec(255) is never accessed. This introduces a subtle bias in the noise distribution — one permutation entry is systematically excluded, slightly reducing the randomness of the noise pattern.

The standard simplex noise algorithm uses mod(i, 256) or equivalently iand(i, 255) (bitwise AND with 255) to wrap indices to the full 0-255 range.

Fix

Replace mod(i, 255) with iand(i, 255) which gives the correct 0-255 range and is also faster (bitwise operation vs division).

Test plan

  • Simplex noise perturbation cases still produce valid noise fields
  • All 256 permutation table entries are now reachable

🤖 Generated with Claude Code


CodeAnt-AI Description

Fix incorrect index wrapping in 2D simplex noise so all permutation entries are used

What Changed

  • Index wrapping for the simplex noise permutation now covers the full 0–255 range, so the previously skipped permutation entry is reachable
  • Noise outputs no longer exclude one permutation value, removing a subtle bias in generated noise patterns
  • Index wrapping uses a bitwise operation which is marginally faster than the previous modulus approach

Impact

✅ Correct simplex noise distribution
✅ Full permutation table coverage (0–255)
✅ Slightly faster noise generation

💡 Usage Guide

Checking Your Pull Request

Every time you make a pull request, our system automatically looks through it. We check for security issues, mistakes in how you're setting up your infrastructure, and common code problems. We do this to make sure your changes are solid and won't cause any trouble later.

Talking to CodeAnt AI

Got a question or need a hand with something in your pull request? You can easily get in touch with CodeAnt AI right here. Just type the following in a comment on your pull request, and replace "Your question here" with whatever you want to ask:

@codeant-ai ask: Your question here

This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.

Example

@codeant-ai ask: Can you suggest a safer alternative to storing this secret?

Preserve Org Learnings with CodeAnt

You can record team preferences so CodeAnt AI applies them in future reviews. Reply directly to the specific CodeAnt AI suggestion (in the same thread) and replace "Your feedback here" with your input:

@codeant-ai: Your feedback here

This helps CodeAnt AI learn and adapt to your team's coding style and standards.

Example

@codeant-ai: Do not flag unused imports.

Retrigger review

Ask CodeAnt AI to review the PR again, by typing:

@codeant-ai: review

Check Your Repository Health

To analyze the health of your code repository, visit our dashboard at https://app.codeant.ai. This tool helps you identify potential issues and areas for improvement in your codebase, ensuring your repository maintains high standards of code health.

Fixes #1223

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings February 21, 2026 05:03
@codeant-ai
Copy link
Contributor

codeant-ai bot commented Feb 21, 2026

CodeAnt AI is reviewing your PR.


Thanks for using CodeAnt! 🎉

We're free for open-source projects. if you're enjoying it, help us grow by sharing.

Share on X ·
Reddit ·
LinkedIn

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 21, 2026

Warning

Rate limit exceeded

@sbryngelson has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 18 minutes and 5 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codeant-ai codeant-ai bot added the size:XS This PR changes 0-9 lines, ignoring generated files label Feb 21, 2026
cubic-dev-ai[bot]

This comment was marked as off-topic.

@codeant-ai
Copy link
Contributor

codeant-ai bot commented Feb 21, 2026

Nitpicks 🔍

🔒 No security issues identified
⚡ Recommended areas for review

  • Portability of bitwise wrap
    The new wrapping uses a bitwise AND (iand(i, 255)) which assumes two's-complement integer representation and that bitwise AND semantics match mathematical modulo 256 for all inputs (including negative i/j). On unusual platforms or non-standard integer representations this may differ from mod(i,256). Verify the intended behaviour for negative inputs and target compilers/platforms.

@codeant-ai
Copy link
Contributor

codeant-ai bot commented Feb 21, 2026

CodeAnt AI finished reviewing your PR.

@sbryngelson sbryngelson added the bug Something isn't working or doesn't seem right label Feb 21, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug in the 2D simplex noise implementation where mod(i, 255) incorrectly produces a range of 0-254, leaving one permutation table entry inaccessible and introducing bias in the noise distribution.

Changes:

  • Replace mod(i, 255) with iand(i, 255) to correctly access all 256 permutation table entries (0-255)
  • Replace mod(j, 255) with iand(j, 255) for the same reason
  • Use faster bitwise AND operation instead of modulo division

@codecov
Copy link

codecov bot commented Feb 21, 2026

Codecov Report

❌ Patch coverage is 0% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 44.05%. Comparing base (84c46e0) to head (590253b).

Files with missing lines Patch % Lines
src/pre_process/m_simplex_noise.fpp 0.00% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1218   +/-   ##
=======================================
  Coverage   44.05%   44.05%           
=======================================
  Files          70       70           
  Lines       20498    20498           
  Branches     1990     1990           
=======================================
  Hits         9030     9030           
  Misses      10329    10329           
  Partials     1139     1139           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working or doesn't seem right size:XS This PR changes 0-9 lines, ignoring generated files

Development

Successfully merging this pull request may close these issues.

2D simplex noise uses mod(i,255) instead of mod(i,256), skipping permutation entry

1 participant