Skip to content

Commit

Permalink
[libclc] Format clc_ldexp.cl and clc_hypot.cl. NFC
Browse files Browse the repository at this point in the history
  • Loading branch information
frasercrmck committed Oct 31, 2024
1 parent 78a98c7 commit fba9f05
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 138 deletions.
110 changes: 56 additions & 54 deletions libclc/generic/lib/math/clc_hypot.cl
Original file line number Diff line number Diff line change
Expand Up @@ -23,76 +23,78 @@
#include <clc/clc.h>
#include <math/clc_hypot.h>

#include "../clcmacro.h"
#include "config.h"
#include "math.h"
#include "../clcmacro.h"

// Returns sqrt(x*x + y*y) with no overflow or underflow unless the result warrants it
_CLC_DEF _CLC_OVERLOAD float __clc_hypot(float x, float y)
{
uint ux = as_uint(x);
uint aux = ux & EXSIGNBIT_SP32;
uint uy = as_uint(y);
uint auy = uy & EXSIGNBIT_SP32;
float retval;
int c = aux > auy;
ux = c ? aux : auy;
uy = c ? auy : aux;

int xexp = clamp((int)(ux >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32, -126, 126);
float fx_exp = as_float((xexp + EXPBIAS_SP32) << EXPSHIFTBITS_SP32);
float fi_exp = as_float((-xexp + EXPBIAS_SP32) << EXPSHIFTBITS_SP32);
float fx = as_float(ux) * fi_exp;
float fy = as_float(uy) * fi_exp;
retval = sqrt(mad(fx, fx, fy*fy)) * fx_exp;

retval = ux > PINFBITPATT_SP32 | uy == 0 ? as_float(ux) : retval;
retval = ux == PINFBITPATT_SP32 | uy == PINFBITPATT_SP32 ? as_float(PINFBITPATT_SP32) : retval;
return retval;
// Returns sqrt(x*x + y*y) with no overflow or underflow unless the result
// warrants it
_CLC_DEF _CLC_OVERLOAD float __clc_hypot(float x, float y) {
uint ux = as_uint(x);
uint aux = ux & EXSIGNBIT_SP32;
uint uy = as_uint(y);
uint auy = uy & EXSIGNBIT_SP32;
float retval;
int c = aux > auy;
ux = c ? aux : auy;
uy = c ? auy : aux;

int xexp = clamp((int)(ux >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32, -126, 126);
float fx_exp = as_float((xexp + EXPBIAS_SP32) << EXPSHIFTBITS_SP32);
float fi_exp = as_float((-xexp + EXPBIAS_SP32) << EXPSHIFTBITS_SP32);
float fx = as_float(ux) * fi_exp;
float fy = as_float(uy) * fi_exp;
retval = sqrt(mad(fx, fx, fy * fy)) * fx_exp;

retval = ux > PINFBITPATT_SP32 | uy == 0 ? as_float(ux) : retval;
retval = ux == PINFBITPATT_SP32 | uy == PINFBITPATT_SP32
? as_float(PINFBITPATT_SP32)
: retval;
return retval;
}
_CLC_BINARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, float, __clc_hypot, float, float)

#ifdef cl_khr_fp64
_CLC_DEF _CLC_OVERLOAD double __clc_hypot(double x, double y)
{
ulong ux = as_ulong(x) & ~SIGNBIT_DP64;
int xexp = ux >> EXPSHIFTBITS_DP64;
x = as_double(ux);
_CLC_DEF _CLC_OVERLOAD double __clc_hypot(double x, double y) {
ulong ux = as_ulong(x) & ~SIGNBIT_DP64;
int xexp = ux >> EXPSHIFTBITS_DP64;
x = as_double(ux);

ulong uy = as_ulong(y) & ~SIGNBIT_DP64;
int yexp = uy >> EXPSHIFTBITS_DP64;
y = as_double(uy);
ulong uy = as_ulong(y) & ~SIGNBIT_DP64;
int yexp = uy >> EXPSHIFTBITS_DP64;
y = as_double(uy);

int c = xexp > EXPBIAS_DP64 + 500 | yexp > EXPBIAS_DP64 + 500;
double preadjust = c ? 0x1.0p-600 : 1.0;
double postadjust = c ? 0x1.0p+600 : 1.0;
int c = xexp > EXPBIAS_DP64 + 500 | yexp > EXPBIAS_DP64 + 500;
double preadjust = c ? 0x1.0p-600 : 1.0;
double postadjust = c ? 0x1.0p+600 : 1.0;

c = xexp < EXPBIAS_DP64 - 500 | yexp < EXPBIAS_DP64 - 500;
preadjust = c ? 0x1.0p+600 : preadjust;
postadjust = c ? 0x1.0p-600 : postadjust;
c = xexp < EXPBIAS_DP64 - 500 | yexp < EXPBIAS_DP64 - 500;
preadjust = c ? 0x1.0p+600 : preadjust;
postadjust = c ? 0x1.0p-600 : postadjust;

double ax = x * preadjust;
double ay = y * preadjust;
double ax = x * preadjust;
double ay = y * preadjust;

// The post adjust may overflow, but this can't be avoided in any case
double r = sqrt(fma(ax, ax, ay*ay)) * postadjust;
// The post adjust may overflow, but this can't be avoided in any case
double r = sqrt(fma(ax, ax, ay * ay)) * postadjust;

// If the difference in exponents between x and y is large
double s = x + y;
c = abs(xexp - yexp) > MANTLENGTH_DP64 + 1;
r = c ? s : r;
// If the difference in exponents between x and y is large
double s = x + y;
c = abs(xexp - yexp) > MANTLENGTH_DP64 + 1;
r = c ? s : r;

// Check for NaN
//c = x != x | y != y;
c = isnan(x) | isnan(y);
r = c ? as_double(QNANBITPATT_DP64) : r;
// Check for NaN
// c = x != x | y != y;
c = isnan(x) | isnan(y);
r = c ? as_double(QNANBITPATT_DP64) : r;

// If either is Inf, we must return Inf
c = x == as_double(PINFBITPATT_DP64) | y == as_double(PINFBITPATT_DP64);
r = c ? as_double(PINFBITPATT_DP64) : r;
// If either is Inf, we must return Inf
c = x == as_double(PINFBITPATT_DP64) | y == as_double(PINFBITPATT_DP64);
r = c ? as_double(PINFBITPATT_DP64) : r;

return r;
return r;
}

_CLC_BINARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, double, __clc_hypot, double, double)
_CLC_BINARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, double, __clc_hypot, double,
double)
#endif
169 changes: 85 additions & 84 deletions libclc/generic/lib/math/clc_ldexp.cl
Original file line number Diff line number Diff line change
Expand Up @@ -20,109 +20,110 @@
* THE SOFTWARE.
*/

#include <clc/clc.h>
#include "config.h"
#include "../clcmacro.h"
#include "config.h"
#include "math.h"
#include <clc/clc.h>

_CLC_DEF _CLC_OVERLOAD float __clc_ldexp(float x, int n) {

if (!__clc_fp32_subnormals_supported()) {

// This treats subnormals as zeros
int i = as_int(x);
int e = (i >> 23) & 0xff;
int m = i & 0x007fffff;
int s = i & 0x80000000;
int v = add_sat(e, n);
v = clamp(v, 0, 0xff);
int mr = e == 0 | v == 0 | v == 0xff ? 0 : m;
int c = e == 0xff;
mr = c ? m : mr;
int er = c ? e : v;
er = e ? er : e;
return as_float( s | (er << 23) | mr );
}

/* supports denormal values */
const int multiplier = 24;
float val_f;
uint val_ui;
uint sign;
int exponent;
val_ui = as_uint(x);
sign = val_ui & 0x80000000;
val_ui = val_ui & 0x7fffffff;/* remove the sign bit */
int val_x = val_ui;

exponent = val_ui >> 23; /* get the exponent */
int dexp = exponent;

/* denormal support */
int fbh = 127 - (as_uint((float)(as_float(val_ui | 0x3f800000) - 1.0f)) >> 23);
int dexponent = 25 - fbh;
uint dval_ui = (( (val_ui << fbh) & 0x007fffff) | (dexponent << 23));
int ex = dexponent + n - multiplier;
dexponent = ex;
uint val = sign | (ex << 23) | (dval_ui & 0x007fffff);
int ex1 = dexponent + multiplier;
ex1 = -ex1 +25;
dval_ui = (((dval_ui & 0x007fffff )| 0x800000) >> ex1);
dval_ui = dexponent > 0 ? val :dval_ui;
dval_ui = dexponent > 254 ? 0x7f800000 :dval_ui; /*overflow*/
dval_ui = dexponent < -multiplier ? 0 : dval_ui; /*underflow*/
dval_ui = dval_ui | sign;
val_f = as_float(dval_ui);

exponent += n;

val = sign | (exponent << 23) | (val_ui & 0x007fffff);
ex1 = exponent + multiplier;
ex1 = -ex1 +25;
val_ui = (((val_ui & 0x007fffff )| 0x800000) >> ex1);
val_ui = exponent > 0 ? val :val_ui;
val_ui = exponent > 254 ? 0x7f800000 :val_ui; /*overflow*/
val_ui = exponent < -multiplier ? 0 : val_ui; /*underflow*/
val_ui = val_ui | sign;

val_ui = dexp == 0? dval_ui : val_ui;
val_f = as_float(val_ui);

val_f = isnan(x) | isinf(x) | val_x == 0 ? x : val_f;
return val_f;
if (!__clc_fp32_subnormals_supported()) {

// This treats subnormals as zeros
int i = as_int(x);
int e = (i >> 23) & 0xff;
int m = i & 0x007fffff;
int s = i & 0x80000000;
int v = add_sat(e, n);
v = clamp(v, 0, 0xff);
int mr = e == 0 | v == 0 | v == 0xff ? 0 : m;
int c = e == 0xff;
mr = c ? m : mr;
int er = c ? e : v;
er = e ? er : e;
return as_float(s | (er << 23) | mr);
}

/* supports denormal values */
const int multiplier = 24;
float val_f;
uint val_ui;
uint sign;
int exponent;
val_ui = as_uint(x);
sign = val_ui & 0x80000000;
val_ui = val_ui & 0x7fffffff; /* remove the sign bit */
int val_x = val_ui;

exponent = val_ui >> 23; /* get the exponent */
int dexp = exponent;

/* denormal support */
int fbh =
127 - (as_uint((float)(as_float(val_ui | 0x3f800000) - 1.0f)) >> 23);
int dexponent = 25 - fbh;
uint dval_ui = (((val_ui << fbh) & 0x007fffff) | (dexponent << 23));
int ex = dexponent + n - multiplier;
dexponent = ex;
uint val = sign | (ex << 23) | (dval_ui & 0x007fffff);
int ex1 = dexponent + multiplier;
ex1 = -ex1 + 25;
dval_ui = (((dval_ui & 0x007fffff) | 0x800000) >> ex1);
dval_ui = dexponent > 0 ? val : dval_ui;
dval_ui = dexponent > 254 ? 0x7f800000 : dval_ui; /*overflow*/
dval_ui = dexponent < -multiplier ? 0 : dval_ui; /*underflow*/
dval_ui = dval_ui | sign;
val_f = as_float(dval_ui);

exponent += n;

val = sign | (exponent << 23) | (val_ui & 0x007fffff);
ex1 = exponent + multiplier;
ex1 = -ex1 + 25;
val_ui = (((val_ui & 0x007fffff) | 0x800000) >> ex1);
val_ui = exponent > 0 ? val : val_ui;
val_ui = exponent > 254 ? 0x7f800000 : val_ui; /*overflow*/
val_ui = exponent < -multiplier ? 0 : val_ui; /*underflow*/
val_ui = val_ui | sign;

val_ui = dexp == 0 ? dval_ui : val_ui;
val_f = as_float(val_ui);

val_f = isnan(x) | isinf(x) | val_x == 0 ? x : val_f;
return val_f;
}

#ifdef cl_khr_fp64

#pragma OPENCL EXTENSION cl_khr_fp64 : enable

_CLC_DEF _CLC_OVERLOAD double __clc_ldexp(double x, int n) {
long l = as_ulong(x);
int e = (l >> 52) & 0x7ff;
long s = l & 0x8000000000000000;
long l = as_ulong(x);
int e = (l >> 52) & 0x7ff;
long s = l & 0x8000000000000000;

ulong ux = as_ulong(x * 0x1.0p+53);
int de = ((int)(ux >> 52) & 0x7ff) - 53;
int c = e == 0;
e = c ? de: e;
ulong ux = as_ulong(x * 0x1.0p+53);
int de = ((int)(ux >> 52) & 0x7ff) - 53;
int c = e == 0;
e = c ? de : e;

ux = c ? ux : l;
ux = c ? ux : l;

int v = e + n;
v = clamp(v, -0x7ff, 0x7ff);
int v = e + n;
v = clamp(v, -0x7ff, 0x7ff);

ux &= ~EXPBITS_DP64;
ux &= ~EXPBITS_DP64;

double mr = as_double(ux | ((ulong)(v+53) << 52));
mr = mr * 0x1.0p-53;
double mr = as_double(ux | ((ulong)(v + 53) << 52));
mr = mr * 0x1.0p-53;

mr = v > 0 ? as_double(ux | ((ulong)v << 52)) : mr;
mr = v > 0 ? as_double(ux | ((ulong)v << 52)) : mr;

mr = v == 0x7ff ? as_double(s | PINFBITPATT_DP64) : mr;
mr = v < -53 ? as_double(s) : mr;
mr = v == 0x7ff ? as_double(s | PINFBITPATT_DP64) : mr;
mr = v < -53 ? as_double(s) : mr;

mr = ((n == 0) | isinf(x) | (x == 0) ) ? x : mr;
return mr;
mr = ((n == 0) | isinf(x) | (x == 0)) ? x : mr;
return mr;
}

#endif
Expand All @@ -132,7 +133,7 @@ _CLC_DEF _CLC_OVERLOAD double __clc_ldexp(double x, int n) {
#pragma OPENCL EXTENSION cl_khr_fp16 : enable

_CLC_OVERLOAD _CLC_DEF half __clc_ldexp(half x, int n) {
return (half)__clc_ldexp((float)x, n);
return (half)__clc_ldexp((float)x, n);
}

_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, half, __clc_ldexp, half, int);
Expand Down

0 comments on commit fba9f05

Please sign in to comment.