Skip to content

Commit

Permalink
util: introduce int56_to_long() type conversion helper
Browse files Browse the repository at this point in the history
This is to remove unnecessary mask with 0x00FFFFFFFFFFFFFF.

Signed-off-by: Tokunori Ikegami <[email protected]>
  • Loading branch information
ikegami-t committed Jan 3, 2025
1 parent 32eaadc commit f8b0589
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
3 changes: 1 addition & 2 deletions plugins/ocp/ocp-print-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@ static void json_smart_extended_log_v1(void *data)
(uint32_t)le32_to_cpu(*(uint32_t *)&log_data[SCAO_EECE]));
json_object_add_value_uint(root, "System data percent used",
(__u8)log_data[SCAO_SDPU]);
json_object_add_value_uint64(root, "Refresh counts",
(uint64_t)(le64_to_cpu(*(uint64_t *)&log_data[SCAO_RFSC]) & 0x00FFFFFFFFFFFFFF));
json_object_add_value_uint64(root, "Refresh counts", int56_to_long(&log_data[SCAO_RFSC]));
json_object_add_value_uint(root, "Max User data erase counts",
(uint32_t)le32_to_cpu(*(uint32_t *)&log_data[SCAO_MXUDEC]));
json_object_add_value_uint(root, "Min User data erase counts",
Expand Down
2 changes: 1 addition & 1 deletion plugins/ocp/ocp-print-stdout.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ static void stdout_smart_extended_log(void *data, unsigned int version)
printf(" System data percent used %d\n",
(__u8)log_data[SCAO_SDPU]);
printf(" Refresh counts %"PRIu64"\n",
(uint64_t)(le64_to_cpu(*(uint64_t *)&log_data[SCAO_RFSC]) & 0x00FFFFFFFFFFFFFF));
int56_to_long(&log_data[SCAO_RFSC]));
printf(" Max User data erase counts %"PRIu32"\n",
(uint32_t)le32_to_cpu(*(uint32_t *)&log_data[SCAO_MXUDEC]));
printf(" Min User data erase counts %"PRIu32"\n",
Expand Down
21 changes: 17 additions & 4 deletions util/types.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <string.h>
#include <locale.h>
#include <time.h>
#include <limits.h>

#include <ccan/endian/endian.h>

Expand Down Expand Up @@ -36,18 +37,30 @@ long double int128_to_double(__u8 *data)
return result;
}

uint64_t int48_to_long(const __u8 *data)
static uint64_t int_to_long(int bits, const __u8 *data)
{
int i;
uint64_t result = 0;
int bytes = (bits + CHAR_BIT - 1) / CHAR_BIT;

for (i = 0; i < 6; i++) {
result *= 256;
result += data[5 - i];
for (i = 0; i < bytes; i++) {
result <<= CHAR_BIT;
result += data[bytes - 1 - i];
}

return result;
}

uint64_t int48_to_long(const __u8 *data)
{
return int_to_long(48, data);
}

uint64_t int56_to_long(const __u8 *data)
{
return int_to_long(56, data);
}

long double uint128_t_to_double(nvme_uint128_t data)
{
long double result = 0;
Expand Down
1 change: 1 addition & 0 deletions util/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ typedef union nvme_uint128 nvme_uint128_t;
nvme_uint128_t le128_to_cpu(__u8 *data);
long double int128_to_double(__u8 *data);
uint64_t int48_to_long(const __u8 *data);
uint64_t int56_to_long(const __u8 *data);

char *uint128_t_to_string(nvme_uint128_t val);
char *uint128_t_to_l10n_string(nvme_uint128_t val);
Expand Down

0 comments on commit f8b0589

Please sign in to comment.