472,985 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,985 software developers and data experts.

Detecting support for unaligned words

fox
Maybe this is not the best group to ask this question,
but I don't know a better one.

I'm looking for a *portable* program in C (I mean source code)
to detect whether unaligned word access is:
a. handled by the main processor (e.g. x86)
b. not supported (e.g. Sparc running Solaris)
c. emulated in software (e.g. Alpha running Linux)

By "unaligned word access" I mean access to a 16-bit word
at an odd address or to a 32-bit word at an address that
is not multiple of 4.

Cases 'b' and 'c' don't need to differentiated.

Thanks,
Piotr

Nov 15 '05 #1
7 2297
fo*@scene.pl wrote:
Maybe this is not the best group to ask this question,
but I don't know a better one.

I'm looking for a *portable* program in C (I mean source code)
to detect whether unaligned word access is:
a. handled by the main processor (e.g. x86)
b. not supported (e.g. Sparc running Solaris)
c. emulated in software (e.g. Alpha running Linux)

By "unaligned word access" I mean access to a 16-bit word
at an odd address or to a 32-bit word at an address that
is not multiple of 4.

Cases 'b' and 'c' don't need to differentiated.


There is no such program -- you of course can do nice little
tricks like memmove()ing inside an array by 1, accessing the
old address+1 and hoping for the same result. However, there
is no guarantee that this works always. In addition, it may
be possible but needs a compiler extension etc.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #2
On 2005-11-07 15:19:45 -0500, fo*@scene.pl said:
Maybe this is not the best group to ask this question,
but I don't know a better one.

I'm looking for a *portable* program in C (I mean source code)
to detect whether unaligned word access is:
a. handled by the main processor (e.g. x86)
b. not supported (e.g. Sparc running Solaris)
c. emulated in software (e.g. Alpha running Linux)


No such program exists (no could it).
--
Clark S. Cox, III
cl*******@gmail.com

Nov 15 '05 #3
fo*@scene.pl wrote:
I'm looking for a *portable* program in C (I mean source code)
to detect whether unaligned word access is:
a. handled by the main processor (e.g. x86)
b. not supported (e.g. Sparc running Solaris)
c. emulated in software (e.g. Alpha running Linux)

By "unaligned word access" I mean access to a 16-bit word
at an odd address or to a 32-bit word at an address that
is not multiple of 4.


it's not perfect, but should fail on the conservative side in most
cases.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int
main(int argc, char **argv)
{
FILE *fp;
char buf[1024];

if (system("uname -m > myfilewithmachineinfo") == 0) {
fp = fopen("myfilewithmachineinfo", "r");
if (fp) {
fgets(buf, sizeof buf, fp);
fclose(fp);
if (strstr(buf, "i386")) {
printf("Unaligned access is ok\n");
return 0;
} else if (strstr(buf, "alpha")) {
printf("Unaligned access is done in software\n");
return 0;
}
}
}
printf("Unaligned access is not ok\n");
}

Nov 15 '05 #4
fox
I'm disappointed with your answers.
Here's a program that differentiates case 'b' from 'a' and 'c':

#include <stdlib.h>
#include <signal.h>
#include <setjmp.h>
jmp_buf jmpbuf;
void bus_err()
{
longjmp(jmpbuf, 1);
}
int main(void)
{
#ifndef SIGBUS
return 0;
#else
int l[2];
if (setjmp(jmpbuf) == 0) {
signal(SIGBUS, bus_err);
*((int *) ((char *) l + 1)) = 1;
signal(SIGBUS, SIG_DFL);
return 0;
}
else {
signal(SIGBUS, SIG_DFL);
return 1;
}
#endif
}

Nov 15 '05 #5
In article <11**********************@g47g2000cwa.googlegroups .com>,
<fo*@scene.pl> wrote:
I'm disappointed with your answers.
Here's a program that differentiates case 'b' from 'a' and 'c': signal(SIGBUS, bus_err);


You asked for a portable standard program.

http://www.opengroup.org/onlinepubs/.../signal.h.html

"The ISO C standard only requires the signal names SIGABRT, SIGFPE,
SIGILL, SIGINT, SIGSEGV, and SIGTERM to be defined."

You do check #ifndef SIGBUS but you return a constant value
if it is not defined, which is NOT a portable way of detecting
whether unaligned accesses are permitted or not.

Your original posting also asked for three cases to be differentiated
and said that the second (no support) and third (software support)
need not be differentiated, but your program cannot differentiate
the third case from the first, and so fails the specifications that you
yourself posted.

You are also mis-using SIGBUS according to opengroup:

SIGBUS Access to an undefined portion of a memory object.

but in your program, the portion was defined, but was being (potentially)
misaddressed. That case is closer to

SIGSEGV Invalid memory reference.
If you found experimentally that you needed to use SIGBUS on
a system instead of SIGSEGV, then clearly on that system there
are system dependancies that make the usage non-portable (SIGSEGV
being the ISO C signal that would appear to be appropriate.)
ISO C says, with respect to the aborting signals,

An implementation need not generate any of these signals, except as
a result of explicit calls to the raise function.

The standard also indicates,

If and when the function returns, if the value of sig is SIGFPE,
SIGILL, SIGSEGV, or any other implementation-defined value
corresponding to a computational exception, the behaviour is
undefined; otherwise the program will resume execution at the
point it was interrrupted.

I would need to look somewhat further to see whether the state
that is restored by longjmp() includes clearing the undefined
state implied by the above paragraph.
--
If you lie to the compiler, it will get its revenge. -- Eric Sosman
Nov 15 '05 #6
fox
> You do check #ifndef SIGBUS but you return a constant value
if it is not defined, which is NOT a portable way of detecting
whether unaligned accesses are permitted or not.
That's right, I conservatively assume that unaligned access is not
supported there.

This is of little importance for me, because this POSIX standard you
pointed me at
says that SIGBUS "shall be supported on all implementations".
Your original posting also asked for three cases to be differentiated
and said that the second (no support) and third (software support)
need not be differentiated, but your program cannot differentiate
the third case from the first, and so fails the specifications that you
yourself posted.
I think I clearly indicated that this program is *not* the one I want.
But at least it does the job partially, so I posted it, so maybe
someone
can improve it rather than start from scratch.
You are also mis-using SIGBUS according to opengroup
No. You can read on this page:
"Signal: SIGBUS, Reason: Invalid address alignment."
I would need to look somewhat further to see whether the state
that is restored by longjmp() includes clearing the undefined
state implied by the above paragraph.


Maybe it's better to use exit() directly in the signal handler.
I didn't write this program.

Nov 15 '05 #7
In article <11**********************@g43g2000cwa.googlegroups .com>,
<fo*@scene.pl> wrote:
[omitting the attribution to me]
You do check #ifndef SIGBUS but you return a constant value
if it is not defined, which is NOT a portable way of detecting
whether unaligned accesses are permitted or not.
That's right, I conservatively assume that unaligned access is not
supported there. This is of little importance for me, because this POSIX standard you
pointed me at
says that SIGBUS "shall be supported on all implementations".
Your original posting emphasized "*portable*" C. POSIX is not portable
within the meaning of comp.lang.c . POSIX is an operating system interface
specification. Windows, for example, did not make a serious attempt
at POSIX until Windows XP (though W2K was getting there.)

When you ask for "portable" code within comp.lang.c, you are going to
get answers that involve ONLY what may be safely done with the C89 or
C99 standards. If the task you ask for cannot be done without OS
extensions, then if you emphasized "portable" then our answer will be
"It can't be done", and if you don't emphasize "portable" then our
answer will be "you can't do it portably, try OS-specific newsgroups."
I think I clearly indicated that this program is *not* the one I want.
No, you wrote:
I'm disappointed with your answers.
Here's a program that differentiates case 'b' from 'a' and 'c':

That is not a "clear" indication that the program was not the one you
wanted.
But at least it does the job partially, so I posted it, so maybe someone
can improve it rather than start from scratch.
Well, if the program supposedly differentiates no support from
hardware support, then if it worked then the only differentiation
left would be software support. If, though, one has software support
of unaligned accesses, then because the access would complete
in time, the only way to differentiate the cases (besides a hardware
micro-instruction level trace) would be to do -very- careful
timing analyses. Timers are not part of portable C, and high resolution
timers are not required by POSIX.1.

You are also mis-using SIGBUS according to opengroup

No. You can read on this page:
"Signal: SIGBUS, Reason: Invalid address alignment."
Dang inconsistancies in POSIX documentation.

But at least it does the job partially, so I posted it, so maybe someone
can improve it rather than start from scratch.


What you are asking for CANNOT reliably be done within ISO C without
operating system extensions such as POSIX.1 .. and differentiating
software emulation from direct hardware support is particularily
problematic. For example, what should the answer be for vmware
emulating an x86 running Windows but really running on (say) a Mac OS X
with PowerPC chip? There could be a hardware instruction (that the
compiler might even have used) in the x86 machine code, but since
the program is emulating -everything- on the PowerPC, that's a software
emulation of what would otherwise be a hardware facility... no matter
which answer the program gives, the program would be wrong.
--
Programming is what happens while you're busy making other plans.
Nov 15 '05 #8

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

Similar topics

3
by: raptor | last post by:
hi, how to detect opera..it seems that even opera8 doesnt support xmlhttp fully (.i.e. sendRequestHeader). I ask this 'cause opera seems to mimic IE, at least in the preferences ?! I havent...
18
by: Pavel A. | last post by:
Hello, What is equivalent of __unaligned attribute in GNU C? I've searched for some time and it seems that it just does not exist... /* For HP and new Microsoft compilers it works this way:...
7
by: Steven Jones | last post by:
Up until now, I was under the impression that when one talks about data alignment, what one means is at what address some data is stored. For instance, if we write unsigned int x ; assuming...
0
by: June Li | last post by:
H I got a problem with detecting sound card with Windows xp I am working on an application to test microphone. First I need detect if there is a sound card installed (or integrated audio...
5
by: Steven Woody | last post by:
hi, i get a struct as below, typedef struct { uint16_t id; long offset; } foo_t;
79
by: VK | last post by:
I wandering about the common proctice of some UA's producers to spoof the UA string to pretend to be another browser (most often IE). Shouldn't it be considered as a trademark violation of the...
3
by: dbuchanan | last post by:
How, at rundime, do I capture the fact that the parametrized query that fills a CheckBoxList results in an empty set. When the dataset is empty the CheckBoxList does not appear. I would like to...
15
by: RobG | last post by:
When using createEvent, an eventType parameter must be provided as an argument. This can be one of those specified in DOM 2 or 3 Events, or it might be a proprietary eventType. My problem is...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.