2014-01-15 14:29:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011-2013 Blender Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
2014-12-25 01:50:24 +00:00
|
|
|
* limitations under the License.
|
2014-01-15 14:29:22 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UTIL_HALF_H__
|
|
|
|
#define __UTIL_HALF_H__
|
|
|
|
|
Cycles: Make all #include statements relative to cycles source directory
The idea is to make include statements more explicit and obvious where the
file is coming from, additionally reducing chance of wrong header being
picked up.
For example, it was not obvious whether bvh.h was refferring to builder
or traversal, whenter node.h is a generic graph node or a shader node
and cases like that.
Surely this might look obvious for the active developers, but after some
time of not touching the code it becomes less obvious where file is coming
from.
This was briefly mentioned in T50824 and seems @brecht is fine with such
explicitness, but need to agree with all active developers before committing
this.
Please note that this patch is lacking changes related on GPU/OpenCL
support. This will be solved if/when we all agree this is a good idea to move
forward.
Reviewers: brecht, lukasstockner97, maiself, nirved, dingto, juicyfruit, swerner
Reviewed By: lukasstockner97, maiself, nirved, dingto
Subscribers: brecht
Differential Revision: https://developer.blender.org/D2586
2017-03-28 18:39:14 +00:00
|
|
|
#include "util/util_types.h"
|
|
|
|
#include "util/util_math.h"
|
2014-01-15 14:29:22 +00:00
|
|
|
|
2014-02-27 13:29:35 +00:00
|
|
|
#ifdef __KERNEL_SSE2__
|
Cycles: Make all #include statements relative to cycles source directory
The idea is to make include statements more explicit and obvious where the
file is coming from, additionally reducing chance of wrong header being
picked up.
For example, it was not obvious whether bvh.h was refferring to builder
or traversal, whenter node.h is a generic graph node or a shader node
and cases like that.
Surely this might look obvious for the active developers, but after some
time of not touching the code it becomes less obvious where file is coming
from.
This was briefly mentioned in T50824 and seems @brecht is fine with such
explicitness, but need to agree with all active developers before committing
this.
Please note that this patch is lacking changes related on GPU/OpenCL
support. This will be solved if/when we all agree this is a good idea to move
forward.
Reviewers: brecht, lukasstockner97, maiself, nirved, dingto, juicyfruit, swerner
Reviewed By: lukasstockner97, maiself, nirved, dingto
Subscribers: brecht
Differential Revision: https://developer.blender.org/D2586
2017-03-28 18:39:14 +00:00
|
|
|
#include "util/util_simd.h"
|
2014-02-27 13:29:35 +00:00
|
|
|
#endif
|
|
|
|
|
2014-01-15 14:29:22 +00:00
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
/* Half Floats */
|
|
|
|
|
|
|
|
#ifdef __KERNEL_OPENCL__
|
|
|
|
|
2014-02-27 10:49:21 +00:00
|
|
|
#define float4_store_half(h, f, scale) vstore_half4(f * (scale), 0, h);
|
2014-01-15 14:29:22 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2016-08-11 20:47:53 +00:00
|
|
|
/* CUDA has its own half data type, no need to define then */
|
|
|
|
#ifndef __KERNEL_CUDA__
|
2014-01-15 14:29:22 +00:00
|
|
|
typedef unsigned short half;
|
2016-08-11 20:47:53 +00:00
|
|
|
#endif
|
|
|
|
|
2014-01-15 14:29:22 +00:00
|
|
|
struct half4 { half x, y, z, w; };
|
|
|
|
|
|
|
|
#ifdef __KERNEL_CUDA__
|
|
|
|
|
2014-02-27 10:49:21 +00:00
|
|
|
ccl_device_inline void float4_store_half(half *h, float4 f, float scale)
|
2014-01-15 14:29:22 +00:00
|
|
|
{
|
2016-08-11 20:47:53 +00:00
|
|
|
h[0] = __float2half(f.x * scale);
|
|
|
|
h[1] = __float2half(f.y * scale);
|
|
|
|
h[2] = __float2half(f.z * scale);
|
|
|
|
h[3] = __float2half(f.w * scale);
|
2014-01-15 14:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2014-02-27 10:49:21 +00:00
|
|
|
ccl_device_inline void float4_store_half(half *h, float4 f, float scale)
|
2014-01-15 14:29:22 +00:00
|
|
|
{
|
|
|
|
#ifndef __KERNEL_SSE2__
|
|
|
|
for(int i = 0; i < 4; i++) {
|
|
|
|
/* optimized float to half for pixels:
|
|
|
|
* assumes no negative, no nan, no inf, and sets denormal to 0 */
|
2015-05-10 19:04:24 +00:00
|
|
|
union { uint i; float f; } in;
|
2014-02-27 10:49:21 +00:00
|
|
|
float fscale = f[i] * scale;
|
2015-05-10 19:04:24 +00:00
|
|
|
in.f = (fscale > 0.0f)? ((fscale < 65504.0f)? fscale: 65504.0f): 0.0f;
|
|
|
|
int x = in.i;
|
2014-01-15 14:29:22 +00:00
|
|
|
|
2015-05-10 19:04:24 +00:00
|
|
|
int absolute = x & 0x7FFFFFFF;
|
2014-01-15 14:29:22 +00:00
|
|
|
int Z = absolute + 0xC8000000;
|
|
|
|
int result = (absolute < 0x38800000)? 0: Z;
|
|
|
|
int rshift = (result >> 13);
|
|
|
|
|
|
|
|
h[i] = (rshift & 0x7FFF);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* same as above with SSE */
|
2015-05-10 16:20:04 +00:00
|
|
|
ssef fscale = load4f(f) * scale;
|
|
|
|
ssef x = min(max(fscale, 0.0f), 65504.0f);
|
|
|
|
|
|
|
|
#ifdef __KERNEL_AVX2__
|
|
|
|
ssei rpack = _mm_cvtps_ph(x, 0);
|
|
|
|
#else
|
|
|
|
ssei absolute = cast(x) & 0x7FFFFFFF;
|
|
|
|
ssei Z = absolute + 0xC8000000;
|
|
|
|
ssei result = andnot(absolute < 0x38800000, Z);
|
|
|
|
ssei rshift = (result >> 13) & 0x7FFF;
|
|
|
|
ssei rpack = _mm_packs_epi32(rshift, rshift);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
_mm_storel_pi((__m64*)h, _mm_castsi128_ps(rpack));
|
2014-01-15 14:29:22 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-06-19 15:31:16 +00:00
|
|
|
ccl_device_inline float half_to_float(half h)
|
|
|
|
{
|
|
|
|
float f;
|
|
|
|
|
|
|
|
*((int*) &f) = ((h & 0x8000) << 16) | (((h & 0x7c00) + 0x1C000) << 13) | ((h & 0x03FF) << 13);
|
|
|
|
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
ccl_device_inline float4 half4_to_float4(half4 h)
|
|
|
|
{
|
|
|
|
float4 f;
|
|
|
|
|
|
|
|
f.x = half_to_float(h.x);
|
|
|
|
f.y = half_to_float(h.y);
|
|
|
|
f.z = half_to_float(h.z);
|
|
|
|
f.w = half_to_float(h.w);
|
|
|
|
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2017-02-23 13:42:06 +00:00
|
|
|
ccl_device_inline half float_to_half(float f)
|
|
|
|
{
|
|
|
|
const uint u = __float_as_uint(f);
|
|
|
|
/* Sign bit, shifted to it's position. */
|
|
|
|
uint sign_bit = u & 0x80000000;
|
|
|
|
sign_bit >>= 16;
|
|
|
|
/* Exponent. */
|
|
|
|
uint exponent_bits = u & 0x7f800000;
|
|
|
|
/* Non-sign bits. */
|
|
|
|
uint value_bits = u & 0x7fffffff;
|
|
|
|
value_bits >>= 13; /* Align mantissa on MSB. */
|
|
|
|
value_bits -= 0x1c000; /* Adjust bias. */
|
|
|
|
/* Flush-to-zero. */
|
|
|
|
value_bits = (exponent_bits < 0x38800000) ? 0 : value_bits;
|
|
|
|
/* Clamp-to-max. */
|
|
|
|
value_bits = (exponent_bits > 0x47000000) ? 0x7bff : value_bits;
|
|
|
|
/* Denormals-as-zero. */
|
|
|
|
value_bits = (exponent_bits == 0 ? 0 : value_bits);
|
|
|
|
/* Re-insert sign bit and return. */
|
|
|
|
return (value_bits | sign_bit);
|
|
|
|
}
|
|
|
|
|
2014-01-15 14:29:22 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif /* __UTIL_HALF_H__ */
|
|
|
|
|