View previous topic :: View next topic |
Author |
Message |
s_mack
Joined: 04 Jun 2009 Posts: 107
|
min, max? |
Posted: Sun Jun 07, 2009 5:45 pm |
|
|
Is there a built in function for determining the minimum of two values? In visual C++ I can use _cpp_min and _cpp_max but that doesn't translate to ccs it seems.
Of course I can just use ifs but wondered if there was a cleaner way.
I can't really find anything specific in the help except it does reference this in the compiler error messages section: Quote: | Macro identifier requires parameters
A #DEFINE identifier is being used but no parameters were specified, as required. For example:
#define min(x,y) ((x<y)?x:y)
When called MIN must have a (--,--) after it such as:
r=min(value, 6);
|
implying there is a MIN function, no?
Thanks. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jun 07, 2009 6:22 pm |
|
|
Most CCS functions that are not built-in to the compiler, will be in these files:
Quote: | c:\program files\picc\examples\ex_macro.c
c:\program files\picc\drivers\input.c
c:\program files\picc\drivers\stdlib.h
c:\program files\picc\drivers\ctype.h
c:\program files\picc\drivers\string.h |
For example, the functions you want are in Ex_Macro.c.
Make a note of these files. |
|
|
s_mack
Joined: 04 Jun 2009 Posts: 107
|
|
Posted: Sun Jun 07, 2009 7:05 pm |
|
|
Thanks.
I think I get it. I have to add that to my script, right? So I'm defining a shorthand kind of. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sun Jun 07, 2009 11:43 pm |
|
|
Strange indeed. Any usual C-compiler defines these macros in stdlib.h. |
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
|
Posted: Mon Jun 08, 2009 5:01 pm |
|
|
I have macros with syntax similar to yours:
Code: | #define MAX(x,y) ((x > y) ? x : y)
#define MIN(x,y) ((x < y) ? x : y) |
and the following compiles just fine:
Code: | if (i < MIN(iStartAddr + iLen - iCurrAddr, i_BUFF_SIZE)) ... |
BTW, since CCS C doesn't support overloading, one pair of MIN & MAX macros will serve for all primitive data types. _________________ Read the label, before opening a can of worms. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Mon Jun 08, 2009 11:00 pm |
|
|
As a disadvantage of using a macro in this place, the expression iStartAddr + iLen - iCurrAddr is calculated twice by CCS C, as most embedded compilers do.
The following - more verbose - text needs 32 instructions with PIC18 and int16 compared to 41 for the first:
Code: | temp = iStartAddr + iLen - iCurrAddr;
if (i < MIN(temp, i_BUFF_SIZE)) ... |
|
|
|
Guest
|
|
Posted: Tue Jun 09, 2009 4:58 am |
|
|
Why will it do the code twice? |
|
|
Ttelmah Guest
|
|
Posted: Tue Jun 09, 2009 5:10 am |
|
|
As a comment though, CCS V4, _does_ support overloading. It has been working for perhaps a year or more, and works well. You will find some posts here in the past, using this.
Macros however do not involve overloading. They are expanded before this is applied (if in use for any of the functions). Macros themselves are always 'type independent'.
Best Wishes |
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
|
Posted: Tue Jun 09, 2009 12:06 pm |
|
|
Anonymous wrote: | Why will it do the code twice? |
It will repeat the calculation of x twice because x appears twice in the macro. 1st in condition 2nd in one of the options.
Hm... Wouldn't a clever optimizer catch something like that? _________________ Read the label, before opening a can of worms. |
|
|
|