Skip to content

Commit

Permalink
Add FastMath utility header
Browse files Browse the repository at this point in the history
  • Loading branch information
tunabrain committed Aug 24, 2017
1 parent 3e3753f commit 149a14a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
60 changes: 60 additions & 0 deletions src/core/math/FastMath.hpp
@@ -0,0 +1,60 @@
#ifndef FASTMATH_HPP_
#define FASTMATH_HPP_

#include "Vec.hpp"

#include "sse/SimdFloat.hpp"

#include <fmath/fmath.hpp>

namespace Tungsten {

namespace FastMath {

static inline float exp(float f)
{
return fmath::exp(f);
}
static inline Vec2f exp(Vec2f f)
{
float4 result = fmath::exp_ps(float4(f[0], f[1], 0.0f, 0.0f).raw());
return Vec2f(result[0], result[1]);
}
static inline Vec3f exp(Vec3f f)
{
float4 result = fmath::exp_ps(float4(f[0], f[1], f[2], 0.0f).raw());
return Vec3f(result[0], result[1], result[2]);
}
static inline Vec4f exp(Vec4f f)
{
float4 result = fmath::exp_ps(float4(f[0], f[1], f[2], f[3]).raw());
return Vec4f(result[0], result[1], result[2], result[3]);
}

static inline float log(float f)
{
return fmath::log(f);
}
static inline Vec2f log(Vec2f f)
{
float4 result = fmath::log_ps(float4(f[0], f[1], 0.0f, 0.0f).raw());
return Vec2f(result[0], result[1]);
}
static inline Vec3f log(Vec3f f)
{
float4 result = fmath::log_ps(float4(f[0], f[1], f[2], 0.0f).raw());
return Vec3f(result[0], result[1], result[2]);
}
static inline Vec4f log(Vec4f f)
{
float4 result = fmath::log_ps(float4(f[0], f[1], f[2], f[3]).raw());
return Vec4f(result[0], result[1], result[2], result[3]);
}

}

}



#endif /* FASTMATH_HPP_ */
15 changes: 8 additions & 7 deletions src/core/media/HomogeneousMedium.cpp
Expand Up @@ -3,6 +3,7 @@
#include "sampling/PathSampleGenerator.hpp"

#include "math/TangentFrame.hpp"
#include "math/FastMath.hpp"
#include "math/Ray.hpp"

#include "io/JsonObject.hpp"
Expand Down Expand Up @@ -73,7 +74,7 @@ bool HomogeneousMedium::sampleDistance(PathSampleGenerator &sampler, const Ray &
if (maxT == Ray::infinity())
return false;
sample.t = maxT;
sample.weight = std::exp(-_sigmaT*maxT);
sample.weight = FastMath::exp(-_sigmaT*maxT);
sample.pdf = 1.0f;
sample.exited = true;
} else {
Expand All @@ -82,7 +83,7 @@ bool HomogeneousMedium::sampleDistance(PathSampleGenerator &sampler, const Ray &

float t = -std::log(1.0f - sampler.next1D())/sigmaTc;
sample.t = min(t, maxT);
sample.weight = std::exp(-sample.t*_sigmaT);
sample.weight = FastMath::exp(-sample.t*_sigmaT);
sample.exited = (t >= maxT);
if (sample.exited) {
sample.pdf = sample.weight.avg();
Expand Down Expand Up @@ -131,7 +132,7 @@ Vec3f HomogeneousMedium::transmittance(PathSampleGenerator &/*sampler*/, const R
if (ray.farT() == Ray::infinity())
return Vec3f(0.0f);
else {
return std::exp(-_sigmaT*ray.farT());
return FastMath::exp(-_sigmaT*ray.farT());
}
}

Expand All @@ -141,9 +142,9 @@ float HomogeneousMedium::pdf(PathSampleGenerator &/*sampler*/, const Ray &ray, b
return 1.0f;
} else {
if (onSurface)
return std::exp(-ray.farT()*_sigmaT).avg();
return FastMath::exp(-ray.farT()*_sigmaT).avg();
else
return (_sigmaT*std::exp(-ray.farT()*_sigmaT)).avg();
return (_sigmaT*FastMath::exp(-ray.farT()*_sigmaT)).avg();
}
}

Expand All @@ -155,9 +156,9 @@ Vec3f HomogeneousMedium::transmittanceAndPdfs(PathSampleGenerator &/*sampler*/,
return Vec3f(0.0f);
} else if (_absorptionOnly) {
pdfForward = pdfBackward = 1.0f;
return std::exp(-_sigmaT*ray.farT());
return FastMath::exp(-_sigmaT*ray.farT());
} else {
Vec3f weight = std::exp(-_sigmaT*ray.farT());
Vec3f weight = FastMath::exp(-_sigmaT*ray.farT());
pdfForward = endOnSurface ? weight.avg() : (_sigmaT*weight).avg();
pdfBackward = startOnSurface ? weight.avg() : (_sigmaT*weight).avg();
return weight;
Expand Down

0 comments on commit 149a14a

Please sign in to comment.