Skip to contentSkip to conversation

can: flexcan: simplify the calculation of priv->mb_count

PATCH In review baozhu.liu 3 participants 3 messages Nov 4, 2024

Subsystems (3)
lore
Share & tools
lore

baozhu.liu

author
#0 lore raw

PATCH 1/1 can: flexcan: simplify the calculation of priv->mb_count

Since mb is a fixed-size two-dimensional array (u8 mb[2][512]), "priv->mb_count = sizeof(priv->regs->mb)/priv->mb_size;", this expression calculates mb_count correctly and is more concise.

---
 drivers/net/can/flexcan/flexcan-core.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/can/flexcan/flexcan-core.c +1 -2
index 6d638c939..e3a8bad21 100644
@@ -1371,8 +1371,7 @@ static int flexcan_rx_offload_setup(struct net_device *dev)
 	if (priv->devtype_data.quirks & FLEXCAN_QUIRK_NR_MB_16)
 		priv->mb_count = 16;
 	else
-		priv->mb_count = (sizeof(priv->regs->mb[0]) / priv->mb_size) +
-				 (sizeof(priv->regs->mb[1]) / priv->mb_size);
+		priv->mb_count = sizeof(priv->regs->mb) / priv->mb_size;
 
 	if (priv->devtype_data.quirks & FLEXCAN_QUIRK_USE_RX_MAILBOX)
 		priv->tx_mb_reserved =
Signature · 1 line
2.17.1
#1 lore raw

Hi Liu,

Quoted 3 lines · baozhu.liu

Since mb is a fixed-size two-dimensional array (u8 mb[2][512]), "priv->mb_count = sizeof(priv->regs->mb)/priv->mb_size;", this expression calculates mb_count correctly and is more concise.

When using integers,

(a1 / q) + (a2 / q)

is not necessarily equal to

(a1 + a2) / q.

If the decimal place of

sizeof(priv->regs->mb[0]) / priv->mb_size

were to be greater than or equal to 0.5, the result would have changed because of the rounding.

This is illustrated in https://godbolt.org/z/bfnhKcKPo.

Here, luckily enough, the two valid results are, for CAN CC:

sizeof(priv->regs->mb[0]) / priv->mb_size = 512 / 16

                                            = 64

and for CAN FD:

sizeof(priv->regs->mb[0]) / priv->mb_size = 512 / 72

                                            = 14.22

and both of these have no rounding issues.

I am not sure if we should take this patch. It is correct at the end because you "won a coin flip", but the current code is doing the correct logical calculation which would always yield the correct result regardless of the rounding.

Quoted 10 lines
---
 drivers/net/can/flexcan/flexcan-core.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/can/flexcan/flexcan-core.c +0 -0
index 6d638c939..e3a8bad21 100644

drivers/net/can/flexcan/flexcan-core.c static int flexcan_rx_offload_setup(struct net_device *dev) · line 1377

Quoted 9 lines
drivers/net/can/flexcan/flexcan-core.c +1 -2
@@ -1371,8 +1371,7 @@ static int flexcan_rx_offload_setup(struct net_device *dev)
        if (priv->devtype_data.quirks & FLEXCAN_QUIRK_NR_MB_16)
                priv->mb_count = 16;
        else
-               priv->mb_count = (sizeof(priv->regs->mb[0]) / priv->mb_size) +
-                                (sizeof(priv->regs->mb[1]) / priv->mb_size);
+               priv->mb_count = sizeof(priv->regs->mb) / priv->mb_size;
 
        if (priv->devtype_data.quirks & FLEXCAN_QUIRK_USE_RX_MAILBOX)
                priv->tx_mb_reserved =

Yours sincerely,
Vincent Mailhol

#2 lore raw
Quoted 31 lines · Vincent Mailhol
Quoted 3 lines · baozhu.liu

Since mb is a fixed-size two-dimensional array (u8 mb[2][512]), "priv->mb_count = sizeof(priv->regs->mb)/priv->mb_size;", this expression calculates mb_count correctly and is more concise.

When using integers,

(a1 / q) + (a2 / q)

is not necessarily equal to

(a1 + a2) / q.

If the decimal place of

sizeof(priv->regs->mb[0]) / priv->mb_size

were to be greater than or equal to 0.5, the result would have changed because of the rounding.

This is illustrated in https://godbolt.org/z/bfnhKcKPo.

Here, luckily enough, the two valid results are, for CAN CC:

sizeof(priv->regs->mb[0]) / priv->mb_size = 512 / 16

                                            = 64

and for CAN FD:

sizeof(priv->regs->mb[0]) / priv->mb_size = 512 / 72

Quoted 8 lines
                                            = 14.22

and both of these have no rounding issues.

I am not sure if we should take this patch. It is correct at the end because you "won a coin flip", but the current code is doing the correct logical calculation which would always yield the correct result regardless of the rounding.

Wow, that's an elaborative answer. Thanks Vincent!

And yes the current code does the correct logical calculation because of the underlying restrictions of the hardware. A CAN-CC/FD frame cannot cross the boundary between the 2 memory areas.

If you want to improve something, feel free to add a comment explaining the reasoning for the existing calculation.

regards,
Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |