GCC Built-in functions

20 views
Skip to first unread message

Rahul Patel

unread,
May 18, 2017, 12:15:29 PM5/18/17
to fundamental...@googlegroups.com
Recently, come across usage of Built-in functions.


Surprisingly, Replacing below piece of code with usage of Built-in functions has reduced elf size by half.
 
#define FAIL_UNLESS(fmt, value, cmp, expect, description...) \
  do { \
    __typeof(0 ? (value) : (expect)) _m_value = (value); \
    __typeof(0 ? (value) : (expect)) _m_expect = (expect); \
    _testCondition(fmt,value,expect,cmp);\
    if(strcmp((char *)fmt,"%s")==0)\
    {\
      (strcmp((char *)_m_value,(char *)_m_expect) cmp 0) \
        ? _tHarnessTestCondition(1, __FILENAME__, __FUNCTION__, __LINE__,description) \
        : _tHarnessTestCondition(0, __FILENAME__, __FUNCTION__, __LINE__,description);\
    }\
    else\
    {\
      ((_m_value) cmp (_m_expect)) \
          ? _tHarnessTestCondition(1, __FILENAME__, __FUNCTION__, __LINE__,description) \
          : _tHarnessTestCondition(0, __FILENAME__, __FUNCTION__, __LINE__,description);\
    }\
  } while (0)
  
  
#define FAIL_UNLESS(fmt, value, cmp, expect, ...)                                     \
  do {                                                                                \
    __typeof(0 ? (value) : (expect)) _m_value  = (value);                             \
    __typeof(0 ? (value) : (expect)) _m_expect = (expect);                            \
    _testCondition(fmt, value, expect, cmp);                                          \
    bool _m_result =                                                                  \
        /* Don't include code to process strings if the argument isn't a string. */   \
        __builtin_choose_expr(                                                        \
            __builtin_types_compatible_p(__typeof(_m_value), const char*),            \
            /* With __builtin_strcmp the compiler should optimize out the check. */   \
            ((__builtin_strcmp((const char*)fmt, "%s") == 0)                          \
                /* The double-typecasts are in case the value can't be converted  */  \
                /* directly to a pointer (e.g. float). Conversion from float to   */  \
                /* pointer causes an error even if this expression is not chosen. */  \
                /* In the final code _m_value is always compatible with (char*).  */  \
                ? (strcmp((const char*)(uint32_t)_m_value,                            \
                          (const char*)(uint32_t)_m_expect) cmp 0)                    \
                : ((_m_value) cmp (_m_expect))),                                      \
            ((_m_value) cmp (_m_expect)));                                            \
    _tHarnessTestCondition(_m_result,__FILENAME__,__func__,__LINE__,__VA_ARGS__);     \
  } while (0)

-- Rahul Patel

Vishal Thanki

unread,
May 19, 2017, 3:56:29 AM5/19/17
to fundamental...@googlegroups.com
Good to know this. I wonder how strcmp can be optimized further ;-) 

--
-- Owner of this group do not accept ANY liability for the views expressed in the email or for the consequences of any computer viruses that may be transmitted with this email. Owner is not responsible for any kind of copyright violations or infringements caused/posted by any user of the group. Messages are posted through moderation also does not include any kind of verification against copyright violations or infringements.
---
You received this message because you are subscribed to the Google Groups "insane" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fundamental-discussion+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Viral Mehta

unread,
May 19, 2017, 4:19:29 AM5/19/17
to fundamental...@googlegroups.com
Ohhh ! Our group is still active. I am missing them under Forum tabs.

And believe me, I dont understand a bit of this code :(


To unsubscribe from this group and stop receiving emails from it, send an email to fundamental-discussion+unsubscri...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
-- Owner of this group do not accept ANY liability for the views expressed in the email or for the consequences of any computer viruses that may be transmitted with this email. Owner is not responsible for any kind of copyright violations or infringements caused/posted by any user of the group. Messages are posted through moderation also does not include any kind of verification against copyright violations or infringements.
---
You received this message because you are subscribed to the Google Groups "insane" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fundamental-discussion+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Thanks,
Viral Mehta

Rahul Patel

unread,
May 19, 2017, 10:35:55 AM5/19/17
to fundamental...@googlegroups.com
Viral, you are beyond such code. you focus on complex code which can be patented. :)


--
Thanks,
Viral Mehta

--
-- Owner of this group do not accept ANY liability for the views expressed in the email or for the consequences of any computer viruses that may be transmitted with this email. Owner is not responsible for any kind of copyright violations or infringements caused/posted by any user of the group. Messages are posted through moderation also does not include any kind of verification against copyright violations or infringements.
---
You received this message because you are subscribed to the Google Groups "insane" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fundamental-discussion+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Rahul Patel

Tanmay Upadhyay

unread,
May 20, 2017, 12:21:33 AM5/20/17
to fundamental...@googlegroups.com
😀👍

Viral Mehta

unread,
May 22, 2017, 7:13:21 AM5/22/17
to fundamental...@googlegroups.com
Ha ha ! That was a joke I know ;)

Thanks,
Viral Mehta
Reply all
Reply to author
Forward
0 new messages