[Beignet] [PATCH] fix built-in function "normalize"
Zhigang Gong
zhigang.gong at linux.intel.com
Tue Oct 29 03:28:00 CET 2013
The same issue as previous patch. If the x is NaN, we should return NaN.
For the vector version, this patch seems ok. But for the scalar version,
it's broken.
On Tue, Oct 29, 2013 at 09:22:33AM +0800, Homer Hsing wrote:
> divide the parameter by its length
>
> Signed-off-by: Homer Hsing <homer.xing at intel.com>
> ---
> backend/src/ocl_stdlib.tmpl.h | 29 +++++++++++++++++++++++++----
> 1 file changed, 25 insertions(+), 4 deletions(-)
>
> diff --git a/backend/src/ocl_stdlib.tmpl.h b/backend/src/ocl_stdlib.tmpl.h
> index 38d1ab0..77b6bd0 100644
> --- a/backend/src/ocl_stdlib.tmpl.h
> +++ b/backend/src/ocl_stdlib.tmpl.h
> @@ -1766,10 +1766,31 @@ INLINE_OVERLOADABLE float distance(float x, float y) { return length(x-y); }
> INLINE_OVERLOADABLE float distance(float2 x, float2 y) { return length(x-y); }
> INLINE_OVERLOADABLE float distance(float3 x, float3 y) { return length(x-y); }
> INLINE_OVERLOADABLE float distance(float4 x, float4 y) { return length(x-y); }
> -INLINE_OVERLOADABLE float normalize(float x) { return 1.f; }
> -INLINE_OVERLOADABLE float2 normalize(float2 x) { return x * rsqrt(dot(x, x)); }
> -INLINE_OVERLOADABLE float3 normalize(float3 x) { return x * rsqrt(dot(x, x)); }
> -INLINE_OVERLOADABLE float4 normalize(float4 x) { return x * rsqrt(dot(x, x)); }
> +INLINE_OVERLOADABLE float normalize(float x) {
> + union { float f; unsigned u; } u;
> + u.f = x;
> + if(u.u == 0)
> + return 0.f;
> + return u.u < 0x7fffffff ? 1.f : -1.f;
> +}
> +INLINE_OVERLOADABLE float2 normalize(float2 x) {
> + float m = length(x);
> + if(m == 0)
> + return 0;
> + return x / m;
> +}
> +INLINE_OVERLOADABLE float3 normalize(float3 x) {
> + float m = length(x);
> + if(m == 0)
> + return 0;
> + return x / m;
> +}
> +INLINE_OVERLOADABLE float4 normalize(float4 x) {
> + float m = length(x);
> + if(m == 0)
> + return 0;
> + return x / m;
> +}
>
> INLINE_OVERLOADABLE float fast_length(float x) { return __gen_ocl_fabs(x); }
> INLINE_OVERLOADABLE float fast_length(float2 x) { return sqrt(dot(x,x)); }
> --
> 1.8.3.2
>
> _______________________________________________
> Beignet mailing list
> Beignet at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/beignet
More information about the Beignet
mailing list