1 /* |
|
2 * copyright 2008 Edouard TISSERANT |
|
3 * copyright 2011 Mario de Sousa (msousa@fe.up.pt) |
|
4 * |
|
5 * Offered to the public under the terms of the GNU Lesser General Public |
|
6 * License as published by the Free Software Foundation; either version 2 |
|
7 * of the License, or (at your option) any later version. |
|
8 * |
|
9 * This program is distributed in the hope that it will be useful, but |
|
10 * WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser |
|
12 * General Public License for more details. |
|
13 * |
|
14 * This code is made available on the understanding that it will not be |
|
15 * used in safety-critical situations without a full and competent review. |
|
16 */ |
|
17 |
|
18 /**** |
|
19 * IEC 61131-3 standard function library |
|
20 */ |
|
21 |
|
22 /* NOTE: This file is full of (what may seem at first) very strange macros. |
|
23 * If you want to know what all these strange macros are doing, |
|
24 * just parse this file through a C preprocessor (e.g. cpp), |
|
25 * and analyse the output! |
|
26 * $gcc -E iec_std_lib.h |
|
27 */ |
|
28 |
|
29 #ifndef _IEC_STD_LIB_H |
|
30 #define _IEC_STD_LIB_H |
|
31 |
|
32 |
|
33 #include <limits.h> |
|
34 #include <float.h> |
|
35 #include <math.h> |
|
36 #include <stdint.h> |
|
37 #include <ctype.h> |
|
38 |
|
39 #include <stdio.h> |
|
40 #include <stdlib.h> |
|
41 #include <string.h> |
|
42 #include <stdarg.h> |
|
43 |
|
44 #ifdef DEBUG_IEC |
|
45 #define DBG(...) printf(__VA_ARGS__); |
|
46 #define DBG_TYPE(TYPENAME, name) __print_##TYPENAME(name); |
|
47 #else |
|
48 #define DBG(...) |
|
49 #define DBG_TYPE(TYPENAME, name) |
|
50 #endif |
|
51 |
|
52 /* |
|
53 * Include type defs. |
|
54 */ |
|
55 #include "iec_types_all.h" |
|
56 |
|
57 extern TIME __CURRENT_TIME; |
|
58 extern BOOL __DEBUG; |
|
59 |
|
60 /* TODO |
|
61 typedef struct { |
|
62 __strlen_t len; |
|
63 u_int16_t body[STR_MAX_LEN]; |
|
64 } WSTRING; |
|
65 */ |
|
66 /* |
|
67 # if __WORDSIZE == 64 |
|
68 #define __32b_sufix |
|
69 #define __64b_sufix L |
|
70 #else |
|
71 #define __32b_sufix L |
|
72 #define __64b_sufix LL |
|
73 #endif |
|
74 */ |
|
75 |
|
76 # if __WORDSIZE == 64 |
|
77 #define __32b_sufix |
|
78 #define __64b_sufix L |
|
79 #else |
|
80 #define __32b_sufix L |
|
81 /* changed this from LL to L temporarily. It was causing a bug when compiling resulting code with gcc. |
|
82 * I have other things to worry about at the moment.. |
|
83 */ |
|
84 #define __64b_sufix L |
|
85 #endif |
|
86 |
|
87 |
|
88 #define __lit(type,value,...) (type)value##__VA_ARGS__ |
|
89 // Keep this macro expention step to let sfx(__VA_ARGS__) change into L or LL |
|
90 #define __literal(type,value,...) __lit(type,value,__VA_ARGS__) |
|
91 |
|
92 #define __BOOL_LITERAL(value) __literal(BOOL,value) |
|
93 #define __SINT_LITERAL(value) __literal(SINT,value) |
|
94 #define __INT_LITERAL(value) __literal(INT,value) |
|
95 #define __DINT_LITERAL(value) __literal(DINT,value,__32b_sufix) |
|
96 #define __LINT_LITERAL(value) __literal(LINT,value,__64b_sufix) |
|
97 #define __USINT_LITERAL(value) __literal(USINT,value) |
|
98 #define __UINT_LITERAL(value) __literal(UINT,value) |
|
99 #define __UDINT_LITERAL(value) __literal(UDINT,value,__32b_sufix) |
|
100 #define __ULINT_LITERAL(value) __literal(ULINT,value,__64b_sufix) |
|
101 #define __REAL_LITERAL(value) __literal(REAL,value,__32b_sufix) |
|
102 #define __LREAL_LITERAL(value) __literal(LREAL,value,__64b_sufix) |
|
103 #define __TIME_LITERAL(value) __literal(TIME,value) |
|
104 #define __DATE_LITERAL(value) __literal(DATE,value) |
|
105 #define __TOD_LITERAL(value) __literal(TOD,value) |
|
106 #define __DT_LITERAL(value) __literal(DT,value) |
|
107 #define __STRING_LITERAL(count,value) (STRING){count,value} |
|
108 #define __BYTE_LITERAL(value) __literal(BYTE,value) |
|
109 #define __WORD_LITERAL(value) __literal(WORD,value) |
|
110 #define __DWORD_LITERAL(value) __literal(DWORD,value,__32b_sufix) |
|
111 #define __LWORD_LITERAL(value) __literal(LWORD,value,__64b_sufix) |
|
112 |
|
113 |
|
114 typedef union __IL_DEFVAR_T { |
|
115 BOOL BOOLvar; |
|
116 |
|
117 SINT SINTvar; |
|
118 INT INTvar; |
|
119 DINT DINTvar; |
|
120 LINT LINTvar; |
|
121 |
|
122 USINT USINTvar; |
|
123 UINT UINTvar; |
|
124 UDINT UDINTvar; |
|
125 ULINT ULINTvar; |
|
126 |
|
127 BYTE BYTEvar; |
|
128 WORD WORDvar; |
|
129 DWORD DWORDvar; |
|
130 LWORD LWORDvar; |
|
131 |
|
132 REAL REALvar; |
|
133 LREAL LREALvar; |
|
134 |
|
135 TIME TIMEvar; |
|
136 TOD TODvar; |
|
137 DT DTvar; |
|
138 DATE DATEvar; |
|
139 } __IL_DEFVAR_T; |
|
140 |
|
141 |
|
142 /**********************************************************************/ |
|
143 /**********************************************************************/ |
|
144 /***** *****/ |
|
145 /***** Some helper functions... *****/ |
|
146 /***** ...used later: *****/ |
|
147 /***** - when declaring the IEC 61131-3 standard functions *****/ |
|
148 /***** - in the C source code itself in SFC and ST expressions *****/ |
|
149 /***** *****/ |
|
150 /**********************************************************************/ |
|
151 /**********************************************************************/ |
|
152 |
|
153 |
|
154 /****************************/ |
|
155 /* Notify IEC runtime error */ |
|
156 /****************************/ |
|
157 |
|
158 /* function that generates an IEC runtime error */ |
|
159 static inline void __iec_error(void) { |
|
160 /* TODO... */ |
|
161 fprintf(stderr, "IEC 61131-3 runtime error.\n"); |
|
162 /*exit(1);*/ |
|
163 } |
|
164 |
|
165 /*******************************/ |
|
166 /* Time normalization function */ |
|
167 /*******************************/ |
|
168 |
|
169 static inline void __normalize_timespec (IEC_TIMESPEC *ts) { |
|
170 if( ts->tv_nsec < -1000000000 || (( ts->tv_sec > 0 ) && ( ts->tv_nsec < 0 ))){ |
|
171 ts->tv_sec--; |
|
172 ts->tv_nsec += 1000000000; |
|
173 } |
|
174 if( ts->tv_nsec > 1000000000 || (( ts->tv_sec < 0 ) && ( ts->tv_nsec > 0 ))){ |
|
175 ts->tv_sec++; |
|
176 ts->tv_nsec -= 1000000000; |
|
177 } |
|
178 } |
|
179 |
|
180 /**********************************************/ |
|
181 /* Time conversion to/from timespec functions */ |
|
182 /**********************************************/ |
|
183 /* NOTE: The following function was turned into a macro, so it could be used to initialize the initial value of TIME variables. |
|
184 * Since each macro parameter is evaluated several times, the macro may result in multiple function invocations if an expression |
|
185 * containing a function invocation is passed as a parameter. However, currently matiec only uses this conversion macro with |
|
186 * constant literals, so it is safe to change it into a macro. |
|
187 */ |
|
188 /* NOTE: I (Mario - msousa@fe.up.pt) believe that the following function contains a bug when handling negative times. |
|
189 * The equivalent macro has this bug fixed. |
|
190 * e.g.; |
|
191 * T#3.8s |
|
192 * using the function, will result in a timespec of 3.8s !!!: |
|
193 * tv_sec = 4 <----- 1 * 3.8 is rounded up when converting a double to an int! |
|
194 * tv_nsec = -200 000 000 <----- 1 * (3.8 - 4)*1e9 |
|
195 * |
|
196 * -T#3.8s |
|
197 * using the function, will result in a timespec of -11.8s !!!: |
|
198 * tv_sec = -4 <----- -1 * 3.8 is rounded down when converting a double to an int! |
|
199 * tv_nsec = -7 800 000 000 <----- -1 * (3.8 - -4)*1e9 |
|
200 */ |
|
201 /* NOTE: Due to the fact that the C compiler may round a tv_sec number away from zero, |
|
202 * the following macro may result in a timespec that is not normalized, i.e. with a tv_sec > 0, and a tv_nsec < 0 !!!! |
|
203 * This is due to the rounding that C compiler applies when converting a (long double) to a (long int). |
|
204 * To produce normalized timespec's we need to use floor(), but we cannot call any library functions since we want this macro to be |
|
205 * useable as a variable initializer. |
|
206 * VAR x : TIME = T#3.5h; END_VAR ---> IEC_TIME x = __time_to_timespec(1, 0, 0, 0, 3.5, 0); |
|
207 */ |
|
208 /* |
|
209 static inline IEC_TIMESPEC __time_to_timespec(int sign, double mseconds, double seconds, double minutes, double hours, double days) { |
|
210 IEC_TIMESPEC ts; |
|
211 |
|
212 // sign is 1 for positive values, -1 for negative time... |
|
213 long double total_sec = ((days*24 + hours)*60 + minutes)*60 + seconds + mseconds/1e3; |
|
214 if (sign >= 0) sign = 1; else sign = -1; |
|
215 ts.tv_sec = sign * (long int)total_sec; |
|
216 ts.tv_nsec = sign * (long int)((total_sec - ts.tv_sec)*1e9); |
|
217 |
|
218 return ts; |
|
219 } |
|
220 */ |
|
221 /* NOTE: Unfortunately older versions of ANSI C (e.g. C99) do not allow explicit identification of elements in initializers |
|
222 * e.g. {tv_sec = 1, tv_nsec = 300} |
|
223 * They are therefore commented out. This however means that any change to the definition of IEC_TIMESPEC may require this |
|
224 * macro to be updated too! |
|
225 */ |
|
226 #define __time_to_timespec(sign,mseconds,seconds,minutes,hours,days) \ |
|
227 ((IEC_TIMESPEC){\ |
|
228 /*tv_sec =*/ ((long int) (((sign>=0)?1:-1)*((((long double)days*24 + (long double)hours)*60 + (long double)minutes)*60 + (long double)seconds + (long double)mseconds/1e3))), \ |
|
229 /*tv_nsec =*/ ((long int)(( \ |
|
230 ((long double)(((sign>=0)?1:-1)*((((long double)days*24 + (long double)hours)*60 + (long double)minutes)*60 + (long double)seconds + (long double)mseconds/1e3))) - \ |
|
231 ((long int) (((sign>=0)?1:-1)*((((long double)days*24 + (long double)hours)*60 + (long double)minutes)*60 + (long double)seconds + (long double)mseconds/1e3))) \ |
|
232 )*1e9))\ |
|
233 }) |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 /* NOTE: The following function was turned into a macro, so it could be used to initialize the initial value of TOD (TIME_OF_DAY) variables */ |
|
239 /* NOTE: many (but not all) of the same comments made regarding __time_to_timespec() are also valid here, so go and read those comments too! |
|
240 /* |
|
241 static inline IEC_TIMESPEC __tod_to_timespec(double seconds, double minutes, double hours) { |
|
242 IEC_TIMESPEC ts; |
|
243 |
|
244 long double total_sec = (hours*60 + minutes)*60 + seconds; |
|
245 ts.tv_sec = (long int)total_sec; |
|
246 ts.tv_nsec = (long int)((total_sec - ts.tv_sec)*1e9); |
|
247 |
|
248 return ts; |
|
249 } |
|
250 */ |
|
251 #define __tod_to_timespec(seconds,minutes,hours) \ |
|
252 ((IEC_TIMESPEC){\ |
|
253 /*tv_sec =*/ ((long int) ((((long double)hours)*60 + (long double)minutes)*60 + (long double)seconds)), \ |
|
254 /*tv_nsec =*/ ((long int)(( \ |
|
255 ((long double)((((long double)hours)*60 + (long double)minutes)*60 + (long double)seconds)) - \ |
|
256 ((long int) ((((long double)hours)*60 + (long double)minutes)*60 + (long double)seconds)) \ |
|
257 )*1e9))\ |
|
258 }) |
|
259 |
|
260 |
|
261 #define EPOCH_YEAR 1970 |
|
262 #define SECONDS_PER_MINUTE 60 |
|
263 #define SECONDS_PER_HOUR (60 * SECONDS_PER_MINUTE) |
|
264 #define SECONDS_PER_DAY (24 * SECONDS_PER_HOUR) |
|
265 #define __isleap(year) \ |
|
266 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0)) |
|
267 static const unsigned short int __mon_yday[2][13] = |
|
268 { |
|
269 /* Normal years. */ |
|
270 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}, |
|
271 /* Leap years. */ |
|
272 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366} |
|
273 }; |
|
274 |
|
275 typedef struct { |
|
276 int tm_sec; /* Seconds. [0-60] (1 leap second) */ |
|
277 int tm_min; /* Minutes. [0-59] */ |
|
278 int tm_hour; /* Hours. [0-23] */ |
|
279 int tm_day; /* Day. [1-31] */ |
|
280 int tm_mon; /* Month. [0-11] */ |
|
281 int tm_year; /* Year */ |
|
282 } tm; |
|
283 |
|
284 static inline tm convert_seconds_to_date_and_time(long int seconds) { |
|
285 tm dt; |
|
286 long int days, rem; |
|
287 days = seconds / SECONDS_PER_DAY; |
|
288 rem = seconds % SECONDS_PER_DAY; |
|
289 if (rem < 0) { |
|
290 rem += SECONDS_PER_DAY; |
|
291 days--; |
|
292 } |
|
293 |
|
294 // time of day |
|
295 dt.tm_hour = rem / SECONDS_PER_HOUR; |
|
296 rem %= SECONDS_PER_HOUR; |
|
297 dt.tm_min = rem / 60; |
|
298 dt.tm_sec = rem % 60; |
|
299 |
|
300 // date |
|
301 dt.tm_year = EPOCH_YEAR; |
|
302 while (days >= (rem = __isleap(dt.tm_year) ? 366 : 365)) { |
|
303 dt.tm_year++; |
|
304 days -= rem; |
|
305 } |
|
306 while (days < 0) { |
|
307 dt.tm_year--; |
|
308 days += __isleap(dt.tm_year) ? 366 : 365; |
|
309 } |
|
310 dt.tm_mon = 1; |
|
311 while (days > __mon_yday[__isleap(dt.tm_year)][dt.tm_mon]) { |
|
312 dt.tm_mon += 1; |
|
313 } |
|
314 dt.tm_day = days - __mon_yday[__isleap(dt.tm_year)][dt.tm_mon - 1] + 1; |
|
315 |
|
316 return dt; |
|
317 } |
|
318 |
|
319 static inline IEC_TIMESPEC __date_to_timespec(int day, int month, int year) { |
|
320 IEC_TIMESPEC ts; |
|
321 int a4, b4, a100, b100, a400, b400; |
|
322 int yday; |
|
323 int intervening_leap_days; |
|
324 |
|
325 if (month < 1 || month > 12) |
|
326 __iec_error(); |
|
327 |
|
328 yday = __mon_yday[__isleap(year)][month - 1] + day; |
|
329 |
|
330 if (yday > __mon_yday[__isleap(year)][month]) |
|
331 __iec_error(); |
|
332 |
|
333 a4 = (year >> 2) - ! (year & 3); |
|
334 b4 = (EPOCH_YEAR >> 2) - ! (EPOCH_YEAR & 3); |
|
335 a100 = a4 / 25 - (a4 % 25 < 0); |
|
336 b100 = b4 / 25 - (b4 % 25 < 0); |
|
337 a400 = a100 >> 2; |
|
338 b400 = b100 >> 2; |
|
339 intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400); |
|
340 |
|
341 ts.tv_sec = ((year - EPOCH_YEAR) * 365 + intervening_leap_days + yday - 1) * 24 * 60 * 60; |
|
342 ts.tv_nsec = 0; |
|
343 |
|
344 return ts; |
|
345 } |
|
346 |
|
347 static inline IEC_TIMESPEC __dt_to_timespec(double seconds, double minutes, double hours, int day, int month, int year) { |
|
348 IEC_TIMESPEC ts_date = __date_to_timespec(day, month, year); |
|
349 IEC_TIMESPEC ts = __tod_to_timespec(seconds, minutes, hours); |
|
350 |
|
351 ts.tv_sec += ts_date.tv_sec; |
|
352 |
|
353 return ts; |
|
354 } |
|
355 |
|
356 /*******************/ |
|
357 /* Time operations */ |
|
358 /*******************/ |
|
359 |
|
360 #define __time_cmp(t1, t2) (t2.tv_sec == t1.tv_sec ? t1.tv_nsec - t2.tv_nsec : t1.tv_sec - t2.tv_sec) |
|
361 |
|
362 static inline TIME __time_add(TIME IN1, TIME IN2){ |
|
363 TIME res ={IN1.tv_sec + IN2.tv_sec, |
|
364 IN1.tv_nsec + IN2.tv_nsec }; |
|
365 __normalize_timespec(&res); |
|
366 return res; |
|
367 } |
|
368 static inline TIME __time_sub(TIME IN1, TIME IN2){ |
|
369 TIME res ={IN1.tv_sec - IN2.tv_sec, |
|
370 IN1.tv_nsec - IN2.tv_nsec }; |
|
371 __normalize_timespec(&res); |
|
372 return res; |
|
373 } |
|
374 static inline TIME __time_mul(TIME IN1, LREAL IN2){ |
|
375 LREAL s_f = IN1.tv_sec * IN2; |
|
376 time_t s = (time_t)s_f; |
|
377 div_t ns = div((int)((LREAL)IN1.tv_nsec * IN2), 1000000000); |
|
378 TIME res = {(long)s + ns.quot, |
|
379 (long)ns.rem + (s_f - s) * 1000000000 }; |
|
380 __normalize_timespec(&res); |
|
381 return res; |
|
382 } |
|
383 static inline TIME __time_div(TIME IN1, LREAL IN2){ |
|
384 LREAL s_f = IN1.tv_sec / IN2; |
|
385 time_t s = (time_t)s_f; |
|
386 TIME res = {(long)s, |
|
387 (long)(IN1.tv_nsec / IN2 + (s_f - s) * 1000000000) }; |
|
388 __normalize_timespec(&res); |
|
389 return res; |
|
390 } |
|
391 |
|
392 |
|
393 /***************/ |
|
394 /* Convertions */ |
|
395 /***************/ |
|
396 /*****************/ |
|
397 /* REAL_TO_INT */ |
|
398 /*****************/ |
|
399 static inline LINT __real_round(LREAL IN) { |
|
400 return fmod(IN, 1) == 0 ? ((LINT)IN / 2) * 2 : (LINT)IN; |
|
401 } |
|
402 static inline LINT __preal_to_sint(LREAL IN) { |
|
403 return IN >= 0 ? __real_round(IN + 0.5) : __real_round(IN - 0.5); |
|
404 } |
|
405 static inline LINT __preal_to_uint(LREAL IN) { |
|
406 return IN >= 0 ? __real_round(IN + 0.5) : 0; |
|
407 } |
|
408 static inline LINT __real_to_sint(LREAL IN) {return (LINT)__preal_to_sint(IN);} |
|
409 static inline LWORD __real_to_bit(LREAL IN) {return (LWORD)__preal_to_uint(IN);} |
|
410 static inline ULINT __real_to_uint(LREAL IN) {return (ULINT)__preal_to_uint(IN);} |
|
411 |
|
412 /***************/ |
|
413 /* TO_STRING */ |
|
414 /***************/ |
|
415 static inline STRING __bool_to_string(BOOL IN) { |
|
416 if(IN) return (STRING){4, "TRUE"}; |
|
417 return (STRING){5,"FALSE"}; |
|
418 } |
|
419 static inline STRING __bit_to_string(LWORD IN) { |
|
420 STRING res; |
|
421 res = __INIT_STRING; |
|
422 res.len = snprintf((char*)res.body, STR_MAX_LEN, "16#%llx",(long long unsigned int)IN); |
|
423 if(res.len > STR_MAX_LEN) res.len = STR_MAX_LEN; |
|
424 return res; |
|
425 } |
|
426 static inline STRING __real_to_string(LREAL IN) { |
|
427 STRING res; |
|
428 res = __INIT_STRING; |
|
429 res.len = snprintf((char*)res.body, STR_MAX_LEN, "%.10g", IN); |
|
430 if(res.len > STR_MAX_LEN) res.len = STR_MAX_LEN; |
|
431 return res; |
|
432 } |
|
433 static inline STRING __sint_to_string(LINT IN) { |
|
434 STRING res; |
|
435 res = __INIT_STRING; |
|
436 res.len = snprintf((char*)res.body, STR_MAX_LEN, "%lld", (long long int)IN); |
|
437 if(res.len > STR_MAX_LEN) res.len = STR_MAX_LEN; |
|
438 return res; |
|
439 } |
|
440 static inline STRING __uint_to_string(ULINT IN) { |
|
441 STRING res; |
|
442 res = __INIT_STRING; |
|
443 res.len = snprintf((char*)res.body, STR_MAX_LEN, "%llu", (long long unsigned int)IN); |
|
444 if(res.len > STR_MAX_LEN) res.len = STR_MAX_LEN; |
|
445 return res; |
|
446 } |
|
447 /***************/ |
|
448 /* FROM_STRING */ |
|
449 /***************/ |
|
450 static inline BOOL __string_to_bool(STRING IN) { |
|
451 int i; |
|
452 if (IN.len == 1) return !memcmp(&IN.body,"1", IN.len); |
|
453 for (i = 0; i < IN.len; i++) IN.body[i] = toupper(IN.body[i]); |
|
454 return IN.len == 4 ? !memcmp(&IN.body,"TRUE", IN.len) : 0; |
|
455 } |
|
456 |
|
457 static inline LINT __pstring_to_sint(STRING* IN) { |
|
458 LINT res = 0; |
|
459 __strlen_t l; |
|
460 unsigned int shift = 0; |
|
461 |
|
462 if(IN->body[0]=='2' && IN->body[1]=='#'){ |
|
463 /* 2#0101_1010_1011_1111 */ |
|
464 for(l = IN->len - 1; l >= 2 && shift < 64; l--) |
|
465 { |
|
466 char c = IN->body[l]; |
|
467 if( c >= '0' && c <= '1'){ |
|
468 res |= ( c - '0') << shift; |
|
469 shift += 1; |
|
470 } |
|
471 } |
|
472 }else if(IN->body[0]=='8' && IN->body[1]=='#'){ |
|
473 /* 8#1234_5665_4321 */ |
|
474 for(l = IN->len - 1; l >= 2 && shift < 64; l--) |
|
475 { |
|
476 char c = IN->body[l]; |
|
477 if( c >= '0' && c <= '7'){ |
|
478 res |= ( c - '0') << shift; |
|
479 shift += 3; |
|
480 } |
|
481 } |
|
482 }else if(IN->body[0]=='1' && IN->body[1]=='6' && IN->body[2]=='#'){ |
|
483 /* 16#1234_5678_9abc_DEFG */ |
|
484 for(l = IN->len - 1; l >= 3 && shift < 64; l--) |
|
485 { |
|
486 char c = IN->body[l]; |
|
487 if( c >= '0' && c <= '9'){ |
|
488 res |= (LWORD)( c - '0') << shift; |
|
489 shift += 4; |
|
490 }else if( c >= 'a' && c <= 'f'){ |
|
491 res |= (LWORD)( c - 'a' + 10 ) << shift; |
|
492 shift += 4; |
|
493 }else if( c >= 'A' && c <= 'F'){ |
|
494 res |= (LWORD)( c - 'A' + 10 ) << shift; |
|
495 shift += 4; |
|
496 } |
|
497 } |
|
498 }else{ |
|
499 /* -123456789 */ |
|
500 LINT fac = IN->body[0] == '-' ? -1 : 1; |
|
501 for(l = IN->len - 1; l >= 0 && shift < 20; l--) |
|
502 { |
|
503 char c = IN->body[l]; |
|
504 if( c >= '0' && c <= '9'){ |
|
505 res += ( c - '0') * fac; |
|
506 fac *= 10; |
|
507 shift += 1; |
|
508 }else if( c >= '.' ){ /* reset value */ |
|
509 res = 0; |
|
510 fac = IN->body[0] == '-' ? -1 : 1; |
|
511 shift = 0; |
|
512 } |
|
513 } |
|
514 } |
|
515 return res; |
|
516 } |
|
517 |
|
518 static inline LINT __string_to_sint(STRING IN) {return (LINT)__pstring_to_sint(&IN);} |
|
519 static inline LWORD __string_to_bit (STRING IN) {return (LWORD)__pstring_to_sint(&IN);} |
|
520 static inline ULINT __string_to_uint(STRING IN) {return (ULINT)__pstring_to_sint(&IN);} |
|
521 static inline LREAL __string_to_real(STRING IN) { |
|
522 __strlen_t l; |
|
523 l = IN.len; |
|
524 /* search the dot */ |
|
525 while(--l > 0 && IN.body[l] != '.'); |
|
526 if(l != 0){ |
|
527 return atof((const char *)&IN.body); |
|
528 }else{ |
|
529 return (LREAL)__pstring_to_sint(&IN); |
|
530 } |
|
531 } |
|
532 |
|
533 /***************/ |
|
534 /* TO_TIME */ |
|
535 /***************/ |
|
536 static inline TIME __int_to_time(LINT IN) {return (TIME){IN, 0};} |
|
537 static inline TIME __real_to_time(LREAL IN) {return (TIME){IN, (IN - (LINT)IN) * 1000000000};} |
|
538 static inline TIME __string_to_time(STRING IN){ |
|
539 __strlen_t l; |
|
540 /* TODO : |
|
541 * |
|
542 * Duration literals without underlines: T#14ms T#-14ms T#14.7s T#14.7m |
|
543 * short prefix T#14.7h t#14.7d t#25h15m |
|
544 * t#5d14h12m18s3.5ms |
|
545 * long prefix TIME#14ms TIME#-14ms time#14.7s |
|
546 * Duration literals with underlines: |
|
547 * short prefix t#25h_15m t#5d_14h_12m_18s_3.5ms |
|
548 * long prefix TIME#25h_15m |
|
549 * time#5d_14h_12m_18s_3.5ms |
|
550 * |
|
551 * Long prefix notation Short prefix notation |
|
552 * DATE#1984-06-25 D#1984-06-25 |
|
553 * date#1984-06-25 d#1984-06-25 |
|
554 * TIME_OF_DAY#15:36:55.36 TOD#15:36:55.36 |
|
555 * time_of_day#15:36:55.36 tod#15:36:55.36 |
|
556 * DATE_AND_TIME#1984-06-25-15:36:55.36 DT#1984-06-25-15:36:55.36 |
|
557 * date_and_time#1984-06-25-15:36:55.36 dt#1984-06-25-15:36:55.36 |
|
558 * |
|
559 */ |
|
560 /* Quick hack : only transform seconds */ |
|
561 /* search the dot */ |
|
562 l = IN.len; |
|
563 while(--l > 0 && IN.body[l] != '.'); |
|
564 if(l != 0){ |
|
565 LREAL IN_val = atof((const char *)&IN.body); |
|
566 return (TIME){(long)IN_val, (long)(IN_val - (LINT)IN_val)*1000000000}; |
|
567 }else{ |
|
568 return (TIME){(long)__pstring_to_sint(&IN), 0}; |
|
569 } |
|
570 } |
|
571 |
|
572 /***************/ |
|
573 /* FROM_TIME */ |
|
574 /***************/ |
|
575 static inline LREAL __time_to_real(TIME IN){ |
|
576 return (LREAL)IN.tv_sec + ((LREAL)IN.tv_nsec/1000000000); |
|
577 } |
|
578 static inline LINT __time_to_int(TIME IN) {return IN.tv_sec;} |
|
579 static inline STRING __time_to_string(TIME IN){ |
|
580 STRING res; |
|
581 div_t days; |
|
582 /*t#5d14h12m18s3.5ms*/ |
|
583 res = __INIT_STRING; |
|
584 days = div(IN.tv_sec, SECONDS_PER_DAY); |
|
585 if(!days.rem && IN.tv_nsec == 0){ |
|
586 res.len = snprintf((char*)&res.body, STR_MAX_LEN, "T#%dd", days.quot); |
|
587 }else{ |
|
588 div_t hours = div(days.rem, SECONDS_PER_HOUR); |
|
589 if(!hours.rem && IN.tv_nsec == 0){ |
|
590 res.len = snprintf((char*)&res.body, STR_MAX_LEN, "T#%dd%dh", days.quot, hours.quot); |
|
591 }else{ |
|
592 div_t minuts = div(hours.rem, SECONDS_PER_MINUTE); |
|
593 if(!minuts.rem && IN.tv_nsec == 0){ |
|
594 res.len = snprintf((char*)&res.body, STR_MAX_LEN, "T#%dd%dh%dm", days.quot, hours.quot, minuts.quot); |
|
595 }else{ |
|
596 if(IN.tv_nsec == 0){ |
|
597 res.len = snprintf((char*)&res.body, STR_MAX_LEN, "T#%dd%dh%dm%ds", days.quot, hours.quot, minuts.quot, minuts.rem); |
|
598 }else{ |
|
599 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); |
|
600 } |
|
601 } |
|
602 } |
|
603 } |
|
604 if(res.len > STR_MAX_LEN) res.len = STR_MAX_LEN; |
|
605 return res; |
|
606 } |
|
607 static inline STRING __date_to_string(DATE IN){ |
|
608 STRING res; |
|
609 tm broken_down_time; |
|
610 /* D#1984-06-25 */ |
|
611 broken_down_time = convert_seconds_to_date_and_time(IN.tv_sec); |
|
612 res = __INIT_STRING; |
|
613 res.len = snprintf((char*)&res.body, STR_MAX_LEN, "D#%d-%2.2d-%2.2d", |
|
614 broken_down_time.tm_year, |
|
615 broken_down_time.tm_mon, |
|
616 broken_down_time.tm_day); |
|
617 if(res.len > STR_MAX_LEN) res.len = STR_MAX_LEN; |
|
618 return res; |
|
619 } |
|
620 static inline STRING __tod_to_string(TOD IN){ |
|
621 STRING res; |
|
622 tm broken_down_time; |
|
623 time_t seconds; |
|
624 /* TOD#15:36:55.36 */ |
|
625 seconds = IN.tv_sec; |
|
626 if (seconds >= SECONDS_PER_DAY){ |
|
627 __iec_error(); |
|
628 return (STRING){9,"TOD#ERROR"}; |
|
629 } |
|
630 broken_down_time = convert_seconds_to_date_and_time(seconds); |
|
631 res = __INIT_STRING; |
|
632 if(IN.tv_nsec == 0){ |
|
633 res.len = snprintf((char*)&res.body, STR_MAX_LEN, "TOD#%2.2d:%2.2d:%2.2d", |
|
634 broken_down_time.tm_hour, |
|
635 broken_down_time.tm_min, |
|
636 broken_down_time.tm_sec); |
|
637 }else{ |
|
638 res.len = snprintf((char*)&res.body, STR_MAX_LEN, "TOD#%2.2d:%2.2d:%09.6f", |
|
639 broken_down_time.tm_hour, |
|
640 broken_down_time.tm_min, |
|
641 (LREAL)broken_down_time.tm_sec + (LREAL)IN.tv_nsec / 1e9); |
|
642 } |
|
643 if(res.len > STR_MAX_LEN) res.len = STR_MAX_LEN; |
|
644 return res; |
|
645 } |
|
646 static inline STRING __dt_to_string(DT IN){ |
|
647 STRING res; |
|
648 tm broken_down_time; |
|
649 /* DT#1984-06-25-15:36:55.36 */ |
|
650 broken_down_time = convert_seconds_to_date_and_time(IN.tv_sec); |
|
651 if(IN.tv_nsec == 0){ |
|
652 res.len = snprintf((char*)&res.body, STR_MAX_LEN, "DT#%d-%2.2d-%2.2d-%2.2d:%2.2d:%2.2d", |
|
653 broken_down_time.tm_year, |
|
654 broken_down_time.tm_mon, |
|
655 broken_down_time.tm_day, |
|
656 broken_down_time.tm_hour, |
|
657 broken_down_time.tm_min, |
|
658 broken_down_time.tm_sec); |
|
659 }else{ |
|
660 res.len = snprintf((char*)&res.body, STR_MAX_LEN, "DT#%d-%2.2d-%2.2d-%2.2d:%2.2d:%09.6f", |
|
661 broken_down_time.tm_year, |
|
662 broken_down_time.tm_mon, |
|
663 broken_down_time.tm_day, |
|
664 broken_down_time.tm_hour, |
|
665 broken_down_time.tm_min, |
|
666 (LREAL)broken_down_time.tm_sec + ((LREAL)IN.tv_nsec / 1e9)); |
|
667 } |
|
668 if(res.len > STR_MAX_LEN) res.len = STR_MAX_LEN; |
|
669 return res; |
|
670 } |
|
671 |
|
672 /**********************************************/ |
|
673 /* [ANY_DATE | TIME] _TO_ [ANY_DATE | TIME] */ |
|
674 /**********************************************/ |
|
675 |
|
676 static inline TOD __date_and_time_to_time_of_day(DT IN) { |
|
677 return (TOD){ |
|
678 IN.tv_sec % SECONDS_PER_DAY + (IN.tv_sec < 0 ? SECONDS_PER_DAY : 0), |
|
679 IN.tv_nsec}; |
|
680 } |
|
681 static inline DATE __date_and_time_to_date(DT IN){ |
|
682 return (DATE){ |
|
683 IN.tv_sec - IN.tv_sec % SECONDS_PER_DAY - (IN.tv_sec < 0 ? SECONDS_PER_DAY : 0), |
|
684 0}; |
|
685 } |
|
686 |
|
687 /*****************/ |
|
688 /* FROM/TO BCD */ |
|
689 /*****************/ |
|
690 |
|
691 static inline BOOL __test_bcd(LWORD IN) { |
|
692 while (IN) { |
|
693 if ((IN & 0xf) > 9) return 1; |
|
694 IN >>= 4; |
|
695 } |
|
696 return 0; |
|
697 } |
|
698 |
|
699 static inline ULINT __bcd_to_uint(LWORD IN){ |
|
700 ULINT res = IN & 0xf; |
|
701 ULINT factor = 10ULL; |
|
702 |
|
703 while (IN >>= 4) { |
|
704 res += (IN & 0xf) * factor; |
|
705 factor *= 10; |
|
706 } |
|
707 return res; |
|
708 } |
|
709 |
|
710 static inline LWORD __uint_to_bcd(ULINT IN){ |
|
711 LWORD res = IN % 10; |
|
712 USINT shift = 4; |
|
713 |
|
714 while (IN /= 10) { |
|
715 res |= (IN % 10) << shift; |
|
716 shift += 4; |
|
717 } |
|
718 return res; |
|
719 } |
|
720 |
|
721 |
|
722 /************/ |
|
723 /* MOVE_* */ |
|
724 /************/ |
|
725 |
|
726 /* some helpful __move_[ANY] functions, used in the *_TO_** and MOVE standard functions */ |
|
727 /* e.g. __move_BOOL, __move_BYTE, __move_REAL, __move_TIME, ... */ |
|
728 #define __move_(TYPENAME)\ |
|
729 static inline TYPENAME __move_##TYPENAME(TYPENAME op1) {return op1;} |
|
730 __ANY(__move_) |
|
731 |
|
732 |
|
733 |
|
734 /*****************************************************************/ |
|
735 /*****************************************************************/ |
|
736 /***** *****/ |
|
737 /***** IEC 61131-3 *****/ |
|
738 /***** S T A N D A R D F U N C T I O N S *****/ |
|
739 /***** *****/ |
|
740 /*****************************************************************/ |
|
741 /*****************************************************************/ |
|
742 |
|
743 /* NOTE: If you want to know what all these strange macros are doing, |
|
744 * just parse this file through a C preprocessor, and analyse the output! |
|
745 * $gcc -E iec_std_lib.h |
|
746 */ |
|
747 |
|
748 /* NOTE: We only define and declare the explicitly typed standard functions |
|
749 * (e.g., SIN_REAL, SIN_LREAL, ..., ADD_SINT, ADD_INT, ADD_LINT, ...) |
|
750 * We do not declare/define the overloaded functions |
|
751 * (SIN, ADD, ...). |
|
752 * When handling a call to an overloaded function, the iec2c compiler |
|
753 * will determine in stage3 the data type of the parameter being passed, |
|
754 * and in stage4 generate the C code to call the correct |
|
755 * typed standard function. |
|
756 */ |
|
757 |
|
758 /* NOTE on explicit typing of: |
|
759 * - Table 25 - Standard bit shift functions |
|
760 * - Table 29 - Character string Functions |
|
761 * |
|
762 * In section 2.5.1.4 (Typing, overloading, and type conversion) of the IEC 61131-3 (version 2) |
|
763 * of the standard, it is stated: |
|
764 * "A standard function, [...] is said to be overloaded when it |
|
765 * can operate on input data elements of various types within a generic type designator as defined in |
|
766 * 2.3.2. For instance, an overloaded addition function on generic type ANY_NUM can operate on data |
|
767 * of types LREAL, REAL, DINT, INT, and SINT." |
|
768 * [...] |
|
769 * "When a function which normally represents an overloaded operator is to be typed, i.e., the types |
|
770 * of its inputs and outputs restricted to a particular elementary or derived data type as defined in |
|
771 * 2.3, this shall be done by appending an "underline" character followed by the required type, as |
|
772 * shown in table 21." |
|
773 * |
|
774 * However, this explanation (as well as the example in table 21) only refers to functions where the same |
|
775 * generic data type is used for the single input and the output parameter. |
|
776 * How can we create explicitly types functions when this is not the case? |
|
777 * It does not seem to be covered by the standard. |
|
778 * |
|
779 * For this reason, we do not define the LEN_SINT, LEN_INT, LEN_STRING, LEN_[ANY_INT], LEN_[ANY_STRING] functions... |
|
780 */ |
|
781 |
|
782 |
|
783 /********************/ |
|
784 /* EN/ENO PARAMS */ |
|
785 /********************/ |
|
786 |
|
787 #define EN_ENO_PARAMS BOOL EN, BOOL *ENO |
|
788 #define EN_ENO EN, ENO |
|
789 |
|
790 #define TEST_EN(TYPENAME)\ |
|
791 if (!EN) {\ |
|
792 if (ENO != NULL)\ |
|
793 *ENO = __BOOL_LITERAL(FALSE);\ |
|
794 return __INIT_##TYPENAME;\ |
|
795 }\ |
|
796 else if (ENO != NULL)\ |
|
797 *ENO = __BOOL_LITERAL(TRUE); |
|
798 |
|
799 #define TEST_EN_COND(TYPENAME, COND)\ |
|
800 if (!EN || (COND)) {\ |
|
801 if (ENO != NULL)\ |
|
802 *ENO = __BOOL_LITERAL(FALSE);\ |
|
803 return __INIT_##TYPENAME;\ |
|
804 }\ |
|
805 else if (ENO != NULL)\ |
|
806 *ENO = __BOOL_LITERAL(TRUE); |
|
807 |
|
808 |
|
809 |
|
810 /*****************************************/ |
|
811 /*****************************************/ |
|
812 /* 2.5.1.5.1 Type Conversion Functions */ |
|
813 /*****************************************/ |
|
814 /*****************************************/ |
|
815 |
|
816 #define __convert_type(from_TYPENAME,to_TYPENAME, oper) \ |
|
817 static inline to_TYPENAME from_TYPENAME##_TO_##to_TYPENAME(EN_ENO_PARAMS, from_TYPENAME op){\ |
|
818 TEST_EN(to_TYPENAME)\ |
|
819 return (to_TYPENAME)oper(op);\ |
|
820 } |
|
821 |
|
822 /******** [ANY_NUM | ANY_NBIT]_TO_BOOL ************/ |
|
823 #define __convert_num_to_bool(TYPENAME) \ |
|
824 static inline BOOL TYPENAME##_TO_BOOL(EN_ENO_PARAMS, TYPENAME op){\ |
|
825 TEST_EN(BOOL)\ |
|
826 return op == 0 ? 0 : 1;\ |
|
827 } |
|
828 __ANY_NUM(__convert_num_to_bool) |
|
829 __ANY_NBIT(__convert_num_to_bool) |
|
830 |
|
831 /******** [TIME | ANY_DATE]_TO_BOOL ************/ |
|
832 #define __convert_time_to_bool(TYPENAME) \ |
|
833 static inline BOOL TYPENAME##_TO_BOOL(EN_ENO_PARAMS, TYPENAME op){\ |
|
834 TEST_EN(BOOL)\ |
|
835 return op.tv_sec == 0 && op.tv_nsec == 0 ? 0 : 1;\ |
|
836 } |
|
837 __convert_time_to_bool(TIME) |
|
838 __ANY_DATE(__convert_time_to_bool) |
|
839 |
|
840 #define __to_anynum_(from_TYPENAME) __ANY_NUM_1(__iec_,from_TYPENAME) |
|
841 #define __to_anyint_(from_TYPENAME) __ANY_INT_1(__iec_,from_TYPENAME) |
|
842 #define __to_anybit_(from_TYPENAME) __ANY_BIT_1(__iec_,from_TYPENAME) |
|
843 #define __to_anynbit_(from_TYPENAME) __ANY_NBIT_1(__iec_,from_TYPENAME) |
|
844 #define __to_anysint_(from_TYPENAME) __ANY_SINT_1(__iec_,from_TYPENAME) |
|
845 #define __to_anyuint_(from_TYPENAME) __ANY_UINT_1(__iec_,from_TYPENAME) |
|
846 #define __to_anyreal_(from_TYPENAME) __ANY_REAL_1(__iec_,from_TYPENAME) |
|
847 #define __to_anydate_(from_TYPENAME) __ANY_DATE_1(__iec_,from_TYPENAME) |
|
848 |
|
849 /******** [ANY_BIT]_TO_[ANY_NUM | ANT_NBIT] ************/ |
|
850 #define __iec_(to_TYPENAME,from_TYPENAME) __convert_type(from_TYPENAME, to_TYPENAME, __move_##to_TYPENAME) |
|
851 __ANY_BIT(__to_anynum_) |
|
852 __ANY_BIT(__to_anynbit_) |
|
853 #undef __iec_ |
|
854 |
|
855 /******** [ANY_INT]_TO_[ANY_NUM | ANT_NBIT] ************/ |
|
856 #define __iec_(to_TYPENAME,from_TYPENAME) __convert_type(from_TYPENAME, to_TYPENAME, __move_##to_TYPENAME) |
|
857 __ANY_INT(__to_anynum_) |
|
858 __ANY_INT(__to_anynbit_) |
|
859 #undef __iec_ |
|
860 |
|
861 /******** [ANY_REAL]_TO_[ANY_NBIT] ************/ |
|
862 #define __iec_(to_TYPENAME,from_TYPENAME) __convert_type(from_TYPENAME, to_TYPENAME, __real_to_bit) |
|
863 __ANY_REAL(__to_anynbit_) |
|
864 #undef __iec_ |
|
865 |
|
866 /******** [ANY_REAL]_TO_[ANY_NINT] ************/ |
|
867 #define __iec_(to_TYPENAME,from_TYPENAME) __convert_type(from_TYPENAME, to_TYPENAME, __real_to_sint) |
|
868 __ANY_REAL(__to_anysint_) |
|
869 #undef __iec_ |
|
870 #define __iec_(to_TYPENAME,from_TYPENAME) __convert_type(from_TYPENAME, to_TYPENAME, __real_to_uint) |
|
871 __ANY_REAL(__to_anyuint_) |
|
872 #undef __iec_ |
|
873 |
|
874 /******** [ANY_REAL]_TO_[ANY_REAL] ************/ |
|
875 #define __iec_(to_TYPENAME,from_TYPENAME) __convert_type(from_TYPENAME, to_TYPENAME, __move_##to_TYPENAME) |
|
876 __ANY_REAL(__to_anyreal_) |
|
877 #undef __iec_ |
|
878 |
|
879 /******** [ANY_BIT | ANY_INT]_TO_[TIME | ANY_DATE] ************/ |
|
880 #define __iec_(from_TYPENAME) __convert_type(from_TYPENAME, TIME, __int_to_time) |
|
881 __ANY_BIT(__iec_) |
|
882 __ANY_INT(__iec_) |
|
883 #undef __iec_ |
|
884 #define __iec_(to_TYPENAME,from_TYPENAME) __convert_type(from_TYPENAME, to_TYPENAME, __int_to_time) |
|
885 __ANY_BIT(__to_anydate_) |
|
886 __ANY_INT(__to_anydate_) |
|
887 #undef __iec_ |
|
888 |
|
889 /******** [ANY_REAL]_TO_[TIME | ANY_DATE] ************/ |
|
890 #define __iec_(from_TYPENAME) __convert_type(from_TYPENAME, TIME, __real_to_time) |
|
891 __ANY_REAL(__iec_) |
|
892 #undef __iec_ |
|
893 #define __iec_(to_TYPENAME,from_TYPENAME) __convert_type(from_TYPENAME, to_TYPENAME, __real_to_time) |
|
894 __ANY_REAL(__to_anydate_) |
|
895 #undef __iec_ |
|
896 |
|
897 /******** [TIME | ANY_DATE]_TO_[ANY_BIT | ANY_INT] ************/ |
|
898 #define __iec_(to_TYPENAME,from_TYPENAME) __convert_type(from_TYPENAME, to_TYPENAME, __time_to_int) |
|
899 __to_anyint_(TIME) |
|
900 __to_anynbit_(TIME) |
|
901 __ANY_DATE(__to_anyint_) |
|
902 __ANY_DATE(__to_anynbit_) |
|
903 #undef __iec_ |
|
904 |
|
905 /******** [TIME | ANY_DATE]_TO_[ANY_REAL] ************/ |
|
906 #define __iec_(to_TYPENAME,from_TYPENAME) __convert_type(from_TYPENAME, to_TYPENAME, __time_to_real) |
|
907 __to_anyreal_(TIME) |
|
908 __ANY_DATE(__to_anyreal_) |
|
909 #undef __iec_ |
|
910 |
|
911 |
|
912 /******** [ANY_DATE]_TO_[ANY_DATE | TIME] ************/ |
|
913 /* Not supported: DT_TO_TIME */ |
|
914 __convert_type(DT, DATE, __date_and_time_to_date) |
|
915 static inline DATE DATE_AND_TIME_TO_DATE(EN_ENO_PARAMS, DT op){ |
|
916 return DT_TO_DATE(EN_ENO, op); |
|
917 } |
|
918 __convert_type(DT, DT, __move_DT) |
|
919 __convert_type(DT, TOD, __date_and_time_to_time_of_day) |
|
920 static inline DATE DATE_AND_TIME_TO_TIME_OF_DAY(EN_ENO_PARAMS, DT op){ |
|
921 return DT_TO_TOD(EN_ENO, op); |
|
922 } |
|
923 /* Not supported: DATE_TO_TIME */ |
|
924 __convert_type(DATE, DATE, __move_DATE) |
|
925 /* Not supported: DATE_TO_DT */ |
|
926 /* Not supported: DATE_TO_TOD */ |
|
927 /* Not supported: TOD_TO_TIME */ |
|
928 /* Not supported: TOD_TO_DATE */ |
|
929 /* Not supported: TOD_TO_DT */ |
|
930 __convert_type(TOD, TOD, __move_TOD) |
|
931 |
|
932 |
|
933 /******** TIME_TO_[ANY_DATE] ************/ |
|
934 /* Not supported: TIME_TO_DATE */ |
|
935 /* Not supported: TIME_TO_DT */ |
|
936 /* Not supported: TIME_TO_TOD */ |
|
937 |
|
938 /******** TIME_TO_TIME ************/ |
|
939 __convert_type(TIME, TIME, __move_TIME) |
|
940 |
|
941 |
|
942 /******** [ANY_BIT]_TO_STRING ************/ |
|
943 __convert_type(BOOL, STRING, __bool_to_string) |
|
944 #define __iec_(from_TYPENAME) __convert_type(from_TYPENAME, STRING, __bit_to_string) |
|
945 __ANY_NBIT(__iec_) |
|
946 #undef __iec_ |
|
947 |
|
948 /******** [ANY_INT]_TO_STRING ************/ |
|
949 #define __iec_(from_TYPENAME) __convert_type(from_TYPENAME, STRING, __sint_to_string) |
|
950 __ANY_SINT(__iec_) |
|
951 #undef __iec_ |
|
952 #define __iec_(from_TYPENAME) __convert_type(from_TYPENAME, STRING, __uint_to_string) |
|
953 __ANY_UINT(__iec_) |
|
954 #undef __iec_ |
|
955 |
|
956 /******** [ANY_REAL]_TO_STRING ************/ |
|
957 #define __iec_(from_TYPENAME) __convert_type(from_TYPENAME, STRING, __real_to_string) |
|
958 __ANY_REAL(__iec_) |
|
959 #undef __iec_ |
|
960 |
|
961 /******** [ANY_DATE]_TO_STRING ************/ |
|
962 __convert_type(DATE, STRING, __date_to_string) |
|
963 __convert_type(DT, STRING, __dt_to_string) |
|
964 __convert_type(TOD, STRING, __tod_to_string) |
|
965 |
|
966 /******** TIME_TO_STRING ************/ |
|
967 __convert_type(TIME, STRING, __time_to_string) |
|
968 |
|
969 |
|
970 /******** STRING_TO_[ANY_BIT] ************/ |
|
971 __convert_type(STRING, BOOL, __string_to_bool) |
|
972 #define __iec_(to_TYPENAME) __convert_type(STRING, to_TYPENAME, __string_to_bit) |
|
973 __ANY_NBIT(__iec_) |
|
974 #undef __iec_ |
|
975 |
|
976 /******** STRING_TO_[ANY_INT] ************/ |
|
977 #define __iec_(to_TYPENAME) __convert_type(STRING, to_TYPENAME, __string_to_sint) |
|
978 __ANY_SINT(__iec_) |
|
979 #undef __iec_ |
|
980 #define __iec_(to_TYPENAME) __convert_type(STRING, to_TYPENAME, __string_to_uint) |
|
981 __ANY_UINT(__iec_) |
|
982 #undef __iec_ |
|
983 |
|
984 /******** STRING_TO_[ANY_REAL] ************/ |
|
985 #define __iec_(to_TYPENAME) __convert_type(STRING, to_TYPENAME, __string_to_real) |
|
986 __ANY_REAL(__iec_) |
|
987 #undef __iec_ |
|
988 |
|
989 /******** STRING_TO_[ANY_DATE] ************/ |
|
990 #define __iec_(to_TYPENAME) __convert_type(STRING, to_TYPENAME, __string_to_time) |
|
991 __ANY_DATE(__iec_) |
|
992 #undef __iec_ |
|
993 |
|
994 /******** STRING_TO_TIME ************/ |
|
995 __convert_type(STRING, TIME, __string_to_time) |
|
996 |
|
997 |
|
998 /******** TRUNC ************/ |
|
999 #define __iec_(to_TYPENAME,from_TYPENAME) \ |
|
1000 static inline to_TYPENAME TRUNC__##to_TYPENAME##__##from_TYPENAME(EN_ENO_PARAMS, from_TYPENAME op){\ |
|
1001 TEST_EN(to_TYPENAME)\ |
|
1002 return (to_TYPENAME)__move_##to_TYPENAME(op);\ |
|
1003 } |
|
1004 __ANY_REAL(__to_anyint_) |
|
1005 #undef __iec_ |
|
1006 |
|
1007 |
|
1008 /******** _TO_BCD ************/ |
|
1009 #define __iec_(to_TYPENAME,from_TYPENAME) \ |
|
1010 static inline to_TYPENAME from_TYPENAME##_TO_BCD_##to_TYPENAME(EN_ENO_PARAMS, from_TYPENAME op){\ |
|
1011 TEST_EN(to_TYPENAME)\ |
|
1012 return (to_TYPENAME)__uint_to_bcd(op);\ |
|
1013 }\ |
|
1014 static inline to_TYPENAME from_TYPENAME##_TO_BCD__##to_TYPENAME##__##from_TYPENAME(EN_ENO_PARAMS, from_TYPENAME op){\ |
|
1015 return from_TYPENAME##_TO_BCD_##to_TYPENAME(EN_ENO, op);\ |
|
1016 } |
|
1017 __ANY_UINT(__to_anynbit_) |
|
1018 #undef __iec_ |
|
1019 |
|
1020 |
|
1021 /******** BCD_TO_ ************/ |
|
1022 #define __iec_(to_TYPENAME,from_TYPENAME) \ |
|
1023 static inline to_TYPENAME from_TYPENAME##_BCD_TO_##to_TYPENAME(EN_ENO_PARAMS, from_TYPENAME op){\ |
|
1024 TEST_EN_COND(to_TYPENAME, __test_bcd(op))\ |
|
1025 return (to_TYPENAME)__bcd_to_uint(op);\ |
|
1026 }\ |
|
1027 static inline to_TYPENAME BCD_TO_##to_TYPENAME##__##to_TYPENAME##__##from_TYPENAME(EN_ENO_PARAMS, from_TYPENAME op){\ |
|
1028 return from_TYPENAME##_BCD_TO_##to_TYPENAME(EN_ENO, op);\ |
|
1029 } |
|
1030 __ANY_NBIT(__to_anyuint_) |
|
1031 #undef __iec_ |
|
1032 |
|
1033 |
|
1034 /***********************************/ |
|
1035 /***********************************/ |
|
1036 /* 2.5.1.5.2 Numerical Functions */ |
|
1037 /***********************************/ |
|
1038 /***********************************/ |
|
1039 |
|
1040 /* workaround for va-args limitation on shorter than int params */ |
|
1041 #define VA_ARGS_REAL LREAL |
|
1042 #define VA_ARGS_LREAL LREAL |
|
1043 #define VA_ARGS_SINT DINT |
|
1044 #define VA_ARGS_INT DINT |
|
1045 #define VA_ARGS_DINT DINT |
|
1046 #define VA_ARGS_LINT LINT |
|
1047 #define VA_ARGS_USINT UDINT |
|
1048 #define VA_ARGS_UINT UDINT |
|
1049 #define VA_ARGS_UDINT UDINT |
|
1050 #define VA_ARGS_ULINT ULINT |
|
1051 #define VA_ARGS_TIME TIME |
|
1052 #define VA_ARGS_BOOL DWORD |
|
1053 #define VA_ARGS_BYTE DWORD |
|
1054 #define VA_ARGS_WORD DWORD |
|
1055 #define VA_ARGS_DWORD DWORD |
|
1056 #define VA_ARGS_LWORD LWORD |
|
1057 #define VA_ARGS_STRING STRING |
|
1058 #define VA_ARGS_WSTRING WSTRING |
|
1059 #define VA_ARGS_DATE DATE |
|
1060 #define VA_ARGS_TOD TOD |
|
1061 #define VA_ARGS_DT DT |
|
1062 |
|
1063 |
|
1064 #define __numeric(fname,TYPENAME, FUNC) \ |
|
1065 /* explicitly typed function */\ |
|
1066 static inline TYPENAME fname##TYPENAME(EN_ENO_PARAMS, TYPENAME op){\ |
|
1067 TEST_EN(TYPENAME)\ |
|
1068 return FUNC(op);\ |
|
1069 }\ |
|
1070 /* overloaded function */\ |
|
1071 static inline TYPENAME fname##_##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, TYPENAME op) {\ |
|
1072 return fname##TYPENAME(EN_ENO, op);\ |
|
1073 } |
|
1074 |
|
1075 /******************************************************************/ |
|
1076 /*** Table 23 - Standard functions of one numeric variable ***/ |
|
1077 /******************************************************************/ |
|
1078 |
|
1079 /**************/ |
|
1080 /* ABS */ |
|
1081 /**************/ |
|
1082 #define __abs_signed(TYPENAME) \ |
|
1083 /* explicitly typed function */\ |
|
1084 static inline TYPENAME ABS_##TYPENAME(EN_ENO_PARAMS, TYPENAME op){\ |
|
1085 TEST_EN(TYPENAME)\ |
|
1086 if (op < 0)\ |
|
1087 return -op;\ |
|
1088 return op;\ |
|
1089 }\ |
|
1090 /* overloaded function */\ |
|
1091 static inline TYPENAME ABS__##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, TYPENAME op) {\ |
|
1092 return ABS_##TYPENAME(EN_ENO, op);\ |
|
1093 } |
|
1094 |
|
1095 #define __abs_unsigned(TYPENAME) \ |
|
1096 /* explicitly typed function */\ |
|
1097 static inline TYPENAME ABS_##TYPENAME(EN_ENO_PARAMS, TYPENAME op){\ |
|
1098 TEST_EN(TYPENAME)\ |
|
1099 return op;\ |
|
1100 }\ |
|
1101 /* overloaded function */\ |
|
1102 static inline TYPENAME ABS__##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, TYPENAME op) {\ |
|
1103 return ABS_##TYPENAME(EN_ENO, op);\ |
|
1104 } |
|
1105 |
|
1106 __ANY_REAL(__abs_signed) |
|
1107 __ANY_SINT(__abs_signed) |
|
1108 __ANY_UINT(__abs_unsigned) |
|
1109 |
|
1110 |
|
1111 /**************/ |
|
1112 /* SQRT */ |
|
1113 /**************/ |
|
1114 #define __sqrt(TYPENAME) __numeric(SQRT_, TYPENAME, sqrt) |
|
1115 __ANY_REAL(__sqrt) |
|
1116 |
|
1117 |
|
1118 /**************/ |
|
1119 /* LN */ |
|
1120 /**************/ |
|
1121 #define __ln(TYPENAME) __numeric(LN_, TYPENAME, log) |
|
1122 __ANY_REAL(__ln) |
|
1123 |
|
1124 |
|
1125 /**************/ |
|
1126 /* LOG */ |
|
1127 /**************/ |
|
1128 #define __log(TYPENAME) __numeric(LOG_, TYPENAME, log10) |
|
1129 __ANY_REAL(__log) |
|
1130 |
|
1131 |
|
1132 /**************/ |
|
1133 /* EXP */ |
|
1134 /**************/ |
|
1135 #define __exp(TYPENAME) __numeric(EXP_, TYPENAME, exp) |
|
1136 __ANY_REAL(__exp) |
|
1137 |
|
1138 |
|
1139 /**************/ |
|
1140 /* SIN */ |
|
1141 /**************/ |
|
1142 #define __sin(TYPENAME) __numeric(SIN_, TYPENAME, sin) |
|
1143 __ANY_REAL(__sin) |
|
1144 |
|
1145 |
|
1146 /**************/ |
|
1147 /* COS */ |
|
1148 /**************/ |
|
1149 #define __cos(TYPENAME) __numeric(COS_, TYPENAME, cos) |
|
1150 __ANY_REAL(__cos) |
|
1151 |
|
1152 /**************/ |
|
1153 /* TAN */ |
|
1154 /**************/ |
|
1155 #define __tan(TYPENAME) __numeric(TAN_, TYPENAME, tan) |
|
1156 __ANY_REAL(__tan) |
|
1157 |
|
1158 |
|
1159 /**************/ |
|
1160 /* ASIN */ |
|
1161 /**************/ |
|
1162 #define __asin(TYPENAME) __numeric(ASIN_, TYPENAME, asin) |
|
1163 __ANY_REAL(__asin) |
|
1164 |
|
1165 /**************/ |
|
1166 /* ACOS */ |
|
1167 /**************/ |
|
1168 #define __acos(TYPENAME) __numeric(ACOS_, TYPENAME, acos) |
|
1169 __ANY_REAL(__acos) |
|
1170 |
|
1171 /**************/ |
|
1172 /* ATAN */ |
|
1173 /**************/ |
|
1174 #define __atan(TYPENAME) __numeric(ATAN_, TYPENAME, atan) |
|
1175 __ANY_REAL(__atan) |
|
1176 |
|
1177 |
|
1178 |
|
1179 /*****************************************************/ |
|
1180 /*** Table 24 - Standard arithmetic functions ***/ |
|
1181 /*****************************************************/ |
|
1182 |
|
1183 #define __arith_expand(fname,TYPENAME, OP)\ |
|
1184 static inline TYPENAME fname(EN_ENO_PARAMS, UINT param_count, TYPENAME op1, ...){\ |
|
1185 va_list ap;\ |
|
1186 UINT i;\ |
|
1187 TEST_EN(TYPENAME)\ |
|
1188 \ |
|
1189 va_start (ap, op1); /* Initialize the argument list. */\ |
|
1190 \ |
|
1191 for (i = 0; i < param_count - 1; i++){\ |
|
1192 op1 = op1 OP va_arg (ap, VA_ARGS_##TYPENAME);\ |
|
1193 }\ |
|
1194 \ |
|
1195 va_end (ap); /* Clean up. */\ |
|
1196 return op1;\ |
|
1197 } |
|
1198 |
|
1199 #define __arith_static(fname,TYPENAME, OP)\ |
|
1200 /* explicitly typed function */\ |
|
1201 static inline TYPENAME fname##TYPENAME(EN_ENO_PARAMS, TYPENAME op1, TYPENAME op2){\ |
|
1202 TEST_EN(TYPENAME)\ |
|
1203 return op1 OP op2;\ |
|
1204 }\ |
|
1205 /* overloaded function */\ |
|
1206 static inline TYPENAME fname##_##TYPENAME##__##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, TYPENAME op1, TYPENAME op2){\ |
|
1207 return fname##TYPENAME(EN_ENO, op1, op2);\ |
|
1208 } |
|
1209 |
|
1210 /**************/ |
|
1211 /* ADD */ |
|
1212 /**************/ |
|
1213 #define __add(TYPENAME) \ |
|
1214 __arith_expand(ADD_##TYPENAME, TYPENAME, +) /* explicitly typed function */\ |
|
1215 __arith_expand(ADD__##TYPENAME##__##TYPENAME, TYPENAME, +) /* overloaded function */ |
|
1216 __ANY_NUM(__add) |
|
1217 |
|
1218 |
|
1219 /**************/ |
|
1220 /* MUL */ |
|
1221 /**************/ |
|
1222 #define __mul(TYPENAME) \ |
|
1223 __arith_expand(MUL_##TYPENAME, TYPENAME, *) /* explicitly typed function */\ |
|
1224 __arith_expand(MUL__##TYPENAME##__##TYPENAME, TYPENAME, *) /* overloaded function */ |
|
1225 __ANY_NUM(__mul) |
|
1226 |
|
1227 |
|
1228 /**************/ |
|
1229 /* SUB */ |
|
1230 /**************/ |
|
1231 #define __sub(TYPENAME) __arith_static(SUB_, TYPENAME, -) |
|
1232 __ANY_NUM(__sub) |
|
1233 |
|
1234 |
|
1235 /**************/ |
|
1236 /* DIV */ |
|
1237 /**************/ |
|
1238 #define __div(TYPENAME)\ |
|
1239 /* The explicitly typed standard functions */\ |
|
1240 static inline TYPENAME DIV_##TYPENAME(EN_ENO_PARAMS, TYPENAME op1, TYPENAME op2){\ |
|
1241 TEST_EN_COND(TYPENAME, op2 == 0)\ |
|
1242 return op1 / op2;\ |
|
1243 }\ |
|
1244 /* The overloaded standard functions */\ |
|
1245 static inline TYPENAME DIV__##TYPENAME##__##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, TYPENAME op1, TYPENAME op2){\ |
|
1246 return DIV_##TYPENAME(EN_ENO, op1, op2);\ |
|
1247 } |
|
1248 __ANY_NUM(__div) |
|
1249 |
|
1250 |
|
1251 /**************/ |
|
1252 /* MOD */ |
|
1253 /**************/ |
|
1254 #define __mod(TYPENAME)\ |
|
1255 /* The explicitly typed standard functions */\ |
|
1256 static inline TYPENAME MOD_##TYPENAME(EN_ENO_PARAMS, TYPENAME op1, TYPENAME op2){\ |
|
1257 TEST_EN(TYPENAME)\ |
|
1258 if (op2 == 0) return 0;\ |
|
1259 return op1 % op2;\ |
|
1260 }\ |
|
1261 /* The overloaded standard functions */\ |
|
1262 static inline TYPENAME MOD__##TYPENAME##__##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, TYPENAME op1, TYPENAME op2){\ |
|
1263 return MOD_##TYPENAME(EN_ENO, op1, op2);\ |
|
1264 } |
|
1265 __ANY_INT(__mod) |
|
1266 |
|
1267 /**************/ |
|
1268 /* EXPT */ |
|
1269 /**************/ |
|
1270 /* overloaded function */ |
|
1271 #define __iec_(in1_TYPENAME,in2_TYPENAME) \ |
|
1272 static inline in1_TYPENAME EXPT__##in1_TYPENAME##__##in1_TYPENAME##__##in2_TYPENAME\ |
|
1273 (EN_ENO_PARAMS, in1_TYPENAME IN1, in2_TYPENAME IN2){\ |
|
1274 TEST_EN(in1_TYPENAME)\ |
|
1275 return pow(IN1, IN2);\ |
|
1276 } |
|
1277 #define __in1_anyreal_(in2_TYPENAME) __ANY_REAL_1(__iec_,in2_TYPENAME) |
|
1278 __ANY_NUM(__in1_anyreal_) |
|
1279 #undef __iec_ |
|
1280 |
|
1281 |
|
1282 |
|
1283 /***************/ |
|
1284 /* MOVE */ |
|
1285 /***************/ |
|
1286 /* The explicitly typed standard functions */ |
|
1287 #define __iec_(TYPENAME)\ |
|
1288 static inline TYPENAME MOVE_##TYPENAME(EN_ENO_PARAMS, TYPENAME op1){\ |
|
1289 TEST_EN(TYPENAME)\ |
|
1290 return op1;\ |
|
1291 } |
|
1292 __ANY(__iec_) |
|
1293 #undef __iec_ |
|
1294 |
|
1295 /* Overloaded function */ |
|
1296 #define __iec_(TYPENAME)\ |
|
1297 static inline TYPENAME MOVE__##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, TYPENAME op1){\ |
|
1298 TEST_EN(TYPENAME)\ |
|
1299 return op1;\ |
|
1300 } |
|
1301 __ANY(__iec_) |
|
1302 #undef __iec_ |
|
1303 |
|
1304 |
|
1305 |
|
1306 |
|
1307 |
|
1308 |
|
1309 /***********************************/ |
|
1310 /***********************************/ |
|
1311 /* 2.5.1.5.3 Bit String Functions */ |
|
1312 /***********************************/ |
|
1313 /***********************************/ |
|
1314 |
|
1315 /****************************************************/ |
|
1316 /*** Table 25 - Standard bit shift functions ***/ |
|
1317 /****************************************************/ |
|
1318 |
|
1319 /* We do not delcare explcitly typed versions of the functions in table 25. |
|
1320 * See note above regarding explicitly typed functions for more details. |
|
1321 */ |
|
1322 #define __in1_anynbit_(in2_TYPENAME) __ANY_NBIT_1(__iec_,in2_TYPENAME) |
|
1323 |
|
1324 #define __shift_(fname, in1_TYPENAME, in2_TYPENAME, OP)\ |
|
1325 static inline in1_TYPENAME fname(EN_ENO_PARAMS, in1_TYPENAME IN, in2_TYPENAME N) {\ |
|
1326 TEST_EN(in1_TYPENAME)\ |
|
1327 return IN OP N;\ |
|
1328 } |
|
1329 |
|
1330 /**************/ |
|
1331 /* SHL */ |
|
1332 /**************/ |
|
1333 #define __iec_(TYPENAME) \ |
|
1334 /* Overloaded function */\ |
|
1335 static inline BOOL SHL__BOOL__##TYPENAME(EN_ENO_PARAMS, BOOL IN, TYPENAME N) { \ |
|
1336 TEST_EN(BOOL);\ |
|
1337 return (N==0)? IN : __INIT_BOOL; /* shifting by N>1 will always introduce a 0 */\ |
|
1338 } |
|
1339 __ANY_INT(__iec_) |
|
1340 #undef __iec_ |
|
1341 |
|
1342 |
|
1343 #define __iec_(in1_TYPENAME,in2_TYPENAME) \ |
|
1344 __shift_(SHL__##in1_TYPENAME##__##in1_TYPENAME##__##in2_TYPENAME, in1_TYPENAME, in2_TYPENAME, << )/* Overloaded function */ |
|
1345 __ANY_INT(__in1_anynbit_) |
|
1346 #undef __iec_ |
|
1347 |
|
1348 |
|
1349 /**************/ |
|
1350 /* SHR */ |
|
1351 /**************/ |
|
1352 #define __iec_(TYPENAME) \ |
|
1353 /* Overloaded function */\ |
|
1354 static inline BOOL SHR__BOOL__##TYPENAME(EN_ENO_PARAMS, BOOL IN, TYPENAME N) { \ |
|
1355 TEST_EN(BOOL);\ |
|
1356 return (N==0)? IN : __INIT_BOOL; /* shifting by N>1 will always introduce a 0 */\ |
|
1357 } |
|
1358 __ANY_INT(__iec_) |
|
1359 #undef __iec_ |
|
1360 |
|
1361 |
|
1362 #define __iec_(in1_TYPENAME,in2_TYPENAME) \ |
|
1363 __shift_(SHR__##in1_TYPENAME##__##in1_TYPENAME##__##in2_TYPENAME, in1_TYPENAME, in2_TYPENAME, >> )/* Overloaded function */ |
|
1364 __ANY_INT(__in1_anynbit_) |
|
1365 #undef __iec_ |
|
1366 |
|
1367 |
|
1368 /**************/ |
|
1369 /* ROR */ |
|
1370 /**************/ |
|
1371 #define __iec_(TYPENAME) \ |
|
1372 /* Overloaded function */\ |
|
1373 static inline BOOL ROR__BOOL__##TYPENAME(EN_ENO_PARAMS, BOOL IN, TYPENAME N) { \ |
|
1374 TEST_EN(BOOL);\ |
|
1375 return IN; /* rotating a single bit by any value N will not change that bit! */\ |
|
1376 } |
|
1377 __ANY_INT(__iec_) |
|
1378 #undef __iec_ |
|
1379 |
|
1380 |
|
1381 #define __iec_(in1_TYPENAME,in2_TYPENAME) \ |
|
1382 static inline in1_TYPENAME ROR__##in1_TYPENAME##__##in1_TYPENAME##__##in2_TYPENAME(EN_ENO_PARAMS, in1_TYPENAME IN, in2_TYPENAME N){\ |
|
1383 TEST_EN(in1_TYPENAME)\ |
|
1384 N %= 8*sizeof(in1_TYPENAME);\ |
|
1385 return (IN >> N) | (IN << (8*sizeof(in1_TYPENAME)-N));\ |
|
1386 } |
|
1387 __ANY_INT(__in1_anynbit_) |
|
1388 #undef __iec_ |
|
1389 |
|
1390 |
|
1391 /**************/ |
|
1392 /* ROL */ |
|
1393 /**************/ |
|
1394 #define __iec_(TYPENAME) \ |
|
1395 /* Overloaded function */\ |
|
1396 static inline BOOL ROL__BOOL__##TYPENAME(EN_ENO_PARAMS, BOOL IN, TYPENAME N) { \ |
|
1397 TEST_EN(BOOL);\ |
|
1398 return IN; /* rotating a single bit by any value N will not change that bit! */\ |
|
1399 } |
|
1400 __ANY_INT(__iec_) |
|
1401 #undef __iec_ |
|
1402 |
|
1403 |
|
1404 #define __iec_(in1_TYPENAME,in2_TYPENAME) \ |
|
1405 static inline in1_TYPENAME ROL__##in1_TYPENAME##__##in1_TYPENAME##__##in2_TYPENAME(EN_ENO_PARAMS, in1_TYPENAME IN, in2_TYPENAME N){\ |
|
1406 TEST_EN(in1_TYPENAME)\ |
|
1407 N %= 8*sizeof(in1_TYPENAME);\ |
|
1408 return (IN << N) | (IN >> (8*sizeof(in1_TYPENAME)-N));\ |
|
1409 } |
|
1410 __ANY_INT(__in1_anynbit_) |
|
1411 #undef __iec_ |
|
1412 |
|
1413 |
|
1414 |
|
1415 /*********************/ |
|
1416 /*** Table 26 ***/ |
|
1417 /*********************/ |
|
1418 |
|
1419 /**************/ |
|
1420 /* AND */ |
|
1421 /**************/ |
|
1422 __arith_expand(AND_BOOL, BOOL, && ) /* The explicitly typed standard functions */ |
|
1423 __arith_expand(AND__BOOL__BOOL, BOOL, && ) /* Overloaded function */ |
|
1424 |
|
1425 #define __iec_(TYPENAME) \ |
|
1426 __arith_expand(AND_##TYPENAME, TYPENAME, &) /* The explicitly typed standard functions */\ |
|
1427 __arith_expand(AND__##TYPENAME##__##TYPENAME, TYPENAME, &) /* Overloaded function */ |
|
1428 __ANY_NBIT(__iec_) |
|
1429 #undef __iec_ |
|
1430 |
|
1431 /*************/ |
|
1432 /* OR */ |
|
1433 /*************/ |
|
1434 __arith_expand(OR_BOOL, BOOL, || ) /* The explicitly typed standard functions */ |
|
1435 __arith_expand(OR__BOOL__BOOL, BOOL, || ) /* Overloaded function */ |
|
1436 |
|
1437 #define __iec_(TYPENAME) \ |
|
1438 __arith_expand(OR_##TYPENAME, TYPENAME, |) /* The explicitly typed standard functions */\ |
|
1439 __arith_expand(OR__##TYPENAME##__##TYPENAME, TYPENAME, |) /* Overloaded function */ |
|
1440 __ANY_NBIT(__iec_) |
|
1441 #undef __iec_ |
|
1442 |
|
1443 /**************/ |
|
1444 /* XOR */ |
|
1445 /**************/ |
|
1446 #define __xorbool_expand(fname) \ |
|
1447 static inline BOOL fname(EN_ENO_PARAMS, UINT param_count, BOOL op1, ...){ \ |
|
1448 va_list ap; \ |
|
1449 UINT i; \ |
|
1450 TEST_EN(BOOL) \ |
|
1451 \ |
|
1452 va_start (ap, op1); /* Initialize the argument list. */ \ |
|
1453 \ |
|
1454 for (i = 0; i < param_count - 1; i++){ \ |
|
1455 BOOL tmp = va_arg (ap, VA_ARGS_BOOL); \ |
|
1456 op1 = (op1 && !tmp) || (!op1 && tmp); \ |
|
1457 } \ |
|
1458 \ |
|
1459 va_end (ap); /* Clean up. */ \ |
|
1460 return op1; \ |
|
1461 } |
|
1462 |
|
1463 __xorbool_expand(XOR_BOOL) /* The explicitly typed standard functions */ |
|
1464 __xorbool_expand(XOR__BOOL__BOOL) /* Overloaded function */ |
|
1465 |
|
1466 #define __iec_(TYPENAME) \ |
|
1467 __arith_expand(XOR_##TYPENAME, TYPENAME, ^) /* The explicitly typed standard functions */\ |
|
1468 __arith_expand(XOR__##TYPENAME##__##TYPENAME, TYPENAME, ^) /* Overloaded function */\ |
|
1469 __ANY_NBIT(__iec_) |
|
1470 #undef __iec_ |
|
1471 |
|
1472 |
|
1473 /**************/ |
|
1474 /* NOT */ |
|
1475 /**************/ |
|
1476 /* The explicitly typed standard functions */ |
|
1477 static inline BOOL NOT_BOOL(EN_ENO_PARAMS, BOOL op1){ |
|
1478 TEST_EN(BOOL) |
|
1479 return !op1; |
|
1480 } |
|
1481 |
|
1482 /* Overloaded function */ |
|
1483 static inline BOOL NOT__BOOL__BOOL(EN_ENO_PARAMS, BOOL op1){ |
|
1484 TEST_EN(BOOL) |
|
1485 return !op1; |
|
1486 } |
|
1487 |
|
1488 /* The explicitly typed standard functions */ |
|
1489 #define __iec_(TYPENAME)\ |
|
1490 static inline TYPENAME NOT_##TYPENAME(EN_ENO_PARAMS, TYPENAME op1){\ |
|
1491 TEST_EN(TYPENAME)\ |
|
1492 return ~op1;\ |
|
1493 } |
|
1494 __ANY_NBIT(__iec_) |
|
1495 #undef __iec_ |
|
1496 |
|
1497 /* Overloaded function */ |
|
1498 #define __iec_(TYPENAME)\ |
|
1499 static inline TYPENAME NOT__##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, TYPENAME op1){\ |
|
1500 TEST_EN(TYPENAME)\ |
|
1501 return ~op1;\ |
|
1502 } |
|
1503 __ANY_NBIT(__iec_) |
|
1504 #undef __iec_ |
|
1505 |
|
1506 |
|
1507 |
|
1508 |
|
1509 |
|
1510 |
|
1511 /***************************************************/ |
|
1512 /***************************************************/ |
|
1513 /* 2.5.1.5.4 Selection and comparison Functions */ |
|
1514 /***************************************************/ |
|
1515 /***************************************************/ |
|
1516 |
|
1517 /*********************/ |
|
1518 /*** Table 27 ***/ |
|
1519 /*********************/ |
|
1520 |
|
1521 |
|
1522 /**************/ |
|
1523 /* SEL */ |
|
1524 /**************/ |
|
1525 |
|
1526 /* The explicitly typed standard functions */ |
|
1527 #define __iec_(TYPENAME)\ |
|
1528 static inline TYPENAME SEL_##TYPENAME(EN_ENO_PARAMS, BOOL G, TYPENAME op0, TYPENAME op1){\ |
|
1529 TEST_EN(TYPENAME)\ |
|
1530 return G ? op1 : op0;\ |
|
1531 } |
|
1532 __ANY(__iec_) |
|
1533 #undef __iec_ |
|
1534 |
|
1535 /* Overloaded function */ |
|
1536 #define __iec_(TYPENAME)\ |
|
1537 static inline TYPENAME SEL__##TYPENAME##__BOOL__##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, BOOL G, TYPENAME op0, TYPENAME op1){\ |
|
1538 TEST_EN(TYPENAME)\ |
|
1539 return G ? op1 : op0;\ |
|
1540 } |
|
1541 __ANY(__iec_) |
|
1542 #undef __iec_ |
|
1543 |
|
1544 |
|
1545 /**************/ |
|
1546 /* MAX */ |
|
1547 /**************/ |
|
1548 |
|
1549 #define __extrem_(fname,TYPENAME, COND) \ |
|
1550 static inline TYPENAME fname(EN_ENO_PARAMS, UINT param_count, TYPENAME op1, ...){\ |
|
1551 va_list ap;\ |
|
1552 UINT i;\ |
|
1553 TEST_EN(TYPENAME)\ |
|
1554 \ |
|
1555 va_start (ap, op1); /* Initialize the argument list. */\ |
|
1556 \ |
|
1557 for (i = 0; i < param_count - 1; i++){\ |
|
1558 TYPENAME tmp = va_arg (ap, VA_ARGS_##TYPENAME);\ |
|
1559 op1 = COND ? tmp : op1;\ |
|
1560 }\ |
|
1561 \ |
|
1562 va_end (ap); /* Clean up. */\ |
|
1563 return op1;\ |
|
1564 } |
|
1565 |
|
1566 /* Max for numerical data types */ |
|
1567 #define __iec_(TYPENAME) \ |
|
1568 __extrem_(MAX_##TYPENAME,TYPENAME, op1 < tmp) /* The explicitly typed standard functions */\ |
|
1569 __extrem_(MAX__##TYPENAME##__##TYPENAME,TYPENAME, op1 < tmp) /* Overloaded function */ |
|
1570 __ANY_BIT(__iec_) |
|
1571 __ANY_NUM(__iec_) |
|
1572 #undef __iec_ |
|
1573 |
|
1574 /* Max for time data types */ |
|
1575 #define __iec_(TYPENAME) \ |
|
1576 __extrem_(MAX_##TYPENAME, TYPENAME, __time_cmp(op1, tmp) < 0) /* The explicitly typed standard functions */\ |
|
1577 __extrem_(MAX__##TYPENAME##__##TYPENAME, TYPENAME, __time_cmp(op1, tmp) < 0) /* Overloaded function */ |
|
1578 __ANY_DATE(__iec_) |
|
1579 __iec_(TIME) |
|
1580 #undef __iec_ |
|
1581 |
|
1582 #define __STR_CMP(str1, str2) memcmp((char*)&str1.body,(char*)&str2.body, str1.len < str2.len ? str1.len : str2.len) |
|
1583 |
|
1584 /* Max for string data types */ |
|
1585 __extrem_(MAX_STRING, STRING, __STR_CMP(op1,tmp) < 0) /* The explicitly typed standard functions */ |
|
1586 __extrem_(MAX__STRING__STRING, STRING, __STR_CMP(op1,tmp) < 0) /* Overloaded function */ |
|
1587 |
|
1588 /**************/ |
|
1589 /* MIN */ |
|
1590 /**************/ |
|
1591 /* Min for numerical data types */ |
|
1592 #define __iec_(TYPENAME) \ |
|
1593 __extrem_(MIN_##TYPENAME, TYPENAME, op1 > tmp) /* The explicitly typed standard functions */\ |
|
1594 __extrem_(MIN__##TYPENAME##__##TYPENAME, TYPENAME, op1 > tmp) /* Overloaded function */ |
|
1595 __ANY_NBIT(__iec_) |
|
1596 __ANY_NUM(__iec_) |
|
1597 #undef __iec_ |
|
1598 |
|
1599 /* Min for time data types */ |
|
1600 #define __iec_(TYPENAME) \ |
|
1601 __extrem_(MIN_##TYPENAME, TYPENAME, __time_cmp(op1, tmp) > 0) /* The explicitly typed standard functions */\ |
|
1602 __extrem_(MIN__##TYPENAME##__##TYPENAME, TYPENAME, __time_cmp(op1, tmp) > 0) /* Overloaded function */ |
|
1603 __ANY_DATE(__iec_) |
|
1604 __iec_(TIME) |
|
1605 #undef __iec_ |
|
1606 |
|
1607 /* Min for string data types */ |
|
1608 __extrem_(MIN_STRING, STRING, __STR_CMP(op1,tmp) > 0) /* The explicitly typed standard functions */ |
|
1609 __extrem_(MIN__STRING__STRING, STRING, __STR_CMP(op1,tmp) > 0) /* Overloaded function */ |
|
1610 |
|
1611 /**************/ |
|
1612 /* LIMIT */ |
|
1613 /**************/ |
|
1614 |
|
1615 /* Limit for numerical data types */ |
|
1616 #define __iec_(TYPENAME)\ |
|
1617 /* The explicitly typed standard functions */\ |
|
1618 static inline TYPENAME LIMIT_##TYPENAME(EN_ENO_PARAMS, TYPENAME MN, TYPENAME IN, TYPENAME MX){\ |
|
1619 TEST_EN(TYPENAME)\ |
|
1620 return IN > MN ? IN < MX ? IN : MX : MN;\ |
|
1621 }\ |
|
1622 /* Overloaded function */\ |
|
1623 static inline TYPENAME LIMIT__##TYPENAME##__##TYPENAME##__##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, TYPENAME MN, TYPENAME IN, TYPENAME MX){\ |
|
1624 TEST_EN(TYPENAME)\ |
|
1625 return IN > MN ? IN < MX ? IN : MX : MN;\ |
|
1626 } |
|
1627 __ANY_NBIT(__iec_) |
|
1628 __ANY_NUM(__iec_) |
|
1629 #undef __iec_ |
|
1630 |
|
1631 |
|
1632 /* Limit for time data types */ |
|
1633 #define __iec_(TYPENAME)\ |
|
1634 /* The explicitly typed standard functions */\ |
|
1635 static inline TYPENAME LIMIT_##TYPENAME(EN_ENO_PARAMS, TYPENAME MN, TYPENAME IN, TYPENAME MX){\ |
|
1636 TEST_EN(TYPENAME)\ |
|
1637 return __time_cmp(IN, MN) > 0 ? /* IN>MN ?*/\ |
|
1638 __time_cmp(IN, MX) < 0 ? /* IN<MX ?*/\ |
|
1639 IN : MX : MN;\ |
|
1640 }\ |
|
1641 /* Overloaded function */\ |
|
1642 static inline TYPENAME LIMIT__##TYPENAME##__##TYPENAME##__##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, TYPENAME MN, TYPENAME IN, TYPENAME MX){\ |
|
1643 TEST_EN(TYPENAME)\ |
|
1644 return __time_cmp(IN, MN) > 0 ? /* IN>MN ?*/\ |
|
1645 __time_cmp(IN, MX) < 0 ? /* IN<MX ?*/\ |
|
1646 IN : MX : MN;\ |
|
1647 } |
|
1648 |
|
1649 __ANY_DATE(__iec_) |
|
1650 __iec_(TIME) |
|
1651 #undef __iec_ |
|
1652 |
|
1653 /* Limit for string data types */ |
|
1654 /* The explicitly typed standard functions */ |
|
1655 static inline STRING LIMIT_STRING(EN_ENO_PARAMS, STRING MN, STRING IN, STRING MX){ |
|
1656 TEST_EN(STRING) |
|
1657 return __STR_CMP(IN, MN) > 0 ? __STR_CMP(IN, MX) < 0 ? IN : MX : MN; |
|
1658 } |
|
1659 |
|
1660 /* Overloaded function */ |
|
1661 static inline STRING LIMIT__STRING__STRING__STRING__STRING(EN_ENO_PARAMS, STRING MN, STRING IN, STRING MX){ |
|
1662 TEST_EN(STRING) |
|
1663 return __STR_CMP(IN, MN) > 0 ? __STR_CMP(IN, MX) < 0 ? IN : MX : MN; |
|
1664 } |
|
1665 |
|
1666 |
|
1667 /**************/ |
|
1668 /* MUX */ |
|
1669 /**************/ |
|
1670 /* The standard states that the inputs for SEL and MUX must be named starting off from 0, |
|
1671 * unlike remaining functions, that start off at 1. |
|
1672 */ |
|
1673 /* The explicitly typed standard functions */ |
|
1674 #define __in1_anyint_(in2_TYPENAME) __ANY_INT_1(__iec_,in2_TYPENAME) |
|
1675 #define __iec_(in1_TYPENAME,in2_TYPENAME) \ |
|
1676 static inline in2_TYPENAME MUX__##in2_TYPENAME##__##in1_TYPENAME##__##in2_TYPENAME(EN_ENO_PARAMS, in1_TYPENAME K, UINT param_count, ...){\ |
|
1677 va_list ap;\ |
|
1678 UINT i;\ |
|
1679 in2_TYPENAME tmp;\ |
|
1680 TEST_EN_COND(in2_TYPENAME, K >= param_count)\ |
|
1681 tmp = __INIT_##in2_TYPENAME;\ |
|
1682 \ |
|
1683 va_start (ap, param_count); /* Initialize the argument list. */\ |
|
1684 \ |
|
1685 for (i = 0; i < param_count; i++){\ |
|
1686 if(K == i){\ |
|
1687 tmp = va_arg (ap, VA_ARGS_##in2_TYPENAME);\ |
|
1688 va_end (ap); /* Clean up. */\ |
|
1689 return tmp;\ |
|
1690 }else{\ |
|
1691 va_arg (ap, VA_ARGS_##in2_TYPENAME);\ |
|
1692 }\ |
|
1693 }\ |
|
1694 \ |
|
1695 va_end (ap); /* Clean up. */\ |
|
1696 return tmp;\ |
|
1697 } |
|
1698 |
|
1699 __ANY(__in1_anyint_) |
|
1700 #undef __iec_ |
|
1701 |
|
1702 |
|
1703 /******************************************/ |
|
1704 /*** Table 28 ***/ |
|
1705 /*** Standard comparison functions ***/ |
|
1706 /******************************************/ |
|
1707 |
|
1708 #define __compare_(fname,TYPENAME, COND) \ |
|
1709 static inline BOOL fname(EN_ENO_PARAMS, UINT param_count, TYPENAME op1, ...){\ |
|
1710 va_list ap;\ |
|
1711 UINT i;\ |
|
1712 TEST_EN(BOOL)\ |
|
1713 \ |
|
1714 va_start (ap, op1); /* Initialize the argument list. */\ |
|
1715 DBG(#fname #TYPENAME "\n")\ |
|
1716 DBG_TYPE(TYPENAME, op1)\ |
|
1717 \ |
|
1718 for (i = 0; i < param_count - 1; i++){\ |
|
1719 TYPENAME tmp = va_arg (ap, VA_ARGS_##TYPENAME);\ |
|
1720 DBG_TYPE(TYPENAME, tmp)\ |
|
1721 if(COND){\ |
|
1722 op1 = tmp;\ |
|
1723 }else{\ |
|
1724 va_end (ap); /* Clean up. */\ |
|
1725 return 0;\ |
|
1726 }\ |
|
1727 }\ |
|
1728 \ |
|
1729 va_end (ap); /* Clean up. */\ |
|
1730 return 1;\ |
|
1731 } |
|
1732 |
|
1733 #define __compare_num(fname, TYPENAME, TEST) __compare_(fname, TYPENAME, op1 TEST tmp ) |
|
1734 #define __compare_time(fname, TYPENAME, TEST) __compare_(fname, TYPENAME, __time_cmp(op1, tmp) TEST 0) |
|
1735 #define __compare_string(fname, TEST) __compare_(fname, STRING, __STR_CMP(op1, tmp) TEST 0 ) |
|
1736 |
|
1737 |
|
1738 /**************/ |
|
1739 /* GT */ |
|
1740 /**************/ |
|
1741 /* Comparison for numerical data types */ |
|
1742 #define __iec_(TYPENAME) \ |
|
1743 __compare_num(GT_##TYPENAME, TYPENAME, > ) /* The explicitly typed standard functions */\ |
|
1744 __compare_num(GT__BOOL__##TYPENAME, TYPENAME, > ) /* Overloaded function */ |
|
1745 __ANY_NBIT(__iec_) |
|
1746 __ANY_NUM(__iec_) |
|
1747 #undef __iec_ |
|
1748 |
|
1749 /* Comparison for time data types */ |
|
1750 #define __iec_(TYPENAME) \ |
|
1751 __compare_time(GT_##TYPENAME, TYPENAME, > ) /* The explicitly typed standard functions */\ |
|
1752 __compare_time(GT__BOOL__##TYPENAME, TYPENAME, > ) /* Overloaded function */ |
|
1753 __ANY_DATE(__iec_) |
|
1754 __iec_(TIME) |
|
1755 #undef __iec_ |
|
1756 |
|
1757 /* Comparison for string data types */ |
|
1758 __compare_string(GT_STRING, > ) /* The explicitly typed standard functions */ |
|
1759 __compare_string(GT__BOOL__STRING, > ) /* Overloaded function */ |
|
1760 |
|
1761 /**************/ |
|
1762 /* GE */ |
|
1763 /**************/ |
|
1764 /* Comparison for numerical data types */ |
|
1765 #define __iec_(TYPENAME) \ |
|
1766 __compare_num(GE_##TYPENAME, TYPENAME, >= ) /* The explicitly typed standard functions */\ |
|
1767 __compare_num(GE__BOOL__##TYPENAME, TYPENAME, >= ) /* Overloaded function */ |
|
1768 __ANY_NBIT(__iec_) |
|
1769 __ANY_NUM(__iec_) |
|
1770 #undef __iec_ |
|
1771 |
|
1772 /* Comparison for time data types */ |
|
1773 #define __iec_(TYPENAME) \ |
|
1774 __compare_time(GE_##TYPENAME, TYPENAME, >= ) /* The explicitly typed standard functions */\ |
|
1775 __compare_time(GE__BOOL__##TYPENAME, TYPENAME, >= ) /* Overloaded function */ |
|
1776 __ANY_DATE(__iec_) |
|
1777 __iec_(TIME) |
|
1778 #undef __iec_ |
|
1779 |
|
1780 /* Comparison for string data types */ |
|
1781 __compare_string(GE_STRING, >= ) /* The explicitly typed standard functions */ |
|
1782 __compare_string(GE__BOOL__STRING, >= ) /* Overloaded function */ |
|
1783 |
|
1784 |
|
1785 |
|
1786 /**************/ |
|
1787 /* EQ */ |
|
1788 /**************/ |
|
1789 /* Comparison for numerical data types */ |
|
1790 #define __iec_(TYPENAME) \ |
|
1791 __compare_num(EQ_##TYPENAME, TYPENAME, == ) /* The explicitly typed standard functions */\ |
|
1792 __compare_num(EQ__BOOL__##TYPENAME, TYPENAME, == ) /* Overloaded function */ |
|
1793 __ANY_NBIT(__iec_) |
|
1794 __ANY_NUM(__iec_) |
|
1795 #undef __iec_ |
|
1796 |
|
1797 /* Comparison for time data types */ |
|
1798 #define __iec_(TYPENAME) \ |
|
1799 __compare_time(EQ_##TYPENAME, TYPENAME, == ) /* The explicitly typed standard functions */\ |
|
1800 __compare_time(EQ__BOOL__##TYPENAME, TYPENAME, == ) /* Overloaded function */ |
|
1801 __ANY_DATE(__iec_) |
|
1802 __iec_(TIME) |
|
1803 #undef __iec_ |
|
1804 |
|
1805 /* Comparison for string data types */ |
|
1806 __compare_string(EQ_STRING, == ) /* The explicitly typed standard functions */ |
|
1807 __compare_string(EQ__BOOL__STRING, == ) /* Overloaded function */ |
|
1808 |
|
1809 |
|
1810 /**************/ |
|
1811 /* LT */ |
|
1812 /**************/ |
|
1813 /* Comparison for numerical data types */ |
|
1814 #define __iec_(TYPENAME) \ |
|
1815 __compare_num(LT_##TYPENAME, TYPENAME, < ) /* The explicitly typed standard functions */\ |
|
1816 __compare_num(LT__BOOL__##TYPENAME, TYPENAME, < ) /* Overloaded function */ |
|
1817 __ANY_NBIT(__iec_) |
|
1818 __ANY_NUM(__iec_) |
|
1819 #undef __iec_ |
|
1820 |
|
1821 /* Comparison for time data types */ |
|
1822 #define __iec_(TYPENAME) \ |
|
1823 __compare_time(LT_##TYPENAME, TYPENAME, < ) /* The explicitly typed standard functions */\ |
|
1824 __compare_time(LT__BOOL__##TYPENAME, TYPENAME, < ) /* Overloaded function */ |
|
1825 __ANY_DATE(__iec_) |
|
1826 __iec_(TIME) |
|
1827 #undef __iec_ |
|
1828 |
|
1829 /* Comparison for string data types */ |
|
1830 __compare_string(LT_STRING, < ) /* The explicitly typed standard functions */ |
|
1831 __compare_string(LT__BOOL__STRING, < ) /* Overloaded function */ |
|
1832 |
|
1833 |
|
1834 /**************/ |
|
1835 /* LE */ |
|
1836 /**************/ |
|
1837 /* Comparison for numerical data types */ |
|
1838 #define __iec_(TYPENAME) \ |
|
1839 __compare_num(LE_##TYPENAME, TYPENAME, <= ) /* The explicitly typed standard functions */\ |
|
1840 __compare_num(LE__BOOL__##TYPENAME, TYPENAME, <= ) /* Overloaded function */ |
|
1841 __ANY_NBIT(__iec_) |
|
1842 __ANY_NUM(__iec_) |
|
1843 #undef __iec_ |
|
1844 |
|
1845 /* Comparison for time data types */ |
|
1846 #define __iec_(TYPENAME) \ |
|
1847 __compare_time(LE_##TYPENAME, TYPENAME, <= ) /* The explicitly typed standard functions */\ |
|
1848 __compare_time(LE__BOOL__##TYPENAME, TYPENAME, <= ) /* Overloaded function */ |
|
1849 __ANY_DATE(__iec_) |
|
1850 __iec_(TIME) |
|
1851 #undef __iec_ |
|
1852 |
|
1853 /* Comparison for string data types */ |
|
1854 __compare_string(LE_STRING, <= ) /* The explicitly typed standard functions */ |
|
1855 __compare_string(LE__BOOL__STRING, <= ) /* Overloaded function */ |
|
1856 |
|
1857 |
|
1858 /**************/ |
|
1859 /* NE */ |
|
1860 /**************/ |
|
1861 #define __ne_num(fname, TYPENAME) \ |
|
1862 static inline BOOL fname(EN_ENO_PARAMS, TYPENAME op1, TYPENAME op2){\ |
|
1863 TEST_EN(BOOL)\ |
|
1864 return op1 != op2 ? 1 : 0;\ |
|
1865 } |
|
1866 |
|
1867 #define __ne_time(fname, TYPENAME) \ |
|
1868 static inline BOOL fname(EN_ENO_PARAMS, TYPENAME op1, TYPENAME op2){\ |
|
1869 TEST_EN(BOOL)\ |
|
1870 return __time_cmp(op1, op2) != 0 ? 1 : 0;\ |
|
1871 } |
|
1872 |
|
1873 #define __ne_string(fname, TYPENAME) \ |
|
1874 static inline BOOL fname(EN_ENO_PARAMS, TYPENAME op1, TYPENAME op2){\ |
|
1875 TEST_EN(BOOL)\ |
|
1876 return __STR_CMP(op1, op2) != 0 ? 1 : 0;\ |
|
1877 } |
|
1878 |
|
1879 /* Comparison for numerical data types */ |
|
1880 #define __iec_(TYPENAME) \ |
|
1881 __ne_num(NE_##TYPENAME, TYPENAME) /* The explicitly typed standard functions */\ |
|
1882 __ne_num(NE__BOOL__##TYPENAME##__##TYPENAME, TYPENAME) /* Overloaded function */ |
|
1883 __ANY_NBIT(__iec_) |
|
1884 __ANY_NUM(__iec_) |
|
1885 #undef __iec_ |
|
1886 |
|
1887 /* Comparison for time data types */ |
|
1888 #define __iec_(TYPENAME) \ |
|
1889 __ne_time(NE_##TYPENAME, TYPENAME) /* The explicitly typed standard functions */\ |
|
1890 __ne_time(NE__BOOL__##TYPENAME##__##TYPENAME, TYPENAME) /* Overloaded function */ |
|
1891 __ANY_DATE(__iec_) |
|
1892 __iec_(TIME) |
|
1893 #undef __iec_ |
|
1894 |
|
1895 /* Comparison for string data types */ |
|
1896 __ne_string(NE_STRING, STRING) /* The explicitly typed standard functions */ |
|
1897 __ne_string(NE__BOOL__STRING__STRING, STRING) /* Overloaded function */ |
|
1898 |
|
1899 |
|
1900 |
|
1901 |
|
1902 |
|
1903 |
|
1904 /*********************************************/ |
|
1905 /*********************************************/ |
|
1906 /* 2.5.1.5.5 Character string Functions */ |
|
1907 /*********************************************/ |
|
1908 /*********************************************/ |
|
1909 |
|
1910 /*************************************/ |
|
1911 /*** Table 29 ***/ |
|
1912 /*** Character string Functions ***/ |
|
1913 /*************************************/ |
|
1914 |
|
1915 /* We do not delcare explcitly typed versions of the functions in table 29. |
|
1916 * See note above regarding explicitly typed functions for more details. |
|
1917 */ |
|
1918 |
|
1919 |
|
1920 |
|
1921 |
|
1922 /***************/ |
|
1923 /* LEN */ |
|
1924 /***************/ |
|
1925 static inline __strlen_t __len(STRING IN) {return IN.len;} |
|
1926 |
|
1927 /* A function, with 1 input paramter, implementing a generic OPERATION */ |
|
1928 #define __genoper_1p_(fname,ret_TYPENAME, par_TYPENAME, OPERATION) \ |
|
1929 static inline ret_TYPENAME fname(EN_ENO_PARAMS, par_TYPENAME par1){\ |
|
1930 TEST_EN(ret_TYPENAME)\ |
|
1931 return (ret_TYPENAME)OPERATION(par1);\ |
|
1932 } |
|
1933 |
|
1934 #define __iec_(TYPENAME) __genoper_1p_(LEN__##TYPENAME##__STRING, TYPENAME, STRING, __len) |
|
1935 __ANY_INT(__iec_) |
|
1936 #undef __iec_ |
|
1937 |
|
1938 |
|
1939 /****************/ |
|
1940 /* LEFT */ |
|
1941 /****************/ |
|
1942 |
|
1943 #define __left(TYPENAME) \ |
|
1944 static inline STRING LEFT__STRING__STRING__##TYPENAME(EN_ENO_PARAMS, STRING IN, TYPENAME L){\ |
|
1945 STRING res;\ |
|
1946 TEST_EN_COND(STRING, L < 0)\ |
|
1947 res = __INIT_STRING;\ |
|
1948 L = L < (TYPENAME)IN.len ? L : (TYPENAME)IN.len;\ |
|
1949 memcpy(&res.body, &IN.body, (size_t)L);\ |
|
1950 res.len = (__strlen_t)L;\ |
|
1951 return res;\ |
|
1952 } |
|
1953 __ANY_INT(__left) |
|
1954 |
|
1955 |
|
1956 /*****************/ |
|
1957 /* RIGHT */ |
|
1958 /*****************/ |
|
1959 |
|
1960 #define __right(TYPENAME) \ |
|
1961 static inline STRING RIGHT__STRING__STRING__##TYPENAME(EN_ENO_PARAMS, STRING IN, TYPENAME L){\ |
|
1962 STRING res;\ |
|
1963 TEST_EN_COND(STRING, L < 0)\ |
|
1964 res = __INIT_STRING;\ |
|
1965 L = L < (TYPENAME)IN.len ? L : (TYPENAME)IN.len;\ |
|
1966 memcpy(&res.body, &IN.body[(TYPENAME)IN.len - L], (size_t)L);\ |
|
1967 res.len = (__strlen_t)L;\ |
|
1968 return res;\ |
|
1969 } |
|
1970 __ANY_INT(__right) |
|
1971 |
|
1972 |
|
1973 /***************/ |
|
1974 /* MID */ |
|
1975 /***************/ |
|
1976 |
|
1977 #define __mid(TYPENAME) \ |
|
1978 static inline STRING MID__STRING__STRING__##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, STRING IN, TYPENAME L, TYPENAME P){\ |
|
1979 STRING res;\ |
|
1980 TEST_EN_COND(STRING, L < 0 || P < 0)\ |
|
1981 res = __INIT_STRING;\ |
|
1982 if(P <= (TYPENAME)IN.len){\ |
|
1983 P -= 1; /* now can be used as [index]*/\ |
|
1984 L = L + P <= (TYPENAME)IN.len ? L : (TYPENAME)IN.len - P;\ |
|
1985 memcpy(&res.body, &IN.body[P] , (size_t)L);\ |
|
1986 res.len = (__strlen_t)L;\ |
|
1987 }\ |
|
1988 return res;\ |
|
1989 } |
|
1990 __ANY_INT(__mid) |
|
1991 |
|
1992 |
|
1993 /******************/ |
|
1994 /* CONCAT */ |
|
1995 /******************/ |
|
1996 |
|
1997 static inline STRING CONCAT(EN_ENO_PARAMS, UINT param_count, ...){ |
|
1998 UINT i; |
|
1999 STRING res; |
|
2000 va_list ap; |
|
2001 __strlen_t charcount; |
|
2002 TEST_EN(STRING) |
|
2003 charcount = 0; |
|
2004 res = __INIT_STRING; |
|
2005 |
|
2006 va_start (ap, param_count); /* Initialize the argument list. */ |
|
2007 |
|
2008 for (i = 0; i < param_count && charcount < STR_MAX_LEN; i++) |
|
2009 { |
|
2010 STRING tmp = va_arg(ap, STRING); |
|
2011 __strlen_t charrem = STR_MAX_LEN - charcount; |
|
2012 __strlen_t to_write = tmp.len > charrem ? charrem : tmp.len; |
|
2013 memcpy(&res.body[charcount], &tmp.body , to_write); |
|
2014 charcount += to_write; |
|
2015 } |
|
2016 |
|
2017 res.len = charcount; |
|
2018 |
|
2019 va_end (ap); /* Clean up. */ |
|
2020 return res; |
|
2021 } |
|
2022 |
|
2023 /******************/ |
|
2024 /* INSERT */ |
|
2025 /******************/ |
|
2026 |
|
2027 static inline STRING __insert(STRING IN1, STRING IN2, __strlen_t P){ |
|
2028 STRING res; |
|
2029 __strlen_t to_copy; |
|
2030 res = __INIT_STRING; |
|
2031 |
|
2032 to_copy = P > IN1.len ? IN1.len : P; |
|
2033 memcpy(&res.body, &IN1.body , to_copy); |
|
2034 P = res.len = to_copy; |
|
2035 |
|
2036 to_copy = IN2.len + res.len > STR_MAX_LEN ? STR_MAX_LEN - res.len : IN2.len; |
|
2037 memcpy(&res.body[res.len], &IN2.body , to_copy); |
|
2038 res.len += to_copy; |
|
2039 |
|
2040 to_copy = IN1.len - P < STR_MAX_LEN - res.len ? IN1.len - P : STR_MAX_LEN - res.len ; |
|
2041 memcpy(&res.body[res.len], &IN1.body[P] , to_copy); |
|
2042 res.len += to_copy; |
|
2043 |
|
2044 return res; |
|
2045 } |
|
2046 |
|
2047 #define __iec_(TYPENAME) \ |
|
2048 static inline STRING INSERT__STRING__STRING__STRING__##TYPENAME(EN_ENO_PARAMS, STRING str1, STRING str2, TYPENAME P){\ |
|
2049 TEST_EN_COND(STRING, P < 0)\ |
|
2050 return (STRING)__insert(str1,str2,(__strlen_t)P);\ |
|
2051 } |
|
2052 __ANY_INT(__iec_) |
|
2053 #undef __iec_ |
|
2054 |
|
2055 |
|
2056 /******************/ |
|
2057 /* DELETE */ |
|
2058 /******************/ |
|
2059 |
|
2060 static inline STRING __delete(STRING IN, __strlen_t L, __strlen_t P){ |
|
2061 STRING res; |
|
2062 __strlen_t to_copy; |
|
2063 res = __INIT_STRING; |
|
2064 |
|
2065 to_copy = P > IN.len ? IN.len : P-1; |
|
2066 memcpy(&res.body, &IN.body , to_copy); |
|
2067 P = res.len = to_copy; |
|
2068 |
|
2069 if( IN.len > P + L ){ |
|
2070 to_copy = IN.len - P - L; |
|
2071 memcpy(&res.body[res.len], &IN.body[P + L], to_copy); |
|
2072 res.len += to_copy; |
|
2073 } |
|
2074 |
|
2075 return res; |
|
2076 } |
|
2077 |
|
2078 #define __iec_(TYPENAME) \ |
|
2079 static inline STRING DELETE__STRING__STRING__##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, STRING str, TYPENAME L, TYPENAME P){\ |
|
2080 TEST_EN_COND(STRING, L < 0 || P < 0)\ |
|
2081 return (STRING)__delete(str,(__strlen_t)L,(__strlen_t)P);\ |
|
2082 } |
|
2083 __ANY_INT(__iec_) |
|
2084 #undef __iec_ |
|
2085 |
|
2086 |
|
2087 /*******************/ |
|
2088 /* REPLACE */ |
|
2089 /*******************/ |
|
2090 |
|
2091 static inline STRING __replace(STRING IN1, STRING IN2, __strlen_t L, __strlen_t P){ |
|
2092 STRING res; |
|
2093 __strlen_t to_copy; |
|
2094 res = __INIT_STRING; |
|
2095 |
|
2096 to_copy = P > IN1.len ? IN1.len : P-1; |
|
2097 memcpy(&res.body, &IN1.body , to_copy); |
|
2098 P = res.len = to_copy; |
|
2099 |
|
2100 to_copy = IN2.len < L ? IN2.len : L; |
|
2101 |
|
2102 if( to_copy + res.len > STR_MAX_LEN ) |
|
2103 to_copy = STR_MAX_LEN - res.len; |
|
2104 |
|
2105 memcpy(&res.body[res.len], &IN2.body , to_copy); |
|
2106 res.len += to_copy; |
|
2107 |
|
2108 P += L; |
|
2109 if( res.len < STR_MAX_LEN && P < IN1.len) |
|
2110 { |
|
2111 to_copy = IN1.len - P; |
|
2112 memcpy(&res.body[res.len], &IN1.body[P] , to_copy); |
|
2113 res.len += to_copy; |
|
2114 } |
|
2115 |
|
2116 return res; |
|
2117 } |
|
2118 |
|
2119 #define __iec_(TYPENAME) \ |
|
2120 static inline STRING REPLACE__STRING__STRING__STRING__##TYPENAME##__##TYPENAME(EN_ENO_PARAMS, STRING str1, STRING str2, TYPENAME L, TYPENAME P){\ |
|
2121 TEST_EN_COND(STRING, L < 0 || P < 0)\ |
|
2122 return (STRING)__replace(str1,str2,(__strlen_t)L,(__strlen_t)P);\ |
|
2123 } |
|
2124 __ANY_INT(__iec_) |
|
2125 #undef __iec_ |
|
2126 |
|
2127 /****************/ |
|
2128 /* FIND */ |
|
2129 /****************/ |
|
2130 |
|
2131 static inline __strlen_t __pfind(STRING* IN1, STRING* IN2){ |
|
2132 UINT count1 = 0; /* offset of first matching char in IN1 */ |
|
2133 UINT count2 = 0; /* count of matching char */ |
|
2134 while(count1 + count2 < IN1->len && count2 < IN2->len) |
|
2135 { |
|
2136 if(IN1->body[count1 + count2] != IN2->body[count2]){ |
|
2137 count1 += count2 + 1; |
|
2138 count2 = 0; |
|
2139 } |
|
2140 else { |
|
2141 count2++; |
|
2142 } |
|
2143 } |
|
2144 return count2 == IN2->len -1 ? 0 : count1 + 1; |
|
2145 } |
|
2146 |
|
2147 #define __iec_(TYPENAME) \ |
|
2148 static inline TYPENAME FIND__##TYPENAME##__STRING__STRING(EN_ENO_PARAMS, STRING str1, STRING str2){\ |
|
2149 TEST_EN(TYPENAME)\ |
|
2150 return (TYPENAME)__pfind(&str1,&str2);\ |
|
2151 } |
|
2152 __ANY_INT(__iec_) |
|
2153 #undef __iec_ |
|
2154 |
|
2155 |
|
2156 /*********************************************/ |
|
2157 /*********************************************/ |
|
2158 /* 2.5.1.5.6 Functions of time data types */ |
|
2159 /*********************************************/ |
|
2160 /*********************************************/ |
|
2161 |
|
2162 /**************************************/ |
|
2163 /*** Table 30 ***/ |
|
2164 /*** Functions of time data types ***/ |
|
2165 /**************************************/ |
|
2166 |
|
2167 |
|
2168 static inline TIME ADD_TIME(EN_ENO_PARAMS, TIME IN1, TIME IN2){ |
|
2169 TEST_EN(TIME) |
|
2170 return __time_add(IN1, IN2); |
|
2171 } |
|
2172 |
|
2173 static inline TOD ADD_TOD_TIME(EN_ENO_PARAMS, TOD IN1, TIME IN2){ |
|
2174 TEST_EN(TOD) |
|
2175 return __time_add(IN1, IN2); |
|
2176 } |
|
2177 |
|
2178 static inline DT ADD_DT_TIME(EN_ENO_PARAMS, DT IN1, TIME IN2){ |
|
2179 TEST_EN(DT) |
|
2180 return __time_add(IN1, IN2); |
|
2181 } |
|
2182 |
|
2183 static inline TIME SUB_TIME(EN_ENO_PARAMS, TIME IN1, TIME IN2){ |
|
2184 TEST_EN(TIME) |
|
2185 return __time_sub(IN1, IN2); |
|
2186 } |
|
2187 |
|
2188 static inline TIME SUB_DATE_DATE(EN_ENO_PARAMS, DATE IN1, DATE IN2){ |
|
2189 TEST_EN(TIME) |
|
2190 return __time_sub(IN1, IN2); |
|
2191 } |
|
2192 |
|
2193 static inline TOD SUB_TOD_TIME(EN_ENO_PARAMS, TOD IN1, TIME IN2){ |
|
2194 TEST_EN(TOD) |
|
2195 return __time_sub(IN1, IN2); |
|
2196 } |
|
2197 |
|
2198 static inline TIME SUB_TOD_TOD(EN_ENO_PARAMS, TOD IN1, TOD IN2){ |
|
2199 TEST_EN(TIME) |
|
2200 return __time_sub(IN1, IN2); |
|
2201 } |
|
2202 |
|
2203 static inline DT SUB_DT_TIME(EN_ENO_PARAMS, DT IN1, TIME IN2){ |
|
2204 TEST_EN(DT) |
|
2205 return __time_sub(IN1, IN2); |
|
2206 } |
|
2207 |
|
2208 static inline TIME SUB_DT_DT(EN_ENO_PARAMS, DT IN1, DT IN2){ |
|
2209 TEST_EN(TIME) |
|
2210 return __time_sub(IN1, IN2); |
|
2211 } |
|
2212 |
|
2213 |
|
2214 /*** MULTIME ***/ |
|
2215 #define __iec_(TYPENAME)\ |
|
2216 static inline TIME MULTIME__TIME__TIME__##TYPENAME(EN_ENO_PARAMS, TIME IN1, TYPENAME IN2){\ |
|
2217 TEST_EN(TIME)\ |
|
2218 return __time_mul(IN1, (LREAL)IN2);\ |
|
2219 } |
|
2220 __ANY_NUM(__iec_) |
|
2221 #undef __iec_ |
|
2222 |
|
2223 /*** MUL ***/ |
|
2224 #define __iec_(TYPENAME)\ |
|
2225 static inline TIME MUL__TIME__TIME__##TYPENAME(EN_ENO_PARAMS, TIME IN1, TYPENAME IN2){\ |
|
2226 TEST_EN(TIME)\ |
|
2227 return __time_mul(IN1, (LREAL)IN2);\ |
|
2228 } |
|
2229 __ANY_NUM(__iec_) |
|
2230 #undef __iec_ |
|
2231 |
|
2232 /*** DIVTIME ***/ |
|
2233 #define __iec_(TYPENAME)\ |
|
2234 static inline TIME DIVTIME__TIME__TIME__##TYPENAME(EN_ENO_PARAMS, TIME IN1, TYPENAME IN2){\ |
|
2235 TEST_EN(TIME)\ |
|
2236 return __time_div(IN1, (LREAL)IN2);\ |
|
2237 } |
|
2238 __ANY_NUM(__iec_) |
|
2239 #undef __iec_ |
|
2240 |
|
2241 /*** DIV ***/ |
|
2242 #define __iec_(TYPENAME)\ |
|
2243 static inline TIME DIV__TIME__TIME__##TYPENAME(EN_ENO_PARAMS, TIME IN1, TYPENAME IN2){\ |
|
2244 TEST_EN(TIME)\ |
|
2245 return __time_div(IN1, (LREAL)IN2);\ |
|
2246 } |
|
2247 __ANY_NUM(__iec_) |
|
2248 #undef __iec_ |
|
2249 |
|
2250 /*** CONCAT_DATE_TOD ***/ |
|
2251 static inline DT CONCAT_DATE_TOD(EN_ENO_PARAMS, DATE IN1, TOD IN2){ |
|
2252 TEST_EN(DT) |
|
2253 return __time_add(IN1, IN2); |
|
2254 } |
|
2255 |
|
2256 |
|
2257 |
|
2258 /****************************************************/ |
|
2259 /****************************************************/ |
|
2260 /* 2.5.1.5.6 Functions of enumerated data types */ |
|
2261 /****************************************************/ |
|
2262 /****************************************************/ |
|
2263 |
|
2264 /********************************************/ |
|
2265 /*** Table 31 ***/ |
|
2266 /*** Functions of enumerated data types ***/ |
|
2267 /********************************************/ |
|
2268 |
|
2269 /* Do we support this? */ |
|
2270 |
|
2271 #endif /* _IEC_STD_LIB_H */ |
|