Fast powf for sgrb.

This commit is contained in:
Acrimon 2019-09-24 11:05:01 +02:00
parent 1936224901
commit 7e768b84e4

View File

@ -2,6 +2,23 @@ pub const GIT_HASH: &str = include_str!(concat!(env!("OUT_DIR"), "/githash"));
use vek::{Mat3, Rgb, Rgba, Vec3};
#[inline(always)]
#[allow(unsafe_code)]
fn fast_powf(b: f32, e: f32) -> f32 {
unsafe {
let b = b as f64;
let e = e as f64;
union Swagger {
f: f64,
a: [i32; 2],
}
let mut b = Swagger { f: b };
b.a[1] = (e * (b.a[1] as f64 - 1072632447.0) + 1072632447.0) as i32;
b.a[0] = 0;
b.f as f32
}
}
#[inline(always)]
pub fn srgb_to_linear(col: Rgb<f32>) -> Rgb<f32> {
#[inline(always)]
@ -9,7 +26,7 @@ pub fn srgb_to_linear(col: Rgb<f32>) -> Rgb<f32> {
if x <= 0.04045 {
x / 12.92
} else {
((x + 0.055) / 1.055).powf(2.4)
fast_powf((x + 0.055) / 1.055, 2.4)
}
}
col.map(to_linear)
@ -21,7 +38,7 @@ pub fn linear_to_srgb(col: Rgb<f32>) -> Rgb<f32> {
if x <= 0.0031308 {
x * 12.92
} else {
x.powf(1.0 / 2.4) * 1.055 - 0.055
fast_powf(x, 1.0 / 2.4) * 1.055 - 0.055
}
}
col.map(to_srgb)
@ -118,6 +135,7 @@ pub fn xyy_to_rgb(xyy: Vec3<f32>) -> Rgb<f32> {
)
}
// TO-DO: speed this up
#[inline(always)]
pub fn saturate_srgb(col: Rgb<f32>, value: f32) -> Rgb<f32> {
let mut hsv = rgb_to_hsv(srgb_to_linear(col));