[Dwarf-discuss] Clarification: DW_OP_mod doesn't specify which definition of modulo

Ben Woodard woodard@redhat.com
Wed Sep 24 18:32:23 GMT 2025


Background:

Evidently, originally DWARF didn't allow arithmetic operations on 
floating point numbers and most uses of the DWARF stack were done with 
the assumption that the values being acted upon were addresses and so 
the computation was assumed to be acting upon unsigned numbers.

At some point, DWARF began to allow the arithmetic operations to work on 
floating point numbers and several operations were explicitly defined to 
work over non-integral values. This led to the paragraph in the current 
DWARF working draft that says in section 2.5.2.4 on page 37 lines 24-27:

"Operations other than DW_OP_abs, DW_OP_div, DW_OP_minus,
DW_OP_mul, DW_OP_neg and DW_OP_plus require integral types of the
operand (either integral base type or the generic type). Operations do 
not cause
an exception on overflow."

Unlike all the other arithmetic operations this explicitly limits 
DW_OP_mod to integral base types and the generic type. It lumps 
DW_OP_mod in with the logical operations. Furthermore, there are 
multiple definitions of the modulo operator which vary in how they 
handle signed values.

According to the dwarf-discuss archives, this issue came up back in 2011 
and at that time Michael Eager made a pronouncement that DW_OP_mod used 
the modulo algorithm for unsigned arithmetic. However, this decision was 
not recorded in the standard. Since that time, consumers have 
implemented different implementations of DW_OP_mod.

This proposal seeks to clarify and harmonize the consumer 
implementations of the DW_OP_mod operator by defining which algorithm to 
use for signed arithmetic as well as define it for floating point numbers.

Proposal:

Add DW_OP_mod to the list of operators which do not require integral 
base types by changing:

Operations other than DW_OP_abs, DW_OP_div, DW_OP_minus, DW_OP_mul, 
DW_OP_neg and DW_OP_plus require integral types of the operand (either 
integral base type or the generic type).

To:

Operations other than DW_OP_abs, DW_OP_div, DW_OP_mod, DW_OP_minus, 
DW_OP_mul, DW_OP_neg and DW_OP_plus require integral types of the 
operand (either integral base type or the generic type).

Then append the following sentence to the description of the DW_OP_mod:

The algorithm used to implement modulo shall be the one defined in The 
Art of Computer Programming Volume 1: Fundamental Algorithms Chapter 2 
Section 4. Knuth.

Alternative proposals:

1) Explicitly state in the standard that DW_OP_mod is only defined for 
unsigned integral arithmetic. This effectively standardizes the Michael 
Eager's pronouncement from 2011.

2) Pick any algorithm for modulo that works for signed as well unsigned 
arithmetic and specify that DW_OP_modulo shall follow it. The current 
GDB implementation follows Knuth 1.2.4 for signed and unsigned integral 
arithmetic but excludes the algorithm for reals and floating point numbers.




More information about the Dwarf-discuss mailing list