Re: D ASM. Program fails
On Friday, 22 January 2016 at 20:54:46 UTC, Iakh wrote: On Friday, 22 January 2016 at 17:27:35 UTC, userABCabc123 wrote: [...] Thanks. It works. Buth shorter version too: asm { naked; push RBP; mov RBP, RSP; //sub RSP, 0x10; //movdqa dword ptr[RBP-0x10], XMM0; //movdqa XMM0, dword ptr[RBP-0x10]; pmovmskb EAX, XMM0; mov RSP, RBP; pop RBP; ret; } Looks like the SIMD param is passed by SIMD reg Right I must be blind. So you can even remove the prelude and the prologue: int pmovmskb2(byte16 v) { asm { naked; pmovmskb EAX, XMM0; ret; } }
Re: Create an empty json object with std.json
On Friday, 22 January 2016 at 16:58:51 UTC, Andrea Fontana wrote: On Friday, 22 January 2016 at 16:45:22 UTC, userABCabc123 wrote: But soon or later you'll need to add values to your object so just imagine it's already an object, even if it will only become one when you'll start to add some values. You're wrong, I need an empty object for an API call! Moreover "{}" it's a valid json object and there's no easy way to create it using std.json. It's true that it can be problematic, for example with an input contract, or for subtyping a JSONValue as something like struct JSONValueThatAlwayObject{}... But there is only 3 ways in std.json, from a literal, using the deprecated way or the "lazy" way.
Re: D ASM. Program fails
On Friday, 22 January 2016 at 17:12:25 UTC, userABCabc123 wrote: Note that there is maybe a DMD codegen bug because the asm generated for the non naked version copy the result to the stack and then the stack to result but after pmovmskb it's already setup in EAX. 0044C580h push rbp 0044C581h mov rbp, rsp 0044C584h sub rsp, 20h 0044C588h movdqa dqword ptr [rbp-10h], xmm0 0044C58Dh mov dword ptr [rbp-18h], h 0044C594h movdqa xmm0, dqword ptr [rbp-10h] 0044C599h pmovmskb eax, xmm0 ; already in result 0044C59Dh mov dword ptr [rbp-18h], eax ; what? 0044C5A0h mov eax, dword ptr [rbp-18h] ; what? 0044C5A3h mov rsp, rbp 0044C5A6h pop rbp 0044C5A7h ret Oops, there no DMD codegen bug, the non naked version explicitly uses a local value for the return so without the local "r" this gives: int pmovmskb(byte16 v) { asm { naked; push RBP; mov RBP, RSP; sub RSP, 0x10; movdqa dword ptr[RBP-0x10], XMM0; movdqa XMM0, dword ptr[RBP-0x10]; pmovmskb EAX, XMM0; mov RSP, RBP; pop RBP; ret; } }
Re: D ASM. Program fails
On Friday, 22 January 2016 at 14:06:42 UTC, Adam D. Ruppe wrote: On Friday, 22 January 2016 at 12:18:53 UTC, anonymous wrote: I don't know much about these things, but it seems to be the `ret;`. Right. This is an ordinary D function so the compiler generates code to set up a stack for local variables. It looks like: push ebp; mov ebp, esp; sub EBP, some_size; /* sometimes a few other register saves */ /* your code here */ /* sometimes a few other register restores */ leave; ret; `leave` btw is the same as `mov esp,ebp; pop ebp;` - it undoes the result of those first three instructions. All this setup stuff is about creating a stack frame for the function's local variables. If you ret without restoring the frame, all local variables (and return addresses!) from there on are going to be out of sync and will lead to memory access violations. That's what happened to you. If you want to write a whole function in assembly without the compiler inserting any additional code, start it off with `asm { naked; }` inside so dmd knows what you are trying to do. Then you are in complete control. Otherwise, remember to clear the frame correctly, or better yet, just return using the ordinary D statement instead of the asm instruction. naked version: int pmovmskb2(byte16 v) { asm { naked; push RBP; mov RBP, RSP; sub RSP, 0x20; movdqa dword ptr[RBP-0x10], XMM0; mov dword ptr[RBP-0x18], 0; movdqa XMM0, dword ptr[RBP-0x10]; pmovmskb EAX, XMM0; mov RSP, RBP; pop RBP; ret; } } Note that there is maybe a DMD codegen bug because the asm generated for the non naked version copy the result to the stack and then the stack to result but after pmovmskb it's already setup in EAX. 0044C580h push rbp 0044C581h mov rbp, rsp 0044C584h sub rsp, 20h 0044C588h movdqa dqword ptr [rbp-10h], xmm0 0044C58Dh mov dword ptr [rbp-18h], h 0044C594h movdqa xmm0, dqword ptr [rbp-10h] 0044C599h pmovmskb eax, xmm0 ; already in result 0044C59Dh mov dword ptr [rbp-18h], eax ; what? 0044C5A0h mov eax, dword ptr [rbp-18h] ; what? 0044C5A3h mov rsp, rbp 0044C5A6h pop rbp 0044C5A7h ret
Re: Create an empty json object with std.json
On Friday, 22 January 2016 at 12:54:38 UTC, Andrea Fontana wrote: On Friday, 22 January 2016 at 12:05:48 UTC, userABCabc123 wrote: when you add the first key, the value will be set to JSON_TYPE.OBJECT import std.json; void main(string[] args) { JSONValue json; json["first"] = 0; assert(json.type == JSON_TYPE.OBJECT); } That's right, but I need an empty object, without any key set! But soon or later you'll need to add values to your object so just imagine it's already an object, even if it will only become one when you'll start to add some values. I don't really get your problem here, as in the first message you also start with an empty json.
Re: Error in DUB Package Page - how to notify the Editor?
On Friday, 22 January 2016 at 16:07:59 UTC, Martin Tschierschke wrote: What about the idea to allow discussion entries/threads to be linked to dub package entries? So they appear in DUB and in a additional section of this forum? So vibe.d for example comes with his own forum that is good, but a solution for all would be nicer? So coming back to my first question: How to notify the author about errors, with out the need for searching for him elsewhere ? Regards mt. Most of the packages are actually hosted on Github so be default they also have a bugtracker module, where you can report problems or even ask questions. Also, you can get the email of the author(s) from the commit messages, they are not alsways displayed online but when the source repository is cloned you can see the mails in the history (assuming it's valid).
Re: Create an empty json object with std.json
On Friday, 22 January 2016 at 11:53:11 UTC, Andrea Fontana wrote: If you declare a JSONValue like this: JSONValue json; then: assert(json.type() == JSON_TYPE.NULL); Documentation at https://dlang.org/phobos/std_json.html#.JSONValue.type.2 suggests not to change type but to assign a new value instead. My problem is: how can I assign an empty object like {}? The only way i found is using that deprecated method: json.type = JSON_TYPE.OBJECT; or json = `{}`.parseJSON; Please notice that to init as array this works: json = JSONValue[].init; read this: https://issues.dlang.org/show_bug.cgi?id=15410 when you add the first key, the value will be set to JSON_TYPE.OBJECT import std.json; void main(string[] args) { JSONValue json; json["first"] = 0; assert(json.type == JSON_TYPE.OBJECT); }
Re: How to represent struct with trailing array member
On Friday, 22 January 2016 at 08:39:06 UTC, Dibyendu Majumdar wrote: On Friday, 22 January 2016 at 01:53:53 UTC, Chris Wright wrote: On Thu, 21 Jan 2016 21:52:06 +, Dibyendu Majumdar wrote: Hi I have C code where the struct has a trailing array member: struct matrix { int rows; int cols; double data[1]; }; D has bounds checking, which makes this awkward. You would be able to access the data using: struct matrix { int rows, cols; double[] data() { void* p = &this; p += this.sizeof; return (cast(double*)p)[0 .. rows * cols]; } } Right - I should use slices in other words. Thanks The basic problem is that double data[1] in D is a static array, which is not a reference type so if you try to use its .ptr member you'll encounter many errors because as it is, your struct is totally POD and it fully resides on the stack. And the previous answer is erroneous because "p" is declared nowhere. struct matrix { int rows, cols; double[] data; } or struct matrix { int rows, cols; double* data; } are correct, although you still have to write the boring code to get the data in sync with rows and cols.
Re: Glad and WGL
On Wednesday, 13 January 2016 at 19:05:30 UTC, Josh Phillips wrote: Oh wow that's easy. They should really make that more clear in the dlang reference. They way it sounds there made me think that if a function doesn't throw any errors it automatically is 'nothrow' No, because actually you can have a function that uses sub-functions that throw, but marked explicitly nothrow, because it hides the stuff under the carpet. --- void bar() { throw new Exception("kaboom"); } void foo() nothrow { try {bar;} catch {/*under the carpet*/} } --- and that will compile.
Re: No static fold in phobos ?
On Friday, 27 November 2015 at 23:46:32 UTC, Ali Çehreli wrote: On 11/27/2015 12:57 PM, userabcABC123 wrote: That would work on an AliasSeq ? I'm surprised not to find one: eg: ~~~ enum seq = AliasSeq!("aa", "bb"); enum val = staticFold!((a,b)=>a~b, seq); static assert(val == "aabb"); ~~~ it works with foreach or a eponymous template that consumes the sequence , but as said where is it in phobos ? Thanks to CTFE, there is usually no need for static of anything in D. Your code works with a few changes: import std.meta; import std.algorithm; void main() { enum seq = AliasSeq!("aa", "bb"); enum val = [ seq ].reduce!((a, b) => a ~ b); static assert(val == "aabb"); } Ali Thx, so the trick was to pack seq in something that's a range...
No static fold in phobos ?
That would work on an AliasSeq ? I'm surprised not to find one: eg: ~~~ enum seq = AliasSeq!("aa", "bb"); enum val = staticFold!((a,b)=>a~b, seq); static assert(val == "aabb"); ~~~ it works with foreach or a eponymous template that consumes the sequence , but as said where is it in phobos ?
Re: Manually allocate delegate?
On Sunday, 12 July 2015 at 09:03:25 UTC, Tofu Ninja wrote: On Sunday, 12 July 2015 at 08:47:37 UTC, ketmar wrote: On Sun, 12 Jul 2015 08:38:00 +, Tofu Ninja wrote: Is it even possible? what do you mean? Sorry, thought the title was enough. The context for a delegate(assuming not a method delegate) is allocated by the GC. Is there any way to allocate the context manually. Yes: class Foo { void bar(){writeln(__PRETTY_FUNCTION__);} } auto uncollectedDelegate(T, string name)(ref T t) { import std.experimental.allocator.mallocator; struct Dg{void* ptr, funcptr;} void* funcptr = &__traits(getMember, T, name); void* ptr = cast(void*)t; Dg* dg = cast(Dg*) Mallocator.instance.allocate(Dg.sizeof); dg.ptr = ptr; dg.funcptr = funcptr; return dg; } void main(string[] args) { Foo foo = new Foo; auto dg = uncollectedDelegate!(Foo, "bar")(foo); auto tdg = cast(void delegate()*) dg; (*tdg)(); } with just a type __traits(getMember,...) on a delegate will only return the function address in the process image. so without the context ptr, just like a static or a global function. Later you set the context by hand, using a pointer to an instance.
Re: CTFE fmod ?
On Friday, 20 November 2015 at 13:44:11 UTC, rumbu wrote: On Friday, 20 November 2015 at 11:16:13 UTC, userABCabc123 wrote: [...] [...] Not thoroughly tested and only works for doubles, but this must do the trick. [...] Thx, it works, easy to adapt to float.
CTFE fmod ?
Does someone have a good CTFE fmod() function ? The problem is that std.math.fmod() is itself not available at CT, neither do floor() or similar functions necessary to get the quotient when the input value is two times over/under the bounds. Currently I have this one... --- auto warp(T)(T x, T min, T max) { if (x > max) { T rng = max - min; while (x > max + rng) x -= rng * 2; if (x > max) x -= rng; } else if (x < min) { T rng = max - min; while (x < min - rng) x += rng * 2; if (x < min) x += rng; } return x; } --- ...but it fails to compile with certain float values. This example will consume a lot of memory ( I guess it's the while() loop in the CTFE VM who's responsible): --- static assert(warp(2357136044, -5f, 5f).approxEqual(-1f)); --- Any suggestion ? Or maybe this is a limit ?