comp.lang.ada
 help / color / mirror / Atom feed
From: Shark8 <onewingedshark@gmail.com>
Subject: Re: Help with low level Ada
Date: Thu, 17 Mar 2011 10:46:43 -0700 (PDT)
Date: 2011-03-17T10:46:43-07:00	[thread overview]
Message-ID: <21a94695-4cdb-4dc8-8b11-389adff8a2d9@v11g2000prb.googlegroups.com> (raw)
In-Reply-To: 21e55933-291b-46a9-8659-3edf83d963b4@u3g2000vbe.googlegroups.com

On Mar 17, 8:31 am, Syntax Issues <syntax.iss...@gmail.com> wrote:
> I am translating a C math library and need help with low level
> operations - setting individual bytes, and-ing data types, and bit
> shifting. Below are the problems I have run in to.
>
> ---------------------------------------------------------------------------------------
> Setting individual bytes
> ---------------------------------------------------------------------------------------
> typedef float                   vec_t;
> typedef vec_t                   vec3_t[3];
> unsigned ColorNormalize (vec3_t rgb){
>         unsigned        c;
>         float           max;
>         ...
>         ((byte *)&c)[0] = rgb[0] * max;
>         ((byte *)&c)[1] = rgb[1] * max;
>         ((byte *)&c)[2] = rgb[2] * max;
>         ((byte *)&c)[3] = 255;
>         return c;}
>
> function Normalize
>         (Red_Green_Blue : in Vector_Color)
>         return Integer_Color
>         is
>         Result  : Integer_Color := 0;
>         Maximum : Float_4       := 0.0;
>         begin
>                 ...
>                 return
>                         -- ???!!??!?!? Byte(Red_Green_Blue(1) * Maximum) +
>                         -- ???!!??!?!? Byte(Red_Green_Blue(2) * Maximum) +
>                         -- ???!!??!?!? Byte(Red_Green_Blue(3) * Maximum);
>         end Normalize;
> ---------------------------------------------------------------------------------------
> And-ing Data types
> ---------------------------------------------------------------------------------------
> float AngleMod (float angle){
>         return (360.0/65536) * ((int)(angle * (65536/360.0)) & 65535);}
>
> function Mod_Angle
>         (Angle : in Float_4)
>         return Float_4
>         is
>         begin
>                 return (360.0 / 65536.0) * (Integer_4_Signed(Angle * (65536.0 /
> 360.0)) ---???!?!?!& 65535);
>         end Mod_Angle;
> ---------------------------------------------------------------------------------------
> Shifting
> ---------------------------------------------------------------------------------------
> int NearestPowerOfTwo (int number, qboolean roundDown){
>         int n = 1;
>         if (number <= 0)
>                 return 1;
>         while (n < number)
>                 n <<= 1;
>         if (roundDown){
>                 if (n > number)
>                         n >>= 1;
>         }
>         return n;}
>
> ---???!!?!??!?!?

For bit-shifts there's the Package Interfaces's Shift_Right,
Shift_Left, Shift_Right_Arithmetic, and so forth.

For bits I would prefer to use Array (1..N) Of Boolean, with
Pragma Packed, and using 'Address to overlay it upon the data
you want to bit-manipulate. {It may be better/easier to
overlay an integer or modular type, like if you were low-level
manipulating floating-point data.}

With the bit_array you can not/and/or/xor with the boolean
operations as you normally would:

Letting I_1 & I_2 be unsigned integers XOR-ing them could
be achieved via this:

  Type Bits is Array ( Positive Range <> ) of Boolean;
  Pragma Pack( Bits );

XOR_DATA:
Declare
 B_1 : Bits( I_1'Size );
 For B_1'Address Use I_1'Address;
 Pragma Import( Ada, B_1 ); -- This prevents the initialization
                            -- of B_1, which would destroy the value
of I_1

 B_2 : Bits( I_2'Size );
 For B_2'Address Use I_1'Address;
 Pragma Import( Ada, B_2 );

 Result_B : Bits( Positive'Max(B_1'Size, B_2'Size) );
 Result_I : Some_Unsigned_Integer;
Begin
 For Index in Reverse Result_B'Range Loop
  -- Raises error if B_1'Size /= B_2'Size when invalid indecies are
used.
  Result_B(Index):= B_1(Index) XOR B_2(Index);
 End Loop;

-- Here you would use Result_I as-needed.
End XOR_DATA;

Setting individual bytes is much like the above.



  reply	other threads:[~2011-03-17 17:46 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-17 14:31 Help with low level Ada Syntax Issues
2011-03-17 17:46 ` Shark8 [this message]
2011-03-17 17:59   ` Jeffrey Carter
2011-03-20  3:45     ` Shark8
2011-03-17 17:55 ` Jeffrey Carter
2011-03-17 19:30   ` Syntax Issues
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox