davidgiven / cowgol

A self-hosted Ada-inspired programming language for very small systems.
BSD 2-Clause "Simplified" License
247 stars 20 forks source link

small optimization: % poweroftwo -> & poweroftwo-1 #142

Closed shattered closed 6 months ago

davidgiven commented 6 months ago

Thanks --- that's definitely worth having. There are likely to be edge conditions regarding signed values, though. Could you make sure that there are tests for this case?

shattered commented 6 months ago

I think divrem tests already cover this:

print("four%two==zero"); if four%two==zero then yes(); else no(); end if;

davidgiven commented 6 months ago

Sorry, I got distracted by other things... also I didn't spot that this just concerns REMU, and not REMS.

Yes, all looks good. Merging.

Trying to do the same optimisation for signed values looks like it'll be nightmare. Getting the result sign right was bad enough just for the normal divmod calculation.