forked from OpenCPN/OpenCPN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcutil.cpp
More file actions
105 lines (88 loc) · 3.18 KB
/
cutil.cpp
File metadata and controls
105 lines (88 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/***************************************************************************
* Copyright (C) 2010 by David S. Register *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, see <https://www.gnu.org/licenses/>. *
**************************************************************************/
/**
* \file
*
* Implement cutil.h -- extern C linked Utilities
*/
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include "model/cutil.h"
double round_msvc(double x) { return (floor(x + 0.5)); }
#ifdef __MSVC__
#include <winsock2.h>
#include <windows.h>
#include <float.h> // for _clear87()
long __stdcall MyUnhandledExceptionFilter(
struct _EXCEPTION_POINTERS *ExceptionInfo) {
// return EXCEPTION_EXECUTE_HANDLER ; // terminates the app
switch (ExceptionInfo->ExceptionRecord->ExceptionCode) {
case EXCEPTION_FLT_DENORMAL_OPERAND:
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
case EXCEPTION_FLT_INEXACT_RESULT:
case EXCEPTION_FLT_INVALID_OPERATION:
case EXCEPTION_FLT_OVERFLOW:
case EXCEPTION_FLT_STACK_CHECK:
case EXCEPTION_FLT_UNDERFLOW:
_clear87();
return EXCEPTION_CONTINUE_EXECUTION; // retry
default:
return EXCEPTION_CONTINUE_SEARCH; // standard fatal dialog box
}
}
#endif
/* Replacement for __MSVC__ in absence of snprintf or _snprintf */
#ifdef __MSVC__
int mysnprintf(char *buffer, int count, const char *format, ...) {
int ret;
va_list arg;
va_start(arg, format);
ret = _vsnprintf(buffer, count, format, arg);
va_end(arg);
return ret;
}
#endif
int NextPow2(int size) {
int n = size - 1; // compute dimensions needed as next larger power of 2
int shift = 1;
while ((n + 1) & n) {
n |= n >> shift;
shift <<= 1;
}
return n + 1;
}
#ifdef __WXMSW__
extern "C" int clock_gettime_monotonic(struct timespec *tv) {
static LARGE_INTEGER ticksPerSec;
LARGE_INTEGER ticks;
if (!ticksPerSec.QuadPart) {
QueryPerformanceFrequency(&ticksPerSec);
if (!ticksPerSec.QuadPart) {
errno = ENOTSUP;
return -1;
}
}
QueryPerformanceCounter(&ticks);
tv->tv_sec = (long)(ticks.QuadPart / ticksPerSec.QuadPart);
tv->tv_nsec = (long)(((ticks.QuadPart % ticksPerSec.QuadPart) * 1e9) /
ticksPerSec.QuadPart);
return 0;
}
#endif