Re: problem with cast and unions



On Aug 20, 6:19 pm, Ben Bacarisse <ben.use...@xxxxxxxxx> wrote:
torri <vincent.to...@xxxxxxxxx> writes:
On Thu, 20 Aug 2009 13:37:41 +0100, Ben Bacarisse wrote:

<corrected in another message to:>

Str s = ((Type)f).s;

the purpose is to get a type of the union from another of its type

Ah. Well that all depends now. Doing so is sufficiently odd that I
can't suggest a best way. It could go horribly wrong in all sorts of
ways. What is the real purpose? I.e. why are you trug to do this?
There may be a better way to get the overall end result.

Ok. First, my aim is to make a library compiling with suncc. That library
is designed to interpret limited Small programs (Small is a language).
Think of it as a compiler for Small programs. Note that I didn't write
that library. I just try to compile on OpenSolaris.

My guess would have been a language implementation.



The part where there is that problem is about conversion from a C float
to a Small "cell" (actually an int).

Here are the used types:

typedef int Embryo_Cell;

typedef union
{
float f;
Embryo_Cell c;
} Embryo_Float_Cell;

/** Float to Embryo_Cell */
#define EMBRYO_FLOAT_TO_CELL(f) ((Embryo_Float_Cell) f).c

and an example of use:

static Embryo_Cell
_embryo_fp(Embryo_Program *ep __UNUSED__, Embryo_Cell *params)
{
/* params[1] = long value to convert to a float */
float f;

if (params[0] != (1 * sizeof(Embryo_Cell))) return 0;
f = (float)params[1];
return EMBRYO_FLOAT_TO_CELL(f);
}

Note that the macro EMBRYO_FLOAT_TO_CELL is used a lot of time in that
part of program.

I hope that it is clearer, now :-)

Yes, but I don't see why you would try to hide the problem in a
macro. I'd write the example as:

static Embryo_Cell
_embryo_fp(Embryo_Program *ep __UNUSED__, Embryo_Cell *params)
{
/* params[1] = long value to convert to a float */
Embryo_Float_Cell efc;

if (params[0] != (1 * sizeof(Embryo_Cell))) return 0;
efc.f = (float)params[1];
return efc.c;
}

but I'd also want to look at the (float)params[1] part (it may be fine
you don't give the Embryo_Cell type).

If you can't re-write to avoid all this messing about, you can convert
via a pointer:

#define EMBRYO_FLOAT_TO_CELL(f) (((Embryo_Float_Cell *)&(f))->c)

is it also valid if the memeber c here would have been something else
(i mean say a struct) then just being a int .(through the typedef
thing)??
can anyone please tell me how is the structure assignment implemented
i mean is the copying done byte-wise or member-wise??
but there is a lot of finger-crossing going on in all these cases.

--
Ben.

.



Relevant Pages

  • Re: cpu type idea
    ... compiler related recently. ... int main ... float a; ... just to parallelize the vectror and tensor operations. ...
    (alt.lang.asm)
  • Re: getting avg of long and std::vector::size_type
    ... the compiler starts by evaluating a/b. ... int and b is an int, so it performs integer division on a and b. ... Now the compiler starts with the cast. ... first step is to convert a to a float. ...
    (comp.lang.cpp)
  • Re: Question on Data Type Declarations
    ... When we declare variables like ... example) a letter 'a' in an int, ... Aren't the names 'float', 'int', 'char' and so forth simply there to ... declaring "float x;" does much more than just telling the compiler ...
    (comp.lang.c)
  • Re: matrix stuff (solving b = A*x) --> using numerical recipes
    ... Numerical recipes (can be seen on ... Ok, I added it now (actually I also had it at some earlier moment, but then I removed it since I couldn't see any compiler warnings/errors without stdio.h). ... void banmul(float **a, unsigned long n, int left, int right, float x, float b); ...
    (comp.lang.c)
  • Re: It Pays to Enrich Your C Skills
    ... Check if you can score a perfect 10 (without using a compiler). ... int main{ ... struct bitfield { ... out if it is a negative integer constant or a constant expression ...
    (comp.lang.c.moderated)