I did try it as an inline function and yes, it still ran 40x slower...
however, I just tried something different. Up until now I was compiling with VC++ 2008 Express Edition in Debug mode. I've since turned on Release mode and I can't even register a single clock tick to provide a measurable difference between using a function or Macro. I've tried up to 8 nested loops each going from 0 to ULONG_MAX and the end result is still 0 clock...
User Profile
Collapse
-
Just an update here... I wrote a function to perform the same task and compared it to the Macro version. On average, the Macro ran 40x faster than the function version.
Also, I was able to optimize the code slightly:
Code:#define M_HIGH(sbit, ebit, data) ( (data[M_BYTENUM(ebit)] & ( (1<<((ebit&0x07) + 1)) - 1 ) ) << (( (ebit&0xf8) - sbit) ) ) // Upper portion of value #define M_MID(sbit, data)
Leave a comment:
-
Found bug.
M_MID should be:
Code:#define M_MID(sbit, data) ( data[M_BYTENUM(sbit)+1] << ((((M_BYTENUM(sbit)+1) << 3) - sbit ) &0x0f) ) // Middle byte value when value spans three bytes
Leave a comment:
-
Left shifting by too many bits would definitely produce inaccurate results. As the name implies, the macro is designed to extract a 16-bit value from some location in a byte array. No error checking is done to see if (ebit-sbit)>16. I've left it up to the programmer to enter correct values from the start.
As you mentioned, I may be prematurely optimizing code here as I've not measured to establish a benchmark. As it is now I've...Leave a comment:
-
I did everything with macros to save on the overhead associated with a function call. My thinking was the preprocessor would do all of the constant math at compile time thus coming up with a condensed line of code rather than an expanded mess.
As for getting the right values, all of my test cases return correct values so I'm fairly confident the casts aren't needed. This, however, may be compiler dependent.
I did run into...Leave a comment:
-
Leave a comment:
-
Working with non byte aligned data values in memory
I'm working with a data stream of 8 bytes in an embedded application. In most cases the data is byte aligned so I can define a structure and then memcpy the data directly to the structure elements. There are, however, a few cases where the 16 bit data values span 2 or 3 bytes. I came up with a few macros to handle these cases. It's working fine, but I'm wondering if anyone can point out some obvious flaws or code optimizations to make the macros... -
Yes, this is an embedded application and I will have to watch the amount of RAM I use. I'll take a look at your recommendation as it looks like a viable solution. Thanks again!...Leave a comment:
-
That is correct. Including the test.c file was mentioned as an alternate approach....Leave a comment:
-
True on the second point... which takes me right back to my initial problem. Forget I even mentioned it. ;)...Leave a comment:
-
The actual structure may look something like this:
Code:TextStructDef TextStruct[]= { {"First option" , TRUE}, {"Second choice" , TRUE}, {"Choice number three" , TRUE}, {"Fourth" , TRUE}, {"Fifth selection" , TRUE}, };
Leave a comment:
-
Good points guys! I do appreciate the input.
Going back to my initial post, when I change the include line to be test.c instead of test.h, the program compiles & works. Any negative to that?
This is starting to open a can beyond my original question... in the actual program, there are about 20 arrays like this each with up to 20 to 30 elements. The text can vary in length but is fixed so I can use const char *. The...Leave a comment:
-
Thanks guys! Those both will work... either adding a function in Test.c which returns the size, or declaring a global constant below the array definition (though I can't use [100] because the number of elements will change-- not dynamically where I would need to allocate space, but in the source file)
I ended up with something like this:
MainTest.c:
Code:#include "test.h" #include "stdlib.h"
Leave a comment:
-
Using sizeof with global structure
Question for all...
I'm trying to determine the size of a global structure. It has to be declared globally because it's used in several different files. When compiling (BCC32) the code I get the error:
Error E2109 MainTest.c 9: Not an allowed type in function main
I know this is happening because the array of structures is of an undefined size []. But I can't define the size ahead of time.
...
No activity results to display
Show More
Leave a comment: