Need macro equivalent for function 
Author Message
 Need macro equivalent for function

Hi,

I have 2 doubts..

1. I want to replace the following function with a macro..Please do
let me know how I can do this...My function uses a local variable..how
do I define the equivalent in a C macro?

int shiftleft( short var1, short var2 )
{
   long varOut ;
   long result ;

   if( ( int )var2 < 0 )
   {
      varOut = shiftright( var1,( int )( -var2 ) ) ;

   }//if
   else
   {
      result = (long)var1 * ( (long)1 << var2 ) ;

     if(( var2 > 15 && var1 != 0 ) || ( result != (long)((int) result
)))
         {
         Overflow = 1 ;
         varOut = ( var1 > 0 ) ? MAX_16 : MIN_16 ;
     }//if
     else
     {
         varOut = extract(result);
     }//else

   }//else

   return( varOut ) ;

Quote:
} //end

The above function calls 2 functions shiftright and extract, MAX_16
and MIN_16 are 2 macros inside this function

2. Also is inline allowed in C??

Which of the two would be better interms of performance??
Will it be macro or inline functions(if allowed..)

Please do respond on this aSAP

-Regards,



Mon, 14 Nov 2005 06:38:34 GMT  
 Need macro equivalent for function

Quote:

> Hi,

> I have 2 doubts..

> 1. I want to replace the following function with a macro..Please do
> let me know how I can do this...My function uses a local variable..how
> do I define the equivalent in a C macro?

#define SHIFTLEFT(var1, var2, result)                           \
(0 > (var2) ? shiftright((var1), (int)(-(var2)))                \
: result = (long)(var1) * (1L << (var2)),                       \
(var2) > 15 && (var1) != 0  || result != (int)result ?          \
Overflow = 1, (var1) > 0 ? MAX_16 : MIN_16 : extract(result))

Quote:
> int shiftleft( short var1, short var2 )
> {
>    long varOut ;
>    long result ;

>    if( ( int )var2 < 0 )
>    {
>       varOut = shiftright( var1,( int )( -var2 ) ) ;

>    }//if
>    else
>    {
>       result = (long)var1 * ( (long)1 << var2 ) ;

>      if(( var2 > 15 && var1 != 0 ) || ( result != (long)((int) result
> )))
>          {
>          Overflow = 1 ;
>          varOut = ( var1 > 0 ) ? MAX_16 : MIN_16 ;
>      }//if
>      else
>      {
>          varOut = extract(result);
>      }//else

>    }//else

>    return( varOut ) ;

> } //end

> The above function calls 2 functions shiftright and extract, MAX_16
> and MIN_16 are 2 macros inside this function

> 2. Also is inline allowed in C??

> Which of the two would be better interms of performance??
> Will it be macro or inline functions(if allowed..)

> Please do respond on this aSAP

> -Regards,

--
pete


Mon, 14 Nov 2005 07:26:49 GMT  
 Need macro equivalent for function

Quote:


> > Hi,

> > I have 2 doubts..

> > 1. I want to replace the following function with a macro..Please do
> > let me know how I can do this...My function uses a local variable..how
> > do I define the equivalent in a C macro?

> #define SHIFTLEFT(var1, var2, result)                           \
> (0 > (var2) ? shiftright((var1), (int)(-(var2)))                \
> : result = (long)(var1) * (1L << (var2)),                       \
> (var2) > 15 && (var1) != 0  || result != (int)result ?          \
> Overflow = 1, (var1) > 0 ? MAX_16 : MIN_16 : extract(result))

I unintentionally neglected to put parentheses around "result"
in the macro.
Also (result) needs to come from outside the macro now.

--
pete



Mon, 14 Nov 2005 07:32:48 GMT  
 Need macro equivalent for function

Quote:


>> Hi,

>> I have 2 doubts..

>> 1. I want to replace the following function with a macro..Please do
>> let me know how I can do this...My function uses a local variable..how
>> do I define the equivalent in a C macro?

> #define SHIFTLEFT(var1, var2, result)                           \
> (0 > (var2) ? shiftright((var1), (int)(-(var2)))                \
>: result = (long)(var1) * (1L << (var2)),                       \
> (var2) > 15 && (var1) != 0  || result != (int)result ?          \
> Overflow = 1, (var1) > 0 ? MAX_16 : MIN_16 : extract(result))

Careful.  Consider the following:

x = 2; y = 3;
SHIFTLEFT(x, ++y, result);

What is the value of y afterwards?

Quote:

>> 2. Also is inline allowed in C??

Inline is allowed in C99, the current standard.  However, your compiler
may not support C99, or may support inline in a nonstandard way.

Quote:

>> Which of the two would be better interms of performance??
>> Will it be macro or inline functions(if allowed..)

That depends on your compiler, your environment, the rest of the
program, etc.  Basically, the only way to find out which is better is to
try both.

Cheers,
Kevin

--
He that breaks a thing to find out what it is has left the path of
wisdom                                         -- Gandalf the Grey



Tue, 15 Nov 2005 11:09:20 GMT  
 Need macro equivalent for function

Quote:



> >> Hi,

> > #define SHIFTLEFT(var1, var2, result)                           \
> > (0 > (var2) ? shiftright((var1), (int)(-(var2)))                \
> >: result = (long)(var1) * (1L << (var2)),                       \
> > (var2) > 15 && (var1) != 0  || result != (int)result ?          \
> > Overflow = 1, (var1) > 0 ? MAX_16 : MIN_16 : extract(result))

> Careful.  Consider the following:

> x = 2; y = 3;
> SHIFTLEFT(x, ++y, result);

> What is the value of y afterwards?

6

--
pete



Tue, 15 Nov 2005 11:24:41 GMT  
 Need macro equivalent for function
On Wed, 28 May 2003 19:26:49 -0400,

Quote:

>> Hi,

>> I have 2 doubts..

>> 1. I want to replace the following function with a macro..Please do
>> let me know how I can do this...My function uses a local variable..how
>> do I define the equivalent in a C macro?

> #define SHIFTLEFT(var1, var2, result)                           \
> (0 > (var2) ? shiftright((var1), (int)(-(var2)))                \
>: result = (long)(var1) * (1L << (var2)),                       \

A conditional expression can't be an lvalue. I think you missed some
brackets.

Quote:
> (var2) > 15 && (var1) != 0  || result != (int)result ?          \
> Overflow = 1, (var1) > 0 ? MAX_16 : MIN_16 : extract(result))

var1 and var2 are evaluated more than once in this macro. If they are
expressions that have side effects, that results in a difference
between the macro call and the function call.

SHIFTLEFT(some_func_with_side_effects(), foo++);

Quote:
>> int shiftleft( short var1, short var2 )

Martien
--
                        |
Martien Verbruggen      |
Trading Post Australia  | values of Beta will give rise to dom!
                        |


Tue, 15 Nov 2005 11:51:44 GMT  
 Need macro equivalent for function
On Thu, 29 May 2003 23:24:41 -0400,

Quote:



>> >> Hi,

>> > #define SHIFTLEFT(var1, var2, result)                           \
>> > (0 > (var2) ? shiftright((var1), (int)(-(var2)))                \
>> >: result = (long)(var1) * (1L << (var2)),                       \
>> > (var2) > 15 && (var1) != 0  || result != (int)result ?          \
>> > Overflow = 1, (var1) > 0 ? MAX_16 : MIN_16 : extract(result))

>> Careful.  Consider the following:

>> x = 2; y = 3;
>> SHIFTLEFT(x, ++y, result);

>> What is the value of y afterwards?

> 6

And what would it be after the function call

shiftleft(x, ++y, result);

Martien
--
                        |
Martien Verbruggen      |
Trading Post Australia  | 42.6% of statistics is made up on the spot.
                        |



Tue, 15 Nov 2005 11:52:35 GMT  
 Need macro equivalent for function

Quote:

> On Thu, 29 May 2003 23:24:41 -0400,




> >> >> Hi,

> >> > #define SHIFTLEFT(var1, var2, result)                           \
> >> > (0 > (var2) ? shiftright((var1), (int)(-(var2)))                \
> >> >: result = (long)(var1) * (1L << (var2)),                       \
> >> > (var2) > 15 && (var1) != 0  || result != (int)result ?          \
> >> > Overflow = 1, (var1) > 0 ? MAX_16 : MIN_16 : extract(result))

> >> Careful.  Consider the following:

> >> x = 2; y = 3;
> >> SHIFTLEFT(x, ++y, result);

> >> What is the value of y afterwards?

> > 6

> And what would it be after the function call

> shiftleft(x, ++y, result);

4

--
pete



Tue, 15 Nov 2005 18:22:19 GMT  
 Need macro equivalent for function

Quote:

> On Wed, 28 May 2003 19:26:49 -0400,


> >> Hi,

> >> I have 2 doubts..

> >> 1. I want to replace the following function with a macro..Please do
> >> let me know how I can do this...My function uses a local variable..how
> >> do I define the equivalent in a C macro?

> > #define SHIFTLEFT(var1, var2, result)                           \
> > (0 > (var2) ? shiftright((var1), (int)(-(var2)))                \
> >: result = (long)(var1) * (1L << (var2)),                       \

> A conditional expression can't be an lvalue.

That's what my compiler says too.

Quote:
> I think you missed some brackets.

Good catch. Thank you.

#define SHIFTLEFT(var1, var2, result)                           \
(0 > (var2) ? shiftright((var1), (-(var2)))                     \
: ((result) = (long)(var1) * (1L << (var2))),                   \
(var2) > 15 && (var1) != 0  || result != (int)(result) ?        \
Overflow = 1, (var1) > 0 ? MAX_16 : MIN_16 : extract(result))

Quote:
> > (var2) > 15 && (var1) != 0  || result != (int)result ?          \
> > Overflow = 1, (var1) > 0 ? MAX_16 : MIN_16 : extract(result))

> var1 and var2 are evaluated more than once in this macro. If they are
> expressions that have side effects, that results in a difference
> between the macro call and the function call.

> SHIFTLEFT(some_func_with_side_effects(), foo++);

I think that the
    (long)(var1) * (1L << (var2))
expression, could be replaced with
    (long)(var1) << (var2)

--
pete



Tue, 15 Nov 2005 19:16:42 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. need an equivalent function with mmap()

2. I need vc ide macro help, trying to create a move to end of word macro

3. I need vc ide macro help, trying to create a move to end of word macro

4. MFC TRACE MACRO equivalent in C

5. C function equivalent to PACK function in Perl

6. Macro to check function return, calling function again?

7. Need help with bitmap.clone() or equivalent

8. I need a C# equivalent

9. Help needed with converting Basic TRIM and Right to C equivalents

10. Need DEC C/Borland equivalent of getche()

11. I need an equivalent of VBs AppActivate() in VC

12. URGENT-need equivalent of atoi() in f77

 

 
Powered by phpBB® Forum Software