Skip to content

Commit

Permalink
use log2
Browse files Browse the repository at this point in the history
  • Loading branch information
teoxoy committed Jan 31, 2023
1 parent 05f2649 commit 254e806
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
10 changes: 4 additions & 6 deletions src/back/glsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2930,8 +2930,6 @@ impl<'a, W: Write> Writer<'a, W> {
Mf::Determinant => "determinant",
// bits
Mf::CountLeadingZeros => {
use std::f32::consts::LOG2_E;

match *ctx.info[arg].ty.inner_with(&self.module.types) {
crate::TypeInner::Vector { size, kind, .. } => {
let s = back::vector_size_str(size);
Expand All @@ -2942,9 +2940,9 @@ impl<'a, W: Write> Writer<'a, W> {
write!(self.out, "ivec{s}(")?;
}

write!(self.out, "mix(vec{s}(31.0) - floor(log(vec{s}(")?;
write!(self.out, "mix(vec{s}(31.0) - floor(log2(vec{s}(")?;
self.write_expr(arg, ctx)?;
write!(self.out, ") + 0.5) * {LOG2_E}), ")?;
write!(self.out, ") + 0.5)), ")?;

if let crate::ScalarKind::Uint = kind {
write!(self.out, "vec{s}(32.0), lessThanEqual(")?;
Expand All @@ -2970,9 +2968,9 @@ impl<'a, W: Write> Writer<'a, W> {
write!(self.out, " == 0 ? 32 : 0) : int(")?;
}

write!(self.out, "31.0 - floor(log(float(")?;
write!(self.out, "31.0 - floor(log2(float(")?;
self.write_expr(arg, ctx)?;
write!(self.out, ") + 0.5) * {LOG2_E})))")?;
write!(self.out, ") + 0.5))))")?;
}
_ => unreachable!(),
};
Expand Down
8 changes: 4 additions & 4 deletions tests/out/glsl/math-functions.main.Vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ void main() {
vec4 g = refract(v, v, 1.0);
int const_dot = ( + ivec2(0, 0).x * ivec2(0, 0).x + ivec2(0, 0).y * ivec2(0, 0).y);
uint first_leading_bit_abs = uint(findMSB(uint(abs(int(0u)))));
int clz_a = (-1 <= 0 ? (-1 == 0 ? 32 : 0) : int(31.0 - floor(log(float(-1) + 0.5) * 1.442695)));
uint clz_b = (1u == 0u ? 32u : uint(31.0 - floor(log(float(1u) + 0.5) * 1.442695)));
int clz_a = (-1 <= 0 ? (-1 == 0 ? 32 : 0) : int(31.0 - floor(log2(float(-1) + 0.5))));
uint clz_b = (1u == 0u ? 32u : uint(31.0 - floor(log2(float(1u) + 0.5))));
ivec2 _e20 = ivec2(-1);
ivec2 clz_c = ivec2(mix(vec2(31.0) - floor(log(vec2(_e20) + 0.5) * 1.442695), mix(vec2(0.0), vec2(32.0), equal(_e20, ivec2(0))), lessThanEqual(_e20, ivec2(0))));
ivec2 clz_c = ivec2(mix(vec2(31.0) - floor(log2(vec2(_e20) + 0.5)), mix(vec2(0.0), vec2(32.0), equal(_e20, ivec2(0))), lessThanEqual(_e20, ivec2(0))));
uvec2 _e23 = uvec2(1u);
uvec2 clz_d = uvec2(mix(vec2(31.0) - floor(log(vec2(_e23) + 0.5) * 1.442695), vec2(32.0), lessThanEqual(_e23, uvec2(0u))));
uvec2 clz_d = uvec2(mix(vec2(31.0) - floor(log2(vec2(_e23) + 0.5)), vec2(32.0), lessThanEqual(_e23, uvec2(0u))));
}

0 comments on commit 254e806

Please sign in to comment.