Skip to content

Add integer division and modulus operators to expression sublanguage#106

Draft
Copilot wants to merge 4 commits intomasterfrom
copilot/support-integer-division-modulus
Draft

Add integer division and modulus operators to expression sublanguage#106
Copilot wants to merge 4 commits intomasterfrom
copilot/support-integer-division-modulus

Conversation

Copy link

Copilot AI commented Dec 6, 2025

Adds / (floored division) and % (floored modulus) operators to the Emboss expression language, enabling expressions like size / 4 and offset % alignment.

Semantics

Uses floored division (Python's // semantics), not truncated division (C's /). This ensures the identity a == (a / b) * b + (a % b) holds for all integers:

10 / 3 == 3      -10 / 3 == -4
10 % 3 == 1      -10 % 3 == 2

Changes

  • Tokenizer/Parser: Added / and % tokens and grammar productions at same precedence as *
  • IR: Added DIVISION and MODULUS to FunctionMapping enum
  • Type checking: Integer arguments required, integer result
  • Bounds computation: Computes conservative bounds excluding zero from divisor range; constant expressions evaluated exactly
  • C++ runtime: FlooredDivisionOperation and FlooredModulusOperation with div-by-zero guards
  • Code generation: Maps to FlooredDivision and FlooredModulus support functions

Example

struct Packet:
  0 [+1]  UInt  total_size
  1 [+1]  UInt  alignment
    [requires: 1 <= this]
  
  let aligned_size = (total_size + alignment - 1) / alignment * alignment
  let padding = aligned_size % alignment
Original prompt

This section details on the original issue you should resolve

<issue_title>Support Integer Division and Modulus in the Emboss Expression Sublanguage</issue_title>
<issue_description>This issue was copied from the upstream repository google/emboss.

Original issue: google#41
Original state: open, created at: 2022-04-19T19:40:27Z, updated at: 2024-12-18T23:14:42Z, by @reventlov


Original description

Emboss should support integer division and modulus.

Note that there are several subtleties to these operations. reventlov to provide a proper design doc.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits December 6, 2025 01:33
…on language

Co-authored-by: AaronWebster <3766083+AaronWebster@users.noreply.github.com>
…n/modulus

Co-authored-by: AaronWebster <3766083+AaronWebster@users.noreply.github.com>
…ng in modulus bounds

Co-authored-by: AaronWebster <3766083+AaronWebster@users.noreply.github.com>
Copilot AI changed the title [WIP] Add support for integer division and modulus in emboss Add integer division and modulus operators to expression sublanguage Dec 6, 2025
Copilot AI requested a review from AaronWebster December 6, 2025 01:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support Integer Division and Modulus in the Emboss Expression Sublanguage

2 participants