BBC BASIC
Programming >> BBC BASIC Language >> Expression simplification http://bbcbasic.conforums.com/index.cgi?board=language&action=display&num=1481845913 Expression simplification
Post by Richard Russell on Dec 15th, 2016, 10:51pm
In something I'm working on I've ended up with this rather unwieldy expression in a time-critical section of code:
Code:
P%!4 AND &80000000 OR ((P%!4 >> 21) - &1C0 << 24 OR P%!4 << 3 AND &FFFFFF OR !P% >>> 29) - ((!P% << 3) < 0)
I've simplified it as much as I can, but maybe a fresh eye will spot something I haven't. Can anybody see a way of making it run faster? I know I can delete the spaces but I've left them in here for clarity.
Richard. Re: Expression simplification
Post by Richard Russell on Dec 16th, 2016, 8:44pm
I've made a major improvement, which I don't know how I missed originally:
Code:
P%!4 AND NOT &7FFFFFFF OR (P%!4-&38000000<<3 OR !P%>>29 AND 7) + (!P%>>28 AND 1)
I also realised that since this code is intended for a library it's important that it works in both *hex32 and *hex64 modes, which the above does but it complicates things significantly.
Richard.
Re: Expression simplification
Post by michael on Dec 17th, 2016, 04:00am
> this code is intended for a library
what kind of library?
Re: Expression simplification
Post by Richard Russell on Dec 17th, 2016, 09:20am
You might recognise what the code is doing, and if so you can probably guess the sort of application it has. But it's largely an academic exercise to see just how much optimisation is possible.