Skip to content

Commit

Permalink
move ld_sprint to port.d
Browse files Browse the repository at this point in the history
  • Loading branch information
rainers committed Sep 18, 2015
1 parent d53b214 commit 7acaeec
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/dmangle.d
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ public:
{
const(size_t) BUFFER_LEN = 36;
char[BUFFER_LEN] buffer;
size_t n = ld_sprint(buffer.ptr, 'A', value);
size_t n = Port.ld_sprint(buffer.ptr, 'A', value);
assert(n < BUFFER_LEN);
for (size_t i = 0; i < n; i++)
{
Expand Down
4 changes: 2 additions & 2 deletions src/hdrgen.d
Original file line number Diff line number Diff line change
Expand Up @@ -2157,13 +2157,13 @@ public:
Plus one for rounding. */
const(size_t) BUFFER_LEN = value.sizeof * 3 + 8 + 1 + 1;
char[BUFFER_LEN] buffer;
ld_sprint(buffer.ptr, 'g', value);
Port.ld_sprint(buffer.ptr, 'g', value);
assert(strlen(buffer.ptr) < BUFFER_LEN);
if (hgs.hdrgen)
{
real_t r = Port.strtold(buffer.ptr, null);
if (r != value) // if exact duplication
ld_sprint(buffer.ptr, 'a', value);
Port.ld_sprint(buffer.ptr, 'a', value);
}
buf.writestring(buffer.ptr);
if (type)
Expand Down
20 changes: 0 additions & 20 deletions src/root/longdouble.d
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,8 @@

module ddmd.root.longdouble;

import core.stdc.stdio;

real ldouble(T)(T x)
{
return cast(real)x;
}

size_t ld_sprint(char* str, int fmt, real x)
{
if ((cast(real)cast(ulong)x) == x)
{
// ((1.5 -> 1 -> 1.0) == 1.5) is false
// ((1.0 -> 1 -> 1.0) == 1.0) is true
// see http://en.cppreference.com/w/cpp/io/c/fprintf
char[5] sfmt = "%#Lg\0";
sfmt[3] = cast(char)fmt;
return sprintf(str, sfmt.ptr, x);
}
else
{
char[4] sfmt = "%Lg\0";
sfmt[2] = cast(char)fmt;
return sprintf(str, sfmt.ptr, x);
}
}
28 changes: 28 additions & 0 deletions src/root/port.d
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ module ddmd.root.port;

import core.stdc.ctype;
import core.stdc.string;
import core.stdc.stdio;
import core.math;

version(CRuntime_DigitalMars) __gshared extern (C) extern const(char)* __locale_decpoint;
version(CRuntime_Microsoft) extern(C++) struct longdouble { real r; }
version(CRuntime_Microsoft) extern(C++) size_t ld_sprint(char* str, int fmt, longdouble x);

extern (C) float strtof(const(char)* p, char** endp);
extern (C) double strtod(const(char)* p, char** endp);
Expand Down Expand Up @@ -172,6 +174,32 @@ extern (C++) struct Port
return r;
}

static size_t ld_sprint(char* str, int fmt, real x)
{
version(CRuntime_Microsoft)
{
return .ld_sprint(str, fmt, longdouble(x));
}
else
{
if ((cast(real)cast(ulong)x) == x)
{
// ((1.5 -> 1 -> 1.0) == 1.5) is false
// ((1.0 -> 1 -> 1.0) == 1.0) is true
// see http://en.cppreference.com/w/cpp/io/c/fprintf
char sfmt[5] = "%#Lg\0";
sfmt[3] = cast(char)fmt;
return sprintf(str, sfmt.ptr, x);
}
else
{
char sfmt[4] = "%Lg\0";
sfmt[2] = cast(char)fmt;
return sprintf(str, sfmt.ptr, x);
}
}
}

static void yl2x_impl(real* x, real* y, real* res)
{
version(DigitalMars)
Expand Down
14 changes: 7 additions & 7 deletions src/tokens.d
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import core.stdc.string;
import ddmd.globals;
import ddmd.id;
import ddmd.identifier;
import ddmd.root.longdouble;
import ddmd.root.port;
import ddmd.root.outbuffer;
import ddmd.root.rmem;
import ddmd.utf;
Expand Down Expand Up @@ -741,26 +741,26 @@ struct Token
sprintf(&buffer[0], "%lluUL", cast(ulong)uns64value);
break;
case TOKfloat32v:
ld_sprint(&buffer[0], 'g', float80value);
Port.ld_sprint(&buffer[0], 'g', float80value);
strcat(&buffer[0], "f");
break;
case TOKfloat64v:
ld_sprint(&buffer[0], 'g', float80value);
Port.ld_sprint(&buffer[0], 'g', float80value);
break;
case TOKfloat80v:
ld_sprint(&buffer[0], 'g', float80value);
Port.ld_sprint(&buffer[0], 'g', float80value);
strcat(&buffer[0], "L");
break;
case TOKimaginary32v:
ld_sprint(&buffer[0], 'g', float80value);
Port.ld_sprint(&buffer[0], 'g', float80value);
strcat(&buffer[0], "fi");
break;
case TOKimaginary64v:
ld_sprint(&buffer[0], 'g', float80value);
Port.ld_sprint(&buffer[0], 'g', float80value);
strcat(&buffer[0], "i");
break;
case TOKimaginary80v:
ld_sprint(&buffer[0], 'g', float80value);
Port.ld_sprint(&buffer[0], 'g', float80value);
strcat(&buffer[0], "Li");
break;
case TOKstring:
Expand Down

0 comments on commit 7acaeec

Please sign in to comment.