Skip to content

Commit

Permalink
handle corner cases involving NAN and/or INF
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-frbg authored Jun 6, 2024
1 parent ffc1ab3 commit 1abafcd
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 32 deletions.
59 changes: 42 additions & 17 deletions kernel/x86_64/cscal.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,22 @@ int CNAME(BLASLONG n, BLASLONG dummy0, BLASLONG dummy1, FLOAT da_r, FLOAT da_i,
while(j < n1)
{

if (isnan(x[i]) || isinf(x[i]))
temp0 = NAN;
else
temp0 = -da_i * x[i+1];
if (!isinf(x[i+1]))
x[i+1] = da_i * x[i];
else
x[i+1] = NAN;
x[i] = temp0;
if (isnan(x[i+inc_x]) || isinf(x[i+inc_x]))
temp1 = NAN;
else
temp1 = -da_i * x[i+1+inc_x];
x[i+1+inc_x] = da_i * x[i+inc_x];
if (!isinf(x[i+1+inc_x]))
x[i+1+inc_x] = da_i * x[i+inc_x];
else x[i+1+inc_x] = NAN;
x[i+inc_x] = temp1;
i += 2*inc_x ;
j+=2;
Expand All @@ -272,9 +283,14 @@ int CNAME(BLASLONG n, BLASLONG dummy0, BLASLONG dummy1, FLOAT da_r, FLOAT da_i,

while(j < n)
{

temp0 = -da_i * x[i+1];

if (isnan(x[i]) || isinf(x[i]))
temp0 = NAN;
else
temp0 = -da_i * x[i+1];
if (!isinf(x[i+1]))
x[i+1] = da_i * x[i];
else x[i+1] = NAN;
x[i] = temp0;
i += inc_x ;
j++;
Expand Down Expand Up @@ -365,42 +381,51 @@ int CNAME(BLASLONG n, BLASLONG dummy0, BLASLONG dummy1, FLOAT da_r, FLOAT da_i,
else
cscal_kernel_16_zero_r(n1 , alpha , x);
else
if ( da_i == 0 )
cscal_kernel_16_zero_i(n1 , alpha , x);
else
cscal_kernel_16(n1 , alpha , x);

i = n1 << 1;
j = n1;
}


if ( da_r == 0.0 )
if ( da_r == 0.0 || isnan(da_r) )
{

if ( da_i == 0.0 )
{

FLOAT res=0.0;
if (isnan(da_r)) res= da_r;
while(j < n)
{

x[i]=0.0;
x[i+1]=0.0;
x[i]=res;
x[i+1]=res;
i += 2 ;
j++;

}

}
else
else if (isinf(da_r)) {
while(j < n)
{
x[i]= NAN;
x[i+1] = da_r;
i += 2 ;
j++;

}

} else
{

while(j < n)
{

temp0 = -da_i * x[i+1];
x[i+1] = da_i * x[i];
x[i] = temp0;
if (isinf(x[i]))
temp0 = NAN;
if (!isinf(x[i+1]))
x[i+1] = da_i * x[i];
else x[i+1] = NAN;
if ( x[i] == x[i]) //preserve NaN
x[i] = temp0;
i += 2 ;
j++;

Expand Down
5 changes: 5 additions & 0 deletions kernel/x86_64/dscal.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ int CNAME(BLASLONG n, BLASLONG dummy0, BLASLONG dummy1, FLOAT da, FLOAT *x, BLAS
else x[i] = 0.0;
}
}
else if (isinf(da)){
for ( i=n1 ; i<n; i++)
if (x[i]==0.) x[i]=NAN;
else x[i] *=da;
}
else
{

Expand Down
44 changes: 29 additions & 15 deletions kernel/x86_64/sscal.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,75 +119,89 @@ int CNAME(BLASLONG n, BLASLONG dummy0, BLASLONG dummy1, FLOAT da, FLOAT *x, BLAS

if ( da == 0.0 )
{

BLASLONG n1 = n & -2;

while(j < n1)
{

x[i]=0.0;
x[i+inc_x]=0.0;
if (isinf(x[i])||isnan(x[i]))
x[i]=NAN;
else x[i]=0.0;
if (isinf(x[i+inc_x])||isnan(x[i+inc_x]))
x[i+inc_x]=NAN;
else x[i+inc_x]=0.0;
i += 2*inc_x ;
j+=2;

}

while(j < n)
{

x[i]=0.0;
if (isinf(x[i])||isnan(x[i]))
x[i]=NAN;
else x[i]=0.0;
i += inc_x ;
j++;

}
}
else
{

#if 1
BLASLONG n1 = n & -8;
if ( n1 > 0 )
{
sscal_kernel_inc_8(n1, &da, x, inc_x);
i = n1 * inc_x;
j = n1;
}

#endif
while(j < n)
{

x[i] *= da;
i += inc_x ;
j++;

}

}

return(0);
}

BLASLONG n1 = n & -16;
if ( n1 > 0 )
{
if ( da == 0.0 )
sscal_kernel_16_zero(n1 , &da , x);
else
//if ( da == 0.0 )
// sscal_kernel_16_zero(n1 , &da , x);
//else
sscal_kernel_16(n1 , &da , x);
}

if ( da == 0.0 )
{
for ( i=n1 ; i<n; i++ )
{
x[i] = 0.0;
if (isinf(x[i])||isnan(x[i]))
x[i]=NAN;
else x[i]=0.0;
}
}
else if ( isinf(da) )
{
for ( i=n1 ; i<n; i++ )
{
if (x[i] == 0.0)
x[i]=NAN;
else x[i] *= da;
}
}
else
{

for ( i=n1 ; i<n; i++ )
{
x[i] *= da;
if (isinf(x[i]))
x[i]=NAN;
else x[i] *= da;
}
}
return(0);
Expand Down

0 comments on commit 1abafcd

Please sign in to comment.