Added cl_khr_fp16 extension support for test_explicit_s2v from basic (#1713)

* Added cl_khr_fp16 support for test_explicit_s2v from basic (issue #142, basic)

* Cosmetic corrections

* cosmetic fix

* Added correction to distinguish signed and unsigned char types for ARM architecture tests

* Added missing pieces of convertion procedure to support half

* Corrected condition to verify if additional pragma is necessary (issue #142, basic)

* Add NaN check for half to float conversion

* check-format fixes

* Add NaN check for all float types

Use std::isnan for float/double types.

Change-Id: I005bddccaa3f8490ac59b2aa431ed315733ad143

* Fix Ubuntu build error with isnan macro definition

Change-Id: I671ed826a9631fbbc66d0aa9b674ab00124c7967

* Check format fixes

* NAN define not needed anymore

---------

Co-authored-by: Vasu Penugonda <vpenugon@qti.qualcomm.com>
Co-authored-by: Sreelakshmi Haridas <sharidas@quicinc.com>
This commit is contained in:
Marcin Hajder
2023-10-17 18:38:36 +02:00
committed by GitHub
parent c73d6a341b
commit 72bb711646
5 changed files with 421 additions and 298 deletions

View File

@@ -21,6 +21,8 @@
#include "mt19937.h"
#include "compat.h"
#include <CL/cl_half.h>
#if defined(__SSE__) || defined(_MSC_VER)
#include <xmmintrin.h>
#endif
@@ -261,6 +263,11 @@ static Long sLowerLimits[kNumExplicitTypes] = {
} \
break;
#define TO_HALF_CASE(inType) \
case kHalf: \
halfPtr = (cl_half *)outRaw; \
*halfPtr = cl_half_from_float((float)(*inType##Ptr), CL_HALF_RTE); \
break;
#define TO_FLOAT_CASE(inType) \
case kFloat: \
floatPtr = (float *)outRaw; \
@@ -281,6 +288,59 @@ static Long sLowerLimits[kNumExplicitTypes] = {
*outType##Ptr = (outType)lrintf_clamped(*floatPtr); \
break;
#define HALF_ROUND_CASE(outEnum, outType, rounding, sat) \
case outEnum: { \
outType##Ptr = (outType *)outRaw; \
/* Get the tens digit */ \
float fltEq = (Long)cl_half_to_float(*halfPtr); \
Long wholeValue = (Long)fltEq; \
float largeRemainder = (fltEq - (float)wholeValue) * 10.f; \
/* What do we do based on that? */ \
if (rounding == kRoundToEven) \
{ \
if (wholeValue & 1LL) /*between 1 and 1.99 */ \
wholeValue += 1LL; /* round up to even */ \
} \
else if (rounding == kRoundToZero) \
{ \
/* Nothing to do, round-to-zero is what C casting does */ \
} \
else if (rounding == kRoundToPosInf) \
{ \
/* Only positive numbers are wrong */ \
if (largeRemainder != 0.f && wholeValue >= 0) wholeValue++; \
} \
else if (rounding == kRoundToNegInf) \
{ \
/* Only negative numbers are off */ \
if (largeRemainder != 0.f && wholeValue < 0) wholeValue--; \
} \
else \
{ /* Default is round-to-nearest */ \
wholeValue = (Long)lrintf_clamped(fltEq); \
} \
/* Now apply saturation rules */ \
if (sat) \
{ \
if ((sLowerLimits[outEnum] < 0 \
&& wholeValue > (Long)sUpperLimits[outEnum]) \
|| (sLowerLimits[outEnum] == 0 \
&& (ULong)wholeValue > sUpperLimits[outEnum])) \
*outType##Ptr = (outType)sUpperLimits[outEnum]; \
else if (wholeValue < sLowerLimits[outEnum]) \
*outType##Ptr = (outType)sLowerLimits[outEnum]; \
else \
*outType##Ptr = (outType)wholeValue; \
} \
else \
{ \
*outType##Ptr = (outType)( \
wholeValue \
& (0xffffffffffffffffLL >> (64 - (sizeof(outType) * 8)))); \
} \
} \
break;
#define FLOAT_ROUND_CASE(outEnum, outType, rounding, sat) \
case outEnum: { \
outType##Ptr = (outType *)outRaw; \
@@ -386,6 +446,7 @@ static Long sLowerLimits[kNumExplicitTypes] = {
break;
typedef unsigned char uchar;
typedef signed char schar;
typedef unsigned short ushort;
typedef unsigned int uint;
typedef unsigned long ulong;
@@ -395,7 +456,7 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
ExplicitType outType)
{
bool *boolPtr;
char *charPtr;
schar *scharPtr;
uchar *ucharPtr;
short *shortPtr;
ushort *ushortPtr;
@@ -403,6 +464,7 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
uint *uintPtr;
Long *LongPtr;
ULong *ULongPtr;
cl_half *halfPtr;
float *floatPtr;
double *doublePtr;
@@ -433,6 +495,11 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
get_explicit_type_size(outType));
break;
case kHalf:
halfPtr = (cl_half *)outRaw;
*halfPtr =
(*boolPtr) ? cl_half_from_float(-1.f, CL_HALF_RTE) : 0;
break;
case kFloat:
floatPtr = (float *)outRaw;
*floatPtr = (*boolPtr) ? -1.f : 0.f;
@@ -449,29 +516,30 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
break;
case kChar:
charPtr = (char *)inRaw;
scharPtr = (schar *)inRaw;
switch (outType)
{
BOOL_CASE(char)
BOOL_CASE(schar)
case kChar:
memcpy(outRaw, inRaw, get_explicit_type_size(inType));
break;
DOWN_CAST_CASE(char, kUChar, uchar, saturate)
SIMPLE_CAST_CASE(char, kUnsignedChar, uchar)
SIMPLE_CAST_CASE(char, kShort, short)
SIMPLE_CAST_CASE(char, kUShort, ushort)
SIMPLE_CAST_CASE(char, kUnsignedShort, ushort)
SIMPLE_CAST_CASE(char, kInt, int)
SIMPLE_CAST_CASE(char, kUInt, uint)
SIMPLE_CAST_CASE(char, kUnsignedInt, uint)
SIMPLE_CAST_CASE(char, kLong, Long)
SIMPLE_CAST_CASE(char, kULong, ULong)
SIMPLE_CAST_CASE(char, kUnsignedLong, ULong)
DOWN_CAST_CASE(schar, kUChar, uchar, saturate)
SIMPLE_CAST_CASE(schar, kUnsignedChar, uchar)
SIMPLE_CAST_CASE(schar, kShort, short)
SIMPLE_CAST_CASE(schar, kUShort, ushort)
SIMPLE_CAST_CASE(schar, kUnsignedShort, ushort)
SIMPLE_CAST_CASE(schar, kInt, int)
SIMPLE_CAST_CASE(schar, kUInt, uint)
SIMPLE_CAST_CASE(schar, kUnsignedInt, uint)
SIMPLE_CAST_CASE(schar, kLong, Long)
SIMPLE_CAST_CASE(schar, kULong, ULong)
SIMPLE_CAST_CASE(schar, kUnsignedLong, ULong)
TO_FLOAT_CASE(char)
TO_DOUBLE_CASE(char)
TO_HALF_CASE(schar)
TO_FLOAT_CASE(schar)
TO_DOUBLE_CASE(schar)
default:
log_error("ERROR: Invalid type given to "
@@ -491,7 +559,7 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
memcpy(outRaw, inRaw, get_explicit_type_size(inType));
break;
DOWN_CAST_CASE(uchar, kChar, char, saturate)
DOWN_CAST_CASE(uchar, kChar, schar, saturate)
SIMPLE_CAST_CASE(uchar, kShort, short)
SIMPLE_CAST_CASE(uchar, kUShort, ushort)
SIMPLE_CAST_CASE(uchar, kUnsignedShort, ushort)
@@ -502,6 +570,7 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
SIMPLE_CAST_CASE(uchar, kULong, ULong)
SIMPLE_CAST_CASE(uchar, kUnsignedLong, ULong)
TO_HALF_CASE(uchar)
TO_FLOAT_CASE(uchar)
TO_DOUBLE_CASE(uchar)
@@ -523,7 +592,7 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
memcpy(outRaw, inRaw, get_explicit_type_size(inType));
break;
DOWN_CAST_CASE(uchar, kChar, char, saturate)
DOWN_CAST_CASE(uchar, kChar, schar, saturate)
SIMPLE_CAST_CASE(uchar, kShort, short)
SIMPLE_CAST_CASE(uchar, kUShort, ushort)
SIMPLE_CAST_CASE(uchar, kUnsignedShort, ushort)
@@ -534,6 +603,7 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
SIMPLE_CAST_CASE(uchar, kULong, ULong)
SIMPLE_CAST_CASE(uchar, kUnsignedLong, ULong)
TO_HALF_CASE(uchar)
TO_FLOAT_CASE(uchar)
TO_DOUBLE_CASE(uchar)
@@ -554,7 +624,7 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
memcpy(outRaw, inRaw, get_explicit_type_size(inType));
break;
DOWN_CAST_CASE(short, kChar, char, saturate)
DOWN_CAST_CASE(short, kChar, schar, saturate)
DOWN_CAST_CASE(short, kUChar, uchar, saturate)
DOWN_CAST_CASE(short, kUnsignedChar, uchar, saturate)
DOWN_CAST_CASE(short, kUShort, ushort, saturate)
@@ -566,6 +636,7 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
SIMPLE_CAST_CASE(short, kULong, ULong)
SIMPLE_CAST_CASE(short, kUnsignedLong, ULong)
TO_HALF_CASE(short)
TO_FLOAT_CASE(short)
TO_DOUBLE_CASE(short)
@@ -587,7 +658,7 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
memcpy(outRaw, inRaw, get_explicit_type_size(inType));
break;
DOWN_CAST_CASE(ushort, kChar, char, saturate)
DOWN_CAST_CASE(ushort, kChar, schar, saturate)
DOWN_CAST_CASE(ushort, kUChar, uchar, saturate)
DOWN_CAST_CASE(ushort, kUnsignedChar, uchar, saturate)
DOWN_CAST_CASE(ushort, kShort, short, saturate)
@@ -598,6 +669,7 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
SIMPLE_CAST_CASE(ushort, kULong, ULong)
SIMPLE_CAST_CASE(ushort, kUnsignedLong, ULong)
TO_HALF_CASE(ushort)
TO_FLOAT_CASE(ushort)
TO_DOUBLE_CASE(ushort)
@@ -619,7 +691,7 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
memcpy(outRaw, inRaw, get_explicit_type_size(inType));
break;
DOWN_CAST_CASE(ushort, kChar, char, saturate)
DOWN_CAST_CASE(ushort, kChar, schar, saturate)
DOWN_CAST_CASE(ushort, kUChar, uchar, saturate)
DOWN_CAST_CASE(ushort, kUnsignedChar, uchar, saturate)
DOWN_CAST_CASE(ushort, kShort, short, saturate)
@@ -630,6 +702,7 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
SIMPLE_CAST_CASE(ushort, kULong, ULong)
SIMPLE_CAST_CASE(ushort, kUnsignedLong, ULong)
TO_HALF_CASE(ushort)
TO_FLOAT_CASE(ushort)
TO_DOUBLE_CASE(ushort)
@@ -650,7 +723,7 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
memcpy(outRaw, inRaw, get_explicit_type_size(inType));
break;
DOWN_CAST_CASE(int, kChar, char, saturate)
DOWN_CAST_CASE(int, kChar, schar, saturate)
DOWN_CAST_CASE(int, kUChar, uchar, saturate)
DOWN_CAST_CASE(int, kUnsignedChar, uchar, saturate)
DOWN_CAST_CASE(int, kShort, short, saturate)
@@ -662,6 +735,7 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
SIMPLE_CAST_CASE(int, kULong, ULong)
SIMPLE_CAST_CASE(int, kUnsignedLong, ULong)
TO_HALF_CASE(int)
TO_FLOAT_CASE(int)
TO_DOUBLE_CASE(int)
@@ -683,7 +757,7 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
memcpy(outRaw, inRaw, get_explicit_type_size(inType));
break;
DOWN_CAST_CASE(uint, kChar, char, saturate)
DOWN_CAST_CASE(uint, kChar, schar, saturate)
DOWN_CAST_CASE(uint, kUChar, uchar, saturate)
DOWN_CAST_CASE(uint, kUnsignedChar, uchar, saturate)
DOWN_CAST_CASE(uint, kShort, short, saturate)
@@ -694,6 +768,7 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
SIMPLE_CAST_CASE(uint, kULong, ULong)
SIMPLE_CAST_CASE(uint, kUnsignedLong, ULong)
TO_HALF_CASE(uint)
TO_FLOAT_CASE(uint)
TO_DOUBLE_CASE(uint)
@@ -715,7 +790,7 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
memcpy(outRaw, inRaw, get_explicit_type_size(inType));
break;
DOWN_CAST_CASE(uint, kChar, char, saturate)
DOWN_CAST_CASE(uint, kChar, schar, saturate)
DOWN_CAST_CASE(uint, kUChar, uchar, saturate)
DOWN_CAST_CASE(uint, kUnsignedChar, uchar, saturate)
DOWN_CAST_CASE(uint, kShort, short, saturate)
@@ -726,6 +801,7 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
SIMPLE_CAST_CASE(uint, kULong, ULong)
SIMPLE_CAST_CASE(uint, kUnsignedLong, ULong)
TO_HALF_CASE(uint)
TO_FLOAT_CASE(uint)
TO_DOUBLE_CASE(uint)
@@ -746,7 +822,7 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
memcpy(outRaw, inRaw, get_explicit_type_size(inType));
break;
DOWN_CAST_CASE(Long, kChar, char, saturate)
DOWN_CAST_CASE(Long, kChar, schar, saturate)
DOWN_CAST_CASE(Long, kUChar, uchar, saturate)
DOWN_CAST_CASE(Long, kUnsignedChar, uchar, saturate)
DOWN_CAST_CASE(Long, kShort, short, saturate)
@@ -758,6 +834,7 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
DOWN_CAST_CASE(Long, kULong, ULong, saturate)
DOWN_CAST_CASE(Long, kUnsignedLong, ULong, saturate)
TO_HALF_CASE(Long)
TO_FLOAT_CASE(Long)
TO_DOUBLE_CASE(Long)
@@ -779,7 +856,7 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
memcpy(outRaw, inRaw, get_explicit_type_size(inType));
break;
U_DOWN_CAST_CASE(ULong, kChar, char, saturate)
U_DOWN_CAST_CASE(ULong, kChar, schar, saturate)
U_DOWN_CAST_CASE(ULong, kUChar, uchar, saturate)
U_DOWN_CAST_CASE(ULong, kUnsignedChar, uchar, saturate)
U_DOWN_CAST_CASE(ULong, kShort, short, saturate)
@@ -790,6 +867,7 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
U_DOWN_CAST_CASE(ULong, kUnsignedInt, uint, saturate)
U_DOWN_CAST_CASE(ULong, kLong, Long, saturate)
TO_HALF_CASE(ULong)
TO_FLOAT_CASE(ULong)
TO_DOUBLE_CASE(ULong)
@@ -811,7 +889,7 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
memcpy(outRaw, inRaw, get_explicit_type_size(inType));
break;
U_DOWN_CAST_CASE(ULong, kChar, char, saturate)
U_DOWN_CAST_CASE(ULong, kChar, schar, saturate)
U_DOWN_CAST_CASE(ULong, kUChar, uchar, saturate)
U_DOWN_CAST_CASE(ULong, kUnsignedChar, uchar, saturate)
U_DOWN_CAST_CASE(ULong, kShort, short, saturate)
@@ -822,6 +900,7 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
U_DOWN_CAST_CASE(ULong, kUnsignedInt, uint, saturate)
U_DOWN_CAST_CASE(ULong, kLong, Long, saturate)
TO_HALF_CASE(ULong)
TO_FLOAT_CASE(ULong)
TO_DOUBLE_CASE(ULong)
@@ -832,13 +911,52 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
}
break;
case kHalf:
halfPtr = (cl_half *)inRaw;
switch (outType)
{
BOOL_CASE(half)
HALF_ROUND_CASE(kChar, schar, roundType, saturate)
HALF_ROUND_CASE(kUChar, uchar, roundType, saturate)
HALF_ROUND_CASE(kUnsignedChar, uchar, roundType, saturate)
HALF_ROUND_CASE(kShort, short, roundType, saturate)
HALF_ROUND_CASE(kUShort, ushort, roundType, saturate)
HALF_ROUND_CASE(kUnsignedShort, ushort, roundType, saturate)
HALF_ROUND_CASE(kInt, int, roundType, saturate)
HALF_ROUND_CASE(kUInt, uint, roundType, saturate)
HALF_ROUND_CASE(kUnsignedInt, uint, roundType, saturate)
HALF_ROUND_CASE(kLong, Long, roundType, saturate)
HALF_ROUND_CASE(kULong, ULong, roundType, saturate)
HALF_ROUND_CASE(kUnsignedLong, ULong, roundType, saturate)
case kHalf:
memcpy(outRaw, inRaw, get_explicit_type_size(inType));
break;
case kFloat:
floatPtr = (float *)outRaw;
*floatPtr = cl_half_to_float(*halfPtr);
break;
case kDouble:
doublePtr = (double *)outRaw;
*doublePtr = cl_half_to_float(*halfPtr);
break;
default:
log_error("ERROR: Invalid type given to "
"convert_explicit_value!!\n");
break;
}
break;
case kFloat:
floatPtr = (float *)inRaw;
switch (outType)
{
BOOL_CASE(float)
FLOAT_ROUND_CASE(kChar, char, roundType, saturate)
FLOAT_ROUND_CASE(kChar, schar, roundType, saturate)
FLOAT_ROUND_CASE(kUChar, uchar, roundType, saturate)
FLOAT_ROUND_CASE(kUnsignedChar, uchar, roundType, saturate)
FLOAT_ROUND_CASE(kShort, short, roundType, saturate)
@@ -851,6 +969,8 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
FLOAT_ROUND_CASE(kULong, ULong, roundType, saturate)
FLOAT_ROUND_CASE(kUnsignedLong, ULong, roundType, saturate)
TO_HALF_CASE(float)
case kFloat:
memcpy(outRaw, inRaw, get_explicit_type_size(inType));
break;
@@ -870,7 +990,7 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
{
BOOL_CASE(double)
DOUBLE_ROUND_CASE(kChar, char, roundType, saturate)
DOUBLE_ROUND_CASE(kChar, schar, roundType, saturate)
DOUBLE_ROUND_CASE(kUChar, uchar, roundType, saturate)
DOUBLE_ROUND_CASE(kUnsignedChar, uchar, roundType, saturate)
DOUBLE_ROUND_CASE(kShort, short, roundType, saturate)
@@ -883,6 +1003,8 @@ void convert_explicit_value(void *inRaw, void *outRaw, ExplicitType inType,
DOUBLE_ROUND_CASE(kULong, ULong, roundType, saturate)
DOUBLE_ROUND_CASE(kUnsignedLong, ULong, roundType, saturate)
TO_HALF_CASE(double)
TO_FLOAT_CASE(double);
case kDouble: