473,657 Members | 2,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9822
pe*********@gma il.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*********@gm ail.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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*********@gma il.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************ **********@z14g 2000cwz.googleg roups.com>,
pe*********@gma il.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
12517
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 and addition / subtraction instead (doing this saved me about 11% time on my 2.26ghz P4). Really easy with powers of two. I implemented this last night when I was tired, and this morning, I realize it was not exactly right (values were slightly...
388
21663
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 worth the $25USD. I'm just looking for a book on Pointers, because from what I've read it's one of the toughest topics to understand. thanks in advanced.
8
9468
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. D is a very nice language, that I hope to see more used. It is copying lot of things from Python. D Floating point values have some proprieties: http://www.digitalmars.com/d/property.html Properties for Floating Point Types:
3
24554
by: sudhanrao | last post by:
please can anyone tell me how to multiply or divide,or add or subtracted two numbers
8
4123
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 of class number whose value is the sum of the numbers added, subtract- returns an object of class number and value is the result subtracting the second number object from the first, multiply and divide (the return formats are the same as for add...
0
3997
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 see my page thru Firefox, everything is okay but when I see it with IE, there are distorted things and some pictures don't show up. My platform is Windows XP home edition. Here's my css code to the site /************* Spiderman 3...
8
4803
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 error. If I attempt to divide a double by zero, the framework returns infinity. From a mathematical standpoint, it would seem to me that the decimal handles this division correctly, while the double's handling of this is flawed. Is there a...
14
5157
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 answer will fit in a 64-bit integer. Let me simplify it by using unsigned chars (8 bits):
2
5472
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 - At one point, I need a random number (float) in the range 0.5 to 1.5 - At one point, I need a random number (float) in the range 0.3 to 3.0 - At one point, I need a random number (float) in the range 0.1 to 10.0 Also, I need to make it sure...
0
8413
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8842
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8513
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.