473,472 Members | 2,140 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

testing for non-negativity

An
Dear all,

I need to test the elements of an array of chars for non-negativity.
I've read the following: "On most architectures testing for
non-negativity can be done several bytes at a time." Is this possible
with C as well? Is there a function to do so? First, I thought I
could use the function memchr, but this is not specific for
non-negativity and I'm not aware if it searches several bytes at a
time. I hope someone can help me out....

Kind regards and thanks in advance,

An
Nov 14 '05 #1
7 1367
An @ 2004-07-14:
I need to test the elements of an array of chars for non-negativity.
I've read the following: "On most architectures testing for
non-negativity can be done several bytes at a time." Is this possible
with C as well?


I'd imagine you could map the sign bit of a couple of chars into a
different type in order to check for any of them being one, which
would mean one of the elements is negative.

However the machine independent solution does not gain anything
over a straight implementation, except if you're using a very
intelligent compiler, but relying on a specific compiler wouldn't
be independent anymore so...

A machine dependent solution, which I'm posting for the purely
academic reason of informing people what they shouldn't do, could
look similar to this:

if(*(unsigned short *)(a+i) & 0x8080)

--
Danny
Nov 14 '05 #2
"Daniel Fischer" <sp**@danny.homeunix.net> wrote in message
news:sl*****************@usenet.danny.homeunix.net ...
An @ 2004-07-14:
I need to test the elements of an array of chars for non-negativity.
Note that plain char need not be signed.
I've read the following: "On most architectures testing for
non-negativity can be done several bytes at a time." Is this
possible with C as well?


I'd imagine you could map the sign bit of a couple of chars into a
different type in order to check for any of them being one, which
would mean one of the elements is negative.

However the machine independent solution does not gain anything
over a straight implementation, except if you're using a very
intelligent compiler, but relying on a specific compiler wouldn't
be independent anymore so...

A machine dependent solution, which I'm posting for the purely
academic reason of informing people what they shouldn't do, could
look similar to this:

if(*(unsigned short *)(a+i) & 0x8080)


You can easily confirm specifics and choose either coarse at your leisure. The only
difficulty is assuming the buffer to be scanned is suitably aligned for the longer type,
but that can be resolved with c/re/malloc().

#include <string.h>
#include <limits.h>

/*
// precondition: p is aligned for unsigned long
*/
int negative_present(const signed char *p, size_t n)
{
/*
// unpadded twos complement signed char?
*/
if (SCHAR_MAX - (unsigned) SCHAR_MIN == UCHAR_MAX)
{
unsigned long ul;
size_t i;

for (ul = -1, i = 1; i < sizeof ul; i++)
ul = ul >> (CHAR_BIT - 1) >> 1;

/*
// unpadded unsigned long?
*/
if (ul == UCHAR_MAX)
{
const unsigned long *ulp = (const unsigned long *) p;
signed char sc[sizeof ul];

for (i = 0; i < sizeof sc; i++)
sc[i] = SCHAR_MIN;

memcpy(&ul, sc, sizeof ul);

i = n / sizeof ul;
n -= i * sizeof ul;

while (i--)
if (*ulp++ & ul)
return 1;

p = (const signed char *) ulp;
}
}

while (n--)
if (*p++ < 0)
return 1;

return 0;
}

I'm not saying it should be done (or should be done this way), merely that it can be done.

--
Peter
Nov 14 '05 #3
"Peter Nilsson" <ai***@acay.com.au> wrote in message
news:40******@news.rivernet.com.au...
"Daniel Fischer" <sp**@danny.homeunix.net> wrote in message
news:sl*****************@usenet.danny.homeunix.net ...
An @ 2004-07-14:
I need to test the elements of an array of chars for non-negativity.
Note that plain char need not be signed.
I've read the following: "On most architectures testing for
non-negativity can be done several bytes at a time." Is this
possible with C as well?


I'd imagine you could map the sign bit of a couple of chars into a
different type in order to check for any of them being one, which
would mean one of the elements is negative.


You can easily confirm specifics and choose either coarse at your
leisure. The only difficulty is assuming the buffer to be scanned is
suitably aligned for the longer type, but that can be resolved with
c/re/malloc().


And better resolved by casting the pointer to a suitable integer type and
checking its alignment. Since the method is platform-specific already, you
might as well.

Alex
Nov 14 '05 #4
One way is strcmp(newsgroup,"comp.lang.c")!=0

an*********@student.kuleuven.ac.be (An) wrote:
# Dear all,
#
# I need to test the elements of an array of chars for non-negativity.
# I've read the following: "On most architectures testing for
# non-negativity can be done several bytes at a time." Is this possible
# with C as well? Is there a function to do so? First, I thought I
# could use the function memchr, but this is not specific for
# non-negativity and I'm not aware if it searches several bytes at a
# time. I hope someone can help me out....

For an eight-bit byte twos complement, you can do
(0x80&character)!=0
Depending on alignment and integer sizes, you can do
(0x8080808080808080 & ((long long*)string)[i])!=0
but this is a machine dependent solution. You can construct the
0x80...80 constant and some of the other details by consulting
sizeof and limits.h, but much of this remains machine dependent.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Leave it to the Catholics to destroy existence.
Nov 14 '05 #5
On 14 Jul 2004 09:06:36 GMT, Daniel Fischer <sp**@danny.homeunix.net>
wrote in comp.lang.c:
An @ 2004-07-14:
I need to test the elements of an array of chars for non-negativity.
I've read the following: "On most architectures testing for
non-negativity can be done several bytes at a time." Is this possible
with C as well?


I'd imagine you could map the sign bit of a couple of chars into a
different type in order to check for any of them being one, which
would mean one of the elements is negative.

However the machine independent solution does not gain anything
over a straight implementation, except if you're using a very
intelligent compiler, but relying on a specific compiler wouldn't
be independent anymore so...

A machine dependent solution, which I'm posting for the purely
academic reason of informing people what they shouldn't do, could
look similar to this:

if(*(unsigned short *)(a+i) & 0x8080)


No, it is not machine dependent as far as C is concerned. It is
undefined behavior to access objects with an lvalue of a different
type, with the exception of accessing any object as an array of
unsigned characters.

It could be doubly undefined if a+i does not have the proper alignment
for pointer to short.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #6

On Wed, 14 Jul 2004, Jack Klein wrote:

Daniel Fischer <sp**@danny.homeunix.net> wrote in comp.lang.c:
An @ 2004-07-14:
I need to test the elements of an array of chars for non-negativity.
I've read the following: "On most architectures testing for
non-negativity can be done several bytes at a time." Is this possible
with C as well?
<snip> A machine dependent solution, which I'm posting for the purely
academic reason of informing people what they shouldn't do, could
look similar to this:

if(*(unsigned short *)(a+i) & 0x8080)


No, it is not machine dependent as far as C is concerned. It is
undefined behavior [...]


s/machine dependent/implementation dependent/ and his statement
is correct. Note that
"implementation-dependent" != "implementation-defined".

-Arthur,
from ghoulies and ghosties,
and long-leggedy beasties,
and language lawyers,
Nov 14 '05 #7
"Alex Fraser" <me@privacy.net> wrote in message news:2l************@uni-berlin.de...
"Peter Nilsson" <ai***@acay.com.au> wrote in message
news:40******@news.rivernet.com.au...
"Daniel Fischer" <sp**@danny.homeunix.net> wrote in message
news:sl*****************@usenet.danny.homeunix.net ...
An @ 2004-07-14:
> I need to test the elements of an array of chars for non-negativity.


Note that plain char need not be signed.
> I've read the following: "On most architectures testing for
> non-negativity can be done several bytes at a time." Is this
> possible with C as well?

I'd imagine you could map the sign bit of a couple of chars into a
different type in order to check for any of them being one, which
would mean one of the elements is negative.


You can easily confirm specifics and choose either coarse at your
leisure. The only difficulty is assuming the buffer to be scanned is
suitably aligned for the longer type, but that can be resolved with
c/re/malloc().


And better resolved by casting the pointer to a suitable integer type and
checking its alignment. Since the method is platform-specific already, you
might as well.


The routine I showed had the same optimisation but was not dependant on implementation, so
I don't know why you're encouraging a line which is known to fail even for some twos
complement machines.

--
Peter
Nov 14 '05 #8

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

Similar topics

4
by: Edvard Majakari | last post by:
Hi, I just found py.test and converted a large unit test module to py.test format (which is actually almost-no-format-at-all, but I won't get there now). Having 348 test cases in the module and...
2
by: Durai | last post by:
Hello All, How to test the "Multiuser testing" in PostgreSQL?. I used the apache bench "ab" tool for this one. The following command execute the "test.php" 50 times concurrently. $ ab -c 1...
19
by: lihua | last post by:
Hi, Group! I got one question here: We all know that fclose() must be called after file operations to avoid unexpected errors.But there are really cases when you forget to do that!Just like...
72
by: Jacob | last post by:
I have compiled a set og unit testing recommendations based on my own experience on the concept. Feedback and suggestions for improvements are appreciated: ...
2
by: gamesforums | last post by:
Hi! I've been programming in VB.Net and done testing with TypeMock and MbUnit (Testdriven.net) for about 2months now at work. I can see that my testing skills and particular my knowledge about...
24
by: David | last post by:
Hi list. What strategies do you use to ensure correctness of new code? Specifically, if you've just written 100 new lines of Python code, then: 1) How do you test the new code? 2) How do...
4
by: David | last post by:
Hi list. Do test-driven development or behaviour-driven development advocate how to do higher-level testing than unit testing? types of testing: unit integration system
11
by: VK | last post by:
In the continuation of the discussion at "Making Site Opaque -- This Strategy Feasible?" and my comment at http://groups.google.com/group/comp.lang.javascript/msg/b515a4408680e8e2 I have...
7
by: alito | last post by:
Hi all, I am new to using packages to group my modules. I can't figure out how to run a module that uses relative imports without writing a wrapper that imports that module. Everything I try...
21
by: =?ISO-8859-1?Q?Fad=A5?= | last post by:
Hello guys, I want to do kinda of an A/B split testing on a website I run. I just created a new version but I need to keep both version running and see which one will perform better. First, I'm...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
1
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...
0
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,...
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.