473,327 Members | 1,979 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,327 software developers and data experts.

align to double or to long double?

I have rewrote the malloc() function of K&R2 chapter 8.7

typedef long Align;
^^^^
Here, should I write 'long', 'double' or 'long double'?

I know that in my pc+compiler sizeof(long)=4, sizeof(double)=8
and sizeof(long double)=10
(if 'long' or 'double' sizeof(Header)=8 if 'long double'
sizeof(Header)=12)

Can my x86 cpu read an array of long double aligned with double?

union header {
struct { union header* ptr;
unsigned size;
}s;
Align x;
};

typedef union header Header;
static Header base;
static Header *freep=NULL;
Nov 14 '05 #1
3 2572

"RoSsIaCrIiLoIA" <n@esiste.ee> wrote in message
news:r3********************************@4ax.com...
I have rewrote the malloc() function of K&R2 chapter 8.7

typedef long Align;
^^^^
Here, should I write 'long', 'double' or 'long double'?

I know that in my pc+compiler sizeof(long)=4, sizeof(double)=8
and sizeof(long double)=10
(if 'long' or 'double' sizeof(Header)=8 if 'long double'
sizeof(Header)=12) Taking up 12 bytes for a 10-byte type helps only in that it sets up padding
for a following 4-byte type. It doesn't align a following 8-byte or larger
type properly.
Can my x86 cpu read an array of long double aligned with double?

Yes, the hardware fixes up such mis-alignments, and the performance
consequences aren't a Standard C issue. One might argue that malloc()
should return a pointer which will allow 128-bit data types to work, at the
same time giving full performance for long double. Practice hasn't evolved
that way, requiring instead that malloc() be discarded in favor of
implementation-dependent replacements. The argument is more often made that
malloc() shouldn't even have 8-byte alignment, given that the hardware has
some support for fixing up alignments. Standard C says something about
"suitability" of alignment. There seems room for more than one view of what
that means, but anyway there is little advantage to offering alignments
other than 8- or (some implementations 4-) or 16- bytes.
Nov 14 '05 #2
On Sun, 26 Sep 2004 06:51:22 GMT, "Tim Prince"
<tp*****@nospamcomputer.org> wrote:
"RoSsIaCrIiLoIA" <n@esiste.ee> wrote in message
news:r3********************************@4ax.com.. .
I have rewrote the malloc() function of K&R2 chapter 8.7

typedef long Align;
^^^^
Here, should I write 'long', 'double' or 'long double'?

I know that in my pc+compiler sizeof(long)=4, sizeof(double)=8
and sizeof(long double)=10
(if 'long' or 'double' sizeof(Header)=8 if 'long double'
sizeof(Header)=12)

Taking up 12 bytes for a 10-byte type helps only in that it sets up padding
for a following 4-byte type. It doesn't align a following 8-byte or larger
type properly.

Can my x86 cpu read an array of long double aligned with double?

Yes, the hardware fixes up such mis-alignments, and the performance
consequences aren't a Standard C issue. One might argue that malloc()
should return a pointer which will allow 128-bit data types to work, at the
same time giving full performance for long double. Practice hasn't evolved
that way, requiring instead that malloc() be discarded in favor of
implementation-dependent replacements. The argument is more often made that
malloc() shouldn't even have 8-byte alignment, given that the hardware has
some support for fixing up alignments. Standard C says something about
"suitability" of alignment. There seems room for more than one view of what
that means, but anyway there is little advantage to offering alignments
other than 8- or (some implementations 4-) or 16- bytes.


Thank you
so 8 is ok?
Nov 14 '05 #3

"RoSsIaCrIiLoIA" <n@esiste.ee> wrote in message
news:h3********************************@4ax.com...
On Sun, 26 Sep 2004 06:51:22 GMT, "Tim Prince"
<tp*****@nospamcomputer.org> wrote:
"RoSsIaCrIiLoIA" <n@esiste.ee> wrote in message
news:r3********************************@4ax.com.. .
I have rewrote the malloc() function of K&R2 chapter 8.7

typedef long Align;
^^^^
Here, should I write 'long', 'double' or 'long double'?

I know that in my pc+compiler sizeof(long)=4, sizeof(double)=8
and sizeof(long double)=10
(if 'long' or 'double' sizeof(Header)=8 if 'long double'
sizeof(Header)=12)

Taking up 12 bytes for a 10-byte type helps only in that it sets up paddingfor a following 4-byte type. It doesn't align a following 8-byte or largertype properly.

Can my x86 cpu read an array of long double aligned with double?

Yes, the hardware fixes up such mis-alignments, and the performance
consequences aren't a Standard C issue. One might argue that malloc()
should return a pointer which will allow 128-bit data types to work, at thesame time giving full performance for long double. Practice hasn't evolvedthat way, requiring instead that malloc() be discarded in favor of
implementation-dependent replacements. The argument is more often made thatmalloc() shouldn't even have 8-byte alignment, given that the hardware hassome support for fixing up alignments. Standard C says something about
"suitability" of alignment. There seems room for more than one view of whatthat means, but anyway there is little advantage to offering alignments
other than 8- or (some implementations 4-) or 16- bytes.


Thank you
so 8 is ok?

8-byte alignment is fine for supporting data types T of sizeof(T) <=8, and
there is no advantage in 10- or 12-byte alignment for supporting a 10-byte
long double.
Nov 14 '05 #4

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

Similar topics

5
by: Daniel Rudy | last post by:
How does one covert a interger number in a unsigned long long int (64-bit) to long double (80-bit) storage? I looked at math.h and I found function that convert double to long long, but didn't...
10
by: Bryan Parkoff | last post by:
The guideline says to use %f in printf() function using the keyword float and double. For example float a = 1.2345; double b = 5.166666667; printf("%.2f\n %f\n", a, b);
69
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after...
67
by: lcw1964 | last post by:
This may be in the category of bush-league rudimentary, but I am quite perplexed on this and diligent Googling has not provided me with a clear straight answer--perhaps I don't know how to ask the...
52
by: lcw1964 | last post by:
Greetings, all, I am trying to port a little bit of math code to gcc, that in the original version used the long double version of several functions (in particular, atanl, fabsl, and expl). I...
11
by: Ole Nielsby | last post by:
First, sorry if this is off-topic, not strictly being a C++ issue. I could not find a ng on numerics or serialization and I figure this ng is the closest I can get. Now the question: I want...
14
by: artifact.one | last post by:
It'd be really pleasant (in my opinion) if the next revision of the C language actually allowed some portable control over data alignment. Compiler-specific mechanisms for this stuff are so...
10
by: ratcharit | last post by:
Currently using cosine function in math.h Currently I get: 1 = cos(1e^-7) Is there another way for cos to return value of high accuracy say: 0.999999 = cos(1e^-7)
5
by: Max2006 | last post by:
Hi, I have a TreeView and this is my node style: <asp:TreeNodeStyle ForeColor = "#000000" Font-Size="9px" Font-Bold="false" Width="100px" NodeSpacing="3px" /> The problem is the expand...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.