From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,ba0524a97c5a11cf X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!postnews.google.com!v11g2000prb.googlegroups.com!not-for-mail From: Shark8 Newsgroups: comp.lang.ada Subject: Re: Help with low level Ada Date: Thu, 17 Mar 2011 10:46:43 -0700 (PDT) Organization: http://groups.google.com Message-ID: <21a94695-4cdb-4dc8-8b11-389adff8a2d9@v11g2000prb.googlegroups.com> References: <21e55933-291b-46a9-8659-3edf83d963b4@u3g2000vbe.googlegroups.com> NNTP-Posting-Host: 174.28.172.140 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1300384003 28165 127.0.0.1 (17 Mar 2011 17:46:43 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 17 Mar 2011 17:46:43 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: v11g2000prb.googlegroups.com; posting-host=174.28.172.140; posting-account=lJ3JNwoAAAAQfH3VV9vttJLkThaxtTfC User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15 ( .NET CLR 3.5.30729; .NET4.0E),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:18285 Date: 2011-03-17T10:46:43-07:00 List-Id: On Mar 17, 8:31=A0am, Syntax Issues 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 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 vec_t; > typedef vec_t =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 vec3_t[3]; > unsigned ColorNormalize (vec3_t rgb){ > =A0 =A0 =A0 =A0 unsigned =A0 =A0 =A0 =A0c; > =A0 =A0 =A0 =A0 float =A0 =A0 =A0 =A0 =A0 max; > =A0 =A0 =A0 =A0 ... > =A0 =A0 =A0 =A0 ((byte *)&c)[0] =3D rgb[0] * max; > =A0 =A0 =A0 =A0 ((byte *)&c)[1] =3D rgb[1] * max; > =A0 =A0 =A0 =A0 ((byte *)&c)[2] =3D rgb[2] * max; > =A0 =A0 =A0 =A0 ((byte *)&c)[3] =3D 255; > =A0 =A0 =A0 =A0 return c;} > > function Normalize > =A0 =A0 =A0 =A0 (Red_Green_Blue : in Vector_Color) > =A0 =A0 =A0 =A0 return Integer_Color > =A0 =A0 =A0 =A0 is > =A0 =A0 =A0 =A0 Result =A0: Integer_Color :=3D 0; > =A0 =A0 =A0 =A0 Maximum : Float_4 =A0 =A0 =A0 :=3D 0.0; > =A0 =A0 =A0 =A0 begin > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ... > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 return > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 -- ???!!??!?!? Byte(Red_G= reen_Blue(1) * Maximum) + > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 -- ???!!??!?!? Byte(Red_G= reen_Blue(2) * Maximum) + > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 -- ???!!??!?!? Byte(Red_G= reen_Blue(3) * Maximum); > =A0 =A0 =A0 =A0 end Normalize; > -------------------------------------------------------------------------= -------------- > And-ing Data types > -------------------------------------------------------------------------= -------------- > float AngleMod (float angle){ > =A0 =A0 =A0 =A0 return (360.0/65536) * ((int)(angle * (65536/360.0)) & 65= 535);} > > function Mod_Angle > =A0 =A0 =A0 =A0 (Angle : in Float_4) > =A0 =A0 =A0 =A0 return Float_4 > =A0 =A0 =A0 =A0 is > =A0 =A0 =A0 =A0 begin > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 return (360.0 / 65536.0) * (Integer_4_Sig= ned(Angle * (65536.0 / > 360.0)) ---???!?!?!& 65535); > =A0 =A0 =A0 =A0 end Mod_Angle; > -------------------------------------------------------------------------= -------------- > Shifting > -------------------------------------------------------------------------= -------------- > int NearestPowerOfTwo (int number, qboolean roundDown){ > =A0 =A0 =A0 =A0 int n =3D 1; > =A0 =A0 =A0 =A0 if (number <=3D 0) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 return 1; > =A0 =A0 =A0 =A0 while (n < number) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 n <<=3D 1; > =A0 =A0 =A0 =A0 if (roundDown){ > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if (n > number) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 n >>=3D 1; > =A0 =A0 =A0 =A0 } > =A0 =A0 =A0 =A0 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 /=3D B_2'Size when invalid indecies are used. Result_B(Index):=3D 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.