Skip to content

Arith

arith

ArithFunctions dataclass

Bases: InterpreterFunctions

Source code in xdsl/interpreters/arith.py
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
@register_impls
class ArithFunctions(InterpreterFunctions):
    @impl(arith.ConstantOp)
    def run_constant(
        self, interpreter: Interpreter, op: arith.ConstantOp, args: PythonValues
    ) -> PythonValues:
        interpreter.interpreter_assert(
            isinstance(op.value, IntegerAttr | FloatAttr),
            f"arith.constant not implemented for {type(op.value)}",
        )
        value = cast(IntegerAttr | FloatAttr, op.value)
        if isa(value, IntegerAttr[builtin.IndexType]):
            return (
                scalar_attr(value.value.data, value.type, interpreter.index_bitwidth),
            )
        return (value,)

    @impl(arith.SubiOp)
    def run_subi(self, interpreter: Interpreter, op: arith.SubiOp, args: PythonValues):
        assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
        lhs = args[0].value.data
        rhs = args[1].value.data
        return (scalar_attr(lhs - rhs, op.result.type, interpreter.index_bitwidth),)

    @impl(arith.AddiOp)
    def run_addi(self, interpreter: Interpreter, op: arith.AddiOp, args: PythonValues):
        assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
        lhs = args[0].value.data
        rhs = args[1].value.data
        return (scalar_attr(lhs + rhs, op.result.type, interpreter.index_bitwidth),)

    @impl(arith.MuliOp)
    def run_muli(self, interpreter: Interpreter, op: arith.MuliOp, args: PythonValues):
        assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
        lhs = args[0].value.data
        rhs = args[1].value.data
        return (scalar_attr(lhs * rhs, op.result.type, interpreter.index_bitwidth),)

    @impl(arith.AndIOp)
    def run_andi(self, interpreter: Interpreter, op: arith.AndIOp, args: PythonValues):
        assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
        lhs = args[0].value.data
        rhs = args[1].value.data
        return (scalar_attr(lhs & rhs, op.result.type, interpreter.index_bitwidth),)

    @impl(arith.OrIOp)
    def run_ori(self, interpreter: Interpreter, op: arith.OrIOp, args: PythonValues):
        assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
        lhs = args[0].value.data
        rhs = args[1].value.data
        return (scalar_attr(lhs | rhs, op.result.type, interpreter.index_bitwidth),)

    @impl(arith.XOrIOp)
    def run_xori(self, interpreter: Interpreter, op: arith.XOrIOp, args: PythonValues):
        assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
        lhs = args[0].value.data
        rhs = args[1].value.data
        return (scalar_attr(lhs ^ rhs, op.result.type, interpreter.index_bitwidth),)

    @impl(arith.SubfOp)
    def run_subf(self, interpreter: Interpreter, op: arith.SubfOp, args: PythonValues):
        assert isinstance(op.result.type, builtin.AnyFloat)
        return (FloatAttr(args[0].value.data - args[1].value.data, op.result.type),)

    @impl(arith.AddfOp)
    def run_addf(self, interpreter: Interpreter, op: arith.AddfOp, args: PythonValues):
        assert isinstance(op.result.type, builtin.AnyFloat)
        return (FloatAttr(args[0].value.data + args[1].value.data, op.result.type),)

    @impl(arith.MulfOp)
    def run_mulf(self, interpreter: Interpreter, op: arith.MulfOp, args: PythonValues):
        assert isinstance(op.result.type, builtin.AnyFloat)
        return (FloatAttr(args[0].value.data * args[1].value.data, op.result.type),)

    @impl(arith.DivfOp)
    def run_divf(self, interpreter: Interpreter, op: arith.DivfOp, args: PythonValues):
        assert isinstance(op.result.type, builtin.AnyFloat)
        lhs: float = args[0].value.data
        rhs: float = args[1].value.data
        if rhs == 0.0:
            # IEEE 754: x / ±0 is ±inf (sign from both operands), 0 / 0 and nan / 0 are nan.
            # Python raises ZeroDivisionError instead.
            if lhs == 0.0 or isnan(lhs):
                return (FloatAttr(nan, op.result.type),)
            return (FloatAttr(copysign(inf, lhs) * copysign(1.0, rhs), op.result.type),)
        return (FloatAttr(lhs / rhs, op.result.type),)

    @impl(arith.NegfOp)
    def run_negf(self, interpreter: Interpreter, op: arith.NegfOp, args: PythonValues):
        assert isinstance(op.result.type, builtin.AnyFloat)
        return (FloatAttr(-args[0].value.data, op.result.type),)

    @impl(arith.MinimumfOp)
    def run_minimumf(
        self, interpreter: Interpreter, op: arith.MinimumfOp, args: PythonValues
    ):
        assert isinstance(op.result.type, builtin.AnyFloat)
        lhs, rhs = args[0].value.data, args[1].value.data
        if isnan(lhs) or isnan(rhs):
            return (FloatAttr(float("NaN"), op.result.type),)
        if lhs == 0 and rhs == 0:
            if copysign(1.0, lhs) < 0 or copysign(1.0, rhs) < 0:
                return (FloatAttr(-0.0, op.result.type),)
            else:
                return (FloatAttr(0.0, op.result.type),)
        return (args[0] if lhs <= rhs else args[1],)

    @impl(arith.MaximumfOp)
    def run_maximumf(
        self, interpreter: Interpreter, op: arith.MaximumfOp, args: PythonValues
    ):
        assert isinstance(op.result.type, builtin.AnyFloat)
        lhs, rhs = args[0].value.data, args[1].value.data
        if isnan(lhs) or isnan(rhs):
            return (FloatAttr(float("NaN"), op.result.type),)
        if lhs == 0 and rhs == 0:
            if copysign(1.0, lhs) > 0 or copysign(1.0, rhs) > 0:
                return (FloatAttr(0.0, op.result.type),)
            else:
                return (FloatAttr(-0.0, op.result.type),)
        return (args[0] if lhs >= rhs else args[1],)

    @impl(arith.MinnumfOp)
    def run_minnumf(
        self, interpreter: Interpreter, op: arith.MinnumfOp, args: PythonValues
    ):
        lhs, rhs = args[0].value.data, args[1].value.data
        if isnan(lhs):
            return (args[1],)
        if isnan(rhs):
            return (args[0],)
        return (args[0] if lhs <= rhs else args[1],)

    @impl(arith.MaxnumfOp)
    def run_maxnumf(
        self, interpreter: Interpreter, op: arith.MaxnumfOp, args: PythonValues
    ):
        lhs, rhs = args[0].value.data, args[1].value.data
        if isnan(lhs):
            return (args[1],)
        if isnan(rhs):
            return (args[0],)
        return (args[0] if lhs >= rhs else args[1],)

    @impl(arith.CmpiOp)
    def run_cmpi(self, interpreter: Interpreter, op: arith.CmpiOp, args: PythonValues):
        assert isa(op.lhs.type, builtin.IndexType | builtin.IntegerType)
        bitwidth = _int_bitwidth(interpreter, op.lhs.type)
        predicate = op.predicate.value.data
        interpret = to_unsigned if predicate >= 6 else to_signed
        lhs = interpret(args[0].value.data, bitwidth)
        rhs = interpret(args[1].value.data, bitwidth)
        match predicate:
            case 0:  # "eq"
                return (IntegerAttr.from_bool(lhs == rhs),)
            case 1:  # "ne"
                return (IntegerAttr.from_bool(lhs != rhs),)
            case 2:  # "slt"
                return (IntegerAttr.from_bool(lhs < rhs),)
            case 3:  # "sle"
                return (IntegerAttr.from_bool(lhs <= rhs),)
            case 4:  # "sgt"
                return (IntegerAttr.from_bool(lhs > rhs),)
            case 5:  # "sge"
                return (IntegerAttr.from_bool(lhs >= rhs),)
            case 6:  # "ult"
                return (IntegerAttr.from_bool(lhs < rhs),)
            case 7:  # "ule"
                return (IntegerAttr.from_bool(lhs <= rhs),)
            case 8:  # "ugt"
                return (IntegerAttr.from_bool(lhs > rhs),)
            case 9:  # "uge"
                return (IntegerAttr.from_bool(lhs >= rhs),)
            case _:
                raise InterpretationError(
                    f"arith.cmpi predicate {op.predicate} mot implemented yet."
                )

    @impl(arith.CmpfOp)
    def run_cmpf(self, interpreter: Interpreter, op: arith.CmpfOp, args: PythonValues):
        x = args[0].value.data
        y = args[1].value.data

        o = not isnan(x) and not isnan(y)
        u = isnan(x) or isnan(y)

        match op.predicate.value.data:
            case 0:
                return (IntegerAttr.from_bool(False),)
            case 1:
                return (IntegerAttr.from_bool((x == y) and o),)
            case 2:
                return (IntegerAttr.from_bool((x > y) and o),)
            case 3:
                return (IntegerAttr.from_bool((x >= y) and o),)
            case 4:
                return (IntegerAttr.from_bool((x < y) and o),)
            case 5:
                return (IntegerAttr.from_bool((x <= y) and o),)
            case 6:
                return (IntegerAttr.from_bool((x != y) and o),)
            case 7:
                return (IntegerAttr.from_bool(o),)
            case 8:
                return (IntegerAttr.from_bool((x == y) or u),)
            case 9:
                return (IntegerAttr.from_bool((x > y) or u),)
            case 10:
                return (IntegerAttr.from_bool((x >= y) or u),)
            case 11:
                return (IntegerAttr.from_bool((x < y) or u),)
            case 12:
                return (IntegerAttr.from_bool((x <= y) or u),)
            case 13:
                return (IntegerAttr.from_bool((x != y) or u),)
            case 14:
                return (IntegerAttr.from_bool(u),)
            case 15:
                return (IntegerAttr.from_bool(True),)
            case _:
                raise InterpretationError(
                    f"arith.cmpf predicate {op.predicate} mot implemented yet."
                )

    @impl(arith.ShLIOp)
    def run_shlsi(self, interpreter: Interpreter, op: arith.ShLIOp, args: PythonValues):
        assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
        bitwidth = _int_bitwidth(interpreter, op.result.type)
        lhs = to_signed(args[0].value.data, bitwidth)
        rhs = args[1].value.data
        assert rhs >= 0
        return (scalar_attr(lhs << rhs, op.result.type, interpreter.index_bitwidth),)

    @impl(arith.ShRSIOp)
    def run_shrsi(
        self, interpreter: Interpreter, op: arith.ShRSIOp, args: PythonValues
    ):
        assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
        lhs = to_signed(args[0].value.data, _int_bitwidth(interpreter, op.result.type))
        rhs = args[1].value.data
        assert rhs >= 0
        return (scalar_attr(lhs >> rhs, op.result.type, interpreter.index_bitwidth),)

    @impl(arith.DivSIOp)
    def run_divsi(
        self, interpreter: Interpreter, op: arith.DivSIOp, args: PythonValues
    ):
        assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
        bitwidth = _int_bitwidth(interpreter, op.result.type)
        lhs = to_signed(args[0].value.data, bitwidth)
        rhs = to_signed(args[1].value.data, bitwidth)
        assert rhs != 0
        div = abs(lhs) // abs(rhs)
        if (lhs > 0) != (rhs > 0):
            div = -div
        return (scalar_attr(div, op.result.type, interpreter.index_bitwidth),)

    @impl(arith.RemSIOp)
    def run_remsi(
        self, interpreter: Interpreter, op: arith.RemSIOp, args: PythonValues
    ):
        assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
        bitwidth = _int_bitwidth(interpreter, op.result.type)
        lhs = to_signed(args[0].value.data, bitwidth)
        rhs = to_signed(args[1].value.data, bitwidth)
        assert rhs != 0
        div = abs(lhs) // abs(rhs)
        if (lhs > 0) != (rhs > 0):
            div = -div
        return (
            scalar_attr(lhs - div * rhs, op.result.type, interpreter.index_bitwidth),
        )

    @impl(arith.ShRUIOp)
    def run_shrui(
        self, interpreter: Interpreter, op: arith.ShRUIOp, args: PythonValues
    ):
        assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
        bitwidth = _int_bitwidth(interpreter, op.result.type)
        lhs = to_unsigned(args[0].value.data, bitwidth)
        rhs = to_unsigned(args[1].value.data, bitwidth)
        return (scalar_attr(lhs >> rhs, op.result.type, interpreter.index_bitwidth),)

    @impl(arith.DivUIOp)
    def run_divui(
        self, interpreter: Interpreter, op: arith.DivUIOp, args: PythonValues
    ):
        assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
        bitwidth = _int_bitwidth(interpreter, op.result.type)
        lhs = to_unsigned(args[0].value.data, bitwidth)
        rhs = to_unsigned(args[1].value.data, bitwidth)
        assert rhs != 0
        return (scalar_attr(lhs // rhs, op.result.type, interpreter.index_bitwidth),)

    @impl(arith.RemUIOp)
    def run_remui(
        self, interpreter: Interpreter, op: arith.RemUIOp, args: PythonValues
    ):
        assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
        bitwidth = _int_bitwidth(interpreter, op.result.type)
        lhs = to_unsigned(args[0].value.data, bitwidth)
        rhs = to_unsigned(args[1].value.data, bitwidth)
        assert rhs != 0
        return (scalar_attr(lhs % rhs, op.result.type, interpreter.index_bitwidth),)

    @impl(arith.FloorDivSIOp)
    def run_floordivsi(
        self, interpreter: Interpreter, op: arith.FloorDivSIOp, args: PythonValues
    ):
        assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
        bitwidth = _int_bitwidth(interpreter, op.result.type)
        lhs = to_signed(args[0].value.data, bitwidth)
        rhs = to_signed(args[1].value.data, bitwidth)
        assert rhs != 0
        return (scalar_attr(lhs // rhs, op.result.type, interpreter.index_bitwidth),)

    @impl(arith.MinSIOp)
    def run_minsi(
        self, interpreter: Interpreter, op: arith.MinSIOp, args: PythonValues
    ):
        assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
        bitwidth = _int_bitwidth(interpreter, op.result.type)
        lhs = to_signed(args[0].value.data, bitwidth)
        rhs = to_signed(args[1].value.data, bitwidth)
        return (scalar_attr(min(lhs, rhs), op.result.type, interpreter.index_bitwidth),)

    @impl(arith.MaxSIOp)
    def run_maxsi(
        self, interpreter: Interpreter, op: arith.MaxSIOp, args: PythonValues
    ):
        assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
        bitwidth = _int_bitwidth(interpreter, op.result.type)
        lhs = to_signed(args[0].value.data, bitwidth)
        rhs = to_signed(args[1].value.data, bitwidth)
        return (scalar_attr(max(lhs, rhs), op.result.type, interpreter.index_bitwidth),)

    @impl(arith.MinUIOp)
    def run_minui(
        self, interpreter: Interpreter, op: arith.MinUIOp, args: PythonValues
    ):
        assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
        bitwidth = _int_bitwidth(interpreter, op.result.type)
        lhs = to_unsigned(args[0].value.data, bitwidth)
        rhs = to_unsigned(args[1].value.data, bitwidth)
        return (scalar_attr(min(lhs, rhs), op.result.type, interpreter.index_bitwidth),)

    @impl(arith.MaxUIOp)
    def run_maxui(
        self, interpreter: Interpreter, op: arith.MaxUIOp, args: PythonValues
    ):
        assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
        bitwidth = _int_bitwidth(interpreter, op.result.type)
        lhs = to_unsigned(args[0].value.data, bitwidth)
        rhs = to_unsigned(args[1].value.data, bitwidth)
        return (scalar_attr(max(lhs, rhs), op.result.type, interpreter.index_bitwidth),)

    @impl(arith.SelectOp)
    def run_select(
        self, interpreter: Interpreter, op: arith.SelectOp, args: PythonValues
    ):
        cond, lhs, rhs = args

        if not isinstance(cond, ShapedArray):
            # A scalar condition selects the whole value, shaped or not.
            return (lhs if cond else rhs,)

        # A shaped condition selects elementwise, all shapes must be identical.
        shaped_cond = cast(ShapedArray[int], cond)
        shaped_lhs = cast(ShapedArray[Any], lhs)
        shaped_rhs = cast(ShapedArray[Any], rhs)
        interpreter.interpreter_assert(
            shaped_cond.shape == shaped_lhs.shape == shaped_rhs.shape,
            f"arith.select shape mismatch: {shaped_cond.shape}, "
            f"{shaped_lhs.shape}, {shaped_rhs.shape}",
        )
        data = [
            l if c else r
            for c, l, r in zip(
                shaped_cond.data, shaped_lhs.data, shaped_rhs.data, strict=True
            )
        ]
        return (
            ShapedArray(
                TypedPtr[Any].new(data, xtype=shaped_lhs.element_type),
                list(shaped_lhs.shape),
            ),
        )

    @impl(arith.IndexCastOp)
    def run_indexcast(
        self, interpreter: Interpreter, op: arith.IndexCastOp, args: PythonValues
    ):
        assert len(args) == 1
        result = args[0].value.data

        assert isa(op.input.type, builtin.IndexType | builtin.IntegerType)

        input_bitwidth = _int_bitwidth(interpreter, op.input.type)
        result_bitwidth = _int_bitwidth(interpreter, op.result.type)

        if input_bitwidth > result_bitwidth:
            result = _truncate(result, result_bitwidth)
        elif input_bitwidth < result_bitwidth:
            result = _sign_extend(result, input_bitwidth)
        else:
            result = to_signed(result, result_bitwidth)

        return (scalar_attr(result, op.result.type, interpreter.index_bitwidth),)

    @impl(arith.ExtSIOp)
    def run_extsi(
        self, interpreter: Interpreter, op: arith.ExtSIOp, args: PythonValues
    ):
        assert isa(op.input.type, builtin.IntegerType)
        value = to_signed(args[0].value.data, op.input.type.width.data)
        return (scalar_attr(value, op.result.type, interpreter.index_bitwidth),)

    @impl(arith.ExtUIOp)
    def run_extui(
        self, interpreter: Interpreter, op: arith.ExtUIOp, args: PythonValues
    ):
        assert isa(op.input.type, builtin.IntegerType)
        value = to_unsigned(args[0].value.data, op.input.type.width.data)
        return (scalar_attr(value, op.result.type, interpreter.index_bitwidth),)

    @impl(arith.TruncIOp)
    def run_trunci(
        self, interpreter: Interpreter, op: arith.TruncIOp, args: PythonValues
    ):
        value = args[0].value.data
        return (scalar_attr(value, op.result.type, interpreter.index_bitwidth),)

run_constant(interpreter: Interpreter, op: arith.ConstantOp, args: PythonValues) -> PythonValues

Source code in xdsl/interpreters/arith.py
@impl(arith.ConstantOp)
def run_constant(
    self, interpreter: Interpreter, op: arith.ConstantOp, args: PythonValues
) -> PythonValues:
    interpreter.interpreter_assert(
        isinstance(op.value, IntegerAttr | FloatAttr),
        f"arith.constant not implemented for {type(op.value)}",
    )
    value = cast(IntegerAttr | FloatAttr, op.value)
    if isa(value, IntegerAttr[builtin.IndexType]):
        return (
            scalar_attr(value.value.data, value.type, interpreter.index_bitwidth),
        )
    return (value,)

run_subi(interpreter: Interpreter, op: arith.SubiOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.SubiOp)
def run_subi(self, interpreter: Interpreter, op: arith.SubiOp, args: PythonValues):
    assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
    lhs = args[0].value.data
    rhs = args[1].value.data
    return (scalar_attr(lhs - rhs, op.result.type, interpreter.index_bitwidth),)

run_addi(interpreter: Interpreter, op: arith.AddiOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.AddiOp)
def run_addi(self, interpreter: Interpreter, op: arith.AddiOp, args: PythonValues):
    assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
    lhs = args[0].value.data
    rhs = args[1].value.data
    return (scalar_attr(lhs + rhs, op.result.type, interpreter.index_bitwidth),)

run_muli(interpreter: Interpreter, op: arith.MuliOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.MuliOp)
def run_muli(self, interpreter: Interpreter, op: arith.MuliOp, args: PythonValues):
    assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
    lhs = args[0].value.data
    rhs = args[1].value.data
    return (scalar_attr(lhs * rhs, op.result.type, interpreter.index_bitwidth),)

run_andi(interpreter: Interpreter, op: arith.AndIOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.AndIOp)
def run_andi(self, interpreter: Interpreter, op: arith.AndIOp, args: PythonValues):
    assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
    lhs = args[0].value.data
    rhs = args[1].value.data
    return (scalar_attr(lhs & rhs, op.result.type, interpreter.index_bitwidth),)

run_ori(interpreter: Interpreter, op: arith.OrIOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.OrIOp)
def run_ori(self, interpreter: Interpreter, op: arith.OrIOp, args: PythonValues):
    assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
    lhs = args[0].value.data
    rhs = args[1].value.data
    return (scalar_attr(lhs | rhs, op.result.type, interpreter.index_bitwidth),)

run_xori(interpreter: Interpreter, op: arith.XOrIOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.XOrIOp)
def run_xori(self, interpreter: Interpreter, op: arith.XOrIOp, args: PythonValues):
    assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
    lhs = args[0].value.data
    rhs = args[1].value.data
    return (scalar_attr(lhs ^ rhs, op.result.type, interpreter.index_bitwidth),)

run_subf(interpreter: Interpreter, op: arith.SubfOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.SubfOp)
def run_subf(self, interpreter: Interpreter, op: arith.SubfOp, args: PythonValues):
    assert isinstance(op.result.type, builtin.AnyFloat)
    return (FloatAttr(args[0].value.data - args[1].value.data, op.result.type),)

run_addf(interpreter: Interpreter, op: arith.AddfOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.AddfOp)
def run_addf(self, interpreter: Interpreter, op: arith.AddfOp, args: PythonValues):
    assert isinstance(op.result.type, builtin.AnyFloat)
    return (FloatAttr(args[0].value.data + args[1].value.data, op.result.type),)

run_mulf(interpreter: Interpreter, op: arith.MulfOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.MulfOp)
def run_mulf(self, interpreter: Interpreter, op: arith.MulfOp, args: PythonValues):
    assert isinstance(op.result.type, builtin.AnyFloat)
    return (FloatAttr(args[0].value.data * args[1].value.data, op.result.type),)

run_divf(interpreter: Interpreter, op: arith.DivfOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.DivfOp)
def run_divf(self, interpreter: Interpreter, op: arith.DivfOp, args: PythonValues):
    assert isinstance(op.result.type, builtin.AnyFloat)
    lhs: float = args[0].value.data
    rhs: float = args[1].value.data
    if rhs == 0.0:
        # IEEE 754: x / ±0 is ±inf (sign from both operands), 0 / 0 and nan / 0 are nan.
        # Python raises ZeroDivisionError instead.
        if lhs == 0.0 or isnan(lhs):
            return (FloatAttr(nan, op.result.type),)
        return (FloatAttr(copysign(inf, lhs) * copysign(1.0, rhs), op.result.type),)
    return (FloatAttr(lhs / rhs, op.result.type),)

run_negf(interpreter: Interpreter, op: arith.NegfOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.NegfOp)
def run_negf(self, interpreter: Interpreter, op: arith.NegfOp, args: PythonValues):
    assert isinstance(op.result.type, builtin.AnyFloat)
    return (FloatAttr(-args[0].value.data, op.result.type),)

run_minimumf(interpreter: Interpreter, op: arith.MinimumfOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.MinimumfOp)
def run_minimumf(
    self, interpreter: Interpreter, op: arith.MinimumfOp, args: PythonValues
):
    assert isinstance(op.result.type, builtin.AnyFloat)
    lhs, rhs = args[0].value.data, args[1].value.data
    if isnan(lhs) or isnan(rhs):
        return (FloatAttr(float("NaN"), op.result.type),)
    if lhs == 0 and rhs == 0:
        if copysign(1.0, lhs) < 0 or copysign(1.0, rhs) < 0:
            return (FloatAttr(-0.0, op.result.type),)
        else:
            return (FloatAttr(0.0, op.result.type),)
    return (args[0] if lhs <= rhs else args[1],)

run_maximumf(interpreter: Interpreter, op: arith.MaximumfOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.MaximumfOp)
def run_maximumf(
    self, interpreter: Interpreter, op: arith.MaximumfOp, args: PythonValues
):
    assert isinstance(op.result.type, builtin.AnyFloat)
    lhs, rhs = args[0].value.data, args[1].value.data
    if isnan(lhs) or isnan(rhs):
        return (FloatAttr(float("NaN"), op.result.type),)
    if lhs == 0 and rhs == 0:
        if copysign(1.0, lhs) > 0 or copysign(1.0, rhs) > 0:
            return (FloatAttr(0.0, op.result.type),)
        else:
            return (FloatAttr(-0.0, op.result.type),)
    return (args[0] if lhs >= rhs else args[1],)

run_minnumf(interpreter: Interpreter, op: arith.MinnumfOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.MinnumfOp)
def run_minnumf(
    self, interpreter: Interpreter, op: arith.MinnumfOp, args: PythonValues
):
    lhs, rhs = args[0].value.data, args[1].value.data
    if isnan(lhs):
        return (args[1],)
    if isnan(rhs):
        return (args[0],)
    return (args[0] if lhs <= rhs else args[1],)

run_maxnumf(interpreter: Interpreter, op: arith.MaxnumfOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.MaxnumfOp)
def run_maxnumf(
    self, interpreter: Interpreter, op: arith.MaxnumfOp, args: PythonValues
):
    lhs, rhs = args[0].value.data, args[1].value.data
    if isnan(lhs):
        return (args[1],)
    if isnan(rhs):
        return (args[0],)
    return (args[0] if lhs >= rhs else args[1],)

run_cmpi(interpreter: Interpreter, op: arith.CmpiOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.CmpiOp)
def run_cmpi(self, interpreter: Interpreter, op: arith.CmpiOp, args: PythonValues):
    assert isa(op.lhs.type, builtin.IndexType | builtin.IntegerType)
    bitwidth = _int_bitwidth(interpreter, op.lhs.type)
    predicate = op.predicate.value.data
    interpret = to_unsigned if predicate >= 6 else to_signed
    lhs = interpret(args[0].value.data, bitwidth)
    rhs = interpret(args[1].value.data, bitwidth)
    match predicate:
        case 0:  # "eq"
            return (IntegerAttr.from_bool(lhs == rhs),)
        case 1:  # "ne"
            return (IntegerAttr.from_bool(lhs != rhs),)
        case 2:  # "slt"
            return (IntegerAttr.from_bool(lhs < rhs),)
        case 3:  # "sle"
            return (IntegerAttr.from_bool(lhs <= rhs),)
        case 4:  # "sgt"
            return (IntegerAttr.from_bool(lhs > rhs),)
        case 5:  # "sge"
            return (IntegerAttr.from_bool(lhs >= rhs),)
        case 6:  # "ult"
            return (IntegerAttr.from_bool(lhs < rhs),)
        case 7:  # "ule"
            return (IntegerAttr.from_bool(lhs <= rhs),)
        case 8:  # "ugt"
            return (IntegerAttr.from_bool(lhs > rhs),)
        case 9:  # "uge"
            return (IntegerAttr.from_bool(lhs >= rhs),)
        case _:
            raise InterpretationError(
                f"arith.cmpi predicate {op.predicate} mot implemented yet."
            )

run_cmpf(interpreter: Interpreter, op: arith.CmpfOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.CmpfOp)
def run_cmpf(self, interpreter: Interpreter, op: arith.CmpfOp, args: PythonValues):
    x = args[0].value.data
    y = args[1].value.data

    o = not isnan(x) and not isnan(y)
    u = isnan(x) or isnan(y)

    match op.predicate.value.data:
        case 0:
            return (IntegerAttr.from_bool(False),)
        case 1:
            return (IntegerAttr.from_bool((x == y) and o),)
        case 2:
            return (IntegerAttr.from_bool((x > y) and o),)
        case 3:
            return (IntegerAttr.from_bool((x >= y) and o),)
        case 4:
            return (IntegerAttr.from_bool((x < y) and o),)
        case 5:
            return (IntegerAttr.from_bool((x <= y) and o),)
        case 6:
            return (IntegerAttr.from_bool((x != y) and o),)
        case 7:
            return (IntegerAttr.from_bool(o),)
        case 8:
            return (IntegerAttr.from_bool((x == y) or u),)
        case 9:
            return (IntegerAttr.from_bool((x > y) or u),)
        case 10:
            return (IntegerAttr.from_bool((x >= y) or u),)
        case 11:
            return (IntegerAttr.from_bool((x < y) or u),)
        case 12:
            return (IntegerAttr.from_bool((x <= y) or u),)
        case 13:
            return (IntegerAttr.from_bool((x != y) or u),)
        case 14:
            return (IntegerAttr.from_bool(u),)
        case 15:
            return (IntegerAttr.from_bool(True),)
        case _:
            raise InterpretationError(
                f"arith.cmpf predicate {op.predicate} mot implemented yet."
            )

run_shlsi(interpreter: Interpreter, op: arith.ShLIOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.ShLIOp)
def run_shlsi(self, interpreter: Interpreter, op: arith.ShLIOp, args: PythonValues):
    assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
    bitwidth = _int_bitwidth(interpreter, op.result.type)
    lhs = to_signed(args[0].value.data, bitwidth)
    rhs = args[1].value.data
    assert rhs >= 0
    return (scalar_attr(lhs << rhs, op.result.type, interpreter.index_bitwidth),)

run_shrsi(interpreter: Interpreter, op: arith.ShRSIOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.ShRSIOp)
def run_shrsi(
    self, interpreter: Interpreter, op: arith.ShRSIOp, args: PythonValues
):
    assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
    lhs = to_signed(args[0].value.data, _int_bitwidth(interpreter, op.result.type))
    rhs = args[1].value.data
    assert rhs >= 0
    return (scalar_attr(lhs >> rhs, op.result.type, interpreter.index_bitwidth),)

run_divsi(interpreter: Interpreter, op: arith.DivSIOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.DivSIOp)
def run_divsi(
    self, interpreter: Interpreter, op: arith.DivSIOp, args: PythonValues
):
    assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
    bitwidth = _int_bitwidth(interpreter, op.result.type)
    lhs = to_signed(args[0].value.data, bitwidth)
    rhs = to_signed(args[1].value.data, bitwidth)
    assert rhs != 0
    div = abs(lhs) // abs(rhs)
    if (lhs > 0) != (rhs > 0):
        div = -div
    return (scalar_attr(div, op.result.type, interpreter.index_bitwidth),)

run_remsi(interpreter: Interpreter, op: arith.RemSIOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.RemSIOp)
def run_remsi(
    self, interpreter: Interpreter, op: arith.RemSIOp, args: PythonValues
):
    assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
    bitwidth = _int_bitwidth(interpreter, op.result.type)
    lhs = to_signed(args[0].value.data, bitwidth)
    rhs = to_signed(args[1].value.data, bitwidth)
    assert rhs != 0
    div = abs(lhs) // abs(rhs)
    if (lhs > 0) != (rhs > 0):
        div = -div
    return (
        scalar_attr(lhs - div * rhs, op.result.type, interpreter.index_bitwidth),
    )

run_shrui(interpreter: Interpreter, op: arith.ShRUIOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.ShRUIOp)
def run_shrui(
    self, interpreter: Interpreter, op: arith.ShRUIOp, args: PythonValues
):
    assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
    bitwidth = _int_bitwidth(interpreter, op.result.type)
    lhs = to_unsigned(args[0].value.data, bitwidth)
    rhs = to_unsigned(args[1].value.data, bitwidth)
    return (scalar_attr(lhs >> rhs, op.result.type, interpreter.index_bitwidth),)

run_divui(interpreter: Interpreter, op: arith.DivUIOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.DivUIOp)
def run_divui(
    self, interpreter: Interpreter, op: arith.DivUIOp, args: PythonValues
):
    assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
    bitwidth = _int_bitwidth(interpreter, op.result.type)
    lhs = to_unsigned(args[0].value.data, bitwidth)
    rhs = to_unsigned(args[1].value.data, bitwidth)
    assert rhs != 0
    return (scalar_attr(lhs // rhs, op.result.type, interpreter.index_bitwidth),)

run_remui(interpreter: Interpreter, op: arith.RemUIOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.RemUIOp)
def run_remui(
    self, interpreter: Interpreter, op: arith.RemUIOp, args: PythonValues
):
    assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
    bitwidth = _int_bitwidth(interpreter, op.result.type)
    lhs = to_unsigned(args[0].value.data, bitwidth)
    rhs = to_unsigned(args[1].value.data, bitwidth)
    assert rhs != 0
    return (scalar_attr(lhs % rhs, op.result.type, interpreter.index_bitwidth),)

run_floordivsi(interpreter: Interpreter, op: arith.FloorDivSIOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.FloorDivSIOp)
def run_floordivsi(
    self, interpreter: Interpreter, op: arith.FloorDivSIOp, args: PythonValues
):
    assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
    bitwidth = _int_bitwidth(interpreter, op.result.type)
    lhs = to_signed(args[0].value.data, bitwidth)
    rhs = to_signed(args[1].value.data, bitwidth)
    assert rhs != 0
    return (scalar_attr(lhs // rhs, op.result.type, interpreter.index_bitwidth),)

run_minsi(interpreter: Interpreter, op: arith.MinSIOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.MinSIOp)
def run_minsi(
    self, interpreter: Interpreter, op: arith.MinSIOp, args: PythonValues
):
    assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
    bitwidth = _int_bitwidth(interpreter, op.result.type)
    lhs = to_signed(args[0].value.data, bitwidth)
    rhs = to_signed(args[1].value.data, bitwidth)
    return (scalar_attr(min(lhs, rhs), op.result.type, interpreter.index_bitwidth),)

run_maxsi(interpreter: Interpreter, op: arith.MaxSIOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.MaxSIOp)
def run_maxsi(
    self, interpreter: Interpreter, op: arith.MaxSIOp, args: PythonValues
):
    assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
    bitwidth = _int_bitwidth(interpreter, op.result.type)
    lhs = to_signed(args[0].value.data, bitwidth)
    rhs = to_signed(args[1].value.data, bitwidth)
    return (scalar_attr(max(lhs, rhs), op.result.type, interpreter.index_bitwidth),)

run_minui(interpreter: Interpreter, op: arith.MinUIOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.MinUIOp)
def run_minui(
    self, interpreter: Interpreter, op: arith.MinUIOp, args: PythonValues
):
    assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
    bitwidth = _int_bitwidth(interpreter, op.result.type)
    lhs = to_unsigned(args[0].value.data, bitwidth)
    rhs = to_unsigned(args[1].value.data, bitwidth)
    return (scalar_attr(min(lhs, rhs), op.result.type, interpreter.index_bitwidth),)

run_maxui(interpreter: Interpreter, op: arith.MaxUIOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.MaxUIOp)
def run_maxui(
    self, interpreter: Interpreter, op: arith.MaxUIOp, args: PythonValues
):
    assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
    bitwidth = _int_bitwidth(interpreter, op.result.type)
    lhs = to_unsigned(args[0].value.data, bitwidth)
    rhs = to_unsigned(args[1].value.data, bitwidth)
    return (scalar_attr(max(lhs, rhs), op.result.type, interpreter.index_bitwidth),)

run_select(interpreter: Interpreter, op: arith.SelectOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.SelectOp)
def run_select(
    self, interpreter: Interpreter, op: arith.SelectOp, args: PythonValues
):
    cond, lhs, rhs = args

    if not isinstance(cond, ShapedArray):
        # A scalar condition selects the whole value, shaped or not.
        return (lhs if cond else rhs,)

    # A shaped condition selects elementwise, all shapes must be identical.
    shaped_cond = cast(ShapedArray[int], cond)
    shaped_lhs = cast(ShapedArray[Any], lhs)
    shaped_rhs = cast(ShapedArray[Any], rhs)
    interpreter.interpreter_assert(
        shaped_cond.shape == shaped_lhs.shape == shaped_rhs.shape,
        f"arith.select shape mismatch: {shaped_cond.shape}, "
        f"{shaped_lhs.shape}, {shaped_rhs.shape}",
    )
    data = [
        l if c else r
        for c, l, r in zip(
            shaped_cond.data, shaped_lhs.data, shaped_rhs.data, strict=True
        )
    ]
    return (
        ShapedArray(
            TypedPtr[Any].new(data, xtype=shaped_lhs.element_type),
            list(shaped_lhs.shape),
        ),
    )

run_indexcast(interpreter: Interpreter, op: arith.IndexCastOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.IndexCastOp)
def run_indexcast(
    self, interpreter: Interpreter, op: arith.IndexCastOp, args: PythonValues
):
    assert len(args) == 1
    result = args[0].value.data

    assert isa(op.input.type, builtin.IndexType | builtin.IntegerType)

    input_bitwidth = _int_bitwidth(interpreter, op.input.type)
    result_bitwidth = _int_bitwidth(interpreter, op.result.type)

    if input_bitwidth > result_bitwidth:
        result = _truncate(result, result_bitwidth)
    elif input_bitwidth < result_bitwidth:
        result = _sign_extend(result, input_bitwidth)
    else:
        result = to_signed(result, result_bitwidth)

    return (scalar_attr(result, op.result.type, interpreter.index_bitwidth),)

run_extsi(interpreter: Interpreter, op: arith.ExtSIOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.ExtSIOp)
def run_extsi(
    self, interpreter: Interpreter, op: arith.ExtSIOp, args: PythonValues
):
    assert isa(op.input.type, builtin.IntegerType)
    value = to_signed(args[0].value.data, op.input.type.width.data)
    return (scalar_attr(value, op.result.type, interpreter.index_bitwidth),)

run_extui(interpreter: Interpreter, op: arith.ExtUIOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.ExtUIOp)
def run_extui(
    self, interpreter: Interpreter, op: arith.ExtUIOp, args: PythonValues
):
    assert isa(op.input.type, builtin.IntegerType)
    value = to_unsigned(args[0].value.data, op.input.type.width.data)
    return (scalar_attr(value, op.result.type, interpreter.index_bitwidth),)

run_trunci(interpreter: Interpreter, op: arith.TruncIOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
@impl(arith.TruncIOp)
def run_trunci(
    self, interpreter: Interpreter, op: arith.TruncIOp, args: PythonValues
):
    value = args[0].value.data
    return (scalar_attr(value, op.result.type, interpreter.index_bitwidth),)