Skip to content

Commit

Permalink
Fixed floating point support (ARMmbed#24)
Browse files Browse the repository at this point in the history
This PR adds:
* Support for hexadecimal printing in lower case
* Fixes to floating point printing (leading zeros in decimal part)
  • Loading branch information
evedon authored Jul 2, 2019
1 parent fe6bdca commit 96b5c1d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
31 changes: 27 additions & 4 deletions features/minimal-printf/TESTS/minimal-printf/compliance/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ static control_t test_printf_x(const size_t call_count)
TEST_ASSERT_EQUAL_INT(result_baseline, result_minimal);
#endif

result_minimal = mbed_printf("x: %x\r\n", 11259375);
result_baseline = printf("x: %x\r\n", 11259375);
TEST_ASSERT_EQUAL_INT(result_baseline, result_minimal);

return CaseNext;
}

Expand Down Expand Up @@ -629,12 +633,21 @@ static control_t test_printf_f(const size_t call_count)

double pi = 3.14159265359;


result_minimal = mbed_printf("f: %f\r\n", 3.0089);
result_baseline = printf("f: %f\r\n", 3.0089);
TEST_ASSERT_EQUAL_INT(result_baseline, result_minimal);

result_minimal = mbed_printf("f: %f\r\n", 7.0);
result_baseline = printf("f: %f\r\n", 7.0);
TEST_ASSERT_EQUAL_INT(result_baseline, result_minimal);

result_minimal = mbed_printf("f: %f\r\n", -1 * pi);
result_baseline = printf("f: %f\r\n", -1 * pi);
TEST_ASSERT_EQUAL_INT(result_baseline, result_minimal);

result_minimal = mbed_printf("f: %f\r\n", 0);
result_baseline = printf("f: %f\r\n", 0);
result_minimal = mbed_printf("f: %f\r\n", 0.0);
result_baseline = printf("f: %f\r\n", 0.0);
TEST_ASSERT_EQUAL_INT(result_baseline, result_minimal);

result_minimal = mbed_printf("f: %f\r\n", pi);
Expand All @@ -656,13 +669,23 @@ static control_t test_snprintf_f(const size_t call_count)

double pi = 3.14159265359;

result_minimal = mbed_snprintf(buffer_minimal, sizeof(buffer_minimal), "f: %f\r\n", 3.0089);
result_baseline = snprintf(buffer_baseline, sizeof(buffer_baseline), "f: %f\r\n", 3.0089);
TEST_ASSERT_EQUAL_STRING(buffer_baseline, buffer_minimal);
TEST_ASSERT_EQUAL_INT(result_baseline, result_minimal);

result_minimal = mbed_snprintf(buffer_minimal, sizeof(buffer_minimal), "f: %f\r\n", 7.0);
result_baseline = snprintf(buffer_baseline, sizeof(buffer_baseline), "f: %f\r\n", 7.0);
TEST_ASSERT_EQUAL_STRING(buffer_baseline, buffer_minimal);
TEST_ASSERT_EQUAL_INT(result_baseline, result_minimal);

result_minimal = mbed_snprintf(buffer_minimal, sizeof(buffer_minimal), "f: %f\r\n", -1 * pi);
result_baseline = snprintf(buffer_baseline, sizeof(buffer_baseline), "f: %f\r\n", -1 * pi);
TEST_ASSERT_EQUAL_STRING(buffer_baseline, buffer_minimal);
TEST_ASSERT_EQUAL_INT(result_baseline, result_minimal);

result_minimal = mbed_snprintf(buffer_minimal, sizeof(buffer_minimal), "f: %f\r\n", 0);
result_baseline = snprintf(buffer_baseline, sizeof(buffer_baseline), "f: %f\r\n", 0);
result_minimal = mbed_snprintf(buffer_minimal, sizeof(buffer_minimal), "f: %f\r\n", 0.0);
result_baseline = snprintf(buffer_baseline, sizeof(buffer_baseline), "f: %f\r\n", 0.0);
TEST_ASSERT_EQUAL_STRING(buffer_baseline, buffer_minimal);
TEST_ASSERT_EQUAL_INT(result_baseline, result_minimal);

Expand Down
26 changes: 21 additions & 5 deletions features/minimal-printf/mbed_printf_implementation.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ typedef enum {
*/
static void mbed_minimal_formatted_string_signed(char* buffer, size_t length, int* result, MBED_SIGNED_STORAGE value);
static void mbed_minimal_formatted_string_unsigned(char* buffer, size_t length, int* result, MBED_UNSIGNED_STORAGE value);
static void mbed_minimal_formatted_string_hexadecimal(char* buffer, size_t length, int* result, MBED_UNSIGNED_STORAGE value);
static void mbed_minimal_formatted_string_hexadecimal(char* buffer, size_t length, int* result, MBED_UNSIGNED_STORAGE value, bool upper);
static void mbed_minimal_formatted_string_void_pointer(char* buffer, size_t length, int* result, const void* value);
static void mbed_minimal_formatted_string_character(char* buffer, size_t length, int* result, char character);
static void mbed_minimal_formatted_string_string(char* buffer, size_t length, int* result, const char* string, size_t precision);
Expand Down Expand Up @@ -268,8 +268,9 @@ static void mbed_minimal_formatted_string_unsigned(char* buffer, size_t length,
* @param[in] length The length of the buffer.
* @param result The current output location.
* @param[in] value The value to be printed.
* @param upper Flag to print the hexadecimal in upper or lower case.
*/
static void mbed_minimal_formatted_string_hexadecimal(char* buffer, size_t length, int* result, MBED_UNSIGNED_STORAGE value)
static void mbed_minimal_formatted_string_hexadecimal(char* buffer, size_t length, int* result, MBED_UNSIGNED_STORAGE value, bool upper)
{
bool print_leading_zero = false;

Expand All @@ -284,8 +285,11 @@ static void mbed_minimal_formatted_string_hexadecimal(char* buffer, size_t lengt
unsigned int nibble_one = (output >> 4);
unsigned int nibble_two = (output & 0x0F);

const char int2hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
const char int2hex_lower[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
const char int2hex_upper[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
const char *int2hex = upper ? int2hex_upper : int2hex_lower;

if (print_leading_zero || nibble_one != 0) {
mbed_minimal_putchar(buffer, length, result, int2hex[nibble_one]);
Expand Down Expand Up @@ -313,7 +317,7 @@ static void mbed_minimal_formatted_string_void_pointer(char* buffer, size_t leng
mbed_minimal_putchar(buffer, length, result, 'x');

/* write rest as a regular hexadecimal number */
mbed_minimal_formatted_string_hexadecimal(buffer, length, result, (ptrdiff_t) value);
mbed_minimal_formatted_string_hexadecimal(buffer, length, result, (ptrdiff_t) value, true);
}

#if MBED_CONF_MINIMAL_PRINTF_ENABLE_FLOATING_POINT
Expand Down Expand Up @@ -367,6 +371,18 @@ static void mbed_minimal_formatted_string_double(char* buffer, size_t length, in
decimal++;
}

/* convert precision to unsigned integer */
MBED_UNSIGNED_STORAGE precision_in_uint = precision;
precision_in_uint /= 10;

/* ensure that leading zeros are printed if decimal equals 0 */
MBED_UNSIGNED_STORAGE val = decimal ? decimal : decimal + 1;
while (precision_in_uint > val) {
/* print leading zeros */
mbed_minimal_putchar(buffer, length, result, '0');
precision_in_uint /= 10;
}

/* write decimal part */
mbed_minimal_formatted_string_unsigned(buffer, length, result, decimal);
}
Expand Down Expand Up @@ -694,7 +710,7 @@ int mbed_minimal_formatted_string(char* buffer, size_t length, const char* forma
}
else
{
mbed_minimal_formatted_string_hexadecimal(buffer, length, &result, value);
mbed_minimal_formatted_string_hexadecimal(buffer, length, &result, value, next == 'X');
}
}
#if MBED_CONF_MINIMAL_PRINTF_ENABLE_FLOATING_POINT
Expand Down

0 comments on commit 96b5c1d

Please sign in to comment.