84 lines · plain
1//===----------------------------------------------------------------------===//2//3 4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.5// See https://llvm.org/LICENSE.txt for license information.6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception7//8//===----------------------------------------------------------------------===//9 10#if (__CLC_VECSIZE_OR_1 == 1 || __CLC_VECSIZE_OR_1 == 2 || \11 __CLC_VECSIZE_OR_1 == 3 || __CLC_VECSIZE_OR_1 == 4)12 13// Scalar normalize14#if defined(__CLC_SCALAR)15 16_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_normalize(__CLC_GENTYPE p) {17 return __clc_sign(p);18}19 20// Vector normalize21#else22 23#if __CLC_FPSIZE == 1624 25#define MIN_VAL HALF_MIN26#define MAX_SQRT 0x1.0p+8h27#define MIN_SQRT 0x1.0p-8h28 29#elif __CLC_FPSIZE == 3230 31#define MIN_VAL FLT_MIN32#define MAX_SQRT 0x1.0p+86F33#if __CLC_VECSIZE_OR_1 == 234#define MIN_SQRT 0x1.0p-65F35#else36#define MIN_SQRT 0x1.0p-66F37#endif38 39#elif __CLC_FPSIZE == 6440 41#define MIN_VAL DBL_MIN42#define MAX_SQRT 0x1.0p+56343#if __CLC_VECSIZE_OR_1 == 244#define MIN_SQRT 0x1.0p-51345#else46#define MIN_SQRT 0x1.0p-51447#endif48 49#else50#error "Invalid FP size"51#endif52 53_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_normalize(__CLC_GENTYPE p) {54 if (__clc_all(p == __CLC_FP_LIT(0.0))) {55 return p;56 }57 58 __CLC_SCALAR_GENTYPE l2 = __clc_dot(p, p);59 60 if (l2 < MIN_VAL) {61 p *= MAX_SQRT;62 l2 = __clc_dot(p, p);63 } else if (l2 == INFINITY) {64 p *= MIN_SQRT;65 l2 = __clc_dot(p, p);66 if (l2 == INFINITY) {67 p = __clc_copysign(__clc_select((__CLC_GENTYPE)__CLC_FP_LIT(0.0),68 (__CLC_GENTYPE)__CLC_FP_LIT(1.0),69 __clc_isinf(p)),70 p);71 l2 = __clc_dot(p, p);72 }73 }74 return p * __clc_rsqrt(l2);75}76 77#undef MIN_VAL78#undef MIN_SQRT79#undef MAX_SQRT80 81#endif82 83#endif84