How do I cast to from byte[] <-> double for making a small assembler language VM to work?

Stefan Koch via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jul 18 05:04:34 PDT 2017


On Tuesday, 18 July 2017 at 11:06:22 UTC, Enjoys Math wrote:
> class OpCode
> {
> private:
> 	byte[] bytes_;
>
> public:
> 	void opCall(Program program) const;
>
> 	byte[] bytes() const {
> 		return bytes_.dup;
> 	}
> }
>
>
>
> class AddD : OpCode
> {
> private:
> 	uint d, s;
>
> public:
> 	this(uint dst, uint src) {
> 		d = dst;
> 		s = src;
> 	}
>
> 	override void opCall(Program p) const
> 	{
> 		p[d..d+8] = cast(byte[])(cast(double)p[d..d+8] + 
> cast(double)p[s..s+8]);
> 	}
> }
>
>
> ---
>
> The cast at the bottom gives a compiler error (can't cast 
> byte[] to double)

Byte[] is a slice
what you want is *(cast(double*)p + d) = *(cast(double*)p + d) + 
*(cast(double*)p + s)


More information about the Digitalmars-d-learn mailing list