Thread (2 messages) 2 messages, 2 authors, 2021-01-18

Re: [PATCH v2] can: length: can_fd_len2dlc(): make legnth calculation readable again

From: Oliver Hartkopp <socketcan@hartkopp.net>
Date: 2021-01-18 20:18:15

Thanks Marc!

[PATCH v2] can: length: can_fd_len2dlc(): make legnth calculation 
readable again

legnth -> length?

;-)

I've sent a V3 which also adds a BUILD_BUG_ON() to make sure that the 
table has the correct size at build time.

Would that make sense to you?

Regards,
Oliver


On 17.01.21 23:40, Marc Kleine-Budde wrote:
quoted hunk ↗ jump to hunk
In commit 652562e5ff06 ("can: length: can_fd_len2dlc(): simplify length
calculcation") the readability of the code degraded and became more error
prone. To counteract this, partially convert that patch and replace open coded
values (of the original code) with proper defines.

Cc: Oliver Hartkopp <socketcan@hartkopp.net>
Cc: Vincent MAILHOL <redacted>
Fixes: 652562e5ff06 ("can: length: can_fd_len2dlc(): simplify length calculcation")
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
changes since v1:
- fix macro name (CANFD_MAX_LEN -> CANFD_MAX_DLEN)

  drivers/net/can/dev/length.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/drivers/net/can/dev/length.c b/drivers/net/can/dev/length.c
index d35c4e82314d..f0d7cc969c1f 100644
--- a/drivers/net/can/dev/length.c
+++ b/drivers/net/can/dev/length.c
@@ -27,12 +27,14 @@ static const u8 len2dlc[] = {
  	13, 13, 13, 13, 13, 13, 13, 13,	/* 25 - 32 */
  	14, 14, 14, 14, 14, 14, 14, 14,	/* 33 - 40 */
  	14, 14, 14, 14, 14, 14, 14, 14,	/* 41 - 48 */
+	15, 15, 15, 15, 15, 15, 15, 15,	/* 49 - 56 */
+	15, 15, 15, 15, 15, 15, 15, 15	/* 57 - 64 */
  };
  
  /* map the sanitized data length to an appropriate data length code */
  u8 can_fd_len2dlc(u8 len)
  {
-	if (len >= ARRAY_SIZE(len2dlc))
+	if (unlikely(len > CANFD_MAX_DLEN))
  		return CANFD_MAX_DLC;
  
  	return len2dlc[len];
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help