2016-11-01 20:44:33 +00:00
|
|
|
/*
|
|
|
|
** SSA float32
|
|
|
|
** Copyright (c) 2016 Magnus Norddahl
|
|
|
|
**
|
|
|
|
** This software is provided 'as-is', without any express or implied
|
|
|
|
** warranty. In no event will the authors be held liable for any damages
|
|
|
|
** arising from the use of this software.
|
|
|
|
**
|
|
|
|
** Permission is granted to anyone to use this software for any purpose,
|
|
|
|
** including commercial applications, and to alter it and redistribute it
|
|
|
|
** freely, subject to the following restrictions:
|
|
|
|
**
|
|
|
|
** 1. The origin of this software must not be misrepresented; you must not
|
|
|
|
** claim that you wrote the original software. If you use this software
|
|
|
|
** in a product, an acknowledgment in the product documentation would be
|
|
|
|
** appreciated but is not required.
|
|
|
|
** 2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
** misrepresented as being the original software.
|
|
|
|
** 3. This notice may not be removed or altered from any source distribution.
|
|
|
|
**
|
|
|
|
*/
|
2016-09-26 07:00:19 +00:00
|
|
|
|
2016-11-28 16:31:56 +00:00
|
|
|
#include "precomp.h"
|
2016-09-26 07:00:19 +00:00
|
|
|
#include "ssa_float.h"
|
|
|
|
#include "ssa_int.h"
|
|
|
|
#include "ssa_scope.h"
|
2016-11-11 23:50:21 +00:00
|
|
|
#include "ssa_bool.h"
|
2016-12-22 20:21:57 +00:00
|
|
|
#include "ssa_vec4f.h"
|
2016-09-26 07:00:19 +00:00
|
|
|
|
|
|
|
SSAFloat::SSAFloat()
|
|
|
|
: v(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SSAFloat::SSAFloat(float constant)
|
|
|
|
: v(0)
|
|
|
|
{
|
|
|
|
v = llvm::ConstantFP::get(SSAScope::context(), llvm::APFloat(constant));
|
|
|
|
}
|
|
|
|
|
|
|
|
SSAFloat::SSAFloat(SSAInt i)
|
|
|
|
: v(0)
|
|
|
|
{
|
|
|
|
v = SSAScope::builder().CreateSIToFP(i.v, llvm::Type::getFloatTy(SSAScope::context()), SSAScope::hint());
|
|
|
|
}
|
|
|
|
|
|
|
|
SSAFloat::SSAFloat(llvm::Value *v)
|
|
|
|
: v(v)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Type *SSAFloat::llvm_type()
|
|
|
|
{
|
|
|
|
return llvm::Type::getFloatTy(SSAScope::context());
|
|
|
|
}
|
|
|
|
|
2016-12-22 20:21:57 +00:00
|
|
|
SSAFloat SSAFloat::rsqrt(SSAFloat f)
|
|
|
|
{
|
2017-01-09 11:11:44 +00:00
|
|
|
#ifdef ARM_TARGET
|
|
|
|
return SSAFloat::from_llvm(SSAScope::builder().CreateCall(SSAScope::intrinsic(llvm::Intrinsic::aarch64_neon_frsqrts), f.v, SSAScope::hint()));
|
|
|
|
#else
|
2016-12-22 20:21:57 +00:00
|
|
|
llvm::Value *f_ss = SSAScope::builder().CreateInsertElement(llvm::UndefValue::get(SSAVec4f::llvm_type()), f.v, llvm::ConstantInt::get(SSAScope::context(), llvm::APInt(32, (uint64_t)0)));
|
|
|
|
f_ss = SSAScope::builder().CreateCall(SSAScope::intrinsic(llvm::Intrinsic::x86_sse_rsqrt_ss), f_ss, SSAScope::hint());
|
|
|
|
return SSAFloat::from_llvm(SSAScope::builder().CreateExtractElement(f_ss, SSAInt(0).v, SSAScope::hint()));
|
2017-01-09 11:11:44 +00:00
|
|
|
#endif
|
2016-12-22 20:21:57 +00:00
|
|
|
}
|
|
|
|
|
2016-11-11 23:50:21 +00:00
|
|
|
SSAFloat SSAFloat::MIN(SSAFloat a, SSAFloat b)
|
|
|
|
{
|
|
|
|
return SSAFloat::from_llvm(SSAScope::builder().CreateSelect((a < b).v, a.v, b.v, SSAScope::hint()));
|
|
|
|
}
|
|
|
|
|
|
|
|
SSAFloat SSAFloat::MAX(SSAFloat a, SSAFloat b)
|
|
|
|
{
|
|
|
|
return SSAFloat::from_llvm(SSAScope::builder().CreateSelect((a > b).v, a.v, b.v, SSAScope::hint()));
|
|
|
|
}
|
|
|
|
|
|
|
|
SSAFloat SSAFloat::clamp(SSAFloat a, SSAFloat b, SSAFloat c)
|
|
|
|
{
|
|
|
|
return SSAFloat::MAX(SSAFloat::MIN(a, c), b);
|
|
|
|
}
|
|
|
|
|
2016-09-26 07:00:19 +00:00
|
|
|
SSAFloat operator+(const SSAFloat &a, const SSAFloat &b)
|
|
|
|
{
|
|
|
|
return SSAFloat::from_llvm(SSAScope::builder().CreateFAdd(a.v, b.v, SSAScope::hint()));
|
|
|
|
}
|
|
|
|
|
|
|
|
SSAFloat operator-(const SSAFloat &a, const SSAFloat &b)
|
|
|
|
{
|
|
|
|
return SSAFloat::from_llvm(SSAScope::builder().CreateFSub(a.v, b.v, SSAScope::hint()));
|
|
|
|
}
|
|
|
|
|
|
|
|
SSAFloat operator*(const SSAFloat &a, const SSAFloat &b)
|
|
|
|
{
|
|
|
|
return SSAFloat::from_llvm(SSAScope::builder().CreateFMul(a.v, b.v, SSAScope::hint()));
|
|
|
|
}
|
|
|
|
|
|
|
|
SSAFloat operator/(const SSAFloat &a, const SSAFloat &b)
|
|
|
|
{
|
|
|
|
return SSAFloat::from_llvm(SSAScope::builder().CreateFDiv(a.v, b.v, SSAScope::hint()));
|
|
|
|
}
|
|
|
|
|
|
|
|
SSAFloat operator+(float a, const SSAFloat &b)
|
|
|
|
{
|
|
|
|
return SSAFloat(a) + b;
|
|
|
|
}
|
|
|
|
|
|
|
|
SSAFloat operator-(float a, const SSAFloat &b)
|
|
|
|
{
|
|
|
|
return SSAFloat(a) - b;
|
|
|
|
}
|
|
|
|
|
|
|
|
SSAFloat operator*(float a, const SSAFloat &b)
|
|
|
|
{
|
|
|
|
return SSAFloat(a) * b;
|
|
|
|
}
|
|
|
|
|
|
|
|
SSAFloat operator/(float a, const SSAFloat &b)
|
|
|
|
{
|
|
|
|
return SSAFloat(a) / b;
|
|
|
|
}
|
|
|
|
|
|
|
|
SSAFloat operator+(const SSAFloat &a, float b)
|
|
|
|
{
|
|
|
|
return a + SSAFloat(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
SSAFloat operator-(const SSAFloat &a, float b)
|
|
|
|
{
|
|
|
|
return a - SSAFloat(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
SSAFloat operator*(const SSAFloat &a, float b)
|
|
|
|
{
|
|
|
|
return a * SSAFloat(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
SSAFloat operator/(const SSAFloat &a, float b)
|
|
|
|
{
|
|
|
|
return a / SSAFloat(b);
|
|
|
|
}
|
|
|
|
|