# PyDSDL/Nunavut Union offsets seem to be incorrect

**URL:** <https://forum.opencyphal.org/t/pydsdl-nunavut-union-offsets-seem-to-be-incorrect/847>\
**Category:** Nunavut\
**Created:** [May 27, 2020, 3:13am UTC](https://forum.opencyphal.org/t/pydsdl-nunavut-union-offsets-seem-to-be-incorrect/847 "2020-05-27T03:13:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![david.lenfesty](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/david.lenfesty/32/488_2.png) [@david.lenfesty](https://forum.opencyphal.org/u/david.lenfesty)\
**Post date:** [May 27, 2020, 3:13am UTC](https://forum.opencyphal.org/t/pydsdl-nunavut-union-offsets-seem-to-be-incorrect/847/1 "2020-05-27T03:13:21Z")

</div>

I’m trying to implement a template for libcanard (in plain C), and I’m running into troubles implementing UnionTypes. There seem to be two parts to this: the UnionType itself does not seem to set the tag bit length correctly, and then that incorrect value is being given to whatever calculates the offset.

From `pydsdl._serializable._composite.UnionType`, [line 335](https://github.com/UAVCAN/pydsdl/blob/f53eb68f7c403fe27db5f8ff18e45289b6e14cae/pydsdl/_serializable/_composite.py#L335).

```auto
tag_bit_length = 2 ** math.ceil(math.log2(max(8, unaligned_tag_bit_length)))

```

Which seems to just be calculating the bit length and then dumping it back into number of fields. To me this doesn’t seem to be correct behaviour, but I don’t know the code well enough to say that definitively (which is why I’m not posting this as an issue). I also can’t find any other reference to the tag bit length elsewhere in pydsdl.

If this code is wrong, I believe `max(8` and the `2 **` should be removed from that line. Otherwise, I’m not sure how I’m supposed to implement the tag bit width.

Here’s a direct snippet from my UnionType.j2 template:

```auto
{%- for field, offset in T.iterate_fields_with_offsets() %}
  case ({{T.full_name | c.macrofy}}_{{field.name | c.macrofy}}_TAG):
    {{ encode.set_size(field, offset) | indent(width=2) }}
    break;
{%- endfor %}

```

And here is it’s output:

```auto
case (SPEAR_DRIVE_DRIVECOMMAND_THING_TAG):
    canardDSDLSetUxx(buf, offset + {8}, data->thing, 8);
    size += 8;
    break;
  case (SPEAR_DRIVE_DRIVECOMMAND_ID_TAG):
    size += uavcan_node_ID_encode(buf, offset + {8}, data->id);
    break;

```

The offset, according to the spec, should be 1 (instead of 8, as it is right now), as that is the bit length required to represent the two data types.

From this DSDL:

```auto
@union
uint8 thing
uavcan.node.ID.1.0 id

```

---

<div class="post-metadata">

**Author:** ![pavel.kirienko](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/pavel.kirienko/32/9_2.png) [@pavel.kirienko](https://forum.opencyphal.org/u/pavel.kirienko)\
**Post date:** [May 27, 2020, 4:36pm UTC](https://forum.opencyphal.org/t/pydsdl-nunavut-union-offsets-seem-to-be-incorrect/847/2 "2020-05-27T16:36:58Z")

</div>

The objective of the line of code you referenced is to ensure that the tag length is rounded up to the nearest one of {8, 16, 32, 64} bits. The rationale is explained in [Proposed change to variable-length arrays](https://forum.opencyphal.org/t/proposed-change-to-variable-length-arrays/740). Unfortunately, the Specification is currently outdated but we will be looking into that shortly (tracking issue: [https://github.com/UAVCAN/specification/issues/75](https://github.com/UAVCAN/specification/issues/75)).

In the example you provided, the correct tag length is 8 bits. I’m sorry that the specification misguided you but our ability to keep things well-maintained is dependent on the availability of external contributors. If you could help us resolve any of the issues related to the existing implementations documented on GitHub, we would have more time to keep the specification up to date.

---

<div class="post-metadata">

**Author:** ![david.lenfesty](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/david.lenfesty/32/488_2.png) [@david.lenfesty](https://forum.opencyphal.org/u/david.lenfesty)\
**Post date:** [May 27, 2020, 5:49pm UTC](https://forum.opencyphal.org/t/pydsdl-nunavut-union-offsets-seem-to-be-incorrect/847/3 "2020-05-27T17:49:17Z")

</div>

Ah, makes sense. Thank you for clarifying.
