Skip to content

Commit 314458a

Browse files
committed
Remove CRT dependency from Math plugin
1 parent 50b19eb commit 314458a

File tree

6 files changed

+122
-12
lines changed

6 files changed

+122
-12
lines changed

Contrib/Math/Math.txt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,28 @@ to perform flow controlled declaration.
194194
SUCH IS NOT POSSIBLE: "#[a<0, #func()(1), #func()(2)]"
195195
IT WILL SIMPLY DEFINE #func as (2), as the latest variant.
196196

197-
(c) Nik Medved (brainsucker)
197+
(c) Nik Medved (brainsucker)
198+
199+
200+
201+
XPRINTF LIBRARY
202+
Copyright (C) 2021, ChaN, all right reserved.
203+
204+
xprintf module is an open source software. Redistribution and use of
205+
xprintf module in source and binary forms, with or without modification,
206+
are permitted provided that the following condition is met:
207+
1. Redistributions of source code must retain the above copyright notice,
208+
this condition and the following disclaimer.
209+
This software is provided by the copyright holder and contributors "AS IS"
210+
and any warranties related to this software are DISCLAIMED.
211+
The copyright owner or contributors be NOT LIABLE for any damages caused
212+
by use of this software.
213+
214+
215+
216+
MATH LIBRARY:
217+
Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
218+
219+
Developed at SunSoft, a Sun Microsystems, Inc. business.
220+
Permission to use, copy, modify, and distribute this
221+
software is freely granted, provided that this notice is preserved.

Contrib/Math/SConscript

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,44 @@ files = Split("""
44
Source/Math.c
55
Source/MyMath.c
66
Source/plugin.c
7+
Source/xprintf.c
8+
""")
9+
10+
libm_files = Split("""
11+
Source/mathlib/e_acos.c
12+
Source/mathlib/e_atan2.c
13+
Source/mathlib/e_asin.c
14+
Source/mathlib/e_cosh.c
15+
Source/mathlib/e_exp.c
16+
Source/mathlib/e_fmod.c
17+
Source/mathlib/e_log.c
18+
Source/mathlib/e_log10.c
19+
Source/mathlib/e_pow.c
20+
Source/mathlib/e_rem_pio2.c
21+
Source/mathlib/e_scalb.c
22+
Source/mathlib/e_sinh.c
23+
Source/mathlib/e_sqrt.c
24+
Source/mathlib/isinf.c
25+
Source/mathlib/k_cos.c
26+
Source/mathlib/k_rem_pio2.c
27+
Source/mathlib/k_sin.c
28+
Source/mathlib/k_tan.c
29+
Source/mathlib/s_atan.c
30+
Source/mathlib/s_ceil.c
31+
Source/mathlib/s_copysign.c
32+
Source/mathlib/s_cos.c
33+
Source/mathlib/s_expm1.c
34+
Source/mathlib/s_fabs.c
35+
Source/mathlib/s_finite.c
36+
Source/mathlib/s_floor.c
37+
Source/mathlib/s_frexp.c
38+
Source/mathlib/s_isnan.c
39+
Source/mathlib/s_modf.c
40+
Source/mathlib/s_rint.c
41+
Source/mathlib/s_scalbn.c
42+
Source/mathlib/s_sin.c
43+
Source/mathlib/s_tan.c
44+
Source/mathlib/s_tanh.c
745
""")
846

947
libs = Split("""
@@ -22,6 +60,10 @@ docs = Split("""
2260
Math.txt
2361
""")
2462

63+
defines = ['__BIG_ENDIAN=4321', '__LITTLE_ENDIAN=1234', '__BYTE_ORDER=1234', 'lint=1']
64+
2565
Import('BuildPlugin env')
2666

27-
BuildPlugin(target, files, libs, examples, docs, nodeflib = False, flags = ['$CPP_FLAG'], entry = None)
67+
files += libm_files
68+
69+
BuildPlugin(target, files, libs, examples, docs, nodeflib = False, flags = ['$CPP_FLAG'], defines = defines)

Contrib/Math/Source/Math.c

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <nsis/pluginapi.h> // nsis plugin
55
#include "MyMath.h"
66
#include "Math.h"
7+
#include "mathlib/math.h"
78

89
extern "C" int _fltused;
910

@@ -1525,7 +1526,20 @@ void __declspec(dllexport) Script(HWND hwndParent, int string_size,
15251526
}
15261527

15271528
double _infinity;
1528-
extern "C" void _fpreset();
1529+
1530+
void FpuReset()
1531+
{
1532+
#if defined(_M_ARM ) || defined(__arm__)
1533+
//TODO
1534+
#else
1535+
#if defined(_MSC_VER)
1536+
__asm { finit }
1537+
#else
1538+
asm("finit");
1539+
#endif
1540+
#endif
1541+
}
1542+
15291543

15301544
void CleanAll(int init)
15311545
{
@@ -1534,7 +1548,7 @@ void CleanAll(int init)
15341548
unsigned char _infinity_base[8] = {0, 0, 0, 0, 0, 0, 0xf0, 0x7f};
15351549
_fltused = 0;
15361550
_infinity = *((double*)(_infinity_base));
1537-
_fpreset();
1551+
FpuReset();
15381552

15391553
stack = NULL;
15401554
UserVarsCount = 0;
@@ -1564,3 +1578,22 @@ extern "C" BOOL WINAPI DllMain(HINSTANCE hInst, ULONG ul_reason_for_call, LPVOID
15641578
return TRUE;
15651579
}
15661580

1581+
//TODO: use NSISdl/util.cpp?
1582+
void *memset(void *mem, int c, size_t len)
1583+
{
1584+
/*
1585+
** Prevent MSVC 14.00.40310.41-AMD64 from generating a recursive call to memset
1586+
**
1587+
** #pragma optimize("", off) + #pragma optimize("ty", on) can also
1588+
** be used but it generates a lot more code.
1589+
*/
1590+
#if defined(_MSC_VER) && _MSC_VER > 1200 && _MSC_FULL_VER <= 140040310
1591+
volatile
1592+
#endif
1593+
char *p=(char*)mem;
1594+
while (len-- > 0)
1595+
{
1596+
*p++=c;
1597+
}
1598+
return mem;
1599+
}

Contrib/Math/Source/MyMath.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Unicode support by Jim Park -- 08/22/2007
22

33
#include <windows.h>
4-
#include <stdio.h>
54
#include "MyMath.h"
65
#include "Math.h"
6+
#include "xprintf.h"
77

88
// Converts String to Int (Dec, Hex) or Float value
99
void StringToItem(TCHAR *&s, ExpressionItem *item, int options)
@@ -203,7 +203,7 @@ void itoa64(__int64 i, TCHAR *buffer)
203203

204204
void FloatFormat(TCHAR *s, double value, int options)
205205
{
206-
TCHAR format[128];
206+
char format[32];
207207
int prec = options & 0xF;
208208

209209
*s = 0;
@@ -219,22 +219,30 @@ void FloatFormat(TCHAR *s, double value, int options)
219219

220220
if (options & FF_NOEXP)
221221
{
222-
_stprintf(format, _T("%%.%df"), prec);
222+
xsprintf(format, "%%.%df", prec);
223223
}
224224
else if (options & FF_EXP)
225225
{
226-
_stprintf(format, _T("%%.%de"), prec);
226+
xsprintf(format, "%%.%de", prec);
227227
}
228228
else if (options & FF_LEXP)
229229
{
230-
_stprintf(format, _T("%%.%dE"), prec);
230+
xsprintf(format, "%%.%dE", prec);
231231
}
232232
else
233233
{
234-
_stprintf(format, _T("%%.%dg"), prec);
234+
xsprintf(format, "%%.%dg", prec);
235235
}
236236

237-
_stprintf(s, format, value);
237+
#ifdef UNICODE
238+
char buffer[128];
239+
xsprintf(buffer, format, value);
240+
int cnt = strlen(buffer);
241+
for (int n = 0; n < cnt; n++) s[n] = buffer[n];
242+
s[cnt] = 0;
243+
#else
244+
xsprintf(s, format, value);
245+
#endif
238246
}
239247

240248
int lstrcmpn(TCHAR *s1, const TCHAR *s2, int chars)

Contrib/Math/Source/MyMath.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#pragma once
44

5-
#include <math.h>
5+
#include "mathlib/math.h"
66
#include <float.h>
77
#include <nsis/nsis_tchar.h>
88

SCons/Config/ms

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ if defenv['MSTOOLKIT'] and msvs_version < 8.0:
4242
if msvs_version >= 11.0:
4343
defenv['SUBSYS_CON'] = '/subsystem:console,5.01' # support windows xp
4444
defenv['SUBSYS_WIN'] = '/subsystem:windows,5.01' # support windows xp
45+
defenv.Append(CCFLAGS = ['/Gy'])
46+
defenv.Append(LINKFLAGS = ['/OPT:REF'])
47+
4548

4649
### defines
4750

0 commit comments

Comments
 (0)