Reply to topic  [ 3 posts ] 
Support for unsigned integers 
Author Message
Yorick Master

Joined: Mon Nov 22, 2004 9:43 am
Posts: 354
Location: Livermore, CA, USA
Post Support for unsigned integers
In response to the post I have added support for unsigned integers with the file i/unsigned.i. The functions are autoloaded. They are:

Six comparison functions: u_gt(x,y), u_lt(x,y), u_ge(x,y), u_le(x,y), u_eq(x,y), u_ne(x,y)

Three arithmetic functions: u_shr(x,n), u_div(x,y), u_mod(x,y)

Two explicit cast functions: u_cast(x,type), s_char(x)

Note that u_div and u_mod accept a third argument as output, so that a single call can return both quotient and remainder.

A large fraction of the uses of unsigned types in C and C++ are incorrect uses of these types as assertions that the value cannot be negative. This usage is incorrect, because no such assertion can be verified -- in fact, no bit pattern represents a negative value, so such an assertion is meaningless.

The harm comes from the fact that signed values are implicitly cast to unsigned values whenever they occur together in an algebraic expression. Division or modulo operations then do not do what you expect, a bug which is difficult to see.

The only correct uses of unsigned types are (1) when you explicitly want all operations involving them to be the unsigned operations, or (2) when you know that the values are non-negative AND that you need the final factor of two in range.

In yorick, the overhead of so many additional primitive data types is (in my judgment) too high, so the support in unsigned.i is to provide as functions all of the operations which differ between signed and unsigned types. The comment at the top of unsigned.i also has advice for what to do about printing unsigned values, which still has no direct support.

Feel free to post disagreements with this position as replies to this announcement.

Sat Nov 21, 2009 3:21 pm
Yorick Master

Joined: Sun Sep 26, 2004 10:33 am
Posts: 150
Location: Australia

my experience with uint is that I don't really need support with uint operations, as long as I have IO. I mean, what I really need for uint is to be able to read them and write them from and to binary files (e.g. uint in fits files).
As far as I understand, what you propose does not add a new type (?)

Being able to do operations/comparison on them is a nice feature, but one I will probably not use often (although it might come handy I guess for operation on very large uint arrays, just to avoid casting to long).

Tue Nov 24, 2009 12:12 am
Profile WWW
Yorick Master

Joined: Mon Nov 22, 2004 9:43 am
Posts: 354
Location: Livermore, CA, USA
I think you'll find that if you read and write uint from binary files using yorick's signed int type that it will generally work. The clumsy part is finding the type with the matching number of bytes on various platforms (for instance, you probably want to pick int rather than long, so that you get 4 byte integers even on 64 bit platforms).

The int/uint clumsiness during binary IO is something that I DO intend to fix. For many years I have been sketching an improved binary IO package for yorick. The package itself will certainly support correct casting for unsigned integer types as they are transferred to and from disk. The integration with yorick interpreted variables will be a little less smooth, since, as I said, I have no plans to support unsigned integer variables (beyond unsigned.i).

So my vision is, that when you are reading or writing to a file whose structure is not automatically specified (as with the yorick save command), but comes from elsewhere (like the FITS format standard), everything should go smoothly -- I don't think the interpreter needs to know whether you think 0x8fffe is -2 or 65534, as long as you have the operations defined in unsigned.i to say explicitly what you mean.

But you do raise an interesting point about what the interface for telling a file handle that you want to declare an unsigned variable should look like. I'm pretty far along in designing the new binary file interface. Hopefully I can post something about it (or its precursors) in the next couple of months, and we can pick up this thread again. Now that NIF operations have begun in earnest, there is increasing demand for a more integrated HDF5 reader than the one you wrote (which people here have started to use), which is pressuring me to actually do something finally.

Mon Nov 30, 2009 4:30 pm
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.