etisserant@107: /* msousa@350: * copyright 2008 Edouard TISSERANT msousa@350: * copyright 2011 Mario de Sousa (msousa@fe.up.pt) etisserant@107: * etisserant@107: * Offered to the public under the terms of the GNU Lesser General Public etisserant@107: * License as published by the Free Software Foundation; either version 2 etisserant@107: * of the License, or (at your option) any later version. etisserant@107: * etisserant@107: * This program is distributed in the hope that it will be useful, but etisserant@107: * WITHOUT ANY WARRANTY; without even the implied warranty of etisserant@107: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser etisserant@107: * General Public License for more details. etisserant@107: * etisserant@107: * This code is made available on the understanding that it will not be etisserant@107: * used in safety-critical situations without a full and competent review. etisserant@107: */ etisserant@107: etisserant@40: /**** etisserant@107: * IEC 61131-3 standard function library etisserant@40: */ etisserant@40: msousa@350: /* NOTE: This file is full of (what may seem at first) very strange macros. msousa@350: * If you want to know what all these strange macros are doing, msousa@350: * just parse this file through a C preprocessor (e.g. cpp), msousa@350: * and analyse the output! msousa@350: * $gcc -E iec_std_lib.h msousa@350: */ msousa@350: etisserant@40: #include etisserant@40: #include etisserant@40: #include etisserant@40: #include etisserant@57: #include etisserant@40: etisserant@41: #include etisserant@40: #include etisserant@40: #include etisserant@40: #include etisserant@40: etisserant@43: #ifdef DEBUG_IEC etisserant@43: #define DBG(...) printf(__VA_ARGS__); etisserant@43: #define DBG_TYPE(TYPENAME, name) __print_##TYPENAME(name); etisserant@43: #else etisserant@43: #define DBG(...) etisserant@43: #define DBG_TYPE(TYPENAME, name) etisserant@43: #endif etisserant@43: etisserant@137: /* etisserant@137: * Include type defs. etisserant@137: */ etisserant@137: #include "iec_types_all.h" etisserant@40: etisserant@41: extern TIME __CURRENT_TIME; etisserant@140: extern BOOL __DEBUG; etisserant@40: etisserant@40: /* TODO etisserant@40: typedef struct { etisserant@40: __strlen_t len; etisserant@40: u_int16_t body[STR_MAX_LEN]; etisserant@40: } WSTRING; etisserant@40: */ msousa@350: /* etisserant@57: # if __WORDSIZE == 64 etisserant@57: #define __32b_sufix etisserant@57: #define __64b_sufix L etisserant@57: #else etisserant@57: #define __32b_sufix L greg@180: #define __64b_sufix LL greg@180: #endif msousa@350: */ msousa@350: msousa@350: # if __WORDSIZE == 64 msousa@350: #define __32b_sufix msousa@350: #define __64b_sufix L msousa@350: #else msousa@350: #define __32b_sufix L msousa@350: /* changed this from LL to L temporarily. It was causing a bug when compiling resulting code with gcc. msousa@350: * I have other things to worry about at the moment.. msousa@350: */ msousa@350: #define __64b_sufix L msousa@350: #endif msousa@350: etisserant@57: etisserant@57: #define __lit(type,value,sfx) (type)value##sfx greg@180: // Keep this macro expention step to let sfx change into L or LL etisserant@57: #define __literal(type,value,sfx) __lit(type,value,sfx) etisserant@57: etisserant@57: #define __BOOL_LITERAL(value) __literal(BOOL,value,) etisserant@57: #define __SINT_LITERAL(value) __literal(SINT,value,) etisserant@57: #define __INT_LITERAL(value) __literal(INT,value,) etisserant@57: #define __DINT_LITERAL(value) __literal(DINT,value,__32b_sufix) etisserant@57: #define __LINT_LITERAL(value) __literal(LINT,value,__64b_sufix) etisserant@57: #define __USINT_LITERAL(value) __literal(USINT,value,) etisserant@57: #define __UINT_LITERAL(value) __literal(UINT,value,) etisserant@57: #define __UDINT_LITERAL(value) __literal(UDINT,value,__32b_sufix) etisserant@57: #define __ULINT_LITERAL(value) __literal(ULINT,value,__64b_sufix) etisserant@57: #define __REAL_LITERAL(value) __literal(REAL,value,__32b_sufix) etisserant@57: #define __LREAL_LITERAL(value) __literal(LREAL,value,__64b_sufix) etisserant@57: #define __TIME_LITERAL(value) __literal(TIME,value,) etisserant@57: #define __DATE_LITERAL(value) __literal(DATE,value,) etisserant@57: #define __TOD_LITERAL(value) __literal(TOD,value,) etisserant@57: #define __DT_LITERAL(value) __literal(DT,value,) laurent@217: #define __STRING_LITERAL(count,value) (STRING){count,value} etisserant@57: #define __BYTE_LITERAL(value) __literal(BYTE,value,) etisserant@57: #define __WORD_LITERAL(value) __literal(WORD,value,) etisserant@57: #define __DWORD_LITERAL(value) __literal(DWORD,value,__32b_sufix) msousa@350: #define __LWORD_LITERAL(value) __literal(LWORD,value,__64b_sufix) etisserant@55: etisserant@55: etisserant@55: etisserant@41: #define __INIT_REAL 0 etisserant@41: #define __INIT_LREAL 0 etisserant@41: #define __INIT_SINT 0 etisserant@41: #define __INIT_INT 0 etisserant@41: #define __INIT_DINT 0 etisserant@41: #define __INIT_LINT 0 etisserant@41: #define __INIT_USINT 0 etisserant@41: #define __INIT_UINT 0 etisserant@41: #define __INIT_UDINT 0 etisserant@41: #define __INIT_ULINT 0 etisserant@41: #define __INIT_TIME (TIME){0,0} etisserant@41: #define __INIT_BOOL 0 etisserant@41: #define __INIT_BYTE 0 etisserant@41: #define __INIT_WORD 0 etisserant@41: #define __INIT_DWORD 0 etisserant@41: #define __INIT_LWORD 0 etisserant@41: #define __INIT_STRING (STRING){0,""} etisserant@41: //#define __INIT_WSTRING etisserant@41: #define __INIT_DATE (DATE){0,0} etisserant@41: #define __INIT_TOD (TOD){0,0} etisserant@41: #define __INIT_DT (DT){0,0} etisserant@41: etisserant@40: typedef union __IL_DEFVAR_T { etisserant@40: BOOL BOOLvar; etisserant@40: etisserant@40: SINT SINTvar; etisserant@40: INT INTvar; etisserant@40: DINT DINTvar; etisserant@40: LINT LINTvar; etisserant@40: etisserant@40: USINT USINTvar; etisserant@40: UINT UINTvar; etisserant@40: UDINT UDINTvar; etisserant@40: ULINT ULINTvar; etisserant@40: etisserant@40: BYTE BYTEvar; etisserant@40: WORD WORDvar; etisserant@40: DWORD DWORDvar; etisserant@40: LWORD LWORDvar; etisserant@40: etisserant@40: REAL REALvar; etisserant@40: LREAL LREALvar; etisserant@40: etisserant@40: TIME TIMEvar; etisserant@40: TOD TODvar; etisserant@40: DT DTvar; etisserant@40: DATE DATEvar; etisserant@40: } __IL_DEFVAR_T; etisserant@40: msousa@350: msousa@350: /**********************************************************************/ msousa@350: /**********************************************************************/ msousa@350: /***** *****/ msousa@350: /***** Some helper functions... *****/ msousa@350: /***** ...used later: *****/ msousa@350: /***** - when declaring the IEC 61131-3 standard functions *****/ msousa@350: /***** - in the C source code itself in SFC and ST expressions *****/ msousa@350: /***** *****/ msousa@350: /**********************************************************************/ msousa@350: /**********************************************************************/ msousa@350: msousa@350: msousa@350: /****************************/ msousa@350: /* Notify IEC runtime error */ msousa@350: /****************************/ etisserant@40: etisserant@40: /* function that generates an IEC runtime error */ msousa@350: static inline void __iec_error(void) { etisserant@40: /* TODO... */ etisserant@40: fprintf(stderr, "IEC 61131-3 runtime error.\n"); etisserant@40: /*exit(1);*/ etisserant@40: } etisserant@40: msousa@350: /*******************************/ msousa@350: /* Time normalization function */ msousa@350: /*******************************/ etisserant@40: Edouard@254: static inline void __normalize_timespec (IEC_TIMESPEC *ts) { etisserant@40: if( ts->tv_nsec < -1000000000 || (( ts->tv_sec > 0 ) && ( ts->tv_nsec < 0 ))){ etisserant@40: ts->tv_sec--; etisserant@40: ts->tv_nsec += 1000000000; etisserant@40: } etisserant@40: if( ts->tv_nsec > 1000000000 || (( ts->tv_sec < 0 ) && ( ts->tv_nsec > 0 ))){ etisserant@40: ts->tv_sec++; etisserant@40: ts->tv_nsec -= 1000000000; etisserant@40: } etisserant@40: } etisserant@40: msousa@350: /**********************************************/ msousa@350: /* Time conversion to/from timespec functions */ msousa@350: /**********************************************/ msousa@350: Edouard@254: static inline IEC_TIMESPEC __time_to_timespec(int sign, double mseconds, double seconds, double minutes, double hours, double days) { Edouard@254: IEC_TIMESPEC ts; greg@180: etisserant@40: /* sign is 1 for positive values, -1 for negative time... */ etisserant@40: long double total_sec = ((days*24 + hours)*60 + minutes)*60 + seconds + mseconds/1e3; etisserant@40: if (sign >= 0) sign = 1; else sign = -1; etisserant@40: ts.tv_sec = sign * (long int)total_sec; etisserant@40: ts.tv_nsec = sign * (long int)((total_sec - ts.tv_sec)*1e9); etisserant@40: etisserant@40: return ts; etisserant@40: } etisserant@40: etisserant@40: Edouard@254: static inline IEC_TIMESPEC __tod_to_timespec(double seconds, double minutes, double hours) { Edouard@254: IEC_TIMESPEC ts; greg@180: etisserant@40: long double total_sec = (hours*60 + minutes)*60 + seconds; etisserant@40: ts.tv_sec = (long int)total_sec; etisserant@40: ts.tv_nsec = (long int)((total_sec - ts.tv_sec)*1e9); greg@180: etisserant@40: return ts; etisserant@40: } etisserant@40: Edouard@254: static inline IEC_TIMESPEC __date_to_timespec(int day, int month, int year) { Edouard@254: IEC_TIMESPEC ts; etisserant@40: struct tm broken_down_time; laurent@200: time_t epoch_seconds; etisserant@40: etisserant@40: broken_down_time.tm_sec = 0; etisserant@40: broken_down_time.tm_min = 0; etisserant@40: broken_down_time.tm_hour = 0; etisserant@40: broken_down_time.tm_mday = day; /* day of month, from 1 to 31 */ etisserant@40: broken_down_time.tm_mon = month - 1; /* month since January, in the range 0 to 11 */ etisserant@40: broken_down_time.tm_year = year - 1900; /* number of years since 1900 */ etisserant@40: laurent@200: epoch_seconds = mktime(&broken_down_time); /* determine number of seconds since the epoch, i.e. Jan 1st 1970 */ etisserant@40: etisserant@40: if ((time_t)(-1) == epoch_seconds) msousa@350: __iec_error(); etisserant@40: etisserant@40: ts.tv_sec = epoch_seconds; etisserant@40: ts.tv_nsec = 0; greg@180: etisserant@40: return ts; etisserant@40: } etisserant@40: Edouard@254: static inline IEC_TIMESPEC __dt_to_timespec(double seconds, double minutes, double hours, int day, int month, int year) { Edouard@254: IEC_TIMESPEC ts; laurent@200: struct tm broken_down_time; laurent@200: time_t epoch_seconds; greg@180: etisserant@40: long double total_sec = (hours*60 + minutes)*60 + seconds; etisserant@40: ts.tv_sec = (long int)total_sec; etisserant@40: ts.tv_nsec = (long int)((total_sec - ts.tv_sec)*1e9); etisserant@40: etisserant@40: broken_down_time.tm_sec = 0; etisserant@40: broken_down_time.tm_min = 0; etisserant@40: broken_down_time.tm_hour = 0; etisserant@40: broken_down_time.tm_mday = day; /* day of month, from 1 to 31 */ etisserant@40: broken_down_time.tm_mon = month - 1; /* month since January, in the range 0 to 11 */ etisserant@40: broken_down_time.tm_year = year - 1900; /* number of years since 1900 */ etisserant@40: laurent@200: epoch_seconds = mktime(&broken_down_time); /* determine number of seconds since the epoch, i.e. Jan 1st 1970 */ etisserant@40: if ((time_t)(-1) == epoch_seconds) msousa@350: __iec_error(); etisserant@40: etisserant@40: ts.tv_sec += epoch_seconds; etisserant@40: if (ts.tv_sec < epoch_seconds) etisserant@40: /* since the TOD is always positive, if the above happens then we had an overflow */ msousa@350: __iec_error(); etisserant@40: etisserant@40: return ts; etisserant@40: } etisserant@40: msousa@350: /*******************/ msousa@350: /* Time operations */ msousa@350: /*******************/ msousa@350: msousa@350: #define __time_cmp(t1, t2) (t2.tv_sec == t1.tv_sec ? t1.tv_nsec - t2.tv_nsec : t1.tv_sec - t2.tv_sec) msousa@350: msousa@350: static inline TIME __time_add(TIME IN1, TIME IN2){ etisserant@40: TIME res ={IN1.tv_sec + IN2.tv_sec, etisserant@40: IN1.tv_nsec + IN2.tv_nsec }; etisserant@40: __normalize_timespec(&res); etisserant@40: return res; etisserant@40: } msousa@350: static inline TIME __time_sub(TIME IN1, TIME IN2){ etisserant@40: TIME res ={IN1.tv_sec - IN2.tv_sec, etisserant@40: IN1.tv_nsec - IN2.tv_nsec }; etisserant@40: __normalize_timespec(&res); etisserant@40: return res; etisserant@40: } msousa@350: static inline TIME __time_mul(TIME IN1, LREAL IN2){ etisserant@40: LREAL s_f = IN1.tv_sec * IN2; etisserant@40: time_t s = s_f; etisserant@40: div_t ns = div((LREAL)IN1.tv_nsec * IN2, 1000000000); etisserant@40: TIME res = {s + ns.quot, etisserant@40: ns.rem + (s_f - s) * 1000000000 }; etisserant@40: __normalize_timespec(&res); etisserant@40: return res; etisserant@40: } msousa@350: static inline TIME __time_div(TIME IN1, LREAL IN2){ etisserant@40: LREAL s_f = IN1.tv_sec / IN2; etisserant@40: time_t s = s_f; etisserant@40: TIME res = {s, etisserant@40: IN1.tv_nsec / IN2 + (s_f - s) * 1000000000 }; etisserant@40: __normalize_timespec(&res); etisserant@40: return res; etisserant@40: } etisserant@40: etisserant@40: etisserant@40: /***************/ etisserant@40: /* Convertions */ etisserant@40: /***************/ laurent@314: /*****************/ laurent@314: /* REAL_TO_INT */ laurent@314: /*****************/ msousa@350: static inline LINT __real_round(LREAL IN) { laurent@314: return fmod(IN, 1) == 0 ? ((LINT)IN / 2) * 2 : (LINT)IN; laurent@314: } msousa@350: static inline LINT __preal_to_sint(LREAL IN) { laurent@314: return IN >= 0 ? __real_round(IN + 0.5) : __real_round(IN - 0.5); laurent@314: } msousa@350: static inline LINT __preal_to_uint(LREAL IN) { laurent@314: return IN >= 0 ? __real_round(IN + 0.5) : 0; laurent@314: } msousa@350: static inline LINT __real_to_sint(LREAL IN) {return (LINT)__preal_to_sint(IN);} msousa@350: static inline LWORD __real_to_bit(LREAL IN) {return (LWORD)__preal_to_uint(IN);} msousa@350: static inline ULINT __real_to_uint(LREAL IN) {return (ULINT)__preal_to_uint(IN);} msousa@350: etisserant@40: /***************/ etisserant@40: /* TO_STRING */ etisserant@40: /***************/ msousa@350: static inline STRING __bool_to_string(BOOL IN) { msousa@350: if(IN) return (STRING){4, "TRUE"}; etisserant@40: return (STRING){5,"FALSE"}; etisserant@40: } msousa@350: static inline STRING __bit_to_string(LWORD IN) { laurent@200: STRING res; laurent@200: res = __INIT_STRING; etisserant@114: res.len = snprintf((char*)res.body, STR_MAX_LEN, "16#%llx", IN); etisserant@40: if(res.len > STR_MAX_LEN) res.len = STR_MAX_LEN; etisserant@40: return res; etisserant@40: } msousa@350: static inline STRING __real_to_string(LREAL IN) { laurent@200: STRING res; laurent@200: res = __INIT_STRING; etisserant@114: res.len = snprintf((char*)res.body, STR_MAX_LEN, "%.10g", IN); etisserant@40: if(res.len > STR_MAX_LEN) res.len = STR_MAX_LEN; etisserant@40: return res; etisserant@40: } msousa@350: static inline STRING __sint_to_string(LINT IN) { laurent@200: STRING res; laurent@200: res = __INIT_STRING; etisserant@114: res.len = snprintf((char*)res.body, STR_MAX_LEN, "%lld", IN); etisserant@40: if(res.len > STR_MAX_LEN) res.len = STR_MAX_LEN; etisserant@40: return res; etisserant@40: } msousa@350: static inline STRING __uint_to_string(ULINT IN) { laurent@200: STRING res; laurent@200: res = __INIT_STRING; etisserant@114: res.len = snprintf((char*)res.body, STR_MAX_LEN, "%llu", IN); etisserant@40: if(res.len > STR_MAX_LEN) res.len = STR_MAX_LEN; etisserant@40: return res; etisserant@40: } etisserant@40: /***************/ etisserant@40: /* FROM_STRING */ etisserant@40: /***************/ msousa@350: static inline BOOL __string_to_bool(STRING IN) { etisserant@40: return IN.len == 5 ? !memcmp(&IN.body,"TRUE", IN.len) : 0; etisserant@40: } etisserant@40: msousa@350: static inline LINT __pstring_to_sint(STRING* IN) { etisserant@40: LINT res = 0; etisserant@40: __strlen_t l; etisserant@40: unsigned int shift = 0; greg@180: etisserant@40: if(IN->body[0]=='2' && IN->body[1]=='#'){ etisserant@40: /* 2#0101_1010_1011_1111 */ etisserant@40: for(l = IN->len - 1; l >= 2 && shift < 64; l--) etisserant@40: { etisserant@40: char c = IN->body[l]; etisserant@40: if( c >= '0' && c <= '1'){ etisserant@40: res |= ( c - '0') << shift; etisserant@40: shift += 1; etisserant@40: } etisserant@40: } etisserant@40: }else if(IN->body[0]=='8' && IN->body[1]=='#'){ etisserant@40: /* 8#1234_5665_4321 */ etisserant@40: for(l = IN->len - 1; l >= 2 && shift < 64; l--) etisserant@40: { etisserant@40: char c = IN->body[l]; etisserant@40: if( c >= '0' && c <= '7'){ etisserant@40: res |= ( c - '0') << shift; etisserant@40: shift += 3; etisserant@40: } etisserant@40: } etisserant@43: }else if(IN->body[0]=='1' && IN->body[1]=='6' && IN->body[2]=='#'){ etisserant@40: /* 16#1234_5678_9abc_DEFG */ etisserant@40: for(l = IN->len - 1; l >= 3 && shift < 64; l--) etisserant@40: { etisserant@40: char c = IN->body[l]; etisserant@40: if( c >= '0' && c <= '9'){ etisserant@43: res |= (LWORD)( c - '0') << shift; etisserant@40: shift += 4; etisserant@40: }else if( c >= 'a' && c <= 'f'){ etisserant@43: res |= (LWORD)( c - 'a' + 10 ) << shift; etisserant@40: shift += 4; etisserant@40: }else if( c >= 'A' && c <= 'F'){ etisserant@43: res |= (LWORD)( c - 'A' + 10 ) << shift; etisserant@40: shift += 4; etisserant@40: } etisserant@40: } etisserant@40: }else{ etisserant@40: /* -123456789 */ etisserant@40: LINT fac = IN->body[0] == '-' ? -1 : 1; etisserant@40: for(l = IN->len - 1; l >= 0 && shift < 20; l--) etisserant@40: { etisserant@40: char c = IN->body[l]; etisserant@40: if( c >= '0' && c <= '9'){ etisserant@40: res += ( c - '0') * fac; etisserant@40: fac *= 10; etisserant@40: shift += 1; etisserant@41: }else if( c >= '.' ){ /* reset value */ etisserant@41: res = 0; etisserant@163: fac = IN->body[0] == '-' ? -1 : 1; etisserant@41: shift = 0; greg@180: } etisserant@40: } etisserant@40: } etisserant@40: return res; etisserant@40: } etisserant@40: msousa@350: static inline LINT __string_to_sint(STRING IN) {return (LINT)__pstring_to_sint(&IN);} msousa@350: static inline LWORD __string_to_bit (STRING IN) {return (LWORD)__pstring_to_sint(&IN);} msousa@350: static inline ULINT __string_to_uint(STRING IN) {return (ULINT)__pstring_to_sint(&IN);} msousa@350: static inline LREAL __string_to_real(STRING IN) { msousa@350: __strlen_t l; msousa@350: l = IN.len; etisserant@40: /* search the dot */ etisserant@40: while(--l > 0 && IN.body[l] != '.'); etisserant@40: if(l != 0){ etisserant@40: return atof((const char *)&IN.body); etisserant@40: }else{ etisserant@40: return (LREAL)__pstring_to_sint(&IN); greg@180: } etisserant@40: } etisserant@40: etisserant@40: /***************/ etisserant@40: /* TO_TIME */ etisserant@40: /***************/ msousa@350: static inline TIME __int_to_time(LINT IN) {return (TIME){IN, 0};} msousa@350: static inline TIME __real_to_time(LREAL IN) {return (TIME){IN, (IN - (LINT)IN) * 1000000000};} msousa@350: static inline TIME __string_to_time(STRING IN){ msousa@350: __strlen_t l; etisserant@40: /* TODO : etisserant@40: * etisserant@40: * Duration literals without underlines: T#14ms T#-14ms T#14.7s T#14.7m etisserant@40: * short prefix T#14.7h t#14.7d t#25h15m etisserant@40: * t#5d14h12m18s3.5ms etisserant@40: * long prefix TIME#14ms TIME#-14ms time#14.7s etisserant@40: * Duration literals with underlines: etisserant@40: * short prefix t#25h_15m t#5d_14h_12m_18s_3.5ms etisserant@40: * long prefix TIME#25h_15m etisserant@40: * time#5d_14h_12m_18s_3.5ms etisserant@40: * etisserant@40: * Long prefix notation Short prefix notation etisserant@40: * DATE#1984-06-25 D#1984-06-25 etisserant@40: * date#1984-06-25 d#1984-06-25 etisserant@40: * TIME_OF_DAY#15:36:55.36 TOD#15:36:55.36 etisserant@40: * time_of_day#15:36:55.36 tod#15:36:55.36 etisserant@40: * DATE_AND_TIME#1984-06-25-15:36:55.36 DT#1984-06-25-15:36:55.36 etisserant@40: * date_and_time#1984-06-25-15:36:55.36 dt#1984-06-25-15:36:55.36 etisserant@40: * etisserant@40: */ etisserant@40: /* Quick hack : only transform seconds */ etisserant@40: /* search the dot */ msousa@350: l = IN.len; etisserant@40: while(--l > 0 && IN.body[l] != '.'); etisserant@40: if(l != 0){ etisserant@40: LREAL IN_val = atof((const char *)&IN.body); etisserant@40: return (TIME){IN_val, (IN_val - (LINT)IN_val)*1000000000}; etisserant@40: }else{ etisserant@40: return (TIME){__pstring_to_sint(&IN), 0}; etisserant@40: } etisserant@40: } etisserant@40: etisserant@40: /***************/ etisserant@40: /* FROM_TIME */ etisserant@40: /***************/ msousa@350: static inline LREAL __time_to_real(TIME IN){ etisserant@40: return (LREAL)IN.tv_sec + ((LREAL)IN.tv_nsec/1000000000); etisserant@40: } msousa@350: static inline LINT __time_to_int(TIME IN) {return IN.tv_sec;} msousa@350: static inline STRING __time_to_string(TIME IN){ laurent@200: STRING res; laurent@200: div_t days; etisserant@40: /*t#5d14h12m18s3.5ms*/ laurent@200: res = __INIT_STRING; laurent@200: days = div(IN.tv_sec ,86400); etisserant@40: if(!days.rem && IN.tv_nsec == 0){ etisserant@40: res.len = snprintf((char*)&res.body, STR_MAX_LEN, "T#%dd", days.quot); etisserant@40: }else{ etisserant@40: div_t hours = div(days.rem, 3600); etisserant@40: if(!hours.rem && IN.tv_nsec == 0){ etisserant@40: res.len = snprintf((char*)&res.body, STR_MAX_LEN, "T#%dd%dh", days.quot, hours.quot); etisserant@40: }else{ etisserant@40: div_t minuts = div(hours.rem, 60); etisserant@40: if(!minuts.rem && IN.tv_nsec == 0){ etisserant@40: res.len = snprintf((char*)&res.body, STR_MAX_LEN, "T#%dd%dh%dm", days.quot, hours.quot, minuts.quot); etisserant@40: }else{ etisserant@40: if(IN.tv_nsec == 0){ etisserant@40: res.len = snprintf((char*)&res.body, STR_MAX_LEN, "T#%dd%dh%dm%ds", days.quot, hours.quot, minuts.quot, minuts.rem); etisserant@40: }else{ etisserant@42: res.len = snprintf((char*)&res.body, STR_MAX_LEN, "T#%dd%dh%dm%ds%gms", days.quot, hours.quot, minuts.quot, minuts.rem, (LREAL)IN.tv_nsec / 1000000); etisserant@40: } etisserant@40: } etisserant@40: } etisserant@40: } etisserant@40: if(res.len > STR_MAX_LEN) res.len = STR_MAX_LEN; etisserant@40: return res; etisserant@40: } msousa@350: static inline STRING __date_to_string(DATE IN){ laurent@200: STRING res; laurent@200: struct tm* broken_down_time; laurent@200: time_t seconds; etisserant@40: /* D#1984-06-25 */ laurent@200: res = __INIT_STRING; laurent@200: seconds = IN.tv_sec; etisserant@57: if (NULL == (broken_down_time = localtime(&seconds))){ /* get the UTC (GMT) broken down time */ msousa@350: __iec_error(); etisserant@40: return (STRING){7,"D#ERROR"}; etisserant@40: } etisserant@57: res.len = snprintf((char*)&res.body, STR_MAX_LEN, "D#%d-%2.2d-%2.2d", broken_down_time->tm_year + 1900, broken_down_time->tm_mon + 1, broken_down_time->tm_mday); etisserant@40: if(res.len > STR_MAX_LEN) res.len = STR_MAX_LEN; etisserant@40: return res; etisserant@40: } msousa@350: static inline STRING __tod_to_string(TOD IN){ laurent@200: STRING res; laurent@200: struct tm* broken_down_time; laurent@200: time_t seconds; etisserant@40: /* TOD#15:36:55.36 */ laurent@200: res = __INIT_STRING; laurent@200: seconds = IN.tv_sec; etisserant@57: if (NULL == (broken_down_time = localtime(&seconds))){ /* get the UTC (GMT) broken down time */ msousa@350: __iec_error(); etisserant@40: return (STRING){9,"TOD#ERROR"}; etisserant@40: } etisserant@40: if(IN.tv_nsec == 0){ etisserant@57: res.len = snprintf((char*)&res.body, STR_MAX_LEN, "TOD#%2.2d:%2.2d:%d", broken_down_time->tm_hour, broken_down_time->tm_min, broken_down_time->tm_sec); etisserant@40: }else{ etisserant@57: res.len = snprintf((char*)&res.body, STR_MAX_LEN, "TOD#%2.2d:%2.2d:%g", broken_down_time->tm_hour, broken_down_time->tm_min, (LREAL)broken_down_time->tm_sec + (LREAL)IN.tv_nsec / 1e9); etisserant@40: } etisserant@40: if(res.len > STR_MAX_LEN) res.len = STR_MAX_LEN; etisserant@40: return res; etisserant@40: } msousa@350: static inline STRING __dt_to_string(DT IN){ laurent@200: STRING res; laurent@200: struct tm* broken_down_time; laurent@200: time_t seconds; etisserant@40: /* DT#1984-06-25-15:36:55.36 */ laurent@200: seconds = IN.tv_sec; etisserant@57: if (NULL == (broken_down_time = localtime(&seconds))){ /* get the UTC (GMT) broken down time */ msousa@350: __iec_error(); etisserant@40: return (STRING){8,"DT#ERROR"}; etisserant@40: } etisserant@40: if(IN.tv_nsec == 0){ etisserant@40: res.len = snprintf((char*)&res.body, STR_MAX_LEN, "DT#%d-%2.2d-%2.2d-%2.2d:%2.2d:%d", etisserant@57: broken_down_time->tm_year, etisserant@57: broken_down_time->tm_mon, etisserant@57: broken_down_time->tm_mday, etisserant@57: broken_down_time->tm_hour, etisserant@57: broken_down_time->tm_min, etisserant@57: broken_down_time->tm_sec); etisserant@40: }else{ etisserant@42: res.len = snprintf((char*)&res.body, STR_MAX_LEN, "DT#%d-%2.2d-%2.2d-%2.2d:%2.2d:%g", etisserant@57: broken_down_time->tm_year, etisserant@57: broken_down_time->tm_mon, etisserant@57: broken_down_time->tm_mday, etisserant@57: broken_down_time->tm_hour, etisserant@57: broken_down_time->tm_min, etisserant@57: (LREAL)broken_down_time->tm_sec + ((LREAL)IN.tv_nsec / 1e9)); etisserant@40: } etisserant@40: if(res.len > STR_MAX_LEN) res.len = STR_MAX_LEN; etisserant@40: return res; etisserant@40: } msousa@350: msousa@350: /**********************************************/ msousa@350: /* [ANY_DATE | TIME] _TO_ [ANY_DATE | TIME] */ msousa@350: /**********************************************/ msousa@350: msousa@350: static inline TOD __date_and_time_to_time_of_day(DT IN) {return (TOD){IN.tv_sec % 86400, IN.tv_nsec};} msousa@350: static inline DATE __date_and_time_to_date(DT IN){return (DATE){IN.tv_sec - (IN.tv_sec % (24*60*60)), 0};} msousa@350: msousa@350: /*****************/ msousa@350: /* FROM/TO BCD */ msousa@350: /*****************/ greg@180: #define __bcd_digit(fac) msousa@350: static inline ULINT __bcd_to_uint(LWORD IN){ greg@212: ULINT res; greg@212: ULINT i; greg@212: greg@212: res = IN & 0xf; greg@212: for(i = 10ULL; i <= 1000000000000000ULL; i *= 10){ greg@212: if(!(IN >>= 4)) greg@212: break; greg@212: res += (IN & 0xf) * i; greg@212: } greg@212: return res; greg@212: } greg@212: msousa@350: static inline LWORD __uint_to_bcd(ULINT IN){ greg@212: LWORD res; greg@212: USINT i; greg@212: greg@212: res = IN % 10; greg@212: for(i = 4; i<=60; i += 4){ greg@212: if(!(IN /= 10)) greg@212: break; greg@212: res |= (IN % 10) << i; greg@212: } greg@212: return res; etisserant@40: } etisserant@40: msousa@350: msousa@350: /************/ msousa@350: /* MOVE_* */ msousa@350: /************/ msousa@350: msousa@350: /* some helpful __move_[ANY] functions, used in the *_TO_** and MOVE standard functions */ msousa@350: /* e.g. __move_BOOL, __move_BYTE, __move_REAL, __move_TIME, ... */ msousa@350: #define __iec_(TYPENAME)\ msousa@350: static inline TYPENAME __move_##TYPENAME(TYPENAME op1) {return op1;} msousa@350: __ANY(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: msousa@350: /*****************************************************************/ msousa@350: /*****************************************************************/ msousa@350: /***** *****/ msousa@350: /***** IEC 61131-3 *****/ msousa@350: /***** S T A N D A R D F U N C T I O N S *****/ msousa@350: /***** *****/ msousa@350: /*****************************************************************/ msousa@350: /*****************************************************************/ msousa@350: msousa@350: /* NOTE: If you want to know what all these strange macros are doing, msousa@350: * just parse this file through a C preprocessor, and analyse the output! msousa@350: * $gcc -E iec_std_lib.h msousa@350: */ msousa@350: msousa@350: /* NOTE: We only define and declare the explicitly typed standard functions msousa@350: * (e.g., SIN_REAL, SIN_LREAL, ..., ADD_SINT, ADD_INT, ADD_LINT, ...) msousa@350: * We do not declare/define the overloaded functions msousa@350: * (SIN, ADD, ...). msousa@350: * When handling a call to an overloaded function, the iec2c compiler msousa@350: * will determine in stage3 the data type of the parameter being passed, msousa@350: * and in stage4 generate the C code to call the correct msousa@350: * typed standard function. msousa@350: */ msousa@350: msousa@350: /* NOTE on explicit typing of: msousa@350: * - Table 25 - Standard bit shift functions msousa@350: * - Table 29 - Character string Functions msousa@350: * msousa@350: * In section 2.5.1.4 (Typing, overloading, and type conversion) of the IEC 61131-3 (version 2) msousa@350: * of the standard, it is stated: msousa@350: * "A standard function, [...] is said to be overloaded when it msousa@350: * can operate on input data elements of various types within a generic type designator as defined in msousa@350: * 2.3.2. For instance, an overloaded addition function on generic type ANY_NUM can operate on data msousa@350: * of types LREAL, REAL, DINT, INT, and SINT." msousa@350: * [...] msousa@350: * "When a function which normally represents an overloaded operator is to be typed, i.e., the types msousa@350: * of its inputs and outputs restricted to a particular elementary or derived data type as defined in msousa@350: * 2.3, this shall be done by appending an "underline" character followed by the required type, as msousa@350: * shown in table 21." msousa@350: * msousa@350: * However, this explanation (as well as the example in table 21) only refers to functions where the same msousa@350: * generic data type is used for the single input and the output parameter. msousa@350: * How can we create explicitly types functions when this is not the case? msousa@350: * It does not seem to be covered by the standard. msousa@350: * msousa@350: * For this reason, we do not define the LEN_SINT, LEN_INT, LEN_STRING, LEN_[ANY_INT], LEN_[ANY_STRING] functions... msousa@350: */ msousa@350: msousa@350: msousa@350: /********************/ msousa@350: /* EN/ENO PARAMS */ msousa@350: /********************/ msousa@350: msousa@350: #define EN_ENO_PARAMS BOOL EN, BOOL *ENO msousa@350: msousa@350: #define TEST_EN(TYPENAME)\ msousa@350: if (!EN) {\ msousa@350: if (ENO != NULL)\ msousa@350: *ENO = __BOOL_LITERAL(FALSE);\ msousa@350: return __INIT_##TYPENAME;\ msousa@350: }\ msousa@350: else if (ENO != NULL)\ msousa@350: *ENO = __BOOL_LITERAL(TRUE); msousa@350: msousa@350: #define TEST_EN_COND(TYPENAME, COND)\ msousa@350: if (!EN || (COND)) {\ msousa@350: if (ENO != NULL)\ msousa@350: *ENO = __BOOL_LITERAL(FALSE);\ msousa@350: return __INIT_##TYPENAME;\ msousa@350: }\ msousa@350: else if (ENO != NULL)\ msousa@350: *ENO = __BOOL_LITERAL(TRUE); msousa@350: msousa@350: msousa@350: msousa@350: /*****************************************/ msousa@350: /*****************************************/ msousa@350: /* 2.5.1.5.1 Type Conversion Functions */ msousa@350: /*****************************************/ msousa@350: /*****************************************/ msousa@350: msousa@350: #define __convert_type(from_TYPENAME,to_TYPENAME, oper) \ msousa@350: static inline to_TYPENAME from_TYPENAME##_TO_##to_TYPENAME(EN_ENO_PARAMS, from_TYPENAME op){\ msousa@350: TEST_EN(to_TYPENAME)\ msousa@350: return (to_TYPENAME)oper(op);\ msousa@350: } msousa@350: msousa@350: msousa@350: #define __to_anynum_(from_TYPENAME) __ANY_NUM_1(__iec_,from_TYPENAME) msousa@350: #define __to_anyint_(from_TYPENAME) __ANY_INT_1(__iec_,from_TYPENAME) msousa@350: #define __to_anybit_(from_TYPENAME) __ANY_BIT_1(__iec_,from_TYPENAME) msousa@350: #define __to_anynbit_(from_TYPENAME) __ANY_NBIT_1(__iec_,from_TYPENAME) msousa@350: #define __to_anysint_(from_TYPENAME) __ANY_SINT_1(__iec_,from_TYPENAME) msousa@350: #define __to_anyuint_(from_TYPENAME) __ANY_UINT_1(__iec_,from_TYPENAME) msousa@350: #define __to_anyreal_(from_TYPENAME) __ANY_REAL_1(__iec_,from_TYPENAME) msousa@350: #define __to_anydate_(from_TYPENAME) __ANY_DATE_1(__iec_,from_TYPENAME) msousa@350: msousa@350: /******** [ANY_BIT]_TO_[ANY_NUM | ANT_BIT] ************/ msousa@350: #define __iec_(to_TYPENAME,from_TYPENAME) __convert_type(from_TYPENAME, to_TYPENAME, __move_##to_TYPENAME) msousa@350: __ANY_BIT(__to_anynum_) msousa@350: __ANY_BIT(__to_anybit_) msousa@350: #undef __iec_ msousa@350: msousa@350: /******** [ANY_INT]_TO_[ANY_NUM | ANT_BIT] ************/ msousa@350: #define __iec_(to_TYPENAME,from_TYPENAME) __convert_type(from_TYPENAME, to_TYPENAME, __move_##to_TYPENAME) msousa@350: __ANY_INT(__to_anynum_) msousa@350: __ANY_INT(__to_anybit_) msousa@350: #undef __iec_ msousa@350: msousa@350: /******** [ANY_REAL]_TO_[ANY_BIT] ************/ msousa@350: #define __iec_(to_TYPENAME,from_TYPENAME) __convert_type(from_TYPENAME, to_TYPENAME, __real_to_bit) msousa@350: __ANY_REAL(__to_anybit_) msousa@350: #undef __iec_ msousa@350: msousa@350: /******** [ANY_REAL]_TO_[ANY_INT] ************/ msousa@350: #define __iec_(to_TYPENAME,from_TYPENAME) __convert_type(from_TYPENAME, to_TYPENAME, __real_to_sint) msousa@350: __ANY_REAL(__to_anysint_) msousa@350: #undef __iec_ msousa@350: #define __iec_(to_TYPENAME,from_TYPENAME) __convert_type(from_TYPENAME, to_TYPENAME, __real_to_uint) msousa@350: __ANY_REAL(__to_anyuint_) msousa@350: #undef __iec_ msousa@350: msousa@350: /******** [ANY_REAL]_TO_[ANY_REAL] ************/ msousa@350: #define __iec_(to_TYPENAME,from_TYPENAME) __convert_type(from_TYPENAME, to_TYPENAME, __move_##to_TYPENAME) msousa@350: __ANY_REAL(__to_anyreal_) msousa@350: #undef __iec_ msousa@350: msousa@350: /******** [ANY_BIT | ANY_INT]_TO_[TIME | ANY_DATE] ************/ msousa@350: #define __iec_(from_TYPENAME) __convert_type(from_TYPENAME, TIME, __int_to_time) msousa@350: __ANY_BIT(__iec_) msousa@350: __ANY_INT(__iec_) msousa@350: #undef __iec_ msousa@350: #define __iec_(to_TYPENAME,from_TYPENAME) __convert_type(from_TYPENAME, to_TYPENAME, __int_to_time) msousa@350: __ANY_BIT(__to_anydate_) msousa@350: __ANY_INT(__to_anydate_) msousa@350: #undef __iec_ msousa@350: msousa@350: /******** [ANY_REAL]_TO_[TIME | ANY_DATE] ************/ msousa@350: #define __iec_(from_TYPENAME) __convert_type(from_TYPENAME, TIME, __real_to_time) msousa@350: __ANY_REAL(__iec_) msousa@350: #undef __iec_ msousa@350: #define __iec_(to_TYPENAME,from_TYPENAME) __convert_type(from_TYPENAME, to_TYPENAME, __real_to_time) msousa@350: __ANY_REAL(__to_anydate_) msousa@350: #undef __iec_ msousa@350: msousa@350: /******** [TIME | ANY_DATE]_TO_[ANY_BIT | ANY_INT] ************/ msousa@350: #define __iec_(to_TYPENAME,from_TYPENAME) __convert_type(from_TYPENAME, to_TYPENAME, __time_to_int) msousa@350: __to_anyint_(TIME) msousa@350: __to_anybit_(TIME) msousa@350: __ANY_DATE(__to_anyint_) msousa@350: __ANY_DATE(__to_anybit_) msousa@350: #undef __iec_ msousa@350: msousa@350: /******** [TIME | ANY_DATE]_TO_[ANY_REAL] ************/ msousa@350: #define __iec_(to_TYPENAME,from_TYPENAME) __convert_type(from_TYPENAME, to_TYPENAME, __time_to_real) msousa@350: __to_anyreal_(TIME) msousa@350: __ANY_DATE(__to_anyreal_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: /******** [ANY_DATE]_TO_[ANY_DATE | TIME] ************/ msousa@350: /* Not supported: DT_TO_TIME */ msousa@350: __convert_type(DT, DATE, __date_and_time_to_date) msousa@350: __convert_type(DT, DT, __move_DT) msousa@350: __convert_type(DT, TOD, __date_and_time_to_time_of_day) msousa@350: /* Not supported: DATE_TO_TIME */ msousa@350: __convert_type(DATE, DATE, __move_DATE) msousa@350: /* Not supported: DATE_TO_DT */ msousa@350: /* Not supported: DATE_TO_TOD */ msousa@350: /* Not supported: TOD_TO_TIME */ msousa@350: /* Not supported: TOD_TO_DATE */ msousa@350: /* Not supported: TOD_TO_DT */ msousa@350: __convert_type(TOD, TOD, __move_TOD) msousa@350: msousa@350: msousa@350: /******** TIME_TO_[ANY_DATE] ************/ msousa@350: /* Not supported: TIME_TO_DATE */ msousa@350: /* Not supported: TIME_TO_DT */ msousa@350: /* Not supported: TIME_TO_TOD */ msousa@350: msousa@350: /******** TIME_TO_TIME ************/ msousa@350: __convert_type(TIME, TIME, __move_TIME) msousa@350: msousa@350: msousa@350: /******** [ANY_BIT]_TO_STRING ************/ msousa@350: __convert_type(BOOL, STRING, __bool_to_string) msousa@350: #define __iec_(from_TYPENAME) __convert_type(from_TYPENAME, STRING, __bit_to_string) msousa@350: __ANY_NBIT(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /******** [ANY_INT]_TO_STRING ************/ msousa@350: #define __iec_(from_TYPENAME) __convert_type(from_TYPENAME, STRING, __sint_to_string) msousa@350: __ANY_SINT(__iec_) msousa@350: #undef __iec_ msousa@350: #define __iec_(from_TYPENAME) __convert_type(from_TYPENAME, STRING, __uint_to_string) msousa@350: __ANY_UINT(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /******** [ANY_REAL]_TO_STRING ************/ msousa@350: #define __iec_(from_TYPENAME) __convert_type(from_TYPENAME, STRING, __real_to_string) msousa@350: __ANY_REAL(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /******** [ANY_DATE]_TO_STRING ************/ msousa@350: __convert_type(DATE, STRING, __date_to_string) msousa@350: __convert_type(DT, STRING, __dt_to_string) msousa@350: __convert_type(TOD, STRING, __tod_to_string) msousa@350: msousa@350: /******** TIME_TO_STRING ************/ msousa@350: __convert_type(TIME, STRING, __time_to_string) msousa@350: msousa@350: msousa@350: /******** STRING_TO_[ANY_BIT] ************/ msousa@350: __convert_type(STRING, BOOL, __string_to_bool) msousa@350: #define __iec_(to_TYPENAME) __convert_type(STRING, to_TYPENAME, __string_to_bit) msousa@350: __ANY_NBIT(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /******** STRING_TO_[ANY_INT] ************/ msousa@350: #define __iec_(to_TYPENAME) __convert_type(STRING, to_TYPENAME, __string_to_sint) msousa@350: __ANY_SINT(__iec_) msousa@350: #undef __iec_ msousa@350: #define __iec_(to_TYPENAME) __convert_type(STRING, to_TYPENAME, __string_to_uint) msousa@350: __ANY_UINT(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /******** STRING_TO_[ANY_REAL] ************/ msousa@350: #define __iec_(to_TYPENAME) __convert_type(STRING, to_TYPENAME, __string_to_real) msousa@350: __ANY_REAL(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /******** STRING_TO_[ANY_DATE] ************/ msousa@350: #define __iec_(to_TYPENAME) __convert_type(STRING, to_TYPENAME, __string_to_time) msousa@350: __ANY_DATE(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /******** STRING_TO_TIME ************/ msousa@350: __convert_type(STRING, TIME, __string_to_time) msousa@350: msousa@350: msousa@350: /******** TRUNC ************/ msousa@350: #define __iec_(to_TYPENAME,from_TYPENAME) \ msousa@350: static inline to_TYPENAME TRUNC__##to_TYPENAME##__##from_TYPENAME(EN_ENO_PARAMS, from_TYPENAME op){\ msousa@350: TEST_EN(to_TYPENAME)\ msousa@350: return (to_TYPENAME)__move_##to_TYPENAME(op);\ msousa@350: } msousa@350: __ANY_REAL(__to_anyint_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: /******** _TO_BCD ************/ msousa@350: #define __iec_(to_TYPENAME,from_TYPENAME) \ msousa@350: static inline to_TYPENAME from_TYPENAME##_TO_BCD_##to_TYPENAME(EN_ENO_PARAMS, from_TYPENAME op){\ msousa@350: TEST_EN(to_TYPENAME)\ msousa@350: return (to_TYPENAME)__uint_to_bcd(op);\ msousa@350: } msousa@350: __ANY_UINT(__to_anynbit_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: /******** BCD_TO_ ************/ msousa@350: #define __iec_(to_TYPENAME,from_TYPENAME) \ msousa@350: static inline to_TYPENAME from_TYPENAME##_BCD_TO_##to_TYPENAME(EN_ENO_PARAMS, from_TYPENAME op){\ msousa@350: TEST_EN(to_TYPENAME)\ msousa@350: return (to_TYPENAME)__bcd_to_uint(op);\ msousa@350: } msousa@350: __ANY_NBIT(__to_anyuint_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: /***********************************/ msousa@350: /***********************************/ msousa@350: /* 2.5.1.5.2 Numerical Functions */ msousa@350: /***********************************/ msousa@350: /***********************************/ msousa@350: Edouard@271: /* workaround for va-args limitation on shorter than int params */ etisserant@40: #define VA_ARGS_REAL LREAL etisserant@40: #define VA_ARGS_LREAL LREAL etisserant@40: #define VA_ARGS_SINT DINT etisserant@40: #define VA_ARGS_INT DINT etisserant@40: #define VA_ARGS_DINT DINT etisserant@40: #define VA_ARGS_LINT LINT etisserant@40: #define VA_ARGS_USINT UDINT etisserant@40: #define VA_ARGS_UINT UDINT etisserant@40: #define VA_ARGS_UDINT UDINT etisserant@40: #define VA_ARGS_ULINT ULINT etisserant@40: #define VA_ARGS_TIME TIME etisserant@40: #define VA_ARGS_BOOL DWORD etisserant@40: #define VA_ARGS_BYTE DWORD etisserant@40: #define VA_ARGS_WORD DWORD etisserant@40: #define VA_ARGS_DWORD DWORD etisserant@40: #define VA_ARGS_LWORD LWORD etisserant@40: #define VA_ARGS_STRING STRING etisserant@40: #define VA_ARGS_WSTRING WSTRING etisserant@40: #define VA_ARGS_DATE DATE etisserant@40: #define VA_ARGS_TOD TOD etisserant@40: #define VA_ARGS_DT DT etisserant@40: msousa@350: msousa@350: #define __numeric(fname,TYPENAME, FUNC) \ msousa@350: static inline TYPENAME fname##TYPENAME(EN_ENO_PARAMS, TYPENAME op){\ msousa@350: TEST_EN(TYPENAME)\ msousa@350: return FUNC(op);\ msousa@350: } msousa@350: msousa@350: /******************************************************************/ msousa@350: /*** Table 23 - Standard functions of one numeric variable ***/ msousa@350: /******************************************************************/ msousa@350: msousa@350: /**************/ msousa@350: /* ABS */ msousa@350: /**************/ msousa@350: /* explicitly typed function */ msousa@350: #define __iec_(TYPENAME) \ msousa@350: static inline TYPENAME ABS_##TYPENAME(EN_ENO_PARAMS, TYPENAME op){\ msousa@350: TEST_EN(TYPENAME)\ msousa@350: if (op < 0)\ msousa@350: return -op;\ msousa@350: return op;\ msousa@350: } msousa@350: __ANY_REAL(__iec_) msousa@350: __ANY_SINT(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: #define __iec_(TYPENAME) \ msousa@350: static inline TYPENAME ABS_##TYPENAME(EN_ENO_PARAMS, TYPENAME op){\ msousa@350: TEST_EN(TYPENAME)\ msousa@350: return op;\ msousa@350: } msousa@350: __ANY_UINT(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /* overloaded function */ msousa@350: #define __iec_(TYPENAME) \ msousa@350: static inline TYPENAME ABS__##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, TYPENAME op){\ msousa@350: TEST_EN(TYPENAME)\ msousa@350: if (op < 0)\ msousa@350: return -op;\ msousa@350: return op;\ msousa@350: } msousa@350: __ANY_REAL(__iec_) msousa@350: __ANY_SINT(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: #define __iec_(TYPENAME) \ msousa@350: static inline TYPENAME ABS__##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, TYPENAME op){\ msousa@350: TEST_EN(TYPENAME)\ msousa@350: return op;\ msousa@350: } msousa@350: __ANY_UINT(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: /**************/ msousa@350: /* SQRT */ msousa@350: /**************/ msousa@350: /* explicitly typed function */ msousa@350: #define __iec_(TYPENAME) \ msousa@350: __numeric(SQRT_, TYPENAME, sqrt) /* explicitly typed function */\ msousa@350: __numeric(SQRT__##TYPENAME##__, TYPENAME, sqrt) /* overloaded function */ msousa@350: __ANY_REAL(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /**************/ msousa@350: /* LN */ msousa@350: /**************/ msousa@350: #define __iec_(TYPENAME) \ msousa@350: __numeric(LN_, TYPENAME, log) /* explicitly typed function */\ msousa@350: __numeric(LN__##TYPENAME##__, TYPENAME, log) /* overloaded function */ msousa@350: __ANY_REAL(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: /**************/ msousa@350: /* LOG */ msousa@350: /**************/ msousa@350: #define __iec_(TYPENAME) \ msousa@350: __numeric(LOG_, TYPENAME, log10) /* explicitly typed function */\ msousa@350: __numeric(LOG__##TYPENAME##__, TYPENAME, log10) /* overloaded function */ msousa@350: __ANY_REAL(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /**************/ msousa@350: /* EXP */ msousa@350: /**************/ msousa@350: #define __iec_(TYPENAME) \ msousa@350: __numeric(EXP_, TYPENAME, exp) /* explicitly typed function */\ msousa@350: __numeric(EXP__##TYPENAME##__, TYPENAME, exp) /* overloaded function */ msousa@350: __ANY_REAL(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: /**************/ msousa@350: /* SIN */ msousa@350: /**************/ msousa@350: #define __iec_(TYPENAME) \ msousa@350: __numeric(SIN_, TYPENAME, sin) /* explicitly typed function */\ msousa@350: __numeric(SIN__##TYPENAME##__, TYPENAME, sin) /* overloaded function */ msousa@350: __ANY_REAL(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: /**************/ msousa@350: /* COS */ msousa@350: /**************/ msousa@350: #define __iec_(TYPENAME) \ msousa@350: __numeric(COS_, TYPENAME, cos) /* explicitly typed function */\ msousa@350: __numeric(COS__##TYPENAME##__, TYPENAME, cos) /* overloaded function */ msousa@350: __ANY_REAL(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /**************/ msousa@350: /* TAN */ msousa@350: /**************/ msousa@350: #define __iec_(TYPENAME) \ msousa@350: __numeric(TAN_, TYPENAME, tan) /* explicitly typed function */\ msousa@350: __numeric(TAN__##TYPENAME##__, TYPENAME, tan) /* overloaded function */ msousa@350: __ANY_REAL(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: /**************/ msousa@350: /* ASIN */ msousa@350: /**************/ msousa@350: #define __iec_(TYPENAME) \ msousa@350: __numeric(ASIN_, TYPENAME, asin) /* explicitly typed function */\ msousa@350: __numeric(ASIN__##TYPENAME##__, TYPENAME, asin) /* overloaded function */ msousa@350: __ANY_REAL(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /**************/ msousa@350: /* ACOS */ msousa@350: /**************/ msousa@350: #define __iec_(TYPENAME) \ msousa@350: __numeric(ACOS_, TYPENAME, acos) /* explicitly typed function */\ msousa@350: __numeric(ACOS__##TYPENAME##__, TYPENAME, acos) /* overloaded function */ msousa@350: __ANY_REAL(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /**************/ msousa@350: /* ATAN */ msousa@350: /**************/ msousa@350: #define __iec_(TYPENAME) \ msousa@350: __numeric(ATAN_, TYPENAME, atan) /* explicitly typed function */\ msousa@350: __numeric(ATAN__##TYPENAME##__, TYPENAME, atan) /* overloaded function */ msousa@350: __ANY_REAL(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: msousa@350: /*****************************************************/ msousa@350: /*** Table 24 - Standard arithmetic functions ***/ msousa@350: /*****************************************************/ msousa@350: /* msousa@350: Unfortunately, the following does not work!! msousa@350: msousa@350: #define NUMARGS(...) (sizeof((int[]){__VA_ARGS__})/sizeof(int)) msousa@350: lbessard@149: #define __arith_expand(fname,TYPENAME, OP) \ msousa@350: #define fname(EN, ENO, ...) fname__(EN, ENO, NUMARGS(__VA_ARGS__), __VA_ARGS__)\ msousa@350: static inline TYPENAME fname__(EN_ENO_PARAMS, UINT param_count, TYPENAME op1, ...){\ msousa@350: va_list ap;\ msousa@350: UINT i;\ msousa@350: TEST_EN(TYPENAME)\ msousa@350: \ msousa@350: va_start (ap, op1); \ msousa@350: \ msousa@350: for (i = 0; i < param_count - 1; i++){\ msousa@350: op1 = op1 OP va_arg (ap, VA_ARGS_##TYPENAME);\ msousa@350: }\ msousa@350: \ msousa@350: va_end (ap); \ msousa@350: return op1;\ msousa@350: } msousa@350: */ msousa@350: msousa@350: msousa@350: #define __arith_expand(fname,TYPENAME, OP) \ msousa@350: static inline TYPENAME fname(EN_ENO_PARAMS, UINT param_count, TYPENAME op1, ...){\ lbessard@149: va_list ap;\ lbessard@149: UINT i;\ laurent@200: TEST_EN(TYPENAME)\ lbessard@149: \ lbessard@149: va_start (ap, op1); /* Initialize the argument list. */\ lbessard@149: \ lbessard@149: for (i = 0; i < param_count - 1; i++){\ lbessard@149: op1 = op1 OP va_arg (ap, VA_ARGS_##TYPENAME);\ lbessard@149: }\ lbessard@149: \ lbessard@149: va_end (ap); /* Clean up. */\ lbessard@149: return op1;\ lbessard@149: } lbessard@149: lbessard@149: #define __arith_static(fname,TYPENAME, OP) \ msousa@350: static inline TYPENAME fname(EN_ENO_PARAMS, TYPENAME op1, TYPENAME op2){\ lbessard@149: TEST_EN(TYPENAME)\ lbessard@149: return op1 OP op2;\ lbessard@149: } lbessard@149: msousa@350: /**************/ msousa@350: /* ADD */ msousa@350: /**************/ msousa@350: #define __iec_(TYPENAME) \ msousa@350: __arith_expand(ADD_##TYPENAME, TYPENAME, +) /* explicitly typed function */\ msousa@350: __arith_expand(ADD__##TYPENAME##__##TYPENAME, TYPENAME, +) /* overloaded function */ msousa@350: __ANY_NUM(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /**************/ msousa@350: /* MUL */ msousa@350: /**************/ msousa@350: #define __iec_(TYPENAME) \ msousa@350: __arith_expand(MUL_##TYPENAME, TYPENAME, *) /* explicitly typed function */\ msousa@350: __arith_expand(MUL__##TYPENAME##__##TYPENAME, TYPENAME, *) /* overloaded function */ msousa@350: __ANY_NUM(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /**************/ msousa@350: /* SUB */ msousa@350: /**************/ msousa@350: #define __iec_(TYPENAME) \ msousa@350: __arith_expand(SUB_##TYPENAME, TYPENAME, -) /* explicitly typed function */\ msousa@350: __arith_expand(SUB__##TYPENAME##__##TYPENAME##__##TYPENAME, TYPENAME, -) /* overloaded function */ msousa@350: __ANY_NUM(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /**************/ msousa@350: /* DIV */ msousa@350: /**************/ msousa@350: /* The explicitly typed standard functions */ msousa@350: #define __iec_(TYPENAME)\ msousa@350: static inline TYPENAME DIV_##TYPENAME(EN_ENO_PARAMS, TYPENAME op1, TYPENAME op2){\ lbessard@149: TEST_EN_COND(TYPENAME, op2 == 0)\ lbessard@149: return op1 / op2;\ lbessard@149: } msousa@350: __ANY_NUM(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /* The overloaded standard functions */ msousa@350: #define __iec_(TYPENAME)\ msousa@350: static inline TYPENAME DIV__##TYPENAME##__##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, TYPENAME op1, TYPENAME op2){\ msousa@350: TEST_EN_COND(TYPENAME, op2 == 0)\ msousa@350: return op1 / op2;\ msousa@350: } msousa@350: __ANY_NUM(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /**************/ msousa@350: /* MOD */ msousa@350: /**************/ msousa@350: /* The explicitly typed standard functions */ msousa@350: #define __iec_(TYPENAME)\ msousa@350: __arith_expand(MOD_##TYPENAME, TYPENAME, %) /* explicitly typed function */\ msousa@350: __arith_expand(MOD__##TYPENAME##__##TYPENAME##__##TYPENAME, TYPENAME, %) /* overloaded function */ msousa@350: __ANY_INT(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /**************/ msousa@350: /* EXPT */ msousa@350: /**************/ msousa@350: /* overloaded function */ msousa@350: #define __iec_(in1_TYPENAME,in2_TYPENAME) \ msousa@350: static inline in1_TYPENAME EXPT__##in1_TYPENAME##__##in1_TYPENAME##__##in2_TYPENAME\ msousa@350: (EN_ENO_PARAMS, in1_TYPENAME IN1, in2_TYPENAME IN2){\ msousa@350: TEST_EN(in1_TYPENAME)\ msousa@350: return pow(IN1, IN2);\ msousa@350: } msousa@350: #define __in1_anyreal_(in2_TYPENAME) __ANY_REAL_1(__iec_,in2_TYPENAME) msousa@350: __ANY_NUM(__in1_anyreal_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: msousa@350: /***************/ msousa@350: /* MOVE */ msousa@350: /***************/ msousa@350: /* The explicitly typed standard functions */ msousa@350: #define __iec_(TYPENAME)\ msousa@350: static inline TYPENAME MOVE_##TYPENAME(EN_ENO_PARAMS, TYPENAME op1){\ msousa@350: TEST_EN(TYPENAME)\ msousa@350: return op1;\ msousa@350: } msousa@350: __ANY(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /* Overloaded function */ msousa@350: #define __iec_(TYPENAME)\ msousa@350: static inline TYPENAME MOVE__##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, TYPENAME op1){\ msousa@350: TEST_EN(TYPENAME)\ msousa@350: return op1;\ msousa@350: } msousa@350: __ANY(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: msousa@350: msousa@350: msousa@350: msousa@350: /***********************************/ msousa@350: /***********************************/ msousa@350: /* 2.5.1.5.3 Bit String Functions */ msousa@350: /***********************************/ msousa@350: /***********************************/ msousa@350: msousa@350: /****************************************************/ msousa@350: /*** Table 25 - Standard bit shift functions ***/ msousa@350: /****************************************************/ msousa@350: msousa@350: /* We do not delcare explcitly typed versions of the functions in table 25. msousa@350: * See note above regarding explicitly typed functions for more details. msousa@350: */ msousa@350: #define __in1_anynbit_(in2_TYPENAME) __ANY_NBIT_1(__iec_,in2_TYPENAME) msousa@350: msousa@350: #define __shift_(fname, in1_TYPENAME, in2_TYPENAME, OP)\ msousa@350: static inline in1_TYPENAME fname(EN_ENO_PARAMS, in1_TYPENAME IN, in2_TYPENAME N) {\ msousa@350: TEST_EN(in1_TYPENAME)\ msousa@350: return IN OP N;\ msousa@350: } msousa@350: msousa@350: /**************/ msousa@350: /* SHL */ msousa@350: /**************/ msousa@350: #define __iec_(TYPENAME) \ msousa@350: /* Overloaded function */\ msousa@350: static inline BOOL SHL__BOOL__##TYPENAME(EN_ENO_PARAMS, BOOL IN, TYPENAME N) { \ msousa@350: TEST_EN(BOOL);\ msousa@350: return (N==0)? IN : __INIT_BOOL; /* shifting by N>1 will always introduce a 0 */\ msousa@350: } msousa@350: __ANY_INT(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: #define __iec_(in1_TYPENAME,in2_TYPENAME) \ msousa@350: __shift_(SHL__##in1_TYPENAME##__##in1_TYPENAME##__##in2_TYPENAME, in1_TYPENAME, in2_TYPENAME, << )/* Overloaded function */ msousa@350: __ANY_INT(__in1_anynbit_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: /**************/ msousa@350: /* SHR */ msousa@350: /**************/ msousa@350: #define __iec_(TYPENAME) \ msousa@350: /* Overloaded function */\ msousa@350: static inline BOOL SHR__BOOL__##TYPENAME(EN_ENO_PARAMS, BOOL IN, TYPENAME N) { \ msousa@350: TEST_EN(BOOL);\ msousa@350: return (N==0)? IN : __INIT_BOOL; /* shifting by N>1 will always introduce a 0 */\ msousa@350: } msousa@350: __ANY_INT(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: #define __iec_(in1_TYPENAME,in2_TYPENAME) \ msousa@350: __shift_(SHR__##in1_TYPENAME##__##in1_TYPENAME##__##in2_TYPENAME, in1_TYPENAME, in2_TYPENAME, >> )/* Overloaded function */ msousa@350: __ANY_INT(__in1_anynbit_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: /**************/ msousa@350: /* ROR */ msousa@350: /**************/ msousa@350: #define __iec_(TYPENAME) \ msousa@350: /* Overloaded function */\ msousa@350: static inline BOOL ROR__BOOL__##TYPENAME(EN_ENO_PARAMS, BOOL IN, TYPENAME N) { \ msousa@350: TEST_EN(BOOL);\ msousa@350: return IN; /* rotating a single bit by any value N will not change that bit! */\ msousa@350: } msousa@350: __ANY_INT(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: #define __iec_(in1_TYPENAME,in2_TYPENAME) \ msousa@350: static inline in1_TYPENAME ROR__##in1_TYPENAME##__##in1_TYPENAME##__##in2_TYPENAME(EN_ENO_PARAMS, in1_TYPENAME IN, in2_TYPENAME N){\ msousa@350: TEST_EN(in1_TYPENAME)\ msousa@350: N %= 8*sizeof(in1_TYPENAME);\ msousa@350: return (IN >> N) | (IN << (8*sizeof(in1_TYPENAME)-N));\ msousa@350: } msousa@350: __ANY_INT(__in1_anynbit_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: /**************/ msousa@350: /* ROL */ msousa@350: /**************/ msousa@350: #define __iec_(TYPENAME) \ msousa@350: /* Overloaded function */\ msousa@350: static inline BOOL ROL__BOOL__##TYPENAME(EN_ENO_PARAMS, BOOL IN, TYPENAME N) { \ msousa@350: TEST_EN(BOOL);\ msousa@350: return IN; /* rotating a single bit by any value N will not change that bit! */\ msousa@350: } msousa@350: __ANY_INT(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: #define __iec_(in1_TYPENAME,in2_TYPENAME) \ msousa@350: static inline in1_TYPENAME ROL__##in1_TYPENAME##__##in1_TYPENAME##__##in2_TYPENAME(EN_ENO_PARAMS, in1_TYPENAME IN, in2_TYPENAME N){\ msousa@350: TEST_EN(in1_TYPENAME)\ msousa@350: N %= 8*sizeof(in1_TYPENAME);\ msousa@350: return (IN << N) | (IN >> (8*sizeof(in1_TYPENAME)-N));\ msousa@350: } msousa@350: __ANY_INT(__in1_anynbit_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: msousa@350: /*********************/ msousa@350: /*** Table 26 ***/ msousa@350: /*********************/ msousa@350: msousa@350: /**************/ msousa@350: /* AND */ msousa@350: /**************/ msousa@350: __arith_expand(AND_BOOL, BOOL, && ) /* The explicitly typed standard functions */ msousa@350: __arith_expand(AND__BOOL__BOOL, BOOL, && ) /* Overloaded function */ msousa@350: msousa@350: #define __iec_(TYPENAME) \ msousa@350: __arith_expand(AND_##TYPENAME, TYPENAME, &) /* The explicitly typed standard functions */\ msousa@350: __arith_expand(AND__##TYPENAME##__##TYPENAME, TYPENAME, &) /* Overloaded function */ msousa@350: __ANY_NBIT(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /*************/ msousa@350: /* OR */ msousa@350: /*************/ msousa@350: __arith_expand(OR_BOOL, BOOL, || ) /* The explicitly typed standard functions */ msousa@350: __arith_expand(OR__BOOL__BOOL, BOOL, || ) /* Overloaded function */ msousa@350: msousa@350: #define __iec_(TYPENAME) \ msousa@350: __arith_expand(OR_##TYPENAME, TYPENAME, |) /* The explicitly typed standard functions */\ msousa@350: __arith_expand(OR__##TYPENAME##__##TYPENAME, TYPENAME, |) /* Overloaded function */ msousa@350: __ANY_NBIT(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /**************/ msousa@350: /* XOR */ msousa@350: /**************/ msousa@350: #define __xorbool_expand(fname) \ msousa@350: static inline BOOL fname(EN_ENO_PARAMS, UINT param_count, BOOL op1, ...){ \ msousa@350: va_list ap; \ msousa@350: UINT i; \ msousa@350: TEST_EN(BOOL) \ msousa@350: \ msousa@350: va_start (ap, op1); /* Initialize the argument list. */ \ msousa@350: \ msousa@350: for (i = 0; i < param_count - 1; i++){ \ msousa@350: BOOL tmp = va_arg (ap, VA_ARGS_BOOL); \ msousa@350: op1 = (op1 && !tmp) || (!op1 && tmp); \ msousa@350: } \ msousa@350: \ msousa@350: va_end (ap); /* Clean up. */ \ msousa@350: return op1; \ msousa@350: } msousa@350: msousa@350: __xorbool_expand(XOR_BOOL) /* The explicitly typed standard functions */ msousa@350: __xorbool_expand(XOR__BOOL__BOOL) /* Overloaded function */ msousa@350: msousa@350: #define __iec_(TYPENAME) \ msousa@350: __arith_expand(XOR_##TYPENAME, TYPENAME, ^) /* The explicitly typed standard functions */\ msousa@350: __arith_expand(XOR__##TYPENAME##__##TYPENAME, TYPENAME, ^) /* Overloaded function */\ msousa@350: __ANY_NBIT(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: /**************/ msousa@350: /* NOT */ msousa@350: /**************/ msousa@350: /* The explicitly typed standard functions */ msousa@350: static inline BOOL NOT_BOOL(EN_ENO_PARAMS, BOOL op1){ lbessard@149: TEST_EN(BOOL) lbessard@149: return !op1; lbessard@149: } lbessard@149: msousa@350: /* Overloaded function */ msousa@350: static inline BOOL NOT__BOOL__BOOL(EN_ENO_PARAMS, BOOL op1){ msousa@350: TEST_EN(BOOL) msousa@350: return !op1; msousa@350: } msousa@350: msousa@350: /* The explicitly typed standard functions */ msousa@350: #define __iec_(TYPENAME)\ msousa@350: static inline TYPENAME NOT_##TYPENAME(EN_ENO_PARAMS, TYPENAME op1){\ lbessard@149: TEST_EN(TYPENAME)\ lbessard@149: return ~op1;\ lbessard@149: } msousa@350: __ANY_NBIT(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /* Overloaded function */ msousa@350: #define __iec_(TYPENAME)\ msousa@350: static inline TYPENAME NOT__##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, TYPENAME op1){\ lbessard@149: TEST_EN(TYPENAME)\ msousa@350: return ~op1;\ msousa@350: } msousa@350: __ANY_NBIT(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: msousa@350: msousa@350: msousa@350: msousa@350: /***************************************************/ msousa@350: /***************************************************/ msousa@350: /* 2.5.1.5.4 Selection and comparison Functions */ msousa@350: /***************************************************/ msousa@350: /***************************************************/ msousa@350: msousa@350: /*********************/ msousa@350: /*** Table 27 ***/ msousa@350: /*********************/ msousa@350: msousa@350: msousa@350: /**************/ msousa@350: /* SEL */ msousa@350: /**************/ msousa@350: msousa@350: /* The explicitly typed standard functions */ msousa@350: #define __iec_(TYPENAME)\ msousa@350: static inline TYPENAME SEL_##TYPENAME(EN_ENO_PARAMS, BOOL G, TYPENAME op0, TYPENAME op1){\ lbessard@149: TEST_EN(TYPENAME)\ lbessard@149: return G ? op1 : op0;\ lbessard@149: } msousa@350: __ANY(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /* Overloaded function */ msousa@350: #define __iec_(TYPENAME)\ msousa@350: static inline TYPENAME SEL__##TYPENAME##__BOOL__##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, BOOL G, TYPENAME op0, TYPENAME op1){\ lbessard@149: TEST_EN(TYPENAME)\ msousa@350: return G ? op1 : op0;\ msousa@350: } msousa@350: __ANY(__iec_) msousa@350: #undef __iec_ msousa@350: lbessard@149: lbessard@149: /**************/ lbessard@149: /* MAX */ lbessard@149: /**************/ lbessard@149: etisserant@40: #define __extrem_(fname,TYPENAME, COND) \ msousa@350: static inline TYPENAME fname(EN_ENO_PARAMS, UINT param_count, TYPENAME op1, ...){\ etisserant@40: va_list ap;\ etisserant@40: UINT i;\ laurent@200: TEST_EN(TYPENAME)\ etisserant@40: \ etisserant@40: va_start (ap, op1); /* Initialize the argument list. */\ etisserant@40: \ lbessard@165: for (i = 0; i < param_count - 1; i++){\ etisserant@40: TYPENAME tmp = va_arg (ap, VA_ARGS_##TYPENAME);\ etisserant@40: op1 = COND ? tmp : op1;\ etisserant@40: }\ etisserant@40: \ etisserant@40: va_end (ap); /* Clean up. */\ etisserant@40: return op1;\ etisserant@40: } etisserant@40: msousa@350: /* Max for numerical data types */ msousa@350: #define __iec_(TYPENAME) \ msousa@350: __extrem_(MAX_##TYPENAME,TYPENAME, op1 < tmp) /* The explicitly typed standard functions */\ msousa@350: __extrem_(MAX__##TYPENAME##__##TYPENAME,TYPENAME, op1 < tmp) /* Overloaded function */ msousa@350: __ANY_BIT(__iec_) msousa@350: __ANY_NUM(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /* Max for time data types */ msousa@350: #define __iec_(TYPENAME) \ msousa@350: __extrem_(MAX_##TYPENAME, TYPENAME, __time_cmp(op1, tmp) < 0) /* The explicitly typed standard functions */\ msousa@350: __extrem_(MAX__##TYPENAME##__##TYPENAME, TYPENAME, __time_cmp(op1, tmp) < 0) /* Overloaded function */ msousa@350: __ANY_DATE(__iec_) msousa@350: __iec_(TIME) msousa@350: #undef __iec_ msousa@350: msousa@350: /* Max for string data types */ msousa@350: __extrem_(MAX_STRING, STRING, __STR_CMP(op1,tmp) < 0) /* The explicitly typed standard functions */ msousa@350: __extrem_(MAX__STRING__STRING, STRING, __STR_CMP(op1,tmp) < 0) /* Overloaded function */ etisserant@40: etisserant@40: /**************/ etisserant@40: /* MIN */ etisserant@40: /**************/ msousa@350: /* Min for numerical data types */ msousa@350: #define __iec_(TYPENAME) \ msousa@350: __extrem_(MIN_##TYPENAME, TYPENAME, op1 > tmp) /* The explicitly typed standard functions */\ msousa@350: __extrem_(MIN__##TYPENAME##__##TYPENAME, TYPENAME, op1 > tmp) /* Overloaded function */ msousa@350: __ANY_NBIT(__iec_) msousa@350: __ANY_NUM(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /* Min for time data types */ msousa@350: #define __iec_(TYPENAME) \ msousa@350: __extrem_(MIN_##TYPENAME, TYPENAME, __time_cmp(op1, tmp) > 0) /* The explicitly typed standard functions */\ msousa@350: __extrem_(MIN__##TYPENAME##__##TYPENAME, TYPENAME, __time_cmp(op1, tmp) > 0) /* Overloaded function */ msousa@350: __ANY_DATE(__iec_) msousa@350: __iec_(TIME) msousa@350: #undef __iec_ msousa@350: msousa@350: /* Min for string data types */ msousa@350: __extrem_(MIN_STRING, STRING, __STR_CMP(op1,tmp) > 0) /* The explicitly typed standard functions */ msousa@350: __extrem_(MIN__STRING__STRING, STRING, __STR_CMP(op1,tmp) > 0) /* Overloaded function */ msousa@350: msousa@350: /**************/ msousa@350: /* LIMIT */ msousa@350: /**************/ msousa@350: msousa@350: /* Limit for numerical data types */ msousa@350: #define __iec_(TYPENAME)\ msousa@350: /* The explicitly typed standard functions */\ msousa@350: static inline TYPENAME LIMIT_##TYPENAME(EN_ENO_PARAMS, TYPENAME MN, TYPENAME IN, TYPENAME MX){\ msousa@350: TEST_EN(TYPENAME)\ msousa@350: return IN > MN ? IN < MX ? IN : MX : MN;\ msousa@350: }\ msousa@350: /* Overloaded function */\ msousa@350: static inline TYPENAME LIMIT__##TYPENAME##__##TYPENAME##__##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, TYPENAME MN, TYPENAME IN, TYPENAME MX){\ msousa@350: TEST_EN(TYPENAME)\ msousa@350: return IN > MN ? IN < MX ? IN : MX : MN;\ msousa@350: } msousa@350: __ANY_NBIT(__iec_) msousa@350: __ANY_NUM(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: /* Limit for time data types */ msousa@350: #define __iec_(TYPENAME)\ msousa@350: /* The explicitly typed standard functions */\ msousa@350: static inline TYPENAME LIMIT_##TYPENAME(EN_ENO_PARAMS, TYPENAME MN, TYPENAME IN, TYPENAME MX){\ msousa@350: TEST_EN(TYPENAME)\ msousa@350: return __time_cmp(IN, MN) > 0 ? /* IN>MN ?*/\ msousa@350: __time_cmp(IN, MX) < 0 ? /* IN 0 ? /* IN>MN ?*/\ msousa@350: __time_cmp(IN, MX) < 0 ? /* IN 0 ? __STR_CMP(IN, MX) < 0 ? IN : MX : MN; msousa@350: } msousa@350: msousa@350: /* Overloaded function */ msousa@350: static inline STRING LIMIT__STRING__STRING__STRING__STRING(EN_ENO_PARAMS, STRING MN, STRING IN, STRING MX){ msousa@350: TEST_EN(STRING) msousa@350: return __STR_CMP(IN, MN) > 0 ? __STR_CMP(IN, MX) < 0 ? IN : MX : MN; msousa@350: } msousa@350: etisserant@40: etisserant@40: /**************/ etisserant@40: /* MUX */ etisserant@40: /**************/ msousa@350: /* The standard states that the inputs for SEL and MUX must be named starting off from 0, msousa@350: * unlike remaining functions, that start off at 1. msousa@350: */ msousa@350: /* The explicitly typed standard functions */ msousa@350: #define __in1_anyint_(in2_TYPENAME) __ANY_INT_1(__iec_,in2_TYPENAME) msousa@350: #define __iec_(in1_TYPENAME,in2_TYPENAME) \ msousa@350: static inline in2_TYPENAME MUX__##in2_TYPENAME##__##in1_TYPENAME##__##in2_TYPENAME(EN_ENO_PARAMS, UINT param_count, in1_TYPENAME K, ...){\ etisserant@40: va_list ap;\ etisserant@40: UINT i;\ msousa@350: in2_TYPENAME tmp;\ msousa@350: TEST_EN_COND(in2_TYPENAME, K >= param_count)\ msousa@350: tmp = __INIT_##in2_TYPENAME;\ etisserant@40: \ etisserant@41: va_start (ap, K); /* Initialize the argument list. */\ etisserant@40: \ etisserant@40: for (i = 0; i < param_count; i++){\ etisserant@40: if(K == i){\ msousa@350: tmp = va_arg (ap, VA_ARGS_##in2_TYPENAME);\ etisserant@40: va_end (ap); /* Clean up. */\ etisserant@40: return tmp;\ etisserant@40: }else{\ msousa@350: va_arg (ap, VA_ARGS_##in2_TYPENAME);\ etisserant@40: }\ etisserant@40: }\ etisserant@40: \ etisserant@40: va_end (ap); /* Clean up. */\ etisserant@41: return tmp;\ etisserant@40: } etisserant@40: msousa@350: __ANY(__in1_anyint_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: /******************************************/ msousa@350: /*** Table 28 ***/ msousa@350: /*** Standard comparison functions ***/ msousa@350: /******************************************/ etisserant@40: etisserant@40: #define __compare_(fname,TYPENAME, COND) \ msousa@350: static inline BOOL fname(EN_ENO_PARAMS, UINT param_count, TYPENAME op1, ...){\ etisserant@40: va_list ap;\ etisserant@40: UINT i;\ laurent@200: TEST_EN(BOOL)\ etisserant@40: \ etisserant@40: va_start (ap, op1); /* Initialize the argument list. */\ etisserant@43: DBG(#fname #TYPENAME "\n")\ etisserant@43: DBG_TYPE(TYPENAME, op1)\ etisserant@40: \ etisserant@43: for (i = 0; i < param_count - 1; i++){\ etisserant@40: TYPENAME tmp = va_arg (ap, VA_ARGS_##TYPENAME);\ etisserant@43: DBG_TYPE(TYPENAME, tmp)\ etisserant@40: if(COND){\ etisserant@40: op1 = tmp;\ etisserant@40: }else{\ etisserant@40: va_end (ap); /* Clean up. */\ etisserant@40: return 0;\ etisserant@40: }\ etisserant@40: }\ etisserant@40: \ etisserant@40: va_end (ap); /* Clean up. */\ etisserant@40: return 1;\ etisserant@40: } etisserant@40: etisserant@40: #define __compare_num(fname, TYPENAME, TEST) __compare_(fname, TYPENAME, op1 TEST tmp ) msousa@350: #define __compare_time(fname, TYPENAME, TEST) __compare_(fname, TYPENAME, __time_cmp(op1, tmp) TEST 0) etisserant@40: #define __compare_string(fname, TEST) __compare_(fname, STRING, __STR_CMP(op1, tmp) TEST 0 ) etisserant@40: lbessard@149: etisserant@40: /**************/ etisserant@40: /* GT */ etisserant@40: /**************/ msousa@350: /* Comparison for numerical data types */ msousa@350: #define __iec_(TYPENAME) \ msousa@350: __compare_num(GT_##TYPENAME, TYPENAME, > ) /* The explicitly typed standard functions */\ msousa@350: __compare_num(GT__BOOL__##TYPENAME, TYPENAME, > ) /* Overloaded function */ msousa@350: __ANY_NBIT(__iec_) msousa@350: __ANY_NUM(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /* Comparison for time data types */ msousa@350: #define __iec_(TYPENAME) \ msousa@350: __compare_time(GT_##TYPENAME, TYPENAME, > ) /* The explicitly typed standard functions */\ msousa@350: __compare_time(GT__BOOL__##TYPENAME, TYPENAME, > ) /* Overloaded function */ msousa@350: __ANY_DATE(__iec_) msousa@350: __iec_(TIME) msousa@350: #undef __iec_ msousa@350: msousa@350: /* Comparison for string data types */ msousa@350: __compare_string(GT_STRING, > ) /* The explicitly typed standard functions */ msousa@350: __compare_string(GT__BOOL__STRING, > ) /* Overloaded function */ etisserant@40: etisserant@40: /**************/ etisserant@40: /* GE */ etisserant@40: /**************/ msousa@350: /* Comparison for numerical data types */ msousa@350: #define __iec_(TYPENAME) \ msousa@350: __compare_num(GE_##TYPENAME, TYPENAME, >= ) /* The explicitly typed standard functions */\ msousa@350: __compare_num(GE__BOOL__##TYPENAME, TYPENAME, >= ) /* Overloaded function */ msousa@350: __ANY_NBIT(__iec_) msousa@350: __ANY_NUM(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /* Comparison for time data types */ msousa@350: #define __iec_(TYPENAME) \ msousa@350: __compare_time(GE_##TYPENAME, TYPENAME, >= ) /* The explicitly typed standard functions */\ msousa@350: __compare_time(GE__BOOL__##TYPENAME, TYPENAME, >= ) /* Overloaded function */ msousa@350: __ANY_DATE(__iec_) msousa@350: __iec_(TIME) msousa@350: #undef __iec_ msousa@350: msousa@350: /* Comparison for string data types */ msousa@350: __compare_string(GE_STRING, >= ) /* The explicitly typed standard functions */ msousa@350: __compare_string(GE__BOOL__STRING, >= ) /* Overloaded function */ msousa@350: msousa@350: etisserant@40: etisserant@40: /**************/ etisserant@40: /* EQ */ etisserant@40: /**************/ msousa@350: /* Comparison for numerical data types */ msousa@350: #define __iec_(TYPENAME) \ msousa@350: __compare_num(EQ_##TYPENAME, TYPENAME, == ) /* The explicitly typed standard functions */\ msousa@350: __compare_num(EQ__BOOL__##TYPENAME, TYPENAME, == ) /* Overloaded function */ msousa@350: __ANY_NBIT(__iec_) msousa@350: __ANY_NUM(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /* Comparison for time data types */ msousa@350: #define __iec_(TYPENAME) \ msousa@350: __compare_time(EQ_##TYPENAME, TYPENAME, == ) /* The explicitly typed standard functions */\ msousa@350: __compare_time(EQ__BOOL__##TYPENAME, TYPENAME, == ) /* Overloaded function */ msousa@350: __ANY_DATE(__iec_) msousa@350: __iec_(TIME) msousa@350: #undef __iec_ msousa@350: msousa@350: /* Comparison for string data types */ msousa@350: __compare_string(EQ_STRING, == ) /* The explicitly typed standard functions */ msousa@350: __compare_string(EQ__BOOL__STRING, == ) /* Overloaded function */ msousa@350: etisserant@40: etisserant@40: /**************/ etisserant@40: /* LT */ etisserant@40: /**************/ msousa@350: /* Comparison for numerical data types */ msousa@350: #define __iec_(TYPENAME) \ msousa@350: __compare_num(LT_##TYPENAME, TYPENAME, < ) /* The explicitly typed standard functions */\ msousa@350: __compare_num(LT__BOOL__##TYPENAME, TYPENAME, < ) /* Overloaded function */ msousa@350: __ANY_NBIT(__iec_) msousa@350: __ANY_NUM(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /* Comparison for time data types */ msousa@350: #define __iec_(TYPENAME) \ msousa@350: __compare_time(LT_##TYPENAME, TYPENAME, < ) /* The explicitly typed standard functions */\ msousa@350: __compare_time(LT__BOOL__##TYPENAME, TYPENAME, < ) /* Overloaded function */ msousa@350: __ANY_DATE(__iec_) msousa@350: __iec_(TIME) msousa@350: #undef __iec_ msousa@350: msousa@350: /* Comparison for string data types */ msousa@350: __compare_string(LT_STRING, < ) /* The explicitly typed standard functions */ msousa@350: __compare_string(LT__BOOL__STRING, < ) /* Overloaded function */ msousa@350: etisserant@40: etisserant@40: /**************/ etisserant@40: /* LE */ etisserant@40: /**************/ msousa@350: /* Comparison for numerical data types */ msousa@350: #define __iec_(TYPENAME) \ msousa@350: __compare_num(LE_##TYPENAME, TYPENAME, <= ) /* The explicitly typed standard functions */\ msousa@350: __compare_num(LE__BOOL__##TYPENAME, TYPENAME, <= ) /* Overloaded function */ msousa@350: __ANY_NBIT(__iec_) msousa@350: __ANY_NUM(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /* Comparison for time data types */ msousa@350: #define __iec_(TYPENAME) \ msousa@350: __compare_time(LE_##TYPENAME, TYPENAME, <= ) /* The explicitly typed standard functions */\ msousa@350: __compare_time(LE__BOOL__##TYPENAME, TYPENAME, <= ) /* Overloaded function */ msousa@350: __ANY_DATE(__iec_) msousa@350: __iec_(TIME) msousa@350: #undef __iec_ msousa@350: msousa@350: /* Comparison for string data types */ msousa@350: __compare_string(LE_STRING, <= ) /* The explicitly typed standard functions */ msousa@350: __compare_string(LE__BOOL__STRING, <= ) /* Overloaded function */ msousa@350: etisserant@40: etisserant@40: /**************/ etisserant@40: /* NE */ etisserant@40: /**************/ msousa@350: /* Comparison for numerical data types */ msousa@350: #define __iec_(TYPENAME) \ msousa@350: __compare_num(NE_##TYPENAME, TYPENAME, != ) /* The explicitly typed standard functions */\ msousa@350: __compare_num(NE__BOOL__##TYPENAME##__##TYPENAME, TYPENAME, != ) /* Overloaded function */ msousa@350: __ANY_NBIT(__iec_) msousa@350: __ANY_NUM(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /* Comparison for time data types */ msousa@350: #define __iec_(TYPENAME) \ msousa@350: __compare_time(NE_##TYPENAME, TYPENAME, != ) /* The explicitly typed standard functions */\ msousa@350: __compare_time(NE__BOOL__##TYPENAME##__##TYPENAME, TYPENAME, != ) /* Overloaded function */ msousa@350: __ANY_DATE(__iec_) msousa@350: __iec_(TIME) msousa@350: #undef __iec_ msousa@350: msousa@350: /* Comparison for string data types */ msousa@350: __compare_string(NE_STRING, != ) /* The explicitly typed standard functions */ msousa@350: __compare_string(NE__BOOL__STRING__STRING, != ) /* Overloaded function */ msousa@350: msousa@350: msousa@350: msousa@350: msousa@350: msousa@350: msousa@350: /*********************************************/ msousa@350: /*********************************************/ msousa@350: /* 2.5.1.5.5 Character string Functions */ msousa@350: /*********************************************/ msousa@350: /*********************************************/ msousa@350: msousa@350: /*************************************/ msousa@350: /*** Table 29 ***/ msousa@350: /*** Character string Functions ***/ msousa@350: /*************************************/ msousa@350: msousa@350: /* We do not delcare explcitly typed versions of the functions in table 29. msousa@350: * See note above regarding explicitly typed functions for more details. msousa@350: */ msousa@350: msousa@350: msousa@350: #define __STR_CMP(str1, str2) memcmp((char*)&str1.body,(char*)&str2.body, str1.len < str2.len ? str1.len : str2.len) msousa@350: msousa@350: msousa@350: /***************/ msousa@350: /* LEN */ msousa@350: /***************/ msousa@350: static inline __strlen_t __len(STRING IN) {return IN.len;} msousa@350: msousa@350: /* A function, with 1 input paramter, implementing a generic OPERATION */ msousa@350: #define __genoper_1p_(fname,ret_TYPENAME, par_TYPENAME, OPERATION) \ msousa@350: static inline ret_TYPENAME fname(EN_ENO_PARAMS, par_TYPENAME par1){\ msousa@350: TEST_EN(ret_TYPENAME)\ msousa@350: return (ret_TYPENAME)OPERATION(par1);\ msousa@350: } msousa@350: msousa@350: #define __iec_(TYPENAME) __genoper_1p_(LEN__##TYPENAME##__STRING, TYPENAME, STRING, __len) msousa@350: __ANY_INT(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: /****************/ msousa@350: /* LEFT */ msousa@350: /****************/ msousa@350: msousa@350: static inline STRING __left(STRING IN, __strlen_t L){ msousa@350: STRING res; msousa@350: res = __INIT_STRING; msousa@350: L = L < IN.len ? L : IN.len; msousa@350: memcpy(&res.body, &IN.body, L); msousa@350: res.len = L; msousa@350: return res; msousa@350: } msousa@350: msousa@350: #define __iec_(TYPENAME) \ msousa@350: static inline STRING LEFT__STRING__STRING__##TYPENAME(EN_ENO_PARAMS, STRING str, TYPENAME L){\ msousa@350: TEST_EN_COND(STRING, L < 0)\ msousa@350: return (STRING)__left(str,L);\ msousa@350: } msousa@350: __ANY_INT(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: /*****************/ msousa@350: /* RIGHT */ msousa@350: /*****************/ msousa@350: msousa@350: static inline STRING __right(STRING IN, __strlen_t L){ msousa@350: STRING res; msousa@350: res = __INIT_STRING; msousa@350: L = L < IN.len ? L : IN.len; msousa@350: memcpy(&res.body, &IN.body[IN.len - L], L); msousa@350: res.len = L; msousa@350: return res; msousa@350: } msousa@350: msousa@350: msousa@350: #define __iec_(TYPENAME) \ msousa@350: static inline STRING RIGHT__STRING__STRING__##TYPENAME(EN_ENO_PARAMS, STRING str, TYPENAME L){\ msousa@350: TEST_EN_COND(STRING, L < 0)\ msousa@350: return (STRING)__right(str,L);\ msousa@350: } msousa@350: __ANY_INT(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: /***************/ msousa@350: /* MID */ msousa@350: /***************/ msousa@350: msousa@350: static inline STRING __mid(STRING IN, __strlen_t L, __strlen_t P){ msousa@350: STRING res; msousa@350: res = __INIT_STRING; msousa@350: if(P <= IN.len){ msousa@350: P -= 1; /* now can be used as [index]*/ msousa@350: L = L + P <= IN.len ? L : IN.len - P; msousa@350: memcpy(&res.body, &IN.body[P] , L); msousa@350: res.len = L; msousa@350: } msousa@350: return res; msousa@350: } msousa@350: msousa@350: #define __iec_(TYPENAME) \ msousa@350: static inline STRING MID__STRING__STRING__##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, STRING str, TYPENAME L, TYPENAME P){\ msousa@350: TEST_EN_COND(STRING, L < 0 || P < 0)\ msousa@350: return (STRING)__mid(str,L,P);\ msousa@350: } msousa@350: __ANY_INT(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: /******************/ msousa@350: /* CONCAT */ msousa@350: /******************/ msousa@350: msousa@350: static inline STRING CONCAT__STRING__STRING(UINT param_count, ...){ msousa@350: UINT i; msousa@350: STRING res; msousa@350: va_list ap; msousa@350: __strlen_t charcount; msousa@350: charcount = 0; msousa@350: res = __INIT_STRING; msousa@350: msousa@350: va_start (ap, param_count); /* Initialize the argument list. */ msousa@350: msousa@350: for (i = 0; i < param_count && charcount < STR_MAX_LEN; i++) msousa@350: { msousa@350: STRING tmp = va_arg(ap, STRING); msousa@350: __strlen_t charrem = STR_MAX_LEN - charcount; msousa@350: __strlen_t to_write = tmp.len > charrem ? charrem : tmp.len; msousa@350: memcpy(&res.body[charcount], &tmp.body , to_write); msousa@350: charcount += to_write; msousa@350: } msousa@350: msousa@350: res.len = charcount; msousa@350: msousa@350: va_end (ap); /* Clean up. */ msousa@350: return res; msousa@350: } msousa@350: msousa@350: /******************/ msousa@350: /* INSERT */ msousa@350: /******************/ msousa@350: msousa@350: static inline STRING __insert(STRING IN1, STRING IN2, __strlen_t P){ msousa@350: STRING res; msousa@350: __strlen_t to_copy; msousa@350: res = __INIT_STRING; msousa@350: msousa@350: to_copy = P > IN1.len ? IN1.len : P; msousa@350: memcpy(&res.body, &IN1.body , to_copy); msousa@350: P = res.len = to_copy; msousa@350: msousa@350: to_copy = IN2.len + res.len > STR_MAX_LEN ? STR_MAX_LEN - res.len : IN2.len; msousa@350: memcpy(&res.body[res.len], &IN2.body , to_copy); msousa@350: res.len += to_copy; msousa@350: msousa@350: to_copy = IN1.len - P < STR_MAX_LEN - res.len ? IN1.len - P : STR_MAX_LEN - res.len ; msousa@350: memcpy(&res.body[res.len], &IN1.body[P] , to_copy); msousa@350: res.len += to_copy; msousa@350: msousa@350: return res; msousa@350: } msousa@350: msousa@350: #define __iec_(TYPENAME) \ msousa@350: static inline STRING INSERT__STRING__STRING__STRING__##TYPENAME(EN_ENO_PARAMS, STRING str1, STRING str2, TYPENAME P){\ msousa@350: TEST_EN_COND(STRING, P < 0)\ msousa@350: return (STRING)__insert(str1,str2,P);\ msousa@350: } msousa@350: __ANY_INT(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: /******************/ msousa@350: /* DELETE */ msousa@350: /******************/ msousa@350: msousa@350: static inline STRING __delete(STRING IN, __strlen_t L, __strlen_t P){ msousa@350: STRING res; msousa@350: __strlen_t to_copy; msousa@350: res = __INIT_STRING; msousa@350: msousa@350: to_copy = P > IN.len ? IN.len : P-1; msousa@350: memcpy(&res.body, &IN.body , to_copy); msousa@350: P = res.len = to_copy; msousa@350: msousa@350: if( IN.len > P + L ){ msousa@350: to_copy = IN.len - P - L; msousa@350: memcpy(&res.body[res.len], &IN.body[P + L], to_copy); msousa@350: res.len += to_copy; msousa@350: } msousa@350: msousa@350: return res; msousa@350: } msousa@350: msousa@350: #define __iec_(TYPENAME) \ msousa@350: static inline STRING DELETE__STRING__STRING__##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, STRING str, TYPENAME L, TYPENAME P){\ msousa@350: TEST_EN_COND(STRING, L < 0 || P < 0)\ msousa@350: return (STRING)__delete(str,L,P);\ msousa@350: } msousa@350: __ANY_INT(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: /*******************/ msousa@350: /* REPLACE */ msousa@350: /*******************/ msousa@350: msousa@350: static inline STRING __replace(STRING IN1, STRING IN2, __strlen_t L, __strlen_t P){ msousa@350: STRING res; msousa@350: __strlen_t to_copy; msousa@350: res = __INIT_STRING; msousa@350: msousa@350: to_copy = P > IN1.len ? IN1.len : P-1; msousa@350: memcpy(&res.body, &IN1.body , to_copy); msousa@350: P = res.len = to_copy; msousa@350: msousa@350: to_copy = IN2.len < L ? IN2.len : L; msousa@350: msousa@350: if( to_copy + res.len > STR_MAX_LEN ) msousa@350: to_copy = STR_MAX_LEN - res.len; msousa@350: msousa@350: memcpy(&res.body[res.len], &IN2.body , to_copy); msousa@350: res.len += to_copy; msousa@350: msousa@350: P += L; msousa@350: if( res.len < STR_MAX_LEN && P < IN1.len) msousa@350: { msousa@350: to_copy = IN1.len - P; msousa@350: memcpy(&res.body[res.len], &IN1.body[P] , to_copy); msousa@350: res.len += to_copy; msousa@350: } msousa@350: msousa@350: return res; msousa@350: } msousa@350: msousa@350: #define __iec_(TYPENAME) \ msousa@350: static inline STRING REPLACE__STRING__STRING__STRING__##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, STRING str1, STRING str2, TYPENAME L, TYPENAME P){\ msousa@350: TEST_EN_COND(STRING, L < 0 || P < 0)\ msousa@350: return (STRING)__replace(str1,str2,L,P);\ msousa@350: } msousa@350: __ANY_INT(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /****************/ msousa@350: /* FIND */ msousa@350: /****************/ msousa@350: msousa@350: static inline __strlen_t __pfind(STRING* IN1, STRING* IN2){ msousa@350: UINT count1 = 0; /* offset of first matching char in IN1 */ msousa@350: UINT count2 = 0; /* count of matching char */ msousa@350: while(count1 + count2 < IN1->len && count2 < IN2->len) msousa@350: { msousa@350: if(IN1->body[count1 + count2] != IN2->body[count2]){ msousa@350: count1 += count2 + 1; msousa@350: count2 = 0; msousa@350: } msousa@350: else { msousa@350: count2++; msousa@350: } msousa@350: } msousa@350: return count2 == IN2->len -1 ? 0 : count1 + 1; msousa@350: } msousa@350: msousa@350: #define __iec_(TYPENAME) \ msousa@350: static inline TYPENAME FIND__##TYPENAME##__STRING__STRING(EN_ENO_PARAMS, STRING str1, STRING str2){\ msousa@350: TEST_EN(TYPENAME)\ msousa@350: return (TYPENAME)__pfind(&str1,&str2);\ msousa@350: } msousa@350: __ANY_INT(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: msousa@350: /*********************************************/ msousa@350: /*********************************************/ msousa@350: /* 2.5.1.5.6 Functions of time data types */ msousa@350: /*********************************************/ msousa@350: /*********************************************/ msousa@350: msousa@350: /**************************************/ msousa@350: /*** Table 30 ***/ msousa@350: /*** Functions of time data types ***/ msousa@350: /**************************************/ msousa@350: msousa@350: msousa@350: static inline TIME ADD_TIME(EN_ENO_PARAMS, TIME IN1, TIME IN2){ msousa@350: TEST_EN(TIME) msousa@350: return __time_add(IN1, IN2); msousa@350: } msousa@350: msousa@350: static inline TOD ADD_TOD_TIME(EN_ENO_PARAMS, TOD IN1, TIME IN2){ msousa@350: TEST_EN(TOD) msousa@350: return __time_add(IN1, IN2); msousa@350: } msousa@350: msousa@350: static inline DT ADD_DT_TIME(EN_ENO_PARAMS, DT IN1, TIME IN2){ msousa@350: TEST_EN(DT) msousa@350: return __time_add(IN1, IN2); msousa@350: } msousa@350: msousa@350: static inline TIME SUB_TIME(EN_ENO_PARAMS, TIME IN1, TIME IN2){ msousa@350: TEST_EN(TIME) msousa@350: return __time_sub(IN1, IN2); msousa@350: } msousa@350: msousa@350: static inline TIME SUB_DATE_DATE(EN_ENO_PARAMS, DATE IN1, DATE IN2){ msousa@350: TEST_EN(TIME) msousa@350: return __time_sub(IN1, IN2); msousa@350: } msousa@350: msousa@350: static inline TOD SUB_TOD_TIME(EN_ENO_PARAMS, TOD IN1, TIME IN2){ msousa@350: TEST_EN(TOD) msousa@350: return __time_sub(IN1, IN2); msousa@350: } msousa@350: msousa@350: static inline TIME SUB_TOD_TOD(EN_ENO_PARAMS, TOD IN1, TOD IN2){ msousa@350: TEST_EN(TIME) msousa@350: return __time_sub(IN1, IN2); msousa@350: } msousa@350: msousa@350: static inline DT SUB_DT_TIME(EN_ENO_PARAMS, DT IN1, TIME IN2){ msousa@350: TEST_EN(DT) msousa@350: return __time_sub(IN1, IN2); msousa@350: } msousa@350: msousa@350: static inline TIME SUB_DT_DT(EN_ENO_PARAMS, DT IN1, DT IN2){ msousa@350: TEST_EN(TIME) msousa@350: return __time_sub(IN1, IN2); msousa@350: } msousa@350: msousa@350: msousa@350: /*** MULTIME ***/ msousa@350: #define __iec_(TYPENAME)\ msousa@350: static inline TIME MULTIME__TIME__TIME__##TYPENAME(EN_ENO_PARAMS, TIME IN1, TYPENAME IN2){\ msousa@350: TEST_EN(TIME)\ msousa@350: return __time_mul(IN1, IN2);\ msousa@350: } msousa@350: __ANY_NUM(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /*** MUL ***/ msousa@350: #define __iec_(TYPENAME)\ msousa@350: static inline TIME MUL__TIME__TIME__##TYPENAME(EN_ENO_PARAMS, TIME IN1, TYPENAME IN2){\ msousa@350: TEST_EN(TIME)\ msousa@350: return __time_mul(IN1, IN2);\ msousa@350: } msousa@350: __ANY_NUM(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /*** DIVTIME ***/ msousa@350: #define __iec_(TYPENAME)\ msousa@350: static inline TIME DIVTIME__TIME__TIME__##TYPENAME(EN_ENO_PARAMS, TIME IN1, TYPENAME IN2){\ msousa@350: TEST_EN(TIME)\ msousa@350: return __time_div(IN1, IN2);\ msousa@350: } msousa@350: __ANY_NUM(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /*** DIV ***/ msousa@350: #define __iec_(TYPENAME)\ msousa@350: static inline TIME DIV__TIME__TIME__##TYPENAME(EN_ENO_PARAMS, TIME IN1, TYPENAME IN2){\ msousa@350: TEST_EN(TIME)\ msousa@350: return __time_div(IN1, IN2);\ msousa@350: } msousa@350: __ANY_NUM(__iec_) msousa@350: #undef __iec_ msousa@350: msousa@350: /*** CONCAT_DATE_TOD ***/ msousa@350: static inline DT CONCAT_DATE_TOD(EN_ENO_PARAMS, DATE IN1, TOD IN2){ msousa@350: TEST_EN(DT) msousa@350: return __time_add(IN1, IN2); msousa@350: } msousa@350: msousa@350: msousa@350: msousa@350: /****************************************************/ msousa@350: /****************************************************/ msousa@350: /* 2.5.1.5.6 Functions of enumerated data types */ msousa@350: /****************************************************/ msousa@350: /****************************************************/ msousa@350: msousa@350: /********************************************/ msousa@350: /*** Table 31 ***/ msousa@350: /*** Functions of enumerated data types ***/ msousa@350: /********************************************/ msousa@350: msousa@350: /* Do we support this? */