From 69d4650c490307bef08a4d6ec820f52a91b770f8 Mon Sep 17 00:00:00 2001 From: Luke Kenneth Casson Leighton Date: Fri, 19 Jun 2020 15:03:28 +0100 Subject: [PATCH] add trunc_div and trunch_rem to decoder helpers https://bugs.libre-soc.org/show_bug.cgi?id=324 --- src/soc/decoder/helpers.py | 27 +++++++++++++++++++++++++++ src/soc/decoder/isa/caller.py | 2 +- src/soc/decoder/power_pseudo.py | 5 ++++- src/soc/decoder/pseudo/pywriter.py | 3 ++- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/soc/decoder/helpers.py b/src/soc/decoder/helpers.py index e8fd6cb7..ef6610e5 100644 --- a/src/soc/decoder/helpers.py +++ b/src/soc/decoder/helpers.py @@ -1,11 +1,38 @@ import unittest from soc.decoder.selectable_int import SelectableInt +""" +Links: +* https://bugs.libre-soc.org/show_bug.cgi?id=324 - add trunc_div and trunc_rem +""" def exts(value, bits): sign = 1 << (bits - 1) return (value & (sign - 1)) - (value & sign) +def trunc_div(n, d): + f = getattr(n, "trunc_div", None) + if f is not None: + return f(d) + fr = getattr(d, "rtrunc_div", None) + if fr is not None: + return fr(n) + abs_n = abs(n) + abs_d = abs(d) + abs_q = n // d + if (n < 0) == (d < 0): + return abs_q + return -abs_q + +def trunc_rem(n, d): + f = getattr(n, "trunc_rem", None) + if f is not None: + return f(d) + fr = getattr(d, "rtrunc_rem", None) + if fr is not None: + return fr(n) + return n - d * trunc_div(n, d) + def EXTS(value): """ extends sign bit out from current MSB to all 256 bits diff --git a/src/soc/decoder/isa/caller.py b/src/soc/decoder/isa/caller.py index 79e8b13e..7bf411fd 100644 --- a/src/soc/decoder/isa/caller.py +++ b/src/soc/decoder/isa/caller.py @@ -10,7 +10,7 @@ from soc.decoder.orderedset import OrderedSet from soc.decoder.selectable_int import (FieldSelectableInt, SelectableInt, selectconcat) from soc.decoder.power_enums import spr_dict, XER_bits, insns, InternalOp -from soc.decoder.helpers import exts +from soc.decoder.helpers import exts, trunc_div, trunc_rem from collections import namedtuple import math import sys diff --git a/src/soc/decoder/power_pseudo.py b/src/soc/decoder/power_pseudo.py index e2b6440a..2e7b2422 100644 --- a/src/soc/decoder/power_pseudo.py +++ b/src/soc/decoder/power_pseudo.py @@ -255,12 +255,15 @@ def test(): print("args", args) print("-->", " ".join(map(str, args))) - from soc.decoder.helpers import (EXTS64, EXTZ64, ROTL64, ROTL32, MASK,) + from soc.decoder.helpers import (EXTS64, EXTZ64, ROTL64, ROTL32, MASK, + trunc_div, trunc_rem) d = {} d["print"] = print_ d["EXTS64"] = EXTS64 d["EXTZ64"] = EXTZ64 + d["trunc_div"] = trunc_div + d["trunc_rem"] = trunc_rem d["SelectableInt"] = SelectableInt d["concat"] = selectconcat d["GPR"] = gsc.gpr diff --git a/src/soc/decoder/pseudo/pywriter.py b/src/soc/decoder/pseudo/pywriter.py index 9acec8ce..bac8c459 100644 --- a/src/soc/decoder/pseudo/pywriter.py +++ b/src/soc/decoder/pseudo/pywriter.py @@ -20,7 +20,8 @@ header = """\ from soc.decoder.isa.caller import inject, instruction_info from soc.decoder.helpers import (EXTS, EXTS64, EXTZ64, ROTL64, ROTL32, MASK, - ne, eq, gt, ge, lt, le, length) + ne, eq, gt, ge, lt, le, length, + trunc_div, trunc_rem) from soc.decoder.selectable_int import SelectableInt from soc.decoder.selectable_int import selectconcat as concat from soc.decoder.orderedset import OrderedSet -- 2.30.2