From 1448210f282437a0d08110ad8d961cd0de46e434 Mon Sep 17 00:00:00 2001 From: Martin Whitaker Date: Sat, 9 Jul 2016 23:33:33 +0100 Subject: [PATCH] Fix for GitHub issue 112 - index calculation for >2D packed arrays. --- netmisc.cc | 33 ++++++++++++++++++--------------- nettypes.cc | 5 ++--- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/netmisc.cc b/netmisc.cc index 8c6e240c5..78ef08c85 100644 --- a/netmisc.cc +++ b/netmisc.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2001-2014 Stephen Williams (steve@icarus.com) + * Copyright (c) 2001-2016 Stephen Williams (steve@icarus.com) * * This source code is free software; you can redistribute it * and/or modify it in source code form under the terms of the GNU @@ -457,22 +457,25 @@ NetExpr *normalize_variable_slice_base(const list&indices, NetExpr*base, long loff; reg->sb_to_slice(indices, sb, loff, lwid); - bool idx_incr = pcur->get_msb() < pcur->get_lsb(); + /* Calculate the space needed for the offset. */ + unsigned min_wid = num_bits(-loff); + /* We need enough space for the larger of the offset or the + * base expression. */ + if (min_wid < base->expr_width()) min_wid = base->expr_width(); + /* Pad the base expression to the correct width. */ + base = pad_to_width(base, min_wid, *base); - if(pcur->get_lsb() != 0) { - // Adjust the base for the case when the array range does not start from 0 - if(idx_incr) - base = make_sub_expr(pcur->get_lsb(), base); - else - base = make_sub_expr(base, pcur->get_lsb()); + if (pcur->get_msb() >= pcur->get_lsb()) { + if (pcur->get_lsb() != 0) + base = make_sub_expr(base, pcur->get_lsb()); + base = make_mult_expr(base, lwid); + base = make_add_expr(base, loff); + } else { + if (pcur->get_msb() != 0) + base = make_sub_expr(base, pcur->get_msb()); + base = make_mult_expr(base, lwid); + base = make_sub_expr(loff, base); } - - base = make_mult_expr(base, lwid); - - // TODO I do not see any influence of the lines below to the test suite - if(!idx_incr) - base = make_add_expr(base, loff); - return base; } diff --git a/nettypes.cc b/nettypes.cc index 2f2e3988f..807956b4b 100644 --- a/nettypes.cc +++ b/nettypes.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012-2013 Stephen Williams (steve@icarus.com) + * Copyright (c) 2012-2016 Stephen Williams (steve@icarus.com) * * This source code is free software; you can redistribute it * and/or modify it in source code form under the terms of the GNU @@ -147,13 +147,12 @@ bool prefix_to_slice(const std::vector&dims, do { -- icur; acc_wid *= pcur->width(); + -- pcur; if (pcur->get_msb() >= pcur->get_lsb()) acc_off += (*icur - pcur->get_lsb()) * acc_wid; else acc_off += (pcur->get_lsb() - *icur) * acc_wid; - -- pcur; - } while (icur != prefix.begin()); // Got our final offset.