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: msousa@739: #ifndef _IEC_STD_LIB_H msousa@739: #define _IEC_STD_LIB_H msousa@739: msousa@739: etisserant@40: #include etisserant@40: #include etisserant@40: #include etisserant@57: #include Laurent@707: #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: conti@582: #define __lit(type,value,...) (type)value##__VA_ARGS__ conti@582: // Keep this macro expention step to let sfx(__VA_ARGS__) change into L or LL Laurent@638: #define __literal(type,value,...) __lit(type,value,__VA_ARGS__) conti@582: conti@582: #define __BOOL_LITERAL(value) __literal(BOOL,value) conti@582: #define __SINT_LITERAL(value) __literal(SINT,value) conti@582: #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) conti@582: #define __USINT_LITERAL(value) __literal(USINT,value) conti@582: #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) conti@582: #define __TIME_LITERAL(value) __literal(TIME,value) conti@582: #define __DATE_LITERAL(value) __literal(DATE,value) conti@582: #define __TOD_LITERAL(value) __literal(TOD,value) conti@582: #define __DT_LITERAL(value) __literal(DT,value) Laurent@625: #define __STRING_LITERAL(count,value) (STRING){count,value} conti@582: #define __BYTE_LITERAL(value) __literal(BYTE,value) conti@582: #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@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: Laurent@711: #define EPOCH_YEAR 1970 Laurent@714: #define SECONDS_PER_MINUTE 60 Laurent@714: #define SECONDS_PER_HOUR (60 * SECONDS_PER_MINUTE) Laurent@711: #define SECONDS_PER_DAY (24 * SECONDS_PER_HOUR) Laurent@711: #define __isleap(year) \ Laurent@711: ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0)) Laurent@711: static const unsigned short int __mon_yday[2][13] = Laurent@711: { Laurent@711: /* Normal years. */ Laurent@711: { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}, Laurent@711: /* Leap years. */ Laurent@711: { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366} Laurent@711: }; Laurent@711: Laurent@711: typedef struct { Laurent@711: int tm_sec; /* Seconds. [0-60] (1 leap second) */ Laurent@711: int tm_min; /* Minutes. [0-59] */ Laurent@711: int tm_hour; /* Hours. [0-23] */ Laurent@711: int tm_day; /* Day. [1-31] */ Laurent@711: int tm_mon; /* Month. [0-11] */ Laurent@711: int tm_year; /* Year */ Laurent@711: } tm; Laurent@711: Laurent@711: static inline tm convert_seconds_to_date_and_time(long int seconds) { Laurent@711: tm dt; Laurent@711: long int days, rem; Laurent@711: days = seconds / SECONDS_PER_DAY; Laurent@711: rem = seconds % SECONDS_PER_DAY; Laurent@711: if (rem < 0) { Laurent@711: rem += SECONDS_PER_DAY; Laurent@711: days--; Laurent@711: } Laurent@711: Laurent@711: // time of day Laurent@711: dt.tm_hour = rem / SECONDS_PER_HOUR; Laurent@711: rem %= SECONDS_PER_HOUR; Laurent@711: dt.tm_min = rem / 60; Laurent@711: dt.tm_sec = rem % 60; Laurent@711: Laurent@711: // date Laurent@711: dt.tm_year = EPOCH_YEAR; Laurent@711: while (days >= (rem = __isleap(dt.tm_year) ? 366 : 365)) { Laurent@711: dt.tm_year++; Laurent@711: days -= rem; Laurent@711: } Laurent@711: while (days < 0) { Laurent@711: dt.tm_year--; Laurent@711: days += __isleap(dt.tm_year) ? 366 : 365; Laurent@711: } Laurent@711: dt.tm_mon = 1; Laurent@711: while (days > __mon_yday[__isleap(dt.tm_year)][dt.tm_mon]) { Laurent@711: dt.tm_mon += 1; Laurent@711: } Laurent@711: dt.tm_day = days - __mon_yday[__isleap(dt.tm_year)][dt.tm_mon - 1] + 1; Laurent@711: Laurent@711: return dt; Laurent@711: } Edouard@709: Edouard@254: static inline IEC_TIMESPEC __date_to_timespec(int day, int month, int year) { Edouard@254: IEC_TIMESPEC ts; Laurent@711: int a4, b4, a100, b100, a400, b400; Laurent@711: int yday; Laurent@711: int intervening_leap_days; Laurent@711: Laurent@711: if (month < 1 || month > 12) Laurent@711: __iec_error(); Laurent@711: Laurent@711: yday = __mon_yday[__isleap(year)][month - 1] + day; Laurent@711: Laurent@711: if (yday > __mon_yday[__isleap(year)][month]) Laurent@711: __iec_error(); Laurent@711: Laurent@711: a4 = (year >> 2) - ! (year & 3); Laurent@711: b4 = (EPOCH_YEAR >> 2) - ! (EPOCH_YEAR & 3); Laurent@711: a100 = a4 / 25 - (a4 % 25 < 0); Laurent@711: b100 = b4 / 25 - (b4 % 25 < 0); Laurent@711: a400 = a100 >> 2; Laurent@711: b400 = b100 >> 2; Laurent@711: intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400); Laurent@705: Laurent@711: ts.tv_sec = ((year - EPOCH_YEAR) * 365 + intervening_leap_days + yday - 1) * 24 * 60 * 60; etisserant@40: ts.tv_nsec = 0; greg@180: etisserant@40: return ts; etisserant@40: } etisserant@40: Laurent@705: static inline IEC_TIMESPEC __dt_to_timespec(double seconds, double minutes, double hours, int day, int month, int year) { Laurent@705: IEC_TIMESPEC ts_date = __date_to_timespec(day, month, year); Laurent@705: IEC_TIMESPEC ts = __tod_to_timespec(seconds, minutes, hours); Laurent@705: Laurent@705: ts.tv_sec += ts_date.tv_sec; 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; Edouard@639: time_t s = (time_t)s_f; conti@700: div_t ns = div((int)((LREAL)IN1.tv_nsec * IN2), 1000000000); Edouard@639: TIME res = {(long)s + ns.quot, Edouard@639: (long)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; Edouard@639: time_t s = (time_t)s_f; Edouard@639: TIME res = {(long)s, Edouard@639: (long)(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; Edouard@389: res.len = snprintf((char*)res.body, STR_MAX_LEN, "16#%llx",(long long unsigned int)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; Edouard@389: res.len = snprintf((char*)res.body, STR_MAX_LEN, "%lld", (long long int)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; Edouard@389: res.len = snprintf((char*)res.body, STR_MAX_LEN, "%llu", (long long unsigned int)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) { Laurent@705: int i; Laurent@705: if (IN.len == 1) return !memcmp(&IN.body,"1", IN.len); Laurent@705: for (i = 0; i < IN.len; i++) IN.body[i] = toupper(IN.body[i]); Laurent@705: return IN.len == 4 ? !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); Edouard@639: return (TIME){(long)IN_val, (long)(IN_val - (LINT)IN_val)*1000000000}; etisserant@40: }else{ Edouard@639: return (TIME){(long)__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@714: days = div(IN.tv_sec, SECONDS_PER_DAY); 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{ Laurent@714: div_t hours = div(days.rem, SECONDS_PER_HOUR); 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{ Laurent@714: div_t minuts = div(hours.rem, SECONDS_PER_MINUTE); 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@711: tm broken_down_time; etisserant@40: /* D#1984-06-25 */ Laurent@711: broken_down_time = convert_seconds_to_date_and_time(IN.tv_sec); laurent@200: res = __INIT_STRING; Laurent@711: res.len = snprintf((char*)&res.body, STR_MAX_LEN, "D#%d-%2.2d-%2.2d", Laurent@711: broken_down_time.tm_year, Laurent@711: broken_down_time.tm_mon, Laurent@711: broken_down_time.tm_day); 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@711: tm broken_down_time; laurent@200: time_t seconds; etisserant@40: /* TOD#15:36:55.36 */ Laurent@711: seconds = IN.tv_sec; Laurent@711: if (seconds >= SECONDS_PER_DAY){ Laurent@711: __iec_error(); Laurent@711: return (STRING){9,"TOD#ERROR"}; Laurent@711: } Laurent@711: broken_down_time = convert_seconds_to_date_and_time(seconds); laurent@200: res = __INIT_STRING; etisserant@40: if(IN.tv_nsec == 0){ Laurent@711: res.len = snprintf((char*)&res.body, STR_MAX_LEN, "TOD#%2.2d:%2.2d:%2.2d", Laurent@711: broken_down_time.tm_hour, Laurent@711: broken_down_time.tm_min, Laurent@711: broken_down_time.tm_sec); etisserant@40: }else{ Laurent@803: res.len = snprintf((char*)&res.body, STR_MAX_LEN, "TOD#%2.2d:%2.2d:%09.6f", Laurent@711: broken_down_time.tm_hour, Laurent@711: broken_down_time.tm_min, Laurent@711: (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@711: tm broken_down_time; etisserant@40: /* DT#1984-06-25-15:36:55.36 */ Laurent@711: broken_down_time = convert_seconds_to_date_and_time(IN.tv_sec); etisserant@40: if(IN.tv_nsec == 0){ Laurent@705: res.len = snprintf((char*)&res.body, STR_MAX_LEN, "DT#%d-%2.2d-%2.2d-%2.2d:%2.2d:%2.2d", Laurent@711: broken_down_time.tm_year, Laurent@711: broken_down_time.tm_mon, Laurent@711: broken_down_time.tm_day, Laurent@711: broken_down_time.tm_hour, Laurent@711: broken_down_time.tm_min, Laurent@711: broken_down_time.tm_sec); etisserant@40: }else{ Laurent@803: res.len = snprintf((char*)&res.body, STR_MAX_LEN, "DT#%d-%2.2d-%2.2d-%2.2d:%2.2d:%09.6f", Laurent@711: broken_down_time.tm_year, Laurent@711: broken_down_time.tm_mon, Laurent@711: broken_down_time.tm_day, Laurent@711: broken_down_time.tm_hour, Laurent@711: broken_down_time.tm_min, Laurent@711: (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: Laurent@714: static inline TOD __date_and_time_to_time_of_day(DT IN) { Laurent@714: return (TOD){ Laurent@714: IN.tv_sec % SECONDS_PER_DAY + (IN.tv_sec < 0 ? SECONDS_PER_DAY : 0), Laurent@714: IN.tv_nsec}; Laurent@714: } Laurent@714: static inline DATE __date_and_time_to_date(DT IN){ Laurent@714: return (DATE){ Laurent@714: IN.tv_sec - IN.tv_sec % SECONDS_PER_DAY - (IN.tv_sec < 0 ? SECONDS_PER_DAY : 0), Laurent@714: 0}; Laurent@714: } msousa@350: msousa@350: /*****************/ msousa@350: /* FROM/TO BCD */ msousa@350: /*****************/ Laurent@715: Laurent@715: static inline BOOL __test_bcd(LWORD IN) { Laurent@715: while (IN) { Laurent@715: if ((IN & 0xf) > 9) return 1; Laurent@715: IN >>= 4; Laurent@715: } Laurent@715: return 0; Laurent@715: } Laurent@715: msousa@350: static inline ULINT __bcd_to_uint(LWORD IN){ Laurent@715: ULINT res = IN & 0xf; Laurent@715: ULINT factor = 10ULL; Laurent@715: Laurent@715: while (IN >>= 4) { Laurent@715: res += (IN & 0xf) * factor; Laurent@715: factor *= 10; greg@212: } greg@212: return res; greg@212: } greg@212: msousa@350: static inline LWORD __uint_to_bcd(ULINT IN){ Laurent@715: LWORD res = IN % 10; Laurent@715: USINT shift = 4; Laurent@715: Laurent@715: while (IN /= 10) { Laurent@715: res |= (IN % 10) << shift; Laurent@715: shift += 4; 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, ... */ laurent@393: #define __move_(TYPENAME)\ msousa@350: static inline TYPENAME __move_##TYPENAME(TYPENAME op1) {return op1;} laurent@393: __ANY(__move_) 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 laurent@393: #define EN_ENO EN, 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: laurent@398: /******** [ANY_NUM | ANY_NBIT]_TO_BOOL ************/ laurent@398: #define __convert_num_to_bool(TYPENAME) \ laurent@398: static inline BOOL TYPENAME##_TO_BOOL(EN_ENO_PARAMS, TYPENAME op){\ laurent@398: TEST_EN(BOOL)\ laurent@398: return op == 0 ? 0 : 1;\ laurent@398: } laurent@398: __ANY_NUM(__convert_num_to_bool) laurent@398: __ANY_NBIT(__convert_num_to_bool) laurent@398: laurent@398: /******** [TIME | ANY_DATE]_TO_BOOL ************/ laurent@398: #define __convert_time_to_bool(TYPENAME) \ laurent@398: static inline BOOL TYPENAME##_TO_BOOL(EN_ENO_PARAMS, TYPENAME op){\ laurent@398: TEST_EN(BOOL)\ laurent@398: return op.tv_sec == 0 && op.tv_nsec == 0 ? 0 : 1;\ laurent@398: } laurent@398: __convert_time_to_bool(TIME) laurent@398: __ANY_DATE(__convert_time_to_bool) 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: laurent@398: /******** [ANY_BIT]_TO_[ANY_NUM | ANT_NBIT] ************/ msousa@350: #define __iec_(to_TYPENAME,from_TYPENAME) __convert_type(from_TYPENAME, to_TYPENAME, __move_##to_TYPENAME) msousa@350: __ANY_BIT(__to_anynum_) laurent@398: __ANY_BIT(__to_anynbit_) laurent@398: #undef __iec_ laurent@398: laurent@398: /******** [ANY_INT]_TO_[ANY_NUM | ANT_NBIT] ************/ msousa@350: #define __iec_(to_TYPENAME,from_TYPENAME) __convert_type(from_TYPENAME, to_TYPENAME, __move_##to_TYPENAME) msousa@350: __ANY_INT(__to_anynum_) laurent@398: __ANY_INT(__to_anynbit_) laurent@398: #undef __iec_ laurent@398: laurent@398: /******** [ANY_REAL]_TO_[ANY_NBIT] ************/ msousa@350: #define __iec_(to_TYPENAME,from_TYPENAME) __convert_type(from_TYPENAME, to_TYPENAME, __real_to_bit) laurent@398: __ANY_REAL(__to_anynbit_) laurent@398: #undef __iec_ laurent@398: laurent@398: /******** [ANY_REAL]_TO_[ANY_NINT] ************/ 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) laurent@398: __to_anynbit_(TIME) msousa@350: __ANY_DATE(__to_anyint_) laurent@398: __ANY_DATE(__to_anynbit_) 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) Laurent@705: static inline DATE DATE_AND_TIME_TO_DATE(EN_ENO_PARAMS, DT op){ Laurent@705: return DT_TO_DATE(EN_ENO, op); Laurent@705: } msousa@350: __convert_type(DT, DT, __move_DT) msousa@350: __convert_type(DT, TOD, __date_and_time_to_time_of_day) Laurent@705: static inline DATE DATE_AND_TIME_TO_TIME_OF_DAY(EN_ENO_PARAMS, DT op){ Laurent@705: return DT_TO_TOD(EN_ENO, op); Laurent@705: } 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: Laurent@704: /******** _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);\ Laurent@704: }\ Laurent@704: static inline to_TYPENAME from_TYPENAME##_TO_BCD__##to_TYPENAME##__##from_TYPENAME(EN_ENO_PARAMS, from_TYPENAME op){\ Laurent@704: return from_TYPENAME##_TO_BCD_##to_TYPENAME(EN_ENO, op);\ msousa@350: } msousa@350: __ANY_UINT(__to_anynbit_) msousa@350: #undef __iec_ msousa@350: msousa@350: Laurent@704: /******** 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){\ Laurent@715: TEST_EN_COND(to_TYPENAME, __test_bcd(op))\ msousa@350: return (to_TYPENAME)__bcd_to_uint(op);\ Laurent@704: }\ Laurent@704: static inline to_TYPENAME BCD_TO_##to_TYPENAME##__##to_TYPENAME##__##from_TYPENAME(EN_ENO_PARAMS, from_TYPENAME op){\ Laurent@704: return from_TYPENAME##_BCD_TO_##to_TYPENAME(EN_ENO, 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) \ laurent@393: /* explicitly typed function */\ msousa@350: static inline TYPENAME fname##TYPENAME(EN_ENO_PARAMS, TYPENAME op){\ msousa@350: TEST_EN(TYPENAME)\ msousa@350: return FUNC(op);\ laurent@393: }\ laurent@393: /* overloaded function */\ laurent@393: static inline TYPENAME fname##_##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, TYPENAME op) {\ laurent@393: return fname##TYPENAME(EN_ENO, 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: /**************/ laurent@393: #define __abs_signed(TYPENAME) \ laurent@393: /* explicitly typed function */\ 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;\ laurent@393: }\ laurent@393: /* overloaded function */\ laurent@393: static inline TYPENAME ABS__##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, TYPENAME op) {\ laurent@393: return ABS_##TYPENAME(EN_ENO, op);\ laurent@393: } laurent@393: laurent@393: #define __abs_unsigned(TYPENAME) \ laurent@393: /* explicitly typed function */\ msousa@350: static inline TYPENAME ABS_##TYPENAME(EN_ENO_PARAMS, TYPENAME op){\ msousa@350: TEST_EN(TYPENAME)\ msousa@350: return op;\ laurent@393: }\ laurent@393: /* overloaded function */\ laurent@393: static inline TYPENAME ABS__##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, TYPENAME op) {\ laurent@393: return ABS_##TYPENAME(EN_ENO, op);\ laurent@393: } laurent@393: laurent@393: __ANY_REAL(__abs_signed) laurent@393: __ANY_SINT(__abs_signed) laurent@393: __ANY_UINT(__abs_unsigned) msousa@350: msousa@350: msousa@350: /**************/ msousa@350: /* SQRT */ msousa@350: /**************/ laurent@393: #define __sqrt(TYPENAME) __numeric(SQRT_, TYPENAME, sqrt) laurent@393: __ANY_REAL(__sqrt) laurent@393: laurent@393: laurent@393: /**************/ msousa@350: /* LN */ msousa@350: /**************/ laurent@393: #define __ln(TYPENAME) __numeric(LN_, TYPENAME, log) laurent@393: __ANY_REAL(__ln) msousa@350: msousa@350: msousa@350: /**************/ msousa@350: /* LOG */ msousa@350: /**************/ laurent@393: #define __log(TYPENAME) __numeric(LOG_, TYPENAME, log10) laurent@393: __ANY_REAL(__log) laurent@393: msousa@350: msousa@350: /**************/ msousa@350: /* EXP */ msousa@350: /**************/ laurent@393: #define __exp(TYPENAME) __numeric(EXP_, TYPENAME, exp) laurent@393: __ANY_REAL(__exp) msousa@350: msousa@350: msousa@350: /**************/ msousa@350: /* SIN */ msousa@350: /**************/ laurent@393: #define __sin(TYPENAME) __numeric(SIN_, TYPENAME, sin) laurent@393: __ANY_REAL(__sin) msousa@350: msousa@350: msousa@350: /**************/ msousa@350: /* COS */ msousa@350: /**************/ laurent@393: #define __cos(TYPENAME) __numeric(COS_, TYPENAME, cos) laurent@393: __ANY_REAL(__cos) msousa@350: msousa@350: /**************/ msousa@350: /* TAN */ msousa@350: /**************/ laurent@393: #define __tan(TYPENAME) __numeric(TAN_, TYPENAME, tan) laurent@393: __ANY_REAL(__tan) msousa@350: msousa@350: msousa@350: /**************/ msousa@350: /* ASIN */ msousa@350: /**************/ laurent@393: #define __asin(TYPENAME) __numeric(ASIN_, TYPENAME, asin) laurent@393: __ANY_REAL(__asin) msousa@350: msousa@350: /**************/ msousa@350: /* ACOS */ msousa@350: /**************/ laurent@393: #define __acos(TYPENAME) __numeric(ACOS_, TYPENAME, acos) laurent@393: __ANY_REAL(__acos) msousa@350: msousa@350: /**************/ msousa@350: /* ATAN */ msousa@350: /**************/ laurent@393: #define __atan(TYPENAME) __numeric(ATAN_, TYPENAME, atan) laurent@393: __ANY_REAL(__atan) msousa@350: msousa@350: msousa@350: msousa@350: /*****************************************************/ msousa@350: /*** Table 24 - Standard arithmetic functions ***/ msousa@350: /*****************************************************/ laurent@393: laurent@393: #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: laurent@393: #define __arith_static(fname,TYPENAME, OP)\ laurent@393: /* explicitly typed function */\ laurent@393: static inline TYPENAME fname##TYPENAME(EN_ENO_PARAMS, TYPENAME op1, TYPENAME op2){\ lbessard@149: TEST_EN(TYPENAME)\ lbessard@149: return op1 OP op2;\ laurent@393: }\ laurent@393: /* overloaded function */\ laurent@393: static inline TYPENAME fname##_##TYPENAME##__##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, TYPENAME op1, TYPENAME op2){\ laurent@393: return fname##TYPENAME(EN_ENO, op1, op2);\ lbessard@149: } lbessard@149: msousa@350: /**************/ msousa@350: /* ADD */ msousa@350: /**************/ laurent@393: #define __add(TYPENAME) \ msousa@350: __arith_expand(ADD_##TYPENAME, TYPENAME, +) /* explicitly typed function */\ msousa@350: __arith_expand(ADD__##TYPENAME##__##TYPENAME, TYPENAME, +) /* overloaded function */ laurent@393: __ANY_NUM(__add) laurent@393: msousa@350: msousa@350: /**************/ msousa@350: /* MUL */ msousa@350: /**************/ laurent@393: #define __mul(TYPENAME) \ msousa@350: __arith_expand(MUL_##TYPENAME, TYPENAME, *) /* explicitly typed function */\ msousa@350: __arith_expand(MUL__##TYPENAME##__##TYPENAME, TYPENAME, *) /* overloaded function */ laurent@393: __ANY_NUM(__mul) laurent@393: msousa@350: msousa@350: /**************/ msousa@350: /* SUB */ msousa@350: /**************/ laurent@393: #define __sub(TYPENAME) __arith_static(SUB_, TYPENAME, -) laurent@393: __ANY_NUM(__sub) laurent@393: msousa@350: msousa@350: /**************/ msousa@350: /* DIV */ msousa@350: /**************/ laurent@393: #define __div(TYPENAME)\ laurent@393: /* The explicitly typed standard functions */\ 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;\ laurent@393: }\ laurent@393: /* The overloaded standard functions */\ msousa@350: static inline TYPENAME DIV__##TYPENAME##__##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, TYPENAME op1, TYPENAME op2){\ laurent@393: return DIV_##TYPENAME(EN_ENO, op1, op2);\ laurent@393: } laurent@393: __ANY_NUM(__div) laurent@393: laurent@393: laurent@393: /**************/ laurent@393: /* MOD */ laurent@393: /**************/ laurent@393: #define __mod(TYPENAME)\ laurent@393: /* The explicitly typed standard functions */\ laurent@393: static inline TYPENAME MOD_##TYPENAME(EN_ENO_PARAMS, TYPENAME op1, TYPENAME op2){\ laurent@398: TEST_EN(TYPENAME)\ laurent@398: if (op2 == 0) return 0;\ laurent@393: return op1 % op2;\ laurent@393: }\ laurent@393: /* The overloaded standard functions */\ laurent@393: static inline TYPENAME MOD__##TYPENAME##__##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, TYPENAME op1, TYPENAME op2){\ laurent@398: return MOD_##TYPENAME(EN_ENO, op1, op2);\ laurent@393: } laurent@393: __ANY_INT(__mod) 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: Edouard@378: #define __STR_CMP(str1, str2) memcmp((char*)&str1.body,(char*)&str2.body, str1.len < str2.len ? str1.len : str2.len) Edouard@378: 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) \ laurent@398: static inline in2_TYPENAME MUX__##in2_TYPENAME##__##in1_TYPENAME##__##in2_TYPENAME(EN_ENO_PARAMS, in1_TYPENAME K, UINT param_count, ...){\ 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: \ laurent@398: va_start (ap, param_count); /* 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: /**************/ laurent@398: #define __ne_num(fname, TYPENAME) \ laurent@398: static inline BOOL fname(EN_ENO_PARAMS, TYPENAME op1, TYPENAME op2){\ laurent@398: TEST_EN(BOOL)\ laurent@398: return op1 != op2 ? 1 : 0;\ laurent@398: } laurent@398: laurent@398: #define __ne_time(fname, TYPENAME) \ laurent@398: static inline BOOL fname(EN_ENO_PARAMS, TYPENAME op1, TYPENAME op2){\ laurent@398: TEST_EN(BOOL)\ laurent@398: return __time_cmp(op1, op2) != 0 ? 1 : 0;\ laurent@398: } laurent@398: Laurent@638: #define __ne_string(fname, TYPENAME) \ Laurent@638: static inline BOOL fname(EN_ENO_PARAMS, TYPENAME op1, TYPENAME op2){\ Laurent@638: TEST_EN(BOOL)\ Laurent@638: return __STR_CMP(op1, op2) != 0 ? 1 : 0;\ Laurent@638: } laurent@398: msousa@350: /* Comparison for numerical data types */ msousa@350: #define __iec_(TYPENAME) \ laurent@398: __ne_num(NE_##TYPENAME, TYPENAME) /* The explicitly typed standard functions */\ laurent@398: __ne_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) \ laurent@398: __ne_time(NE_##TYPENAME, TYPENAME) /* The explicitly typed standard functions */\ laurent@398: __ne_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 */ Laurent@638: __ne_string(NE_STRING, STRING) /* The explicitly typed standard functions */ Laurent@638: __ne_string(NE__BOOL__STRING__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: 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: Edouard@384: #define __left(TYPENAME) \ Edouard@384: static inline STRING LEFT__STRING__STRING__##TYPENAME(EN_ENO_PARAMS, STRING IN, TYPENAME L){\ Edouard@384: STRING res;\ Edouard@384: TEST_EN_COND(STRING, L < 0)\ Edouard@384: res = __INIT_STRING;\ Edouard@384: L = L < (TYPENAME)IN.len ? L : (TYPENAME)IN.len;\ conti@696: memcpy(&res.body, &IN.body, (size_t)L);\ conti@696: res.len = (__strlen_t)L;\ Edouard@384: return res;\ Edouard@384: } Edouard@384: __ANY_INT(__left) msousa@350: msousa@350: msousa@350: /*****************/ msousa@350: /* RIGHT */ msousa@350: /*****************/ msousa@350: laurent@379: #define __right(TYPENAME) \ laurent@379: static inline STRING RIGHT__STRING__STRING__##TYPENAME(EN_ENO_PARAMS, STRING IN, TYPENAME L){\ laurent@379: STRING res;\ msousa@350: TEST_EN_COND(STRING, L < 0)\ laurent@379: res = __INIT_STRING;\ Edouard@384: L = L < (TYPENAME)IN.len ? L : (TYPENAME)IN.len;\ conti@696: memcpy(&res.body, &IN.body[(TYPENAME)IN.len - L], (size_t)L);\ conti@696: res.len = (__strlen_t)L;\ laurent@379: return res;\ laurent@379: } laurent@379: __ANY_INT(__right) msousa@350: msousa@350: msousa@350: /***************/ msousa@350: /* MID */ msousa@350: /***************/ msousa@350: laurent@379: #define __mid(TYPENAME) \ laurent@379: static inline STRING MID__STRING__STRING__##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, STRING IN, TYPENAME L, TYPENAME P){\ laurent@379: STRING res;\ msousa@350: TEST_EN_COND(STRING, L < 0 || P < 0)\ laurent@379: res = __INIT_STRING;\ Edouard@384: if(P <= (TYPENAME)IN.len){\ laurent@379: P -= 1; /* now can be used as [index]*/\ Edouard@384: L = L + P <= (TYPENAME)IN.len ? L : (TYPENAME)IN.len - P;\ conti@696: memcpy(&res.body, &IN.body[P] , (size_t)L);\ conti@696: res.len = (__strlen_t)L;\ laurent@379: }\ laurent@379: return res;\ laurent@379: } laurent@379: __ANY_INT(__mid) msousa@350: msousa@350: msousa@350: /******************/ msousa@350: /* CONCAT */ msousa@350: /******************/ msousa@350: laurent@379: static inline STRING CONCAT(EN_ENO_PARAMS, UINT param_count, ...){ msousa@350: UINT i; msousa@350: STRING res; msousa@350: va_list ap; msousa@350: __strlen_t charcount; laurent@379: TEST_EN(STRING) 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)\ conti@696: return (STRING)__insert(str1,str2,(__strlen_t)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)\ conti@696: return (STRING)__delete(str,(__strlen_t)L,(__strlen_t)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)\ conti@696: return (STRING)__replace(str1,str2,(__strlen_t)L,(__strlen_t)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)\ conti@696: return __time_mul(IN1, (LREAL)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)\ conti@696: return __time_mul(IN1, (LREAL)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)\ conti@696: return __time_div(IN1, (LREAL)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)\ conti@696: return __time_div(IN1, (LREAL)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? */ msousa@739: msousa@739: #endif /* _IEC_STD_LIB_H */