Files
sizzletracker-android/core/src/wavetable/JavaRandom.hpp
T
ReactorcoremeltdownandClaude Opus 5.5 2bc5cb1cbb 0.37.0 (71): one engine, one version for Android and iOS
- Three source trees: core/ (shared C++ engine, moved from ios/core),
  app/ (Android UI), ios/app/ (iOS UI)
- Android plays through core/ via JNI (EngineHost, jni_engine.cpp): the Oboe
  callback renders natively; the Kotlin model is mirrored as project text and
  applied as a diff, so playback continues through edits
- The Kotlin engine moves to the unit tests as ReferenceEngine (golden reference)
- Shared MasterRecorder in core/src/host, used by both apps; host_test
- versions.properties: one versionName/versionCode for both platforms,
  continuing Android's numbering (iOS 0.6.0/11 -> 0.37.0/71)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 14:49:46 +01:00

80 lines
2.7 KiB
C++

// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
// java.util.Random, bit for bit: the 48-bit LCG and the derivations of
// nextInt / nextInt(bound) / nextLong / nextBoolean / nextFloat / nextDouble /
// nextGaussian as OpenJDK implements them. Seeded factory tables (WaveTables'
// Glass) come out exactly as on Android.
#include <cmath>
#include <cstdint>
namespace sizzle::wavetable {
class JavaRandom {
public:
explicit JavaRandom(int64_t seed) { setSeed(seed); }
void setSeed(int64_t seed) {
seed_ = ((uint64_t)seed ^ MULT) & MASK;
haveNextNextGaussian_ = false;
}
/** Random.next(bits): the top [bits] of the 48-bit state, as a (wrapping) Int. */
int32_t next(int bits) {
seed_ = (seed_ * MULT + ADD) & MASK;
return (int32_t)(uint32_t)(seed_ >> (48 - bits));
}
int32_t nextInt() { return next(32); }
/** nextInt(bound), bound > 0 (Java throws otherwise; here 0 is returned). */
int32_t nextInt(int32_t bound) {
if (bound <= 0) return 0;
int32_t r = next(31);
int32_t m = bound - 1;
if ((bound & m) == 0) {
r = (int32_t)(((int64_t)bound * (int64_t)r) >> 31);
} else {
// for (int u = r; u - (r = u % bound) + m < 0; u = next(31)); with Int wrap
for (int32_t u = r;; u = next(31)) {
r = u % bound;
if ((int32_t)((uint32_t)u - (uint32_t)r + (uint32_t)m) >= 0) break;
}
}
return r;
}
int64_t nextLong() { return (int64_t)((uint64_t)(int64_t)next(32) << 32) + (int64_t)next(32); }
bool nextBoolean() { return next(1) != 0; }
float nextFloat() { return (float)next(24) / (float)(1 << 24); }
double nextDouble() { return (double)(((int64_t)next(26) << 27) + next(27)) * 0x1.0p-53; }
/** Marsaglia polar method with the cached second value. Java uses StrictMath
* (fdlibm) log/sqrt; libm's log may differ by an ulp. */
double nextGaussian() {
if (haveNextNextGaussian_) { haveNextNextGaussian_ = false; return nextNextGaussian_; }
double v1, v2, s;
do {
v1 = 2 * nextDouble() - 1;
v2 = 2 * nextDouble() - 1;
s = v1 * v1 + v2 * v2;
} while (s >= 1 || s == 0);
double multiplier = std::sqrt(-2 * std::log(s) / s);
nextNextGaussian_ = v2 * multiplier;
haveNextNextGaussian_ = true;
return v1 * multiplier;
}
private:
static constexpr uint64_t MULT = 0x5DEECE66DULL;
static constexpr uint64_t ADD = 0xBULL;
static constexpr uint64_t MASK = (1ULL << 48) - 1;
uint64_t seed_ = 0;
double nextNextGaussian_ = 0;
bool haveNextNextGaussian_ = false;
};
} // namespace sizzle::wavetable