// is_zero(v): Returns -1 (all bits set) if v is 0, and 0 otherwise.
// Uses only bitwise operations. Assumes v is relatively small,
// so propagating bits via shifts doesn't need full 32 steps.
// Input v comes from (x & mask), max value is 48 (0b110000).
function is_zero(v) {
    // Propagate any set bit down to the least significant bit (LSB).
    // If v was initially 0, LSB remains 0. Otherwise, LSB becomes 1.
    v = v | (v >> 1);
    v = v | (v >> 2);
    v = v | (v >> 4); // Sufficient for values up to 6 bits (0-63)
    // Result is 0 if LSB is 1 (v!=0), or -1 if LSB is 0 (v==0).
    return (v & 1) - 1;
}

/**
 * @param {number} x Input bitmask representing two active frustum planes.
 * Bits 0-5: x0, x1, y0, y1, z0, z1
 * Exactly two bits are set.
 * @returns {number} A 6-bit number where bits 0-2 are the index of the first corner (0-7)
 * and bits 3-5 are the index of the second corner (0-7).
 * The corners lie on the intersection of the two active planes.
 */
export function analytical_biplanar_corner_mapping(x) {


    // Determine which plane index (0 or 1) is active for each axis pair.
    // px=0 if x0 (bit 0) is set, px=1 if x1 (bit 1) is set.
    // py=0 if y0 (bit 2) is set, py=1 if y1 (bit 3) is set.
    // pz=0 if z0 (bit 4) is set, pz=1 if z1 (bit 5) is set.
    const px = (x >> 1) & 1;
    const py = (x >> 3) & 1;
    const pz = (x >> 5) & 1;

    // Calculate potential results assuming XY planes are active (z bits 4,5 are 0)
    // Corners c = z*4 + y*2 + x*1
    // Based on test cases [0,1], [4,5], [2,3], [6,7] for inputs 5, 6, 9, 10
    const c1_xy = (py << 1) | (px << 2); // generates 0, 4, 2, 6
    const c2_xy = c1_xy | 1;             // generates 1, 5, 3, 7
    const result_xy = (c2_xy << 3) | c1_xy; // Combined output: 8, 44, 26, 62

    // Calculate potential results assuming YZ planes are active (x bits 0,1 are 0)
    // Based on test cases [0,4], [2,6], [1,5], [3,7] for inputs 20, 24, 36, 40
    const c1_yz = (py << 1) | pz;         // generates 0, 2, 1, 3
    const c2_yz = c1_yz | 4;             // generates 4, 6, 5, 7
    const result_yz = (c2_yz << 3) | c1_yz; // Combined output: 32, 50, 41, 59

    // Calculate potential results assuming XZ planes are active (y bits 2,3 are 0)
    // Based on test cases [0,2], [4,6], [1,3], [5,7] for inputs 17, 18, 33, 34
    const c1_xz = (px << 2) | pz;         // generates 0, 4, 1, 5
    const c2_xz = c1_xz | 2;             // generates 2, 6, 3, 7
    const result_xz = (c2_xz << 3) | c1_xz; // Combined output: 16, 52, 25, 61

    // Determine which pair of axes is active by checking which pair of bits is zero.
    // Create masks: -1 (0xFFFFFFFF) if the condition is met, 0 otherwise.
    const mask_xy = is_zero(x & 0b110000); // -1 if z0 and z1 bits are 0
    const mask_yz = is_zero(x & 0b000011); // -1 if x0 and x1 bits are 0
    const mask_xz = is_zero(x & 0b001100); // -1 if y0 and y1 bits are 0

    // Combine the results. Only the result corresponding to the active axis pair
    // will be preserved, as others will be ANDed with 0.
    const final_result = (mask_xy & result_xy) | (mask_yz & result_yz) | (mask_xz & result_xz);

    return final_result;

}
