473,569 Members | 3,015 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Fixed-point multiply.

I'm trying to convert some source code containing floating point into
fixed-point arithmetic.

I am having some trouble understanding fixed point signed multiply. I
have a 18.14 base integer with 18 bits integer and 14 bits for
fraction. Now what I understand is that if I multiply two 18.14
values, I will get

18.14 * 18.14 = 36.28 for a result

But need to fit in 32-bits. So in the fraction part we must drop the
lower 14 bits and then in the integer part we drop the upper 18 bits.
Correct?

If so, then here is my pseudocode.

typedef long fp14;

fp14 FPMUL(fp14 a1, fp14 a2)
{
fp14 frac = 0, result = 0;
char sign = 0;

if (a1 < 0) { sign = 1; a1 = -a1; }
if (a1 < 0) { sign ^= 1; a2 = -a2; }

frac = (a1 * a2) >> 14;
// Is this correct? Or do I need to say ((a1 & 0x3FF) * (a2 & 0x3FF)
14)


result = (a1 >> 14) * (a2 >> 18); // 18 bits * 14 bits = 32 bits
result = result + (a1 >> 14) * ((a2 >> 14) & 0xF); // 18 bits * 4
lower bits of a2

result = (result << 18) | (frac & 0x3FF); // concatante lower 18
bits of result with lower 14 bits of frac to get 32 bit result

return result * -sign;
}

Does this code look correct or am I missing something? Also is there a
more efficient way to implement this? Any help is appreciated.

Thanks
Nov 14 '05 #1
2 2504
ax********@yaho o.com (Pallav) wrote:
# I'm trying to convert some source code containing floating point into
# fixed-point arithmetic.
#
# I am having some trouble understanding fixed point signed multiply. I
# have a 18.14 base integer with 18 bits integer and 14 bits for
# fraction. Now what I understand is that if I multiply two 18.14
# values, I will get
#
# 18.14 * 18.14 = 36.28 for a result
#
# But need to fit in 32-bits. So in the fraction part we must drop the
# lower 14 bits and then in the integer part we drop the upper 18 bits.
# Correct?

Do you have 64-bit integers availables? If so

int64 product = ((int64)a) * ((int64)b);

(Some implementations provide a 32bit*32bit -> 64bit, so you don't have
to widen the factors to get a wide product. Otherwise you have to widen
first so you don't get a truncated 32bit product.)

int32 scaledproduct = product>>14;

--
SM Ryan http://www.rawbw.com/~wyrmwif/
JUSTICE!
Justice is dead.
Nov 14 '05 #2
"Pallav" <ax********@yah oo.com> wrote in message
news:ba******** *************** ***@posting.goo gle.com...
I'm trying to convert some source code containing floating point into
fixed-point arithmetic.

I am having some trouble understanding fixed point signed multiply. I
have a 18.14 base integer with 18 bits integer and 14 bits for
fraction. Now what I understand is that if I multiply two 18.14
values, I will get

18.14 * 18.14 = 36.28 for a result

But need to fit in 32-bits. So in the fraction part we must drop the
lower 14 bits and then in the integer part we drop the upper 18 bits.
Correct?
It's your call how you deal with precision and overflow.
If so, then here is my pseudocode.

typedef long fp14;
Better to use an unsigned type as it's behaviour more like the two's complement that you
might wish to rely on.
fp14 FPMUL(fp14 a1, fp14 a2)
{
fp14 frac = 0, result = 0;
char sign = 0;

if (a1 < 0) { sign = 1; a1 = -a1; }
if (a1 < 0) { sign ^= 1; a2 = -a2; } ^
ITYM (a2 < 0)

frac = (a1 * a2) >> 14;
This will work (to a point) if fp14 happens to be 64 bits. If you have access to C99's
long long you could try...

frac = ( ((unsigned long long) a1)
* ((unsigned long long) a1)) >> 14;
// Is this correct? Or do I need to say ((a1 & 0x3FF) * (a2 & 0x3FF)
14)

Your question is really an algorithm one, not a C one. There are methods for multiplying
large integer numbers and keeping full precision, but this isn't the group to discus them.
...Also is there a more efficient way to implement this?


If you want practical efficiency, then ISO C and comp.lang.c may not be what you're
looking for. For starters you would have to define the nature of what you percieve as
efficient, because ISO C doesn't.

--
Peter
Nov 14 '05 #3

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

Similar topics

26
9648
by: Adrian Parker | last post by:
I'm using the code below in my project. When I print all of these fixed length string variables, one per line, they strings in questions do not properly pad with 0s. strQuantity prints as " 4". Six spaces than the value of intQuantity. This is correct. But all the others end up being string objects of only 6 characters long (with...
1
3517
by: Jaz | last post by:
Trying to use a fixed layer for a couple of NAV buttons. I found this code, but the IE part is commented, and I don't understand the IF statement. It works great on Moz/Firebird and Opera BUT not IE. Would someone please take a look at this and tell me if it can made to work in IE? If it can, a hint on the syntax/code would be much...
9
17703
by: Paul Trautwein | last post by:
I'm trying to get an image to float in a window despite scrolling. I've gotten it to work on my Mac using IE 5.2, Netscape, and Safari, but it goes wonky when I test it on a PC. (testing with IE only at the moment.) Positioning is wrong, and it doesn't float at all. Here's a test page: http://www.bdiusa.com/mirrors/test.html I've tested...
6
5397
by: Mason A. Clark | last post by:
Masters: On two or three-column layouts, one column often has a list of links. Scrolling the page hides them. I'm aware there's supposed to be the ability to fix the column (frame-like). I have some bits of such code but haven't yet made it work well. Question: Why have I never seen an example on the web? Not that I've seen...
2
4996
by: hq105862 | last post by:
Hi, Is it possible to simulate frames behaviour with CSS? Currently, I've used CSS to create the visual look of my old framed site. For this I've used styles that place the images at absolute positions. Then I thought, if I fixed the image positions, the images will stay put and the 'body' will scroll up, like in frames.
4
11166
by: Otie | last post by:
Hello, I am using the MSFlexGrd Control in VB5. I have 1 fixed row and one fixed column. I am trying to do a sort when the user clicks a column in the FIXED ROW. But when I capture the row number in the click event I get row = 1 if I click on the FIXED row OR the actual row 1. How can I get the grid control to tell me when the user has...
1
10405
by: O.B. | last post by:
In the example below, I'm trying to convert a fixed byte array to a string. I get an error about needing to use "fixed" but I have no clue where to apply it. Help? using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices;
1
9407
by: Rick Knospler | last post by:
I am trying to convert a vb6 project to vb.net. The conversion worked for the most part except for the fixed length strings and fixed length string arrays. Bascially the vb6 programmer stored all form data in a fixed length structure that is written direct to disk. I need to load the existing files, into a fixed length structure to...
4
3975
by: Jeff | last post by:
Hey I'm wondering how the Fixed-Width Text Format is What I know is that the top line in this text format will contain column names. and each row beneath the top line represent for example a row in a table etc... But does fixed-with mean that every column has a fixed with: for example first column is 10 char wide and second column is...
15
2695
by: ingejg | last post by:
I am starting to study internet synchronization, and my head is still spinning since internet is not my forte, however my boss is breathing down my neck at the moment. Our company has only one server with fixed IP address provided by our ISP, while the other sites (which I wish in the future hold the replicas databases) have only standard...
0
7703
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...
0
8138
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7681
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...
0
6290
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
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...
0
5228
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...
0
3651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
950
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.