473,396 Members | 2,099 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.

quick question: \floor(\log_2(ix))

What is the fastest way to implement this function if ix is
an integer. Would i need to convert ix to float? What is the
fastest way to code this function?

Thanks,
--j

Jul 23 '05 #1
9 1939
> What is the fastest way to implement this function if ix is
an integer. Would i need to convert ix to float? What is the
fastest way to code this function?


The fastest way is a giant lookup table which maybe is impossible to
implement.
Second fastest (for common 32 bit integers) might be a 16 bit lookup table
and something like:

int logInt[65536]; // Should be initialized properly
....
if (ix & 0xffff0000)
return logInt[ix >> 16]+16;
else
return logInt[ix & 0xffff];

Niels Dybdahl
Jul 23 '05 #2

"John" <we**********@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
What is the fastest way to implement this function if ix is
an integer.


If the design counts as a part of "implementing", just start typing
the first idea you get. If the design does _not_ count as a part of
"implementing", use some time to find the shortest design you can,
and type it in really quickly.

Jul 23 '05 #3
John wrote:

What is the fastest way to implement this function if ix is
an integer. Would i need to convert ix to float? What is the
fastest way to code this function?


What function? Many systems do not show the subject when
displaying the message.

At any rate speed is not the concern of the standard, and depends
on the exact system in use. Thus it is off-topic in c.l.c.
Probably also in c.l.c++, but at any rate that is a different
language and things should not be crossposted between there and
here. F'ups set.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Jul 23 '05 #4
On Mon, 11 Apr 2005 09:17:20 +0200, Niels Dybdahl
<nd*@fjern.detteesko-graphics.com> wrote:
What is the fastest way to implement this function if ix is
an integer. Would i need to convert ix to float? What is the
fastest way to code this function?
The fastest way is a giant lookup table which maybe is impossible to
implement.


It's certainly impractical.
Second fastest (for common 32 bit integers) might be a 16 bit lookup table
and something like:

int logInt[65536]; // Should be initialized properly
...
if (ix & 0xffff0000)
return logInt[ix >> 16]+16;
else
return logInt[ix & 0xffff];


If you want to make it generic for any size of integer, you could do the
first part in a loop:

int log2int(uint_type ix)
{
int i;
while (ix & ~0xFFFF)
{
i += 16;
ix >>= 16;
}
return logInt[ix] + i;
}

where uint_type is whatever unsigned integer type you like.

Chris C
Jul 23 '05 #5
Risto Lankinen wrote:
What is the fastest way to implement this function if ix is
an integer.


If the design counts as a part of "implementing", just start typing
the first idea you get. If the design does _not_ count as a part of
"implementing", use some time to find the shortest design you can,
and type it in really quickly.


Or write a program that produces the code.

Jul 23 '05 #6
John wrote:

What is the fastest way to implement this function if ix is
an integer. Would i need to convert ix to float? What is the
fastest way to code this function?


This is the easiest way to implement log2.

#include <math.h>

double l_og2(double x)
{
static double log_2;

if (0.5 > log_2) {
log_2 = log(2);
}
return x > 0 ? log(x) / log_2 : log(x);
}

--
pete
Jul 23 '05 #7
John wrote:
What is the fastest way to implement this function if ix is
an integer. Would i need to convert ix to float? What is the
fastest way to code this function?


It's equivalent to the position of the highest set bit in ix, and you
can google for algorithms to find that.

--
Quidquid latine dictum sit, altum viditur.
Jul 23 '05 #8

I figured that out,
and I figured out that on x86 machines there is a bit scan
command in assembly (bzr or something). What is the
fastest way to do this on portable machines? Right now
I just use a loop to find the largest bit. It's portable and fast.

Is there a bitscan function for opterons by any chance?

Thanks,
--j

Jul 23 '05 #9
On 10 Apr 2005 23:03:46 -0700, "John" <we**********@yahoo.com> wrote:
What is the fastest way to implement this function if ix is
an integer. Would i need to convert ix to float? What is the
fastest way to code this function?

Thanks,
--j

As CBFalconer said, this really isn't a C/C++ question. I think
there's a newsgroup which covers numeric programming;
sci.math.num-analysis may be it (check its FAQ). You've faced enough
ribbing, though, so here's a little more help.

A binary search algorithm on an integer using integer powers of 2 as
bounds will give you O(lg(n)) timing, where n is the number of bits in
the integer representation (assuming multiplication and division by
powers of 2 and calculating powers of 2 are O(1)). This technique can
be extended to representations in base b, using integer powers of b as
bounds. Timing for the algorithm for base b will be:
O(lg(n))*T(a*B)*T(a/B)*T(b**k),
where T(f) is the timing for f, B is any integer power of b and x**y
is x to the y-th power.

There may be a HAKMEM about floorlg which has better timing using some
number theoretic approach or involving properties of a specific
representation.

Kanenas
--
PROBLEM 96:
Solve Go. About 10^170 positions.

Jul 23 '05 #10

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

Similar topics

1
by: jack | last post by:
I pulled this script out of a larger one to test, and I can't figure it out. If I keep this script in the same directory as the files I want to read, it works fine, but if I move the script out of...
10
by: Snake | last post by:
Hello guys, I have a quick question about pointers. When I say int *p; *p = 40; cout << *p; why does that produce and error message(something about fault address)? Isnt that I created a...
30
by: Christopher Benson-Manica | last post by:
Given the following: char s="Hello, world\n!"; Are all the following guaranteed to produce the same output? printf( "%s", s ); fprintf( stdout, "%s", s ); fwrite( s, sizeof(char),...
5
by: tariq | last post by:
Hi, got a quick question on al.exe i have an assembly say library.dll without any strong name i have a sn pair say org.snk how do i sign library.dll with org.snk using al? tx -T
1
by: Mike | last post by:
Hi, I am using an enum to map to certain columns instead of using the string name. When I use the enum as the indexer in a datarow it forces me to cast it into an int. Shouldn't this be...
6
by: z_learning_tester | last post by:
Quick question- What happens if you have a private class with a public static method? Can you still say the following? Lets say you are making this call from another class, say class2... int...
3
by: craig | last post by:
Quick question for the experts... Whenever I set the Image property of a PictureBox on one of my forms to a PNG graphic file on my hard drive, I get the following runtime error when I try to run...
2
by: Rudy | last post by:
Hello all! Have a quick question on drop down list. Need to make a year ddl, 1900 to 1987. Is there a quick way to do this. I'm not binding this, just loading it in the items collection. ...
3
by: Brian | last post by:
A quick question here - What can be achieved in IL which is not possible in C# ? o Creation of an ArrayList o Creation of a Dictionary o Creation of a two dimensional array...
1
by: JustTrev | last post by:
Hi I have a quick question. I'm studying how XML works in sq server 2005, and I've hit the following code online that I can't completely understand (see below code). I know what it does, how it...
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
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,...
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
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.