blob: f9b154866a146c0449a1f8976b176ee4f4fbd84e [file] [edit]
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Test data for the `//` (flooring integer division) and `%` (flooring modulus)
# operators.
[$default byte_order: "LittleEndian"]
[(cpp) namespace: "emboss::test"]
struct ArrayOfUint16BySizeInBytes:
0 [+1] UInt size_in_bytes (n)
# (n // 2) * 2 rounds an odd byte count down to the nearest whole element.
1 [+(n // 2) * 2] UInt:16[] elements
struct ChunkedPayload:
0 [+1] UInt chunk_count (n)
# ((n + 3) // 4) * 4 rounds n up to a multiple of 4.
1 [+((n + 3) // 4) * 4] UInt:8[] padded
# Virtual fields exercise constant folding through `//` and `%`, including the
# flooring sign table from the design doc.
struct Constants:
let exact_quotient = 100 // 5
let truncating_quotient = 100 // 7
let negative_quotient = (0 - 7) // 2
let exact_modulus = 100 % 5
let truncating_modulus = 100 % 7
let negative_modulus = (0 - 7) % 2
let chained_div = 100 // 5 // 2
let chained_mod = 17 % 7 % 4
let mixed_divmod = 17 % 7 // 2
struct SignedRuntimeDivision:
# Runtime (not constant-folded) `//` and `%` with signed dividends and both
# constant and non-constant divisors, exercising the C++ runtime's flooring
# adjustment, including the most negative dividend of each width.
0 [+8] Int wide_dividend
8 [+4] Int narrow_dividend
12 [+1] UInt divisor_magnitude (d)
let wide_quotient_by_constant = wide_dividend // 4
let wide_remainder_by_constant = wide_dividend % 4
# `$max(d, 1)` keeps the divisor's range clear of zero.
let wide_quotient_by_divisor = wide_dividend // $max(d, 1)
let wide_remainder_by_negative_divisor = wide_dividend % (0 - $max(d, 1))
let narrow_quotient_by_negative_divisor = narrow_dividend // (0 - $max(d, 1))
let narrow_remainder_by_negative_divisor = narrow_dividend % (0 - $max(d, 1))
# A field whose size depends on a runtime `//` with a *possibly*-zero divisor.
# When divisor == 0 the payload size -- and therefore the whole structure's
# size -- is undefined, so Ok() is false. A nonzero divisor works normally.
struct PayloadSizedByDivision:
0 [+1] UInt divisor
1 [+16 // divisor] UInt:8[] payload
# A field guarded by an existence condition that involves a possibly-zero
# divisor. This exercises the interaction with the Ok()/switch discriminant
# optimization: when divisor == 0 the condition `12 // divisor == 3` is
# undefined, so `gated`'s presence is unknown and Ok() must be false.
struct FieldGatedByDivision:
0 [+1] UInt divisor
if 12 // divisor == 3:
1 [+1] UInt gated
# A virtual field whose `//` result *collapses* to a single defined value ({0})
# yet can be undefined when the divisor is zero. Without special handling,
# codegen would fold it to a bogus literal 0; instead the accessor must return
# an Unknown result when divisor == 0.
struct CollapsingQuotient:
0 [+1] UInt divisor
let zero_or_undefined = 0 // divisor