convert the following HLSL code into normal C code:
#define madfrac(A,B) mad((A),(B),-floor((A)(B)))
float inverseSF(float3 p, float n) {
float phi = min(atan2(p.y, p.x), PI), cosTheta = p.z;
float k = max(2, floor(
log(n * PI * sqrt(5) * (1 - cosThetacosTheta))
/ log(PHIPHI)));
float Fk = pow(PHI, k)/sqrt(5);
float F0 = round(Fk), F1 = round(Fk * PHI);
float2x2 B = float2x2(
2PImadfrac(F0+1, PHI-1) - 2PI*(PHI-1),
2PImadfrac(F1+1, PHI-1) - 2PI(PHI-1),
-2F0/n,
-2F1/n);
float2x2 invB = inverse(B);
float2 c = floor(mul(invB, float2(phi, cosTheta - (1-1/n))));
float d = INFINITY, j = 0;
for (uint s = 0; s < 4; ++s) {
float cosTheta = dot(B[1], float2(s%2, s/2) + c) + (1-1/n);
cosTheta = clamp(cosTheta, -1, +1)2 - cosTheta;
float i = floor(n0.5 - cosThetan0.5);
float phi = 2PImadfrac(i, PHI-1);
cosTheta = 1 - (2*i + 1)rcp(n);
float sinTheta = sqrt(1 - cosThetacosTheta);
float3 q = float3(
cos(phi)*sinTheta,
sin(phi)*sinTheta,
cosTheta);
float squaredDistance = dot(q-p, q-p);
if (squaredDistance < d) {
d = squaredDistance;
j = i;
}
}
return j;
}
Assistant
Here is the conversion of the provided HLSL code into standard C code:
c
#include <math.h>
#include <float.h>
// Define constants
#define PI 3.14159265358979323846f
#define PHI ((1.0f + sqrtf(5.0f)) * 0.5f)
// Define vector and matrix structures
typedef struct {
float x, y;
} float2;
typedef struct {
float x, y, z;
} float3;
typedef struct {
float2 row[2];
} float2x2;
// Function to compute the fractional part of a number
float frac(float x) {
return x - floorf(x);
}
// Function to compute the minimum of two numbers
float minf(float a, float b) {
return a < b ? a : b;
}
// Function to compute the maximum of two numbers
float maxf(float a, float b) {
return a > b ? a : b;
}
// Function to clamp a value between a minimum and maximum
float clamp(float x, float lower, float upper) {
return x < lower ? lower : (x > upper ? upper : x);
}
// Function to perform dot product on float2 vectors
float dot(float2 a, float2 b) {
return a.x * b.x + a.y * b.y;
}
// Function to perform dot product on float3 vectors
float dot3(float3 a, float3 b) {
return a.x * b.x + a.y * b.y + a.z * b.z;
}
// Function to add two float2 vectors
float2 float2_add(float2 a, float2 b) {
float2 result = { a.x + b.x, a.y + b.y };
return result;
}
// Function to subtract two float3 vectors
float3 float3_subtract(float3 a, float3 b) {
float3 result = { a.x - b.x, a.y - b.y, a.z - b.z };
return result;
}
// Function to multiply a 2x2 matrix with a 2D vector
float2 mul(float2x2 mat, float2 vec) {
float2 result;
result.x = mat.row[0].x * vec.x + mat.row[0].y * vec.y;
result.y = mat.row[1].x * vec.x + mat.row[1].y * vec.y;
return result;
}
// Function to compute the floor of each component in a float2 vector
float2 floorf2(float2 v) {
float2 result = { floorf(v.x), floorf(v.y) };
return result;
}
// Function to compute the inverse of a 2x2 matrix
float2x2 inverse(float2x2 mat) {
float a = mat.row[0].x;
float b = mat.row[0].y;
float c = mat.row[1].x;
float d = mat.row[1].y;
float det = a * d - b * c;
float2x2 invMat;
if (fabsf(det) > 1e-6f) {
float invDet = 1.0f / det;
invMat.row[0].x = d * invDet;
invMat.row[0].y = -b * invDet;
invMat.row[1].x = -c * invDet;
invMat.row[1].y = a * invDet;
} else {
// Determinant is zero; set to zero matrix
invMat.row[0].x = invMat.row[0].y = invMat.row[1].x = invMat.row[1].y = 0.0f;
}
return invMat;
}
// The converted inverseSF function
float inverseSF(float3 p, float n) {
float phi = minf(atan2f(p.y, p.x), PI);
float cosTheta = p.z;
float cosTheta2 = cosTheta * cosTheta;
float k = maxf(2.0f, floorf(logf(n * PI * sqrtf(5.0f) * (1.0f - cosTheta2)) / logf(PHI * PHI)));
float Fk = powf(PHI, k) / sqrtf(5.0f);
float F0 = roundf(Fk);
float F1 = roundf(Fk * PHI);
// Compute B matrix
float2x2 B;
B.row[0].x = 2.0f * PI * frac((F0 + 1.0f) * (PHI - 1.0f)) - 2.0f * PI * (PHI - 1.0f);
B.row[0].y = 2.0f * PI * frac((F1 + 1.0f) * (PHI - 1.0f)) - 2.0f * PI * (PHI - 1.0f);
B.row[1].x = -2.0f * F0 / n;
B.row[1].y = -2.0f * F1 / n;
float2x2 invB = inverse(B);
float2 tmpVec = { phi, cosTheta - (1.0f - 1.0f / n) };
float2 c = floorf2(mul(invB, tmpVec));
float d = INFINITY;
float j = 0.0f;
for (unsigned int s = 0; s < 4; ++s) {
float sx = (float)(s % 2);
float sy = (float)(s / 2);
float2 sVec = { sx, sy };
float2 vecToAdd = float2_add(sVec, c);
float cosTheta = dot(B.row[1], vecToAdd) + (1.0f - 1.0f / n);
cosTheta = clamp(cosTheta, -1.0f, +1.0f) * 2.0f - cosTheta;
float i = floorf(n * 0.5f - cosTheta * n * 0.5f);
float phi = 2.0f * PI * frac(i * (PHI - 1.0f));
cosTheta = 1.0f - (2.0f * i + 1.0f) / n;
float sinTheta = sqrtf(1.0f - cosTheta * cosTheta);
float3 q;
q.x = cosf(phi) * sinTheta;
q.y = sinf(phi) * sinTheta;
q.z = cosTheta;
float3 diff = float3_subtract(q, p);
float squaredDistance = dot3(diff, diff);
if (squaredDistance < d) {
d = squaredDistance;
j = i;
}
}
return j;
}
This C code provides equivalent functionality to the original HLSL code, using standard C constructs and math functions. The vector and matrix operations are implemented using custom structures and functions to simulate the behavior of HLSL's built-in types and functions.