Skip to content

Commit f575064

Browse files
committed
- Added omc_msvc.h: A compatibility header that adds inf,nan and round macros for MSVC.
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@5500 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 41d728d commit f575064

File tree

3 files changed

+57
-8
lines changed

3 files changed

+57
-8
lines changed

Compiler/runtime/omc_msvc.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* This file is part of OpenModelica.
3+
*
4+
* Copyright (c) 1998-2010, Linköpings University,
5+
* 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 THIS OSMC PUBLIC
11+
* LICENSE (OSMC-PL). ANY USE, REPRODUCTION OR DISTRIBUTION OF
12+
* THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THE OSMC
13+
* PUBLIC LICENSE.
14+
*
15+
* The OpenModelica software and the Open Source Modelica
16+
* Consortium (OSMC) Public License (OSMC-PL) are obtained
17+
* from Linköpings University, either from the above address,
18+
* from the URL: http://www.ida.liu.se/projects/OpenModelica
19+
* and in the OpenModelica distribution.
20+
*
21+
* This program is distributed WITHOUT ANY WARRANTY; without
22+
* even the implied warranty of MERCHANTABILITY or FITNESS
23+
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
24+
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
25+
* OF OSMC-PL.
26+
*
27+
* See the full OSMC Public License conditions for more details.
28+
*
29+
*/
30+
#ifndef __OPENMODELICA_MSVC_H
31+
#define __OPENMODELICA_MSVC_H
32+
33+
/* Compatibility header for MSVC compiler */
34+
#if defined(_MSC_VER)
35+
36+
union MSVC_FLOAT_HACK
37+
{
38+
unsigned char Bytes[4];
39+
float Value;
40+
};
41+
static union MSVC_FLOAT_HACK __INFINITY = {{0x00, 0x00, 0x80, 0x7F}};
42+
static union MSVC_FLOAT_HACK __NAN = {{0x00, 0x00, 0xC0, 0x7F}};
43+
#define INFINITY (__INFINITY.Value)
44+
#define NAN (__NAN.Value)
45+
46+
#define round(dbl) (dbl >= 0.0 ? (int)(dbl + 0.5) : ((dbl - (double)(int)dbl) <= -0.5 ? (int)dbl : (int)(dbl - 0.5)))
47+
48+
#endif
49+
50+
#endif

Compiler/runtime/ptolemyio.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ extern "C"
5050
#include <string.h>
5151
#include <stdlib.h>
5252
#include <math.h>
53+
#include "omc_msvc.h" /* For INFINITY and NAN */
5354
void print_error_buf_impl(const char* str);
5455

5556
/* Given a file name and an array of variables, return the RML datastructure
@@ -123,19 +124,19 @@ void * read_ptolemy_dataset(char*filename, int size,char**vars,int datasize)
123124

124125
buf1 = values.substr(commapos+1).c_str();
125126
val = strtod(buf1,&buf2); // Second value after comma
126-
// INFINITY and NAN is not defined in the MSVC version of math.h., hence the exclusion of the code snippet below on MSVC.
127-
#if !defined(_MSC_VER)
127+
128128
if (buf1 == buf2) {
129129
// We may be trying to parse Infinity on a Windows platform.
130130
// Don't we feel stupid expecting this to work?
131-
// Let's do these extra tests on Linux as well to check for NaN
132131
if (0 == strncmp(buf1,"Inf",3)) val = INFINITY;
133132
else if (0 == strncmp(buf1,"-Inf",4)) val = -INFINITY;
134133
else if (0 == strncmp(buf1,"inf",3)) val = INFINITY;
135134
else if (0 == strncmp(buf1,"-inf",4)) val = -INFINITY;
135+
// Don't put 0.0 if the value is undefined.
136+
// NaN sends a clear signal to the user that he has a problem.
136137
else val = NAN;
137138
}
138-
#endif
139+
139140
lst = (void*)mk_cons(Values__REAL(mk_rcon(val)),lst);
140141
j++;
141142
}

Compiler/runtime/unitparser.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,11 @@
2828
*
2929
*/
3030

31-
#if defined(_MSC_VER)
32-
#define round(dbl) (dbl >= 0.0 ? (int)(dbl + 0.5) : ((dbl - (double)(int)dbl) <= -0.5 ? (int)dbl : (int)(dbl - 0.5)))
33-
#endif
34-
3531
#include "unitparser.h"
3632
#include <iostream>
3733
#include <sstream>
3834
#include <stack>
35+
#include "omc_msvc.h" /* For round() */
3936
#ifndef NO_LPLIB
4037
#include "lp_lib.h"
4138
#endif
@@ -282,6 +279,7 @@ void UnitParser::addPrefix(const string symbol, Rational exponent) {
282279
}
283280

284281
void UnitParser::addBase(const string quantityName, const string unitName,
282+
285283
const string unitSymbol, bool prefixAllowed) {
286284
if (_units.find(unitSymbol) == _units.end()) {
287285
Base b(quantityName, unitName, unitSymbol, prefixAllowed);

0 commit comments

Comments
 (0)