Skip to content

Commit

Permalink
714-2 Fix vector 16 printf (#731)
Browse files Browse the repository at this point in the history
* Fix vector size detection for size 16

* add tests and make logic simple

* Update src/printf.cpp

---------

Co-authored-by: Kévin Petit <kpet@free.fr>
  • Loading branch information
Rekt3421 and kpet authored Oct 26, 2024
1 parent 0ef41e9 commit 56d99b3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/printf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@ std::string get_vector_fmt(std::string fmt, int& vector_size, int& element_size,
fmt = fmt_specifier;

size_t vec_length_pos_start = ++pos;
size_t vec_length_pos_end =
fmt.find_first_not_of("123468", vec_length_pos_start);
// Assume vec len is at max 8 in which case we need just one digit.
size_t vec_length_pos_end = pos + 1;
// Check if len is 16 and update accordingly. This is only possible if
// the first char of the size is 1.
if (fmt[vec_length_pos_start] == '1') {
vec_length_pos_end++;
}
auto vec_length_str = fmt.substr(vec_length_pos_start,
vec_length_pos_end - vec_length_pos_start);
int vec_length = std::atoi(vec_length_str.c_str());
Expand Down
31 changes: 31 additions & 0 deletions tests/api/printf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,35 @@ TEST_F(WithCommandQueueAndPrintf, PrintfMissingLengthModifier) {
ASSERT_STREQ(m_printf_output.c_str(), message);
}

TEST_F(WithCommandQueueAndPrintf, VectorLen8) {
const char message[] = "+1,+2,+3,+4,+5,+6,+7,+8";
char source[512];
sprintf(source, "kernel void test_printf() { printf(\"%%+v8i\", "
"(int8)(1,2,3,4,5,6,7,8));}");
auto kernel = CreateKernel(source, "test_printf");

size_t gws = 1;
size_t lws = 1;
EnqueueNDRangeKernel(kernel, 1, nullptr, &gws, &lws, 0, nullptr, nullptr);
Finish();

ASSERT_STREQ(m_printf_output.c_str(), message);
}

TEST_F(WithCommandQueueAndPrintf, VectorLen16) {
const char message[] =
"+1,+2,+3,+4,+5,+6,+7,+8,+9,+10,+11,+12,+13,+14,+15,+16";
char source[512];
sprintf(source, "kernel void test_printf() { printf(\"%%+v16i\", "
"(int16)(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16));}");
auto kernel = CreateKernel(source, "test_printf");

size_t gws = 1;
size_t lws = 1;
EnqueueNDRangeKernel(kernel, 1, nullptr, &gws, &lws, 0, nullptr, nullptr);
Finish();

ASSERT_STREQ(m_printf_output.c_str(), message);
}

#endif

0 comments on commit 56d99b3

Please sign in to comment.