Using Config Words to Define Which Bits of Program Run

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 5.
To post in this forum you must have a registered copy of Flowcode 5 or higher.

Moderator: Benj

Post Reply
User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times

Using Config Words to Define Which Bits of Program Run

Post by Jay Dee »

Hi Guys,
I'm after some general advice on good program design and structure, I have quite a few flowcode programs working but would like future versions to be better structured and reasonably efficient.

I've done a very simplified flowcode to try to show what I wish to achieve and want to confirm if this is best programming practice.
Using Config Words.fcf
(24.2 KiB) Downloaded 268 times

Example #1.
A system monitors several similar processes and returns relevant variables for each.

Process #1 → Var1A, Var1B, Var1C
Process #2 → Var2A, Var2B, Var2C
Process #3 → Var3A, Var3B, Var3C

There are a selection of user defined macros (functions) which can be used to manipulate/analyse this data,

Function1
Function2
Function3
Function4

I would like the user to be able to specify some configuration words and these config words would determine not only if a process should be analysed but also which specific functions should be applied.
In a real example the config words would be come from a VB program and sent over using USB Slave.

The attached Flowcode does achieve this task using lots of IF statements but is it the best method?
In my example the program must evaluate every IF statement, every loop of the program; even though the config words are fixed at the start of the program and do not change. This seems to be doing a lot of extra work but i can't see how to do it any other way. Is this normal practice or am I making the PIC evaluate more lines of code than it needs too?
Thanks, J.

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 63 times
Been thanked: 290 times
Contact:

Re: Using Config Words to Define Which Bits of Program Run

Post by JonnyW »

Hello.

Usually for fixed identifiers/config words you would use a switch statement - it is then the compilers job to optimise this as best it can.

Unfortunately this is about as good as it gets when evaluating things like this. I write interpreters which do a similar job on the instruction set and the most efficient (i.e. fast) outcomes I tend to get are using a switch statement in a tight loop. If you want efficiency try to make your codes sequential - compilers can optimise a switch of {1, 2, 3, 4, 5...} better than {10, 20, 30, 40, 50...}.

In some languages it is possible to use callbacks, but this is basically doing behind the scenes what you are doing anyway, and you would still need to map your values to functions.

In specific cases it is possible to use parts of your identifiers as bit-masks (eg: if bit 7 is set then do this else do that) but that just tends to distribute your program over a wider and less readable area. It tends to be when you have to use specific codes and still have a large number of them.

I hope this helps,

Jonny

User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times

Re: Using Config Words to Define Which Bits of Program Run

Post by Jay Dee »

Hi Jonny,
Thanks, I think I followed that,
So staying within flowcode my options are.

>Use Switch if you have a Single variable and use it to decide which Single option from a range of options to use.
> Some compilers will create more concise C code if consecutive numbers are used in the switch function.

>The 8 bits of a single config byte can be used as flags, one flag for each user option. The config byte would be masked and then compared to expression in an IF statement.
>If I have multiple identifiers/config words/flags determining which combination of multiple options are to be acted upon, then I do need to use an sequence of IF statements that are evaluated each cycle of the loop.
J.

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 63 times
Been thanked: 290 times
Contact:

Re: Using Config Words to Define Which Bits of Program Run

Post by JonnyW »

Hi. Yes, that's pretty much your options. This isn't limited to Flowcode, its just how programming languages in general work.

I would suggest if you are using bits instead of values that you make certain this is what is required - you will find you run out of bits very quickly, especially with just a byte. Flags are typically used if more than one can be specified at once, or to specify a 'mode', eg: if the top bit is set then the bottom 7 bits are added to the previous byte, else they are not. Examples of this are found in run-length encoding compression and floating point format numbers.

Personally I would use a switch statement regardless of what values to expect - it is more easily extendable, easier to read and better managed by the compiler than nested if statements, even if in reality it does generate the expected code.

If speed really is a concern then put the most commonly read items at the start of the switch statement and anything that is not a long process try in-lining it, as the overhead of calling a macro is typically much greater than comparing a value.

Cheers,

Jonny

User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times

Re: Using Config Words to Define Which Bits of Program Run

Post by Jay Dee »

Thanks Jonny,

My only query is that a Switch Statement is will only action one of the switches legs, correct?
If I want more than one task to be actioned ( in one pass of the loop) I do need to use a series of IF statements? Or am I missing a trick with Switch Statements.

Good info on how to keep the switch statements speedy and efficient, thanks.
J.

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 63 times
Been thanked: 290 times
Contact:

Re: Using Config Words to Define Which Bits of Program Run

Post by JonnyW »

Hi. Yes, that is the case.

If you are using a set of bits then maybe a set of if statements will do just as well, but never being one to do things the easy way you can use bit-hacks to get the bits of your data. This is slightly more efficient than the ifs as you only check the bits you have set, but that all depends on how the ASM for the switch is generated:

Code: Select all

loop while (input != 0)
  single_bit = input & ~(input - 1)  // Calculation: Get the bottom bit of the input
  input = input & ~single_bit        // Calculation: Remove the bottom bit of the input
  switch(single_bit)
    case 1 .....
    case 2 .....
    case 4 .....
    .....
    case 128 .....
Of course, you may argue that that is over kill, and you're probably right.

Jonny

Post Reply