CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

min, max?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
s_mack



Joined: 04 Jun 2009
Posts: 107

View user's profile Send private message

min, max?
PostPosted: Sun Jun 07, 2009 5:45 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Jun 07, 2009 6:22 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Jun 07, 2009 7:05 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Jun 07, 2009 11:43 pm     Reply with quote

Strange indeed. Any usual C-compiler defines these macros in stdlib.h.
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Mon Jun 08, 2009 5:01 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jun 08, 2009 11:00 pm     Reply with quote

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








PostPosted: Tue Jun 09, 2009 4:58 am     Reply with quote

Why will it do the code twice?
Ttelmah
Guest







PostPosted: Tue Jun 09, 2009 5:10 am     Reply with quote

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

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Tue Jun 09, 2009 12:06 pm     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group