utility.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * This file is part of OpenModelica.
  3. *
  4. * Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
  5. * c/o Linköpings universitet, Department of Computer and Information Science,
  6. * SE-58183 Linköping, Sweden.
  7. *
  8. * All rights reserved.
  9. *
  10. * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE
  11. * GPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
  12. * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
  13. * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
  14. * ACCORDING TO RECIPIENTS CHOICE.
  15. *
  16. * The OpenModelica software and the OSMC (Open Source Modelica Consortium)
  17. * Public License (OSMC-PL) are obtained from OSMC, either from the above
  18. * address, from the URLs: http://www.openmodelica.org or
  19. * http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica
  20. * distribution. GNU version 3 is obtained from:
  21. * http://www.gnu.org/copyleft/gpl.html. The New BSD License is obtained from:
  22. * http://www.opensource.org/licenses/BSD-3-Clause.
  23. *
  24. * This program is distributed WITHOUT ANY WARRANTY; without even the implied
  25. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS
  26. * EXPRESSLY SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE
  27. * CONDITIONS OF OSMC-PL.
  28. *
  29. */
  30. #ifndef UTILITY_H
  31. #define UTILITY_H
  32. #include "openmodelica.h"
  33. #include <math.h>
  34. static inline int in_range_boolean(modelica_integer b,
  35. modelica_integer start,
  36. modelica_integer stop)
  37. {
  38. if(start <= stop) {
  39. if((b >= start) && (b <= stop)) {
  40. return 1;
  41. }
  42. } else {
  43. if((b >= stop) && (b <= start)) {
  44. return 1;
  45. }
  46. }
  47. return 0;
  48. }
  49. static inline int in_range_integer(modelica_integer i,
  50. modelica_integer start,
  51. modelica_integer stop)
  52. {
  53. if(start <= stop) {
  54. if((i >= start) && (i <= stop)) {
  55. return 1;
  56. }
  57. } else {
  58. if((i >= stop) && (i <= start)) {
  59. return 1;
  60. }
  61. }
  62. return 0;
  63. }
  64. static inline int in_range_real(modelica_real i,
  65. modelica_real start,
  66. modelica_real stop)
  67. {
  68. if(start <= stop) {
  69. if((i >= start) && (i <= stop)) {
  70. return 1;
  71. }
  72. } else {
  73. if((i >= stop) && (i <= start)) {
  74. return 1;
  75. }
  76. }
  77. return 0;
  78. }
  79. /* div is already defined in stdlib, so it's redefined here to modelica_div */
  80. static inline modelica_real modelica_div(modelica_real x, modelica_real y)
  81. {
  82. return (modelica_real)((modelica_integer)(x/y));
  83. }
  84. /* fmod in math.h does not work in the same way as mod defined by modelica, so
  85. * we need to define our own mod. */
  86. static inline modelica_real modelica_mod_real(modelica_real x, modelica_real y)
  87. {
  88. return (x - (floor(x/y) * y));
  89. }
  90. static inline modelica_integer modelica_mod_integer(modelica_integer x, modelica_integer y)
  91. {
  92. return x % y;
  93. }
  94. static inline modelica_integer modelica_integer_min(modelica_integer x,modelica_integer y)
  95. {
  96. return (x < y) ? x : y;
  97. }
  98. static inline modelica_integer modelica_integer_max(modelica_integer x,modelica_integer y)
  99. {
  100. return (x > y) ? x : y;
  101. }
  102. static inline modelica_real modelica_real_min(modelica_real x,modelica_real y)
  103. {
  104. return (x < y) ? x : y;
  105. }
  106. static inline modelica_real modelica_real_max(modelica_real x,modelica_real y)
  107. {
  108. return (x > y) ? x : y;
  109. }
  110. #define reduction_sum(X,Y) ((X)+(Y))
  111. #define reduction_product(X,Y) ((X)*(Y))
  112. /* pow(), but for integer exponents (faster implementation) */
  113. extern modelica_real real_int_pow(threadData_t *threadData, modelica_real base,modelica_integer n);
  114. #if !defined(OMC_MINIMAL_RUNTIME)
  115. /* Returns 0 on failure. The first element in nmatches contains the error-message. */
  116. extern int OpenModelica_regexImpl(const char* str, const char* re, const int maxn, int extended, int sensitive, void*(*)(const char*), void **result);
  117. /* Wrapper for the builtin call */
  118. extern int OpenModelica_regex(const char* str, const char* re, int maxn, int extended, int sensitive, const char **result);
  119. #endif
  120. #endif