[Dwarf-discuss] Clarification: DW_OP_mod doesn't specify which definition of modulo
Mark Wielaard
mark@klomp.org
Mon Nov 17 18:46:14 GMT 2025
Hi Fangrui,
On Mon, 2025-11-10 at 10:03 -0800, Fangrui Song wrote:
> On Mon, Nov 10, 2025 at 9:16 AM Ben Woodard via Dwarf-discuss
> <dwarf-discuss@lists.dwarfstd.org> wrote:
> > > For GDB there might be some assumption it
> > > would be modulo using floored division.
> > Yes it breaks GDB's current implementation but other consumers have
> > interpreted it as truncated division. If you add DW_OP_modulo, GDB's
> > current implementation could be used for that and DW_OP_mod could be
> > implemented with % like it is in other consumers.
>
> While gdb's comment mentions Knuth 1.2.4, the implementation actually
> uses truncating remainder when the divisor is not 0
> `v = v1 / v2; v = v1 - (v2 * v);` (v1/v2 computes the truncating
> quotient with mpz_tdiv_q)
>
> https://sourceware.org/cgit/binutils-gdb/tree/gdb/valarith.c#n1277
O! Thanks for that. So besides using truncating division it also works
when the divisor is zero. But DW_OP_div raises an error on division by
zero.
Does it make sense to explicitly define what it means when the divisor
is zero? Or is it fine to make the expression "undefined" or error out
in that case?
Cheers,
Mark
More information about the Dwarf-discuss
mailing list