Skip to content

Commit

Permalink
Std: delete 5 trig functions in favor of their Jolt replacements
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Feb 20, 2025
1 parent 25efc8b commit d61c5ff
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 121 deletions.
25 changes: 12 additions & 13 deletions src/main/java/com/github/stephengold/joltjni/Quat.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ of this software and associated documentation files (the "Software"), to deal
import com.github.stephengold.joltjni.readonly.QuatArg;
import com.github.stephengold.joltjni.readonly.Vec3Arg;
import com.github.stephengold.joltjni.std.RandomNumberEngine;
import com.github.stephengold.joltjni.std.Std;
import com.github.stephengold.joltjni.std.UniformFloatDistribution;
import java.nio.FloatBuffer;

Expand Down Expand Up @@ -170,12 +169,12 @@ public static Quat sEulerAngles(Vec3 angles) {
float halfY = 0.5f * angles.getY();
float halfZ = 0.5f * angles.getZ();

float cx = Std.cos(halfX);
float cy = Std.cos(halfY);
float cz = Std.cos(halfZ);
float sx = Std.sin(halfX);
float sy = Std.sin(halfY);
float sz = Std.sin(halfZ);
float cx = Jolt.cos(halfX);
float cy = Jolt.cos(halfY);
float cz = Jolt.cos(halfZ);
float sx = Jolt.sin(halfX);
float sy = Jolt.sin(halfY);
float sz = Jolt.sin(halfZ);

Quat result = new Quat(
cz * sx * cy - sz * cx * sy,
Expand Down Expand Up @@ -240,10 +239,10 @@ public static Quat sRandom(RandomNumberEngine engine) {
float px = 2f * Jolt.JPH_PI * distro.nextFloat(engine);
float py = 2f * Jolt.JPH_PI * distro.nextFloat(engine);

float x = r1 * Std.sin(px);
float y = r1 * Std.cos(px);
float z = r2 * Std.sin(py);
float w = r2 * Std.cos(py);
float x = r1 * Jolt.sin(px);
float y = r1 * Jolt.cos(px);
float z = r2 * Jolt.sin(py);
float w = r2 * Jolt.cos(py);
Quat result = new Quat(x, y, z, w);

return result;
Expand All @@ -259,8 +258,8 @@ public static Quat sRandom(RandomNumberEngine engine) {
public static Quat sRotation(Vec3 axis, float angle) {
assert axis.isNormalized();

float qw = Std.cos(0.5f * angle);
float s = Std.sin(0.5f * angle);
float qw = Jolt.cos(0.5f * angle);
float s = Jolt.sin(0.5f * angle);
float qx = axis.getX() * s;
float qy = axis.getY() * s;
float qz = axis.getZ() * s;
Expand Down
47 changes: 0 additions & 47 deletions src/main/java/com/github/stephengold/joltjni/std/Std.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,36 +69,6 @@ private Std() {
// *************************************************************************
// new methods exposed

/**
* Return the inverse cosine of the specified single-precision ratio.
* There's evidence this is faster than {@link java.lang.Math#acos(double)}
* on Linux and Windows.
*
* @param ratio the input cosine ratio (≥-1, ≤1)
* @return the angle (in radians)
*/
native public static float acos(float ratio);

/**
* Return the inverse tangent of the specified single-precision ratio.
* There's evidence this is faster than {@link java.lang.Math#atan(double)}
* on Linux and Windows.
*
* @param ratio the input tangent ratio
* @return the angle (in radians)
*/
native public static float atan(float ratio);

/**
* Return the cosine of the specified single-precision angle. There's
* evidence this is faster than {@link java.lang.Math#cos(double)} on Linux
* and Windows.
*
* @param angle the input angle (in radians)
* @return the cosine ratio
*/
native public static float cos(float angle);

/**
* Return the exponential of the specified single-precision value. There's
* evidence this is faster than {@link java.lang.Math#exp(double)}.
Expand Down Expand Up @@ -171,15 +141,6 @@ public static void shuffle(
}
}

/**
* Return the sine of the specified single-precision angle. There's evidence
* this is faster than {@link java.lang.Math#sin(double)}.
*
* @param angle the input angle (in radians)
* @return the sine ratio
*/
native public static float sin(float angle);

/**
* Count the number of elements in the specified array.
*
Expand Down Expand Up @@ -212,14 +173,6 @@ public static int strcmp(String lhs, String rhs) {
int result = lhs.compareTo(rhs);
return result;
}

/**
* Return the tangent ratio of the specified single-precision angle.
*
* @param angle the input angle (in radians)
* @return the tangent ratio
*/
native public static float tan(float angle);
// *************************************************************************
// native private methods

Expand Down
55 changes: 0 additions & 55 deletions src/main/native/glue/st/Std.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,6 @@ SOFTWARE.
#include "Jolt/Jolt.h"
#include "auto/com_github_stephengold_joltjni_std_Std.h"

/*
* Class: com_github_stephengold_joltjni_std_Std
* Method: acos
* Signature: (F)F
*/
JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_std_Std_acos
(JNIEnv *, jclass, jfloat ratio) {
const float result = std::acos(ratio);
return result;
}

/*
* Class: com_github_stephengold_joltjni_std_Std
* Method: atan
* Signature: (F)F
*/
JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_std_Std_atan
(JNIEnv *, jclass, jfloat ratio) {
const float result = std::atan(ratio);
return result;
}

/*
* Class: com_github_stephengold_joltjni_std_Std
* Method: cos
* Signature: (F)F
*/
JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_std_Std_cos
(JNIEnv *, jclass, jfloat angle) {
const float result = std::cos(angle);
return result;
}

/*
* Class: com_github_stephengold_joltjni_std_Std
* Method: exp
Expand Down Expand Up @@ -103,17 +70,6 @@ JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_std_Std_pow
return result;
}

/*
* Class: com_github_stephengold_joltjni_std_Std
* Method: sin
* Signature: (F)F
*/
JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_std_Std_sin
(JNIEnv *, jclass, jfloat angle) {
const float result = std::sin(angle);
return result;
}

/*
* Class: com_github_stephengold_joltjni_std_Std
* Method: sqrt
Expand All @@ -125,17 +81,6 @@ JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_std_Std_sqrt
return result;
}

/*
* Class: com_github_stephengold_joltjni_std_Std
* Method: tan
* Signature: (F)F
*/
JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_std_Std_tan
(JNIEnv *, jclass, jfloat value) {
const float result = std::tan(value);
return result;
}

/*
* Class: com_github_stephengold_joltjni_std_Std
* Method: shuffle
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2024 Stephen Gold
Copyright (c) 2024-2025 Stephen Gold
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -22,7 +22,7 @@ of this software and associated documentation files (the "Software"), to deal
package testjoltjni.app.performancetest;
import com.github.stephengold.joltjni.*;
import com.github.stephengold.joltjni.enumerate.*;
import static com.github.stephengold.joltjni.std.Std.*;
import static com.github.stephengold.joltjni.Jolt.*;

/**
* A line-for-line Java translation of the Jolt Physics "convex vs mesh scene" performance test.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2024 Stephen Gold
Copyright (c) 2024-2025 Stephen Gold
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -22,7 +22,7 @@ of this software and associated documentation files (the "Software"), to deal
package testjoltjni.app.performancetest;
import com.github.stephengold.joltjni.*;
import com.github.stephengold.joltjni.enumerate.*;
import static com.github.stephengold.joltjni.std.Std.*;
import static com.github.stephengold.joltjni.Jolt.*;
import static testjoltjni.app.performancetest.PerformanceTest.Trace;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2024 Stephen Gold
Copyright (c) 2024-2025 Stephen Gold
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -23,7 +23,7 @@ of this software and associated documentation files (the "Software"), to deal
import com.github.stephengold.joltjni.*;
import com.github.stephengold.joltjni.enumerate.*;
import testjoltjni.app.samples.*;
import static com.github.stephengold.joltjni.std.Std.*;
import static com.github.stephengold.joltjni.Jolt.*;
/**
* A line-for-line Java translation of the Jolt Physics change motion-type test.
* <p>
Expand Down

0 comments on commit d61c5ff

Please sign in to comment.