Array assign

2009-09-18 Thread bearophile
I've found that when lenght is small (about 50-250 4-byte items or less) the 
array filling operation is quite slow compared to a normal loop:
a[] = x;

So I suggest DMD frontend to inline little loop when a.length is small.

To further improve the a[]=x; I have tried to speed up the larger case too, so 
I've used the movntps instruction. See info:

http://www.gamedev.net/community/forums/topic.asp?topic_id=532112

http://www.sesp.cse.clrc.ac.uk/html/SoftwareTools/vtune/users_guide/mergedProjects/analyzer_ec/mergedProjects/reference_olh/mergedProjects/instructions/instruct32_hh/vc197.htm

The following is my attempt, my experience of x86 asm programming is minimal 
still (I'll try to learn more) so while this program seems to somehow work, 
it's a pessimization compared to a normal loop, so probably I've done several 
mistakes/bugs. Can someone help me improve the code? If the result is fast 
enough, something similar can be added to d runtime.

This array4Set() is designed for 4 bytes long items, but I can write something 
similar for other data sizes too.


version (Tango) {
import tango.stdc.stdio: printf;
import tango.stdc.time: clock, CLOCKS_PER_SEC;
import tango.math.Math: sqrt;
import tango.stdc.string: memset;
} else {
import std.c.stdio: printf;
import std.c.time: clock, CLOCKS_PER_SEC;
import std.math: sqrt;
import std.c.string: memset;
}


double myclock() {
return cast(double)clock() / CLOCKS_PER_SEC;
}


void array4Set(T)(T[] a, T value) {
static assert(T.sizeof == 4);
if (!a.length)
return;
auto a_ptr = a.ptr;
auto a_end = a_ptr + a.length;

// align pointer
size_t aux = ((cast(size_t)a_ptr + 15) & ~15);
auto n = cast(T*)aux;
while (a_ptr < n)
*a_ptr++ = value;
n = cast(T*)((cast(size_t)a_end) & ~15);

if (a_ptr < n && (a_end - a_ptr) >= 16) {
// Aligned case
asm {
mov ESI, a_ptr;
mov EDI, n;

movss XMM0, value; // XMM0 = value,value,value,value
shufps XMM0, XMM0, 0;

align 8;
LOOP:
add ESI, 64;
movntps [ESI+ 0-64], XMM0;
movntps [ESI+16-64], XMM0;
movntps [ESI+32-64], XMM0;
movntps [ESI+48-64], XMM0;
cmp ESI, EDI;
jb LOOP;

mov a_ptr, ESI;
}
}

// trailing ones
while (a_ptr < a_end)
*a_ptr++ = value;
}


T test(T, bool withLoop)(int len, int nloops) {
auto a = new T[len];

for (int i; i < nloops; i++) {
static if (withLoop)
for (int j; j < a.length; j++)
a[j] = T.init;
else
//a[] = T.init;
//memset(a.ptr, 0, a.length * T.sizeof);
array4Set(a, 0);

a[i % len] = i;
}

return a[0];
}


void show(float[] a) {
printf("[");

if (a.length > 1)
foreach (el; a[0 .. $-1])
printf("%.1f, ", el);
if (a.length)
printf("%.1f", a[$-1]);
printf("]\n");
}

void main() {
// small test
auto a = new float[20];
foreach (i, ref el; a)
el = i;

// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
show(a);

array4Set(a[2 .. 2], 0.5f);

// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
show(a);

array4Set(a[2 .. 9], 0.5f);

// [0, 1, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9, 10, 11, 12, 13, 14, 15, 16, 
17, 18, 19]
show(a);
printf("-\n\n");

auto lens = [2, 4, 5, 8, 15, 16, 25, 32, 50, 64, 100, 250, 256, 1000, 2048, 
2500];
//auto lens = [1 << 12, 1 << 13, 1 << 15, 1 << 16, 1 << 17];
foreach (len; lens) {
int nloops = cast(int)(cast(double)60_000_000 / sqrt(cast(double)len));
auto t0 = myclock();

alias int T;
test!(T, true)(len, nloops);
auto t1 = myclock();

auto t2 = myclock();
test!(T, false)(len, nloops);
auto t3 = myclock();

printf("len=%d, nloops=%d, time with loop=%.3f, time without loop=%.3f, 
ratio=%.3f\n",
   len, nloops, t1-t0, t3-t2, (t1-t0) / (t3-t2));
}
}

Bye,
bearophile


Re: better than union and array questions

2009-09-18 Thread Saaa

> (D1 Phobos)
> I use the struct below like: Struct[][char[]] _struct;
>
> Is there a better way to support arrays of any type?
> Currently all the code working with these Structs are templated with loads 
> of static ifs in them.
>
> Also, is it possible to add a .deepdup property to all arrays?
>
> One last question:
> Will "a[]=b.dup;" copy b twice?
>
One more :)

int[] array;
array.length = 100;
array.length = 0;
//no other arrays pointing/slicing to this array

This way I can be sure for the following 100 element concatenations the 
array won't be copied.
Or isn't this implicitly part of the D spec? 




Re: Re:[OT] std.bitarray

2009-09-18 Thread Saaa
BCS,

Thanks for pointing that out.
I'll keep to that convention in my next posts. 




Re: D1: std.md5: corrections for the given example

2009-09-18 Thread Stewart Gordon

notna wrote:

grauzone schrieb:



http://d.puremagic.com/issues/enter_bug.cgi


???

What do you wanna tell us?


Where to report bugs.


That
  while (auto len = file.readBlock(buffer.ptr, buffer.sizeof))
has to work and does not because it's a bug?


If you want to know whether a piece of code is legal or not, try reading 
the D documentation.



And that's why you think a bug report should be opened?


No, that this code example in the Phobos documentation is wrong and 
therefore a bug report should be opened.


Stewart.


Re:[OT] std.bitarray

2009-09-18 Thread BCS
Could you /please/ include the "reply to" line in the text of your posts. 
Most clients seem to add it by default and it makes it much easier for people 
to find replies to there posts. (I have a filter set up to mark post with 
my username so, in my case I'd guess that without that line, I'm about 3-5 
times more likely to not see a reply.)


BTW the following line is the "reply to" line:

Hello Saaa,


Ah, slicing, of course, thanks!


Hello Saaa,


I understand a bitarray being faster than a boolean array as the
first
uses
bitwise (hardware) operators.
Is this a correct understanding of the situation?
Why then is a boolean array not implemented in that way?

Because you can't slice a bit array the same way you can slice a int
or byte array. D used to have a bit type and bit arrays where
primitives but it made for all kinds of problems when you wanted to
do slices.






Re: C equivalent for the D "float" type

2009-09-18 Thread TSalm

What is the C equivalent for the D "float" type ?


float and double is double.

in c, real support is compiler dependant, it may or may not
be available.


Thanks !


Re: C equivalent for the D "float" type

2009-09-18 Thread div0
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

TSalm wrote:
> Hello,
> 
> What is the C equivalent for the D "float" type ?
> 
> Thanks in advance,
> TSalm

float and double is double.

in c, real support is compiler dependant, it may or may not
be available.

- --
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iD8DBQFKs/YuT9LetA9XoXwRAg5jAJ98cKfmnt2XfFFODbqbG0CzjN78xACeNWkQ
bLYQ4yOZKLSmn8ZNvXXGCX4=
=YvgC
-END PGP SIGNATURE-


C equivalent for the D "float" type

2009-09-18 Thread TSalm

Hello,

What is the C equivalent for the D "float" type ?

Thanks in advance,
TSalm


Re: D1: std.md5: corrections for the given example

2009-09-18 Thread notna

grauzone schrieb:


md5_example_2.d(7): expression expected, not 'auto'
md5_example_2.d(7): found 'len' when expecting ')'
md5_example_2.d(7): found '=' instead of statement

(this is line 7 after I removed comments and blank lines from your edit)

Stewart.


God I'm stupid.

Sorry.

I guess I wish that worked.


http://d.puremagic.com/issues/enter_bug.cgi


???

What do you wanna tell us? That
  while (auto len = file.readBlock(buffer.ptr, buffer.sizeof))
has to work and does not because it's a bug? And that's why you think a 
bug report should be opened?


better than union and array questions

2009-09-18 Thread Saaa
(D1 Phobos)
I use the struct below like: Struct[][char[]] _struct;

Is there a better way to support arrays of any type?
Currently all the code working with these Structs are templated with loads 
of static ifs in them.

Also, is it possible to add a .deepdup property to all arrays?

One last question:
Will "a[]=b.dup;" copy b twice?



struct Struct
{
enum TYPE{ UNKNOWN, BOOL, BYTE,INT, FLOAT};
// I excluded boolean and byte to shorten this message
TYPE type = TYPE.UNKNOWN;
union
{
 float floatMin;
 int intMin;
}
union
{
 float floatMax;
 int intMax;
}
union
{
 int[][] intArray;
 float[][] floatArray;
}
}




printable unicode

2009-09-18 Thread Ellery Newcomer
Is there anything analogous to tango.text.Unicode.isPrintable in phobos?


Re: std.bitarray

2009-09-18 Thread Saaa

Ah, slicing, of course, thanks!


> Hello Saaa,
>
>> I understand a bitarray being faster than a boolean array as the first
>> uses
>> bitwise (hardware) operators.
>> Is this a correct understanding of the situation?
>> Why then is a boolean array not implemented in that way?
>
> Because you can't slice a bit array the same way you can slice a int or 
> byte array. D used to have a bit type and bit arrays where primitives but 
> it made for all kinds of problems when you wanted to do slices.
>
> 




Re: tango version identifiers

2009-09-18 Thread Ellery Newcomer
Steven Schveighoffer wrote:
> On Fri, 18 Sep 2009 10:23:38 -0400, Ellery Newcomer
>  wrote:
> 
>> Steven Schveighoffer wrote:
>>> On Thu, 17 Sep 2009 23:48:27 -0400, Ellery Newcomer
>>>  wrote:
>>>
 Does the tango build by any chance set any distinguishing predefined
 version identifiers?
>>>
>>> http://www.dsource.org/projects/tango/docs/current/tango.core.Version.html
>>>
>>
>> I was thinking more along the lines of
>>
>> version(Special_Tango_Version){
>>   *special tango behavior*
>> }else{
>>   *special phobos behavior*
>> }
> 
> Typically, if you have installed your tango lib properly, the compiler
> configuration defines "Tango" as a version identifier you can use to
> distinguish Tango from Phobos.
> 
> So the above code would look like
> 
> version(Tango){
>   ...
> }else{
>   ...
> }
> 
> -Steve

Excellent.

Thanks * 2!


Re: tango version identifiers

2009-09-18 Thread Steven Schveighoffer
On Fri, 18 Sep 2009 10:23:38 -0400, Ellery Newcomer  
 wrote:



Steven Schveighoffer wrote:

On Thu, 17 Sep 2009 23:48:27 -0400, Ellery Newcomer
 wrote:


Does the tango build by any chance set any distinguishing predefined
version identifiers?


http://www.dsource.org/projects/tango/docs/current/tango.core.Version.html


I was thinking more along the lines of

version(Special_Tango_Version){
  *special tango behavior*
}else{
  *special phobos behavior*
}


Typically, if you have installed your tango lib properly, the compiler  
configuration defines "Tango" as a version identifier you can use to  
distinguish Tango from Phobos.


So the above code would look like

version(Tango){
  ...
}else{
  ...
}

-Steve


Re: tango version identifiers

2009-09-18 Thread Ellery Newcomer
Steven Schveighoffer wrote:
> On Thu, 17 Sep 2009 23:48:27 -0400, Ellery Newcomer
>  wrote:
> 
>> Does the tango build by any chance set any distinguishing predefined
>> version identifiers?
> 
> http://www.dsource.org/projects/tango/docs/current/tango.core.Version.html

I was thinking more along the lines of

version(Special_Tango_Version){
  *special tango behavior*
}else{
  *special phobos behavior*
}


Re: tango version identifiers

2009-09-18 Thread Steven Schveighoffer
On Thu, 17 Sep 2009 23:48:27 -0400, Ellery Newcomer  
 wrote:



Does the tango build by any chance set any distinguishing predefined
version identifiers?


http://www.dsource.org/projects/tango/docs/current/tango.core.Version.html


Re: D1: std.md5: corrections for the given example

2009-09-18 Thread grauzone

downs wrote:

Stewart Gordon wrote:

downs wrote:


while (auto len = file.readBlock(buffer.ptr, buffer.sizeof))



md5_example_2.d(7): expression expected, not 'auto'
md5_example_2.d(7): found 'len' when expecting ')'
md5_example_2.d(7): found '=' instead of statement

(this is line 7 after I removed comments and blank lines from your edit)

Stewart.


God I'm stupid.

Sorry.

I guess I wish that worked.


http://d.puremagic.com/issues/enter_bug.cgi