473,394 Members | 2,100 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,394 software developers and data experts.

Re: is this portable?

"copx" <co**@gazeta.plwrites:
"Thad Smith" <Th*******@acm.orgschrieb im Newsbeitrag
news:48***********************@auth.newsreader.oct anews.com...
>copx wrote:
[ ... ]
>>
for (p2 = 1; p2 && p2 <= x; p2 *= 2) ;

Yes, that is a straight forward and safe way to do it, but I want this to
work really fast. That's the reason why I searched for some bit vodoo in the
first place.

What Thad is getting at here is that this the solution you found
simply does not fit your requirements, whether it is portable or
effective is thus irrelevant, tell us wheter your requirements are :

"to round up any given integer to the next power of two"

As you initially state, or :

"to round a number (x) to the next multiple of "a", where "a" is a
power of 2"
As indicated by the solution you found, those two are not the same.
If the first are your requirements you would probably need to isolate
the most significant bit set in x, this is a O(log N) operation, and
then shift that left.

Thad's solution is O(N)

Something like (for 32 bit):

a = x&0xffff0000 ; if (a) x=a; /* in upper half? */
a = x&0xff00ff00 ; if (a) x=a; /* in upper half of that */
( The rest is left as an excercise for the reader :-)
x = x << 1;

This is straight from the back of my head so don't take it at face
value. If it works at all it will be for unsigned only.

I'm not sure if this qualifies as woodoo though.

--
... __o Øyvind
... _`\(, http://www.darkside.no/olr/
... (_)/(_) ... biciclare necesse est ...
Jun 27 '08 #1
2 1076
ol*@218.80-202-60.ne (Øyvind Røtvold) writes:

[ ... ]
>
a = x&0xffff0000 ; if (a) x=a; /* in upper half? */
a = x&0xff00ff00 ; if (a) x=a; /* in upper half of that */
( The rest is left as an excercise for the reader :-)
x = x << 1;
I suppose I should mention the singularities her:

If x has MSB set the result will be shifted out (if x is a 32 bit
type) leaving the result 0.

If x is already a power of two it will still be rounded up to the next
power of two, this is what the spec says, but may not be what is
intended, subtract 1 at the start if thats the case.

If x is 0, result will be 0, the correct result should probably be 1

--
... __o Øyvind
... _`\(, http://www.darkside.no/olr/
... (_)/(_) ... biciclare necesse est ...
Jun 27 '08 #2
On Apr 12, 9:20*am, o...@218.80-202-60.ne (Øyvind Røtvold) wrote:
[snip]

[snip]
*"to round up any given integer to the next power of two"
[ snip ]
Something like (for 32 bit):

a = x&0xffff0000 ; if (a) x=a; */* in upper half? */
a = x&0xff00ff00 ; if (a) x=a; */* in upper half of that */
( The rest is left as an excercise for the reader :-)
x = x << 1;

This is straight from the back of my head so don't take it at face
value. *If it works at all it will be for unsigned only.
at : http://graphics.stanford.edu/~seande...tml#IntegerLog

You can find some solutions, this:

x |= x >1; // first round down to power of 2
x |= x >2;
x |= x >4;
x |= x >8;
x |= x >16;
x += 1;

Should do the trick, and don't involve any branching.

--
.. __o Øyvind
.. _`\(, http://www.darkside.no/olr/
.. (_)/(_) ... biciclare necesse est ...
Jun 27 '08 #3

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

Similar topics

13
by: James Harris | last post by:
Hi, Can someone recommend a book that will teach me how to approach C programming so that code is modularised, will compile for different environments (such as variations of Unix and Windows),...
22
by: SeeBelow | last post by:
Is there any way, in C, of interacting with a running program, but still using code that is portable, at least between linux and Windows? By "interacting" it could be something as simple as...
2
by: sugaray | last post by:
i was just told in the 'simple base-conveter program' thread, that using 'return' is not portable in such case: if(argc!=2) { fprintf("error message\n"); return 1; } it should be replace by...
20
by: Lowell Kirsh | last post by:
I saw something recently for the first time: int main(int argc, char **argv, char **envp) ^ The thing I had never seen was a third arg to the main function. I thought main either took no args...
131
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write...
13
by: Tomás | last post by:
Let's start off with: class Nation { public: virtual const char* GetName() const = 0; } class Norway : public Nation { public: virtual const char* GetName() const
28
by: lovecreatesbeauty | last post by:
On gcc, which version of C standard has the most compliant: -c89, -ansi or c99? For more portable C code, which options should be applied to compilation? Can the following options guarantee the...
14
by: Kristan Dyson | last post by:
I was just wondering whether you knew whether it is possible to compile a fully portable C program? I want to write a 'C' CGI program on Windows, then just upload it to my Plus.Net Debian-based...
1
by: skillzero | last post by:
Is there a portable way to pass a va_list as a parameter to another function taking a variable argument list? I have a function that takes a printf-like format string and I'd like to use...
22
by: Amali | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.