Skip to content

Commit

Permalink
printf: Fix printf 'mixed_format_random'
Browse files Browse the repository at this point in the history
%a or %A with printf on MSVC platforms have a default precision
of 13, which is in contrast to OpenCL C specification for printf
which only allows for exact digits required if precision is not
specified.
  • Loading branch information
Sreelakshmi Haridas Maruthur committed Jan 17, 2025
1 parent 2ff5cda commit dbd78c5
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion test_conformance/printf/test_printf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,34 @@ cl_program makeMixedFormatPrintfProgram(cl_kernel* kernel_ptr,
const float max_range = 100000.f;
float arg = get_random_float(-max_range, max_range, gMTdata);
args_str << str_sprintf("%f", arg) << "f, ";
ref_str << str_sprintf(format, arg) << ", ";

// %a and %A has default precision of 13 on MSVC which results in
// padded zeroes. As per OpenCL C Specification 3.0: 6.15.14.2. if
// precision is missing, implementations only need to have
// sufficient precision for an exact representation of the value.
if (format == "%a" || format == "%A")
{
std::string hex_float = str_sprintf(format, arg);
for (size_t index = hex_float.size() - 1; index > 0; index--)
{
if (hex_float[index] == 'p' || hex_float[index] == 'P')
{
index--;
while (hex_float[index] == '0'
&& hex_float[index - 1] != '.')
{
hex_float.erase(index, 1);
index--;
}
break;
}
}
ref_str << hex_float << ", ";
}
else
{
ref_str << str_sprintf(format, arg) << ", ";
}
}
}
// Restore the original CPU rounding mode
Expand Down

0 comments on commit dbd78c5

Please sign in to comment.