473,775 Members | 2,570 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2354
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 > myfilewithmachi neinfo") == 0) {
fp = fopen("myfilewi thmachineinfo", "r");
if (fp) {
fgets(buf, sizeof buf, fp);
fclose(fp);
if (strstr(buf, "i386")) {
printf("Unalign ed access is ok\n");
return 0;
} else if (strstr(buf, "alpha")) {
printf("Unalign ed access is done in software\n");
return 0;
}
}
}
printf("Unalign ed 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************ **********@g47g 2000cwa.googleg roups.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************ **********@g43g 2000cwa.googleg roups.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
1857
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 used opera till now, but it seems very buggy piece of software !! I have one very annoyng problem, fighting already ~4 hours. I found that if I use something like this in table (test are
18
10074
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: long_pointer = (some unaligned address) long_value = (* (__unaligned long *) long_pointer );
7
3731
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 that sizeof(unsigned int) is 4 then &x will evaluate to some address the value of which will be a multiple of 4. The reasoning is analogous for the other fundamental data types supported by a given compiler.
0
2600
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 device). In a system without sound card (no full onboard audio support either), I use waveInGetNumDevs() to test if there is a sound card, it returns 1. And waveInGetDevCaps() returns no error. But it will fail when I test recording Guess the system...
5
13924
by: Steven Woody | last post by:
hi, i get a struct as below, typedef struct { uint16_t id; long offset; } foo_t;
79
3794
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 relevant name owner? If I make a whisky and call it "Jack Daniels", I most probably will have some serious legal problems. "Mozilla" partially appeared because NCSA stopped them from using "Mosaic" in the UA string. Is it some different...
3
2789
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 properly inform the user of the fact. How is ths done? Thank you, Doug
15
4233
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 testing for support of particular eventTypes - the DOM 2 Events Interface DocumentEvent says that if the eventType is not supported, it throws a DOM exception. This makes testing rather tough - if you try something like: if (document &&...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10268
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10048
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9916
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8939
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6718
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5486
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4017
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2853
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.