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

macro help

Hi all,

I wrote a macro to read 8 bytes from a byte stream. Am not
sure if it is working ok. Can anyone please point out if there looks
to be a problem!

#define READ64(b) ( (uint64_t(*b)) << 56) + ((uint64_t(*(b+1))) << 48)
+ ((uint64_t(*(b+2))) << 40) +((uint64_t(*(b+3))) << 32)+((uint64_t(*(b
+4))) << 24) + (( uint64_t(*(b+5))) << 16) + (( uint64_t(*(b+6))) <<
8) + ( *(b+7))

Here b is a pointer to an unsigned char.

Thanks,

Vishal

Sep 30 '07 #1
7 1735
in*******@gmail.com wrote:
Hi all,

I wrote a macro to read 8 bytes from a byte stream. Am not
sure if it is working ok. Can anyone please point out if there looks
to be a problem!
What did your test cases show you?
#define READ64(b) ( (uint64_t(*b)) << 56) + ((uint64_t(*(b+1))) << 48)
+ ((uint64_t(*(b+2))) << 40) +((uint64_t(*(b+3))) << 32)+((uint64_t(*(b
+4))) << 24) + (( uint64_t(*(b+5))) << 16) + (( uint64_t(*(b+6))) <<
8) + ( *(b+7))
Why on Earth did you write a totally horrible macro when a function can
do the job and is a kinder on the eye and debugger?

--
Ian Collins.
Sep 30 '07 #2
On Sun, 30 Sep 2007 09:26:36 +0000, inftoconc wrote:
Hi all,

I wrote a macro to read 8 bytes from a byte stream. Am not
sure if it is working ok. Can anyone please point out if there looks
to be a problem!

#define READ64(b) ( (uint64_t(*b)) << 56) + ((uint64_t(*(b+1))) << 48)
+ ((uint64_t(*(b+2))) << 40) +((uint64_t(*(b+3))) << 32)+((uint64_t(*(b
+4))) << 24) + (( uint64_t(*(b+5))) << 16) + (( uint64_t(*(b+6))) <<
8) + ( *(b+7))

Here b is a pointer to an unsigned char.
1. The syntax of the cast is (type-name)cast-expression, e.g.
(uint64_t)*b, not uint64_t(*b). Maybe you were thinking about C++?
2. What's wrong with good ol' b[0], b[1] etc.? They're somewhat
clearer than (*(b+1)).

Try:
#define READ64(b) ( ((uint64_t)(b)[0] << 56) + ((uint64_t)(b)[1] << 48) +\
((uint64_t)(b)[2] << 40) + etc... )
(Also, if you're ever going to port this to somewhere CHAR_BIT 8
you should mask each addend with & 0xFF, and if you aren't you
could add #if CHAR_BIT != 8
#error something
#endif
somewhere.)
--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.

Sep 30 '07 #3
On Sun, 30 Sep 2007 04:14:19 -0700, William Pursell wrote:
On 30 Sep, 10:26, inftoc...@gmail.com wrote:
> I wrote a macro to read 8 bytes from a byte stream. Am not
sure if it is working ok. Can anyone please point out if there looks
to be a problem!

#define READ64(b) ( (uint64_t(*b)) << 56) + ((uint64_t(*(b+1))) << 48)
+ ((uint64_t(*(b+2))) << 40) +((uint64_t(*(b+3))) << 32)+((uint64_t(*(b
+4))) << 24) + (( uint64_t(*(b+5))) << 16) + (( uint64_t(*(b+6))) <<
8) + ( *(b+7))

Here b is a pointer to an unsigned char.

Lots of problems:

The endianness of the machine will have
an impact, certainly.
I think he did that to have the same format on all machines.
Otherwise he could use memcpy().
--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.

Sep 30 '07 #4
In article <46***********************@news.free.fr>
Charlie Gordon <ne**@chqrlie.orgwrote:
>You SHOULD use a function for this, possibly an inline function.
Indeed, in some cases it turns out to be faster to call a regular
(non-inline) function than to do the code in line (either with a
macro, or with an inline function). (As always, the way to find
out is to measure it. If you do a lot of these conversions, it
may be worth looking at your compiler's output. Many of today's
ILP32-LL64 compilers produce poor 64-bit shift-and-mask code, so
this sort of micro-optimization is occasionally worthwhile.)
>Function:

static inline uint64_t read64(unsigned char const *b) {
return ((uint64_t)b[0] << 56) | ((uint64_t)b[1] << 48) |
((uint64_t)b[2] << 40) | ((uint64_t)b[3] << 32) |
((uint64_t)b[4] << 24) | ((uint64_t)b[5] << 16) |
((uint64_t)b[6] << 8) | ((uint64_t)b[7] << 0);
}

If your compiler is not too smart, this might be better:

static inline uint64_t read64(unsigned char const *b) {
return ((uint64_t)(((uint32_t)b[0] << 24) | ((uint32_t)b[1] << 16) |
((uint32_t)b[2] << 8) | b[3]) << 32) |
(((uint32_t)b[4] << 24) | ((uint32_t)b[5] << 16) |
((uint32_t)b[6] << 8) | b[7]);
}
I find that applications that do this tend also to do 16 and/or
32-bit numbers, so for the second case, consider, e.g.:

static inline uint32_t read32(const unsigned char *b) {
return ((uint32_t)b[0] << 24) | ((uint32_t)b[1] << 16) |
((unsigned)b[2] << 8) | b[3];
}
static inline uint64_t read64(const unsigned char *b) {
return ((uint64_t)read32(b) << 32) | read32(b + 4);
}

One might also consider also using "unsigned long" and "unsigned
long long" instead of the (possibly overspecified) uint32_t and
uint64_t aliases; and read32 can call read16() twice, and read16()
can use plain "unsigned int" (as the code in read32 above shows).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Oct 1 '07 #5
"Thad Smith" <Th*******@acm.orga écrit dans le message de news:
47***********************@auth.newsreader.octanews .com...
William Pursell wrote:
>On 30 Sep, 10:26, inftoc...@gmail.com wrote:
>> I wrote a macro to read 8 bytes from a byte stream. Am not
sure if it is working ok. Can anyone please point out if there looks
to be a problem!

#define READ64(b) ( (uint64_t(*b)) << 56) + ((uint64_t(*(b+1))) << 48)
+ ((uint64_t(*(b+2))) << 40) +((uint64_t(*(b+3))) << 32)+((uint64_t(*(b
+4))) << 24) + (( uint64_t(*(b+5))) << 16) + (( uint64_t(*(b+6))) <<
8) + ( *(b+7))

Here b is a pointer to an unsigned char.
>What if a caller wants chars 2 through 9 from a buffer
and calls the macro as:
uint64_t x = READ64( b+2 );

It works properly. It would, however, fail with arguments such as b+1<<2.
No, you are mistaken: it does not work for b+2 either. It compiles, but
does not behave as intended. The MSB is read from b[0] and offset by 2.

--
Chqrlie.
Oct 1 '07 #6
"Charlie Gordon" <ne**@chqrlie.orga écrit dans le message de news:
47**********************@news.free.fr...
"Thad Smith" <Th*******@acm.orga écrit dans le message de news:
47***********************@auth.newsreader.octanews .com...
>William Pursell wrote:
>>On 30 Sep, 10:26, inftoc...@gmail.com wrote:
I wrote a macro to read 8 bytes from a byte stream. Am not
sure if it is working ok. Can anyone please point out if there looks
to be a problem!

#define READ64(b) ( (uint64_t(*b)) << 56) + ((uint64_t(*(b+1))) << 48)
+ ((uint64_t(*(b+2))) << 40) +((uint64_t(*(b+3))) << 32)+((uint64_t(*(b
+4))) << 24) + (( uint64_t(*(b+5))) << 16) + (( uint64_t(*(b+6))) <<
8) + ( *(b+7))

Here b is a pointer to an unsigned char.
>>What if a caller wants chars 2 through 9 from a buffer
and calls the macro as:
uint64_t x = READ64( b+2 );

It works properly. It would, however, fail with arguments such as
b+1<<2.

No, you are mistaken: it does not work for b+2 either. It compiles, but
does not behave as intended. The MSB is read from b[0] and offset by 2.
Actually, it might compile as C++, but one has to fix the cast syntax for it
to compile as C ;-)

Complete answers have been posted on a different thread.

--
Chqrlie.
Oct 1 '07 #7
Charlie Gordon wrote:
"Charlie Gordon" <ne**@chqrlie.orga écrit dans le message de news:
47**********************@news.free.fr...
>"Thad Smith" <Th*******@acm.orga écrit dans le message de news:
47***********************@auth.newsreader.octanew s.com...
>>William Pursell wrote:
On 30 Sep, 10:26, inftoc...@gmail.com wrote:
I wrote a macro to read 8 bytes from a byte stream. Am not
sure if it is working ok. Can anyone please point out if there looks
to be a problem!
>
#define READ64(b) ( (uint64_t(*b)) << 56) + ((uint64_t(*(b+1))) << 48)
+ ((uint64_t(*(b+2))) << 40) +((uint64_t(*(b+3))) << 32)+((uint64_t(*(b
+4))) << 24) + (( uint64_t(*(b+5))) << 16) + (( uint64_t(*(b+6))) <<
8) + ( *(b+7))
>
Here b is a pointer to an unsigned char.
What if a caller wants chars 2 through 9 from a buffer
and calls the macro as:
uint64_t x = READ64( b+2 );
It works properly. It would, however, fail with arguments such as
b+1<<2.
No, you are mistaken: it does not work for b+2 either. It compiles, but
does not behave as intended. The MSB is read from b[0] and offset by 2.

Actually, it might compile as C++, but one has to fix the cast syntax for it
to compile as C ;-)
Thanks for correcting my blunders!
--
Thad
Oct 6 '07 #8

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

Similar topics

25
by: Andrew Dalke | last post by:
Here's a proposed Q&A for the FAQ based on a couple recent threads. Appropriate comments appreciated X.Y: Why doesn't Python have macros like in Lisp or Scheme? Before answering that, a...
2
by: Pete | last post by:
In Access 95/97 I used to be able to create pull down menus (File,Edit ...) from a macro. It seems there used to be some wizard for that. However in Access 2000 it seems you have to build your...
8
by: Nick M | last post by:
Hello All, Excellent info here Thanks! I am very new to using access in general and I am on a learning curve. I'm trying to import an excel workbook (with worksheets) into an access db via a...
10
by: Karim Thapa | last post by:
Why following macro does not work? #define removebrace(x) x void Foo(int a, int b, char *txt, int d, int e); main() {
4
by: Garry Freemyer | last post by:
I'm trying to convert this macro to a c# function but I have a big problem. It's on the LEFT side of an assignment statement and I am extremely flustered over this one because I'm a little rusty...
6
by: Takeadoe | last post by:
Dear NG, Can someone assist me with writing the little code that is needed to run an update table query each time the database is opened? From what I've been able to glean from this group, the...
2
by: Senthil | last post by:
Hi All I need to create an Excel report and create a command button and have to run a macro on the click event that will print all the pages in the Excel workbook. I am able to create the report...
0
by: =?Utf-8?B?TGV0emRvXzF0?= | last post by:
I'd like to create a Macro that will sort some raw data, apprx 20k lines, remove some lines based upon a condition in a certain column. Then copy this data into a new spreadsheet and sort the ...
1
by: skennd | last post by:
Hello, All your help is appreciated in this problem. I am running a macro to execute certain queries and the macro is started to run by a windows task scheduler. However, after the macro runs...
2
by: Stratocaster | last post by:
Hello, and thank you for any help in advance. I need help determining if any commands exist in VB (Excel macro style) that can enable a user to select cells and run a macro which performs...
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: 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
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?
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
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
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...

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.