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.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,f9957894e0bdf128 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!news3.google.com!feeder1-2.proxad.net!proxad.net!feeder2-2.proxad.net!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: How to put 200 into an integer sub-type of 16 bits (code included) Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 8bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <407ae64d-3cb3-4310-b59e-f1bbae9910a5@t39g2000prh.googlegroups.com> <71gqm49eatq868htrvd7eghm3m8su8kcbl@4ax.com> <3d3719f4-355c-4094-9902-495d612d46fe@n33g2000pri.googlegroups.com> <139961e9-bae6-4e60-8ff7-4f4779b27481@z6g2000pre.googlegroups.com> <87816592-c947-4bbc-92ed-7473646a105e@a12g2000pro.googlegroups.com> <1a2b31ac-cf6b-44e3-85b7-04594460db87@d36g2000prf.googlegroups.com> <86ab9s7jlx.fsf@stephe-leake.org> Date: Thu, 15 Jan 2009 16:54:43 +0100 Message-ID: NNTP-Posting-Date: 15 Jan 2009 16:54:48 CET NNTP-Posting-Host: 8618f903.newsspool2.arcor-online.net X-Trace: DXC=BW`@^k5Xg<6i6K;>iZ]763A9EHlD;3Yc24Fo<]lROoR1^YC2XCjHcb92X?Mig[YN737U=>XSAhb0Xjk7?l4AcO? X-Complaints-To: usenet-abuse@arcor.de Xref: g2news2.google.com comp.lang.ada:4296 Date: 2009-01-15T16:54:48+01:00 List-Id: On Thu, 15 Jan 2009 09:02:34 -0500, Stephen Leake wrote: > ChristopherL writes: > >> On Jan 14, 12:41�pm, Adam Beneschan wrote: >>> On Jan 14, 12:13 pm, ChristopherL wrote: >>> >>> > I want to copy bits from an Ada float to a user defined short integer >>> > maintaining the same >>> > bit representation. >> >> This problem was given to me to try to solve who said it's probably >> impossible! > > Ok. This could eventually be used to verify the floating point format > used by your machine, for example. > > The right way to proceed is to write a bit-level record clause for the > floating point type on your hardware; something like: > > type unsigned_24 is ...; > type usigned_7 is ...; > > type IEEE_32_Float_Rep_Type is record > sign : boolean; > exponent : unsigned_7; > mantissa_1 : Interfaces.unsigned_8; > mantissa_2 : Interfaces.unsigned_8; > mantissa_3 : Interfaces.unsigned_8; > end record; > > for IEEE_32_Float_Rep_Type use record > sign : at 0 range 0 .. 0; > exponent : at 0 range 1 .. 8; > mantissa_1 : at 0 range 9 .. 15; > mantissa_2 : at 0 range 16 .. 23; > mantissa_3 : at 0 range 24 .. 31; > end record; > > Note that I broke the mantissa down into three parts, each fitting in > 8 bits. > > I probably have the details wrong for IEEE 32 bit float; look it up > somewhere. Well, it is not that simple, because there are special patterns for ideals like NaN and also denormalized representations. Also there is an issue of octet endianness. Anyway here is an implementation that handles IEEE 754 representations: http://www.dmitry-kazakov.de/ada/components.htm#IEEE_754 > Especially note that the 'range' values need to have the correct > endianness for your machine (sign will be at 31 on some machines, 0 on > others). Or even somewhere in the middle, provided that the machine numbers are indeed IEEE... (:-)) > Then use unchecked conversion from float to IEEE_32_Float_Rep_type, > then simple assignment (possibly with an integer type conversion) from > any field to an Unsigned_8. Well, that is IMO a different task, and there are floating point attributes like S'Exponent and S'Fraction for this, which do the job properly. I am afraid that the OP's question does not make any sense. It looks like a C-ism... -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de