473,396 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Multiply/Divide a float with a power of two

Hey there!

I want to multiply/divide (shift) a float with variable containing a
power-of-two value: 2, 4, 8, 16, ....

Is there such a function in the C standard library or the glibc library
that does this?

If not has anybody written some clever high-performance "hack" like:
1. Cast float pointer to a 32-bit integer pointer.
2. Perform the a bitwise shift on the mantissa part.
3. Cast it back.
Many thanks in advance,

Per Nordlöw

Nov 15 '05 #1
5 9797
pe*********@gmail.com wrote:
Hey there!

I want to multiply/divide (shift) a float with variable containing a
power-of-two value: 2, 4, 8, 16, ....

Is there such a function in the C standard library or the glibc library
that does this?
I don't think there's something like this in the standard library. I
don't know about glibc.

Why don't you just use * and / ?
If not has anybody written some clever high-performance "hack" like:
1. Cast float pointer to a 32-bit integer pointer.
2. Perform the a bitwise shift on the mantissa part.
3. Cast it back.


Maybe because this is the work of the compiler and the processor to
choose that kind of optimizations.

In all modern generic purpose processors there are FP multiplication and
division units, so there is no need for FPU emulation - which is what
you propose. If you have a processor (maybe a DSP) it would make sense,
but in all modern machines it would be useless...

You may speed things up using (1/a) * b, instead of a/b, but there is a
precision penalty.
--
one's freedom stops where others' begin

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.
Nov 15 '05 #2
<pe*********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...

I want to multiply/divide (shift) a float with variable containing a
power-of-two value: 2, 4, 8, 16, ....

Is there such a function in the C standard library or the glibc library
that does this?

If not has anybody written some clever high-performance "hack" like:
1. Cast float pointer to a 32-bit integer pointer.
2. Perform the a bitwise shift on the mantissa part.
3. Cast it back.

[pjp] ldexp(x, 3) returns x * 2^3. Close enough?

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Nov 15 '05 #3
pe*********@gmail.com wrote:
Hey there!

I want to multiply/divide (shift) a float with variable containing a
power-of-two value: 2, 4, 8, 16, ....

Is there such a function in the C standard library or the glibc library
that does this?

If not has anybody written some clever high-performance "hack" like:
1. Cast float pointer to a 32-bit integer pointer.
2. Perform the a bitwise shift on the mantissa part.
3. Cast it back.


For one thing, this would be implementation dependent, and is more than
certainly OT.

For other thing, the algorithm you describe wouldn't work. IEEE
standarized format (which is the most widespread format AFAIK) uses an
implicit 1 in the mantisa, so shifting the bits in the mantisa will
only multiply/divide the decimal part of the number (when expressed in
binary scientific notation).

If you really want to hack your way through this, the easiest way to do
it is add/substract to the exponent part. Anyway, there are special
cases you should consider:

- An exponent of 0 and a mantisa all 0's means 0. You should leave the
number unaffected.
- An exponent of 0 and a mantisa different of 0 is a special format for
extremely small numbers. In this case there is no implicit one. You
should shift the mantisa in this case. However keep in mind that you
may overflow this special format when multiplying, if this is the case
you should convert to the regular format (shift the mantisa till the
first one goes out, and add to the exponent part).
- An exponent all 1's and a mantisa all 1's means infinit. This value
should not be altered by your operations.
- An exponent all 1's and a mantisa with at least one 0 in it means NaN
(Not a Number, the result you get when you try to divide by 0). This
value should also be left untouched.

(I'm not a 100% sure about the last two cases, I would have to read the
IEEE standard to check. Anyway I am a 100% an exponent of all ones is a
special case.)

As you can see it's much better to simply use * and / and let the FPU
do its job (or whatever FPU emulation software your C implementation
uses if there's no FPU available).

HTH

Nov 15 '05 #4
Great!

I had the feeling, such a function existed in the standard.
Thank you very much,

Per Nordlöw

Nov 15 '05 #5
In article <11**********************@z14g2000cwz.googlegroups .com>,
pe*********@gmail.com wrote:
Hey there!

I want to multiply/divide (shift) a float with variable containing a
power-of-two value: 2, 4, 8, 16, ....

Is there such a function in the C standard library or the glibc library
that does this?

If not has anybody written some clever high-performance "hack" like:
1. Cast float pointer to a 32-bit integer pointer.
2. Perform the a bitwise shift on the mantissa part.
3. Cast it back.


I suggest to use the "*" or "/" operator.
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Nobody | last post by:
I have some code that I am trying to optimize for speed... trying to squeeze every last CPU cycle out... I remembered an old trick where dividing & multiplying can be sped up by using bitshifts...
388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
8
by: bearophileHUGS | last post by:
sys.maxint gives the largest positive integer supported by Python's regular integer type. But maybe such attribute, with few others (they can be called min and max) can be given to int type itself....
3
by: sudhanrao | last post by:
please can anyone tell me how to multiply or divide,or add or subtracted two numbers
8
by: gavinstone007 | last post by:
Who can solve this? Wrte a class number which represents all numbers. Implement member functions to carry out the following arithmetic operations on class instances, add- which returns an object...
0
by: komandjaja | last post by:
Hi everybody. I am new here and I don't know much about CSS. However, I have a blog at multiply http://komandjaja.multiply.com and I have customized the CSS codes there to make a new theme. When I...
8
by: =?Utf-8?B?bWljaGFlbGd3ZWllcg==?= | last post by:
Hello! I was working on some code the other day, and I came across an odd discrepancy between the decimal and the double type. If I attempt to divide a decimal by zero, the framework throws an...
14
by: Default User | last post by:
Hi, If I have three 64 bit integers and I want to do this operation on them: x*y/z Lets say that what we are multiplying by (y) is offset by what we are dividing by (z) so that the final...
2
by: alishaikhji | last post by:
I am working on a program which will need several different integer and float random numbers at different stages, for example: - At one point, I need a random number (float) in the range 0.1 to 10.0...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.