John <sampson164@gmail.com> wrote:>What happens when you come across a sqrt, atan2, log, pow, etc? Would >fixed point versions be installed automatically? If automatic >conversion code exists, please post a link.It is usually not necessary to re-code such functions to implement a bit-exact fixed point design. All that is usually necessary is to quantize the input and output of the function. Steve
Online · Live + on demand
February 9-11, 2027
Price goes up Jan 1: register now to lock in today's rate
Register Now →
floating pt. to fixed pt. conversion
Started by ●August 16, 2010
Reply by ●August 16, 20102010-08-16
Reply by ●August 16, 20102010-08-16
Kappa <NO_SPAM_78kappa78@virgilio.it_NO_SPAM> wrote:> I have some routine for conversion ...> unsigned int MaskBit(unsigned char Bit) > [snip]> float Fixed2Float(unsigned int Value, unsigned char Data, unsigned char > Binary, unsigned char Signed) > [snip]I find it much simpler, when working in C/C++, to represent fixed-point values as doubles rather than as ints. Steve
Reply by ●August 16, 20102010-08-16
>PL/I is one of the few languages with direct support for fixed >point with the radix point other than to the immediate right >of the least significant digit. (both binary and decimal.) >PL/I allows the scale factor (radix point position) to be either >positive or negative. In 32 bit binary, it is usual to allow >between -31 and 31 bits to the right of the binary point, >or between -15 and +15 to the right of the decimal point >(for fixed decimal arithmetic).Fixed point was a common feature in languages 30 years ago, especially ones targeted at military applications. For some reason most the languages which have survived don't offer this. Ada is probably the last language with a decent user base that still has a fixed point type. Steve
Reply by ●August 16, 20102010-08-16
steveu <steveu@n_o_s_p_a_m.coppice.org> wrote:> [attribution lost] wrote,>>PL/I is one of the few languages with direct support for fixed >>point with the radix point other than to the immediate right >>of the least significant digit. (both binary and decimal.) >>PL/I allows the scale factor (radix point position) to be either >>positive or negative. In 32 bit binary, it is usual to allow >>between -31 and 31 bits to the right of the binary point, >>or between -15 and +15 to the right of the decimal point >>(for fixed decimal arithmetic).>Fixed point was a common feature in languages 30 years ago, especially ones >targeted at military applications. For some reason most the languages which >have survived don't offer this. Ada is probably the last language with a >decent user base that still has a fixed point type.Depends on how you define "a language". If we consider System C / IEEE 1666 a language, it and its fixed point types are widely used. You could argue the System C is merely a library, not a language. (You could also argue that because the System C stuff is in neither STL nor Boost, it is not part of the language standard, but rather a non-standard extension. But that argument would be a little weak, since it has its own standards path...) In any case, I think the reason that modern languages don't natively support fixed point types is that the ability to define new types has been vastly improved, to the point of needing fewer built-in types. Steve
Reply by ●August 17, 20102010-08-17
>steveu <steveu@n_o_s_p_a_m.coppice.org> wrote: > >> [attribution lost] wrote, > >>>PL/I is one of the few languages with direct support for fixed >>>point with the radix point other than to the immediate right >>>of the least significant digit. (both binary and decimal.) >>>PL/I allows the scale factor (radix point position) to be either >>>positive or negative. In 32 bit binary, it is usual to allow >>>between -31 and 31 bits to the right of the binary point, >>>or between -15 and +15 to the right of the decimal point >>>(for fixed decimal arithmetic). > >>Fixed point was a common feature in languages 30 years ago, especiallyones>>targeted at military applications. For some reason most the languageswhich>>have survived don't offer this. Ada is probably the last language with a >>decent user base that still has a fixed point type. > >Depends on how you define "a language". If we consider System C / >IEEE 1666 a language, it and its fixed point types are widely used. > >You could argue the System C is merely a library, not a language. > >(You could also argue that because the System C stuff is in neither >STL nor Boost, it is not part of the language standard, but >rather a non-standard extension. But that argument would be a little >weak, since it has its own standards path...) > >In any case, I think the reason that modern languages don't natively >support fixed point types is that the ability to define new types has >been vastly improved, to the point of needing fewer built-in types. > >SteveIts true that support for arbitrarily created types has greatly increased, but they bring their own problems. With people doing so much with their own arbitrarily generated types, mix and match construction, based on chunks of useful code (libraries or otherwise), suffers badly. Look how poor the support for something as basic as complex numbers is today. Steve
Reply by ●August 17, 20102010-08-17
steveu <steveu@n_o_s_p_a_m.coppice.org> replies to my post,>>Depends on how you define "a language". If we consider System C / >>IEEE 1666 a language, it and its fixed point types are widely used. >> >>You could argue the System C is merely a library, not a language. >> >>(You could also argue that because the System C stuff is in neither >>STL nor Boost, it is not part of the language standard, but >>rather a non-standard extension. But that argument would be a little >>weak, since it has its own standards path...) >> >>In any case, I think the reason that modern languages don't natively >>support fixed point types is that the ability to define new types has >>been vastly improved, to the point of needing fewer built-in types.>Its true that support for arbitrarily created types has greatly increased, >but they bring their own problems. With people doing so much with their own >arbitrarily generated types, mix and match construction, based on chunks of >useful code (libraries or otherwise), suffers badly.This is why I tend to recommend that people use IEEE 1666 fixed-point types rather than rolling their own.> Look how poor the support for something as basic as complex numbers > is today.Hmm... I find the complex<> types in C++ work perfectly well. Again, it depends upon what you mean by "supported". Maybe there is not a large enough math library that uses complex types. Steve
Reply by ●August 17, 20102010-08-17
On 08/17/2010 03:53 AM, steveu wrote:> Fixed point was a common feature in languages 30 years ago, especially ones > targeted at military applications. For some reason most the languages which > have survived don't offer this. Ada is probably the last language with a > decent user base that still has a fixed point type.I don't see much use for a fixed-point type in programming languages. Sure, it makes the one to one conversion easier and more readable, but in the end that approach never worked well for me. With C++ I was able to write my own fixed-point data-type, but beyond prototyping and using it for trivial things I never really used a fixed class. I often end up with code that sat halfway between fixed and floating point math. I don't use floating point as is, but I tend to express critical variables as 1.31 fixed point values with an external exponent (shift or bias) part. With todays CPUs and DSPs you can often hide the extra work under the latency of other multiplications. Sure it has some overhead, but in practice it nearly comes for free. Maybe it's just me, but I hate to throw away perfectly valid bits of information just because I decided on a radix once. Also I've become a big fan of the "count leading zeros" or "position of first significant bit" instructions. In fact when I port to a new CPU instruction set the presence and latency of such an instructions is among the first things I check. Nevertheless, thanks for pointing out Ada. I've been always interested in it and always wanted to take a closer look. Will do so once I've need a break from learning LISP :-) Nils Pipenbrinck
Reply by ●August 17, 20102010-08-17
Steve Pope <spope33@speedymail.org> wrote:> steveu <steveu@n_o_s_p_a_m.coppice.org> wrote:(I wrote)>>>PL/I is one of the few languages with direct support for fixed >>>point with the radix point other than to the immediate right >>>of the least significant digit. (both binary and decimal.) >>>PL/I allows the scale factor (radix point position) to be either >>>positive or negative. In 32 bit binary, it is usual to allow >>>between -31 and 31 bits to the right of the binary point, >>>or between -15 and +15 to the right of the decimal point >>>(for fixed decimal arithmetic).>> Fixed point was a common feature in languages 30 years ago, >> especially ones targeted at military applications. >> For some reason most the languages which have survived don't >> offer this. Ada is probably the last language with a decent >> user base that still has a fixed point type.I thought it might have come from COBOL. PL/I features came from ALGOL, Fortran, and COBOL. Support for fixed decimal in COBOL makes sense, the generalization to fixed binary isn't so obvious. (Not many business applications use binary fractions.) It isn't easy at all to implement multiply and divide to full width using integer multiply and divide as supplied in most high-level languages. It is fairly easy on most machines to do in assembly, though.> Depends on how you define "a language". If we consider System C / > IEEE 1666 a language, it and its fixed point types are widely used.> You could argue the System C is merely a library, not a language.> (You could also argue that because the System C stuff is in neither > STL nor Boost, it is not part of the language standard, but > rather a non-standard extension. But that argument would be a little > weak, since it has its own standards path...)> In any case, I think the reason that modern languages don't natively > support fixed point types is that the ability to define new types has > been vastly improved, to the point of needing fewer built-in types.Maybe. Well, one problem is that using types with user specified precision and scale factor isn't so easy. For PL/I floating point you specify the number of (decimal or binary) digits you need, for fixed point the number of digits and position of the radix point. Fortran now has the KIND feature, which allows one to specify the number of decimal digits (no scale factor) that one needs. But much of the time one doesn't know so exactly how many digits one needs. For those times that you do, or at least know the needed scale factor, it is nice. -- glen
Reply by ●August 17, 20102010-08-17
Also I've become a>big fan of the "count leading zeros" or "position of first significant >bit" instructions. In fact when I port to a new CPU instruction set the >presence and latency of such an instructions is among the first things I >check.Use this for fixed pool buffer management. find index directly from the bitmap, using this instruction on the bitmap. Free-ing is also easy.
Reply by ●August 17, 20102010-08-17
On Aug 17, 4:59=A0am, spop...@speedymail.org (Steve Pope) wrote:> Hmm... I find the complex<> types in C++ work perfectly > well. =A0Again, it depends upon what you mean by "supported". > Maybe there is not a large enough math library that uses > complex types.I'm actually doing just that plus SystemC headers and it works quite well if you know what you'r doing. Still things are not as easy and you'd like them to be and you have to watch out for bit-growth among other things. I'm halfway there I think but find myself having developed a tool on top a tool. In C++, I ended up using almost every feature of the language including policies and traits to make this happen. I'd like to talk to some silicon powerhouse and see if I have reinvented some de facto wheel that these heavyweights have long been using. -Momo







