Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
add convert tests + tabs + hash asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Stepanov committed Nov 17, 2013
1 parent 8df094c commit df2dd51
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 53 deletions.
8 changes: 4 additions & 4 deletions mak/COPY
Expand Up @@ -12,10 +12,10 @@ COPY=\
$(IMPDIR)\core\thread.di \
$(IMPDIR)\core\time.d \
$(IMPDIR)\core\vararg.d \
\
$(IMPDIR)\core\internal\hash.d \
$(IMPDIR)\core\internal\convert.d \
\
\
$(IMPDIR)\core\internal\hash.d \
$(IMPDIR)\core\internal\convert.d \
\
$(IMPDIR)\core\stdc\complex.d \
$(IMPDIR)\core\stdc\config.d \
$(IMPDIR)\core\stdc\ctype.d \
Expand Down
8 changes: 4 additions & 4 deletions mak/MANIFEST
Expand Up @@ -31,10 +31,10 @@ MANIFEST=\
src\core\threadasm.S \
src\core\time.d \
src\core\vararg.d \
\
src\core\internal\hash.d \
src\core\internal\convert.d \
\
\
src\core\internal\hash.d \
src\core\internal\convert.d \
\
src\core\stdc\complex.d \
src\core\stdc\config.d \
src\core\stdc\ctype.d \
Expand Down
8 changes: 4 additions & 4 deletions mak/SRCS
Expand Up @@ -13,10 +13,10 @@ SRCS=\
src\core\thread.d \
src\core\time.d \
src\core\vararg.d \
\
src\core\internal\hash.d \
src\core\internal\convert.d \
\
\
src\core\internal\hash.d \
src\core\internal\convert.d \
\
src\core\stdc\config.d \
src\core\stdc\ctype.d \
src\core\stdc\errno.d \
Expand Down
43 changes: 32 additions & 11 deletions src/core/internal/convert.d
@@ -1,3 +1,12 @@
/**
* Written in the D programming language.
* This module provides functions to converting different values to const(ubyte)[]
*
* Copyright: Copyright Igor Stepanov 2013-2013.
* License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
* Authors: Igor Stepanov
* Source: $(DRUNTIMESRC core/internal/_convert.d)
*/
module core.internal.convert;

@trusted pure nothrow
Expand Down Expand Up @@ -68,8 +77,8 @@ private Float parse(bool is_denormalized = false, T)(T x) if(is(T == float) || i
if(x is cast(T)-0.0) return FloatTraits!T.NZERO;
if(x is T.nan) return FloatTraits!T.NAN;
if(x is -T.nan) return FloatTraits!T.NNAN;
if(x is T.infinity) return FloatTraits!T.INF;
if(x is -T.infinity) return FloatTraits!T.NINF;
if(x is T.infinity || x > T.max) return FloatTraits!T.INF;
if(x is -T.infinity || x < -T.max) return FloatTraits!T.NINF;

uint sign = x < 0;
x = sign ? -x : x;
Expand All @@ -79,7 +88,10 @@ private Float parse(bool is_denormalized = false, T)(T x) if(is(T == float) || i

if(!exp)
{
return Float(denormalizedMantissa(x), 0, sign);
if(is_denormalized)
return Float(0, 0, sign);
else
return Float(denormalizedMantissa(x), 0, sign);
}

x2 /= binPow2(e);
Expand Down Expand Up @@ -339,6 +351,21 @@ version(unittest)
testNumberConvert!("0.0Fi");
testNumberConvert!("0.0i");
testNumberConvert!("0.0Li");

testNumberConvert!("cast(real)-0x9.0f7ee55df77618fp-13829L");
testNumberConvert!("cast(real)0x7.36e6e2640120d28p+8797L");
testNumberConvert!("cast(real)-0x1.05df6ce4702ccf8p+15835L");
testNumberConvert!("cast(real)0x9.54bb0d88806f714p-7088L");

testNumberConvert!("cast(double)-0x9.0f7ee55df77618fp-13829L");
testNumberConvert!("cast(double)0x7.36e6e2640120d28p+8797L");
testNumberConvert!("cast(double)-0x1.05df6ce4702ccf8p+15835L");
testNumberConvert!("cast(double)0x9.54bb0d88806f714p-7088L");

testNumberConvert!("cast(float)-0x9.0f7ee55df77618fp-13829L");
testNumberConvert!("cast(float)0x7.36e6e2640120d28p+8797L");
testNumberConvert!("cast(float)-0x1.05df6ce4702ccf8p+15835L");
testNumberConvert!("cast(float)0x9.54bb0d88806f714p-7088L");
}


Expand All @@ -348,12 +375,6 @@ version(unittest)
}
}







private template Unqual(T)
{
static if (is(T U == shared(const U))) alias U Unqual;
Expand All @@ -372,7 +393,7 @@ const(ubyte)[] toUbyte(T)(T[] arr) if (T.sizeof == 1)
}

@trusted pure nothrow
const(ubyte)[] toUbyte(T)(T[] arr) if (is(typeof(toUbyte(arr[0])) == const(ubyte)[]))
const(ubyte)[] toUbyte(T)(T[] arr) if ((is(typeof(toUbyte(arr[0])) == const(ubyte)[]))&&(T.sizeof > 1))
{
if (__ctfe)
{
Expand Down Expand Up @@ -527,4 +548,4 @@ const(ubyte)[] toUbyte(T)(ref T val) if (is(T == struct) || is(T == union))
{
return (cast(const(ubyte)*)&val)[0 .. T.sizeof];
}
}
}
46 changes: 20 additions & 26 deletions src/core/internal/hash.d
@@ -1,16 +1,15 @@
/**
* Hash uniform caluclation implementation
* Written in the D programming language.
* This module provides functions to uniform calculating hash values for different types
*
* Author: Igor Stepanov
*
* Copyright: Copyright Igor Stepanov 2013-2013.
* License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
* Authors: Igor Stepanov
* Source: $(DRUNTIMESRC core/internal/_hash.d)
*/

module core.internal.hash;

import core.internal.convert;
/**
Compute hash value for different types.
*/

//enum hash. CTFE depends on base type
@trusted nothrow
Expand Down Expand Up @@ -64,6 +63,7 @@ size_t hashOf(T)(auto ref T val, size_t seed = 0) if (!is(T == enum) && is(T S:
}
else //Other types. CTFE unsupported
{
assert(!__ctfe, "unable to compute hash of "~T.stringof);
return bytesHash(val.ptr, ElementType.sizeof*val.length, seed);
}
}
Expand All @@ -72,23 +72,15 @@ size_t hashOf(T)(auto ref T val, size_t seed = 0) if (!is(T == enum) && is(T S:
@trusted nothrow pure
size_t hashOf(T)(auto ref T val, size_t seed = 0) if (!is(T == enum) && __traits(isArithmetic, T))
{
static if (is(typeof(toUbyte(val)) == const(ubyte)[])) //most of numerics CTFE ready
static if(__traits(isFloating, val))
{
static if(__traits(isFloating, val))
{
T data = (val != val) ? T.nan : val;
auto bytes = toUbyte(data);
return bytesHash(bytes.ptr, bytes.length, seed);
}
else
{
auto bytes = toUbyte(val);
return bytesHash(bytes.ptr, bytes.length, seed);
}
T data = (val != val) ? T.nan : val;
auto bytes = toUbyte(data);
return bytesHash(bytes.ptr, bytes.length, seed);
}
else //real, ireal, creal. CTFE unsupproted
else
{
const(ubyte)[] bytes = (cast(const(ubyte)*)&val)[0 .. T.sizeof];
auto bytes = toUbyte(val);
return bytesHash(bytes.ptr, bytes.length, seed);
}
}
Expand Down Expand Up @@ -127,6 +119,7 @@ size_t hashOf(T)(auto ref T val, size_t seed = 0) if (!is(T == enum) && (is(T ==
}
else // CTFE unsupproreted for structs with reference fields
{
assert(!__ctfe, "unable to compute hash of "~T.stringof);
const(ubyte)[] bytes = (cast(const(ubyte)*)&val)[0 .. T.sizeof];
return bytesHash(bytes.ptr, bytes.length, seed);
}
Expand All @@ -136,6 +129,7 @@ size_t hashOf(T)(auto ref T val, size_t seed = 0) if (!is(T == enum) && (is(T ==
@trusted nothrow pure
size_t hashOf(T)(auto ref T val, size_t seed = 0) if (!is(T == enum) && is(T == delegate))
{
assert(!__ctfe, "unable to compute hash of "~T.stringof);
const(ubyte)[] bytes = (cast(const(ubyte)*)&val)[0 .. T.sizeof];
return bytesHash(bytes.ptr, bytes.length, seed);
}
Expand Down Expand Up @@ -404,9 +398,9 @@ version(AnyX86)
@trusted pure nothrow
size_t bytesHash(const(void)* buf, size_t len, size_t seed = 0)
{
static uint rotl32(uint x, byte r) pure nothrow @safe
static uint rotl32(uint n)(in uint x) pure nothrow @safe
{
return (x << r) | (x >> (32 - r));
return (x << n) | (x >> (32 - n));
}

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -459,11 +453,11 @@ size_t bytesHash(const(void)* buf, size_t len, size_t seed = 0)
{
uint k1 = get32bits(data);
k1 *= c1;
k1 = rotl32(k1,15);
k1 = rotl32!15(k1);
k1 *= c2;

h1 ^= k1;
h1 = rotl32(h1,13);
h1 = rotl32!13(h1);
h1 = h1*5+c3;
}

Expand All @@ -476,7 +470,7 @@ size_t bytesHash(const(void)* buf, size_t len, size_t seed = 0)
case 3: k1 ^= data[2] << 16; goto case;
case 2: k1 ^= data[1] << 8; goto case;
case 1: k1 ^= data[0];
k1 *= c1; k1 = rotl32(k1,15); k1 *= c2; h1 ^= k1;
k1 *= c1; k1 = rotl32!15(k1); k1 *= c2; h1 ^= k1;
goto default;
default:
}
Expand Down
4 changes: 2 additions & 2 deletions win32.mak
Expand Up @@ -183,10 +183,10 @@ $(IMPDIR)\core\vararg.d : src\core\vararg.d
copy $** $@

$(IMPDIR)\core\internal\hash.d : src\core\internal\hash.d
copy $** $@
copy $** $@

$(IMPDIR)\core\internal\convert.d : src\core\internal\convert.d
copy $** $@
copy $** $@

$(IMPDIR)\core\stdc\complex.d : src\core\stdc\complex.d
copy $** $@
Expand Down
4 changes: 2 additions & 2 deletions win64.mak
Expand Up @@ -190,10 +190,10 @@ $(IMPDIR)\core\vararg.d : src\core\vararg.d
copy $** $@

$(IMPDIR)\core\internal\hash.d : src\core\internal\hash.d
copy $** $@
copy $** $@

$(IMPDIR)\core\internal\convert.d : src\core\internal\convert.d
copy $** $@
copy $** $@

$(IMPDIR)\core\stdc\complex.d : src\core\stdc\complex.d
copy $** $@
Expand Down

0 comments on commit df2dd51

Please sign in to comment.