On Wed, Mar 24, 2021 at 12:11:50PM +0100, BALATON Zoltan wrote: > On Wed, 24 Mar 2021, David Gibson wrote: > > On Tue, Mar 23, 2021 at 02:31:07PM +0100, BALATON Zoltan wrote: > > > On Tue, 23 Mar 2021, David Gibson wrote: > > > > On Wed, Mar 17, 2021 at 02:17:51AM +0100, BALATON Zoltan wrote: > > [snip] > > > > > +static void setup_mem_windows(MV64361State *s, uint32_t val) > > > > > +{ > > > > > + MV64361PCIState *p; > > > > > + MemoryRegion *mr; > > > > > + uint32_t mask; > > > > > + int i; > > > > > + > > > > > + val &= 0x1fffff; > > > > > + for (mask = 1, i = 0; i < 21; i++, mask <<= 1) { > > > > > > > > Having a loop, where nearly the entire body is a switch over the loop > > > > variable seems a rather odd choice to me, compared to just unrolling > > > > the whole thing. Or alternatively, maybe more can be be factored out > > > > of the switch into common body code. > > > > > > The loop is really over the bits in val that say which memory regions to > > > > I see that, but it doesn't change the point. > > > > > enable so depending on this we need to touch different mem regions. For > > > selecting those memory regions and what to do with them a switch seems to be > > > obvious choice. I probably can't factor out anything as these lines in > > > switch cases are similar but all differ in some little details (like which > > > PCI bus, name of the region, etc.). Unrolling could be possible but would > > > just add lines between cases that's now in the loop head so I really don't > > > > I see only 2 common lines, which basically balances about the case and > > break lines in every switchcase. > > Not sure what you mean by that. To me that means that these cannot be > factored out as they are in the middle so can't put them neither before nor > after the switch (without adding more local variables that would just make > the result longer than it is now). I'm saying that I don't think unrolling would actually make things longer, because the small amount of duplication would balanced by removing the switch boilerplate. > Does that mean you prefer this to be unrolled then (written without a for > just repeating the if ((val & mask) != (s->base_addr_enable & mask)) at > every case)? That would also be longer by about 20 lines as we also log > regions that are not in the switch that would need their enable bits checked > separately if it's unrolled. Basically the trace is the common part of the > loop and handling of the individual regions are branching out from the > switch as they are different enough that factoring out the common parts is > not shorter than this way due to then needing local variables to hold the > different parts (name, address, size, base) the assigning of which are as > many or more lines than the map_pci_region call that could be factored out > that way. > > I don't see why it is a problem to have a switch in a loop. If you insist I > can try to turn the switch into if else but I don't see how would that be > any better. Please explain a bit more what would you prefer here as I'm not > sure what to do with this. To me this is the shortest and simplest way to > write this. Hmm... you know, it still seems kinda weird to me, but I am getting too hung up on details here. It's good enough, go with it. -- David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson