Hello, I would like to create a numarray array from a C buffer of 'int' but I don't quite know how to handle different 'int' representation (16 or 32 bits). For instante should I use : int a[10][20]; x = NA_NewArray(a, tInt16, 2, 10, 20); or : int a[10][20]; x = NA_NewArray(a, tInt32, 2, 10, 20); Should I use some macro to determine size of int ? Thanks in advance for any help,
On Mon, 2005-06-27 at 02:33 +0200, Nicolas Pernetty wrote:
Hello,
I would like to create a numarray array from a C buffer of 'int' but I don't quite know how to handle different 'int' representation (16 or 32 bits).
On the platforms we test on at least, int == Int32. This is even true for the 64-bit platforms I've seen.
For instante should I use :
int a[10][20]; x = NA_NewArray(a, tInt16, 2, 10, 20);
or : int a[10][20]; x = NA_NewArray(a, tInt32, 2, 10, 20);
Should I use some macro to determine size of int ?
You can do that with: sizeof(int) Regards, Todd
On Sun, 26 Jun 2005 21:28:25 -0400, Todd Miller
I would like to create a numarray array from a C buffer of 'int' but I don't quite know how to handle different 'int' representation (16 or 32 bits).
On the platforms we test on at least, int == Int32. This is even true for the 64-bit platforms I've seen.
For instante should I use :
int a[10][20]; x = NA_NewArray(a, tInt16, 2, 10, 20);
or : int a[10][20]; x = NA_NewArray(a, tInt32, 2, 10, 20);
Should I use some macro to determine size of int ?
You can do that with: sizeof(int)
Ok so If I want to make a really multiplatforms program, I should type : int a[10][20]; if (sizeof(**a)==4) x = NA_NewArray(a, tInt32, 2, 10, 20); else x = NA_NewArray(a, tInt16, 2, 10, 20); This is the recommended way ? Thanks !
Nicolas Pernetty
On the platforms we test on at least, int == Int32. This is even true for the 64-bit platforms I've seen.
Didn't some Crays have 64bit ints? They definitely had 64bit float and 128bit double.
Ok so If I want to make a really multiplatforms program, I should type :
int a[10][20]; if (sizeof(**a)==4) x = NA_NewArray(a, tInt32, 2, 10, 20); else x = NA_NewArray(a, tInt16, 2, 10, 20);
Maybe you should right away include a test for 8byte as well. Greetings, Jochen -- Einigkeit und Recht und Freiheit http://www.Jochen-Kuepper.de Liberté, Égalité, Fraternité GnuPG key: CC1B0B4D (Part 3 you find in my messages before fall 2003.)
On Mon, 27 Jun 2005 11:27:40 +0200, Jochen Küpper
On the platforms we test on at least, int == Int32. This is even true for the 64-bit platforms I've seen.
Didn't some Crays have 64bit ints? They definitely had 64bit float and 128bit double.
Ok so If I want to make a really multiplatforms program, I should type :
int a[10][20]; if (sizeof(**a)==4) x = NA_NewArray(a, tInt32, 2, 10, 20); else x = NA_NewArray(a, tInt16, 2, 10, 20);
Maybe you should right away include a test for 8byte as well.
On this page you have some exotic 'int' : http://www.doc.ic.ac.uk/lab/secondyear/cstyle/node20.html The problem was no as simple as it seems to be. I'll go with the sizeof trick and some error message if I'm dealing with an exotic 'int'...
On Mon, 2005-06-27 at 09:36 +0200, Nicolas Pernetty wrote:
On Sun, 26 Jun 2005 21:28:25 -0400, Todd Miller
wrote : I would like to create a numarray array from a C buffer of 'int' but I don't quite know how to handle different 'int' representation (16 or 32 bits).
On the platforms we test on at least, int == Int32. This is even true for the 64-bit platforms I've seen.
For instante should I use :
int a[10][20]; x = NA_NewArray(a, tInt16, 2, 10, 20);
or : int a[10][20]; x = NA_NewArray(a, tInt32, 2, 10, 20);
Should I use some macro to determine size of int ?
You can do that with: sizeof(int)
Ok so If I want to make a really multiplatforms program, I should type :
int a[10][20]; if (sizeof(**a)==4) x = NA_NewArray(a, tInt32, 2, 10, 20); else x = NA_NewArray(a, tInt16, 2, 10, 20);
This is the recommended way ?
I think this would be better: Int32 a[10][20]; x = NA_NewArray(a, tInt32, 2, 10, 20); Or, just do: x = NA_NewArray(NULL, tInt32, 2, 10, 20) and numarray will allocate the array storage which is then pointed to by x->data. Regards, Todd
On Mon, 27 Jun 2005 06:16:33 -0400, Todd Miller
I would like to create a numarray array from a C buffer of 'int' but I don't quite know how to handle different 'int' representation (16 or 32 bits).
On the platforms we test on at least, int == Int32. This is even true for the 64-bit platforms I've seen.
For instante should I use :
int a[10][20]; x = NA_NewArray(a, tInt16, 2, 10, 20);
or : int a[10][20]; x = NA_NewArray(a, tInt32, 2, 10, 20);
Should I use some macro to determine size of int ?
You can do that with: sizeof(int)
Ok so If I want to make a really multiplatforms program, I should type :
int a[10][20]; if (sizeof(**a)==4) x = NA_NewArray(a, tInt32, 2, 10, 20); else x = NA_NewArray(a, tInt16, 2, 10, 20);
This is the recommended way ?
I think this would be better:
Int32 a[10][20];
x = NA_NewArray(a, tInt32, 2, 10, 20);
Or, just do:
x = NA_NewArray(NULL, tInt32, 2, 10, 20)
and numarray will allocate the array storage which is then pointed to by x->data.
Ok but in my particular case I can't : I have to deal with legacy code which use 'int' so I'm stuck with the sizeof trick... Thanks anyway !
On 6/26/05, Todd Miller
On Mon, 2005-06-27 at 02:33 +0200, Nicolas Pernetty wrote:
Hello,
I would like to create a numarray array from a C buffer of 'int' but I don't quite know how to handle different 'int' representation (16 or 32 bits).
On the platforms we test on at least, int == Int32. This is even true for the 64-bit platforms I've seen.
Hi, The June 2005 C/C++ Users Journal (vol 23 no 5 http://www.cuj.com) had a couple of articles on porting to 64 bit computing. One was by Rodney Mach who gave this information: the 32-bit environment is a "ILP32 model because the C data-type model has 32-bit integers, long and pointers". "All modern 64-bit UNIX-like platforms use the LP64 data model" where longs and pointers are 64 bit but ints are 32-bit. He also looked at some of the porting problems. Bruce
For instante should I use :
int a[10][20]; x = NA_NewArray(a, tInt16, 2, 10, 20);
or : int a[10][20]; x = NA_NewArray(a, tInt32, 2, 10, 20);
Should I use some macro to determine size of int ?
You can do that with: sizeof(int)
Regards, Todd
------------------------------------------------------- SF.Net email is sponsored by: Discover Easy Linux Migration Strategies from IBM. Find simple to follow Roadmaps, straightforward articles, informative Webcasts and more! Get everything you need to get up to speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click _______________________________________________ Numpy-discussion mailing list Numpy-discussion@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/numpy-discussion
On Monday 27 June 2005 02:33, Nicolas Pernetty wrote:
I would like to create a numarray array from a C buffer of 'int' but I don't quite know how to handle different 'int' representation (16 or 32 bits).
For instante should I use :
int a[10][20]; x = NA_NewArray(a, tInt16, 2, 10, 20);
or : int a[10][20]; x = NA_NewArray(a, tInt32, 2, 10, 20);
Should I use some macro to determine size of int ?
As Todd said, int is 32 bits in most compilers. If you want to use 16-bit ints, use the short int declaration: short int a[10][20]; x = NA_NewArray(a, tInt16, 2, 10, 20); which should work on most modern platforms. -- Francesc
On Mon, 27 Jun 2005 09:00:52 +0200, Francesc Altet
I would like to create a numarray array from a C buffer of 'int' but I don't quite know how to handle different 'int' representation (16 or 32 bits).
For instante should I use :
int a[10][20]; x = NA_NewArray(a, tInt16, 2, 10, 20);
or : int a[10][20]; x = NA_NewArray(a, tInt32, 2, 10, 20);
Should I use some macro to determine size of int ?
As Todd said, int is 32 bits in most compilers. If you want to use 16-bit ints, use the short int declaration:
short int a[10][20]; x = NA_NewArray(a, tInt16, 2, 10, 20);
which should work on most modern platforms.
Well It was to be interfaced with legacy code which include 'int' declaration (which I couldn't change). So I think that I'll keep the 'sizeof' trick. Thanks,
Nicolas Pernetty wrote:
On Mon, 27 Jun 2005 09:00:52 +0200, Francesc Altet
wrote : I would like to create a numarray array from a C buffer of 'int' but I don't quite know how to handle different 'int' representation (16 or 32 bits). ... It was to be interfaced with legacy code which include 'int' declaration (which I couldn't change). So I think that I'll keep the 'sizeof' trick. I would not treat the test as either-or, but rather as a triple. So: if (sizeof(**a) == 4) x = NA_NewArray(a, tInt16, 2, 10, 20); else if (sizeof(**a) == 2) x = NA_NewArray(a, tInt16, 2, 10, 20); else abort(); /* or raise some exception here. */
If you compile this on a machine with 64-bit ints, it is better if it fails here than proceed as if working with 2-byte ints. If I am sure I'll never run into that case, I'd do it as above (with the abort()). A good C compiler can realize the test is constant and simply generate one arm of the conditions anyway. If I think it might possibly be used on a 64-bit int system, I'd go ahead and worry about what exception to raise (probably PyTypeError, but ...). --Scott David Daniels Scott.Daniels@Acm.Org
On Mon, 27 Jun 2005 10:40:13 -0700, Scott David Daniels
I would like to create a numarray array from a C buffer of 'int' but I don't quite know how to handle different 'int' representation (16 or 32 bits). ... It was to be interfaced with legacy code which include 'int' declaration (which I couldn't change). So I think that I'll keep the 'sizeof' trick. I would not treat the test as either-or, but rather as a triple. So: if (sizeof(**a) == 4) x = NA_NewArray(a, tInt16, 2, 10, 20); else if (sizeof(**a) == 2) x = NA_NewArray(a, tInt16, 2, 10, 20); else abort(); /* or raise some exception here. */
If you compile this on a machine with 64-bit ints, it is better if it fails here than proceed as if working with 2-byte ints. If I am sure I'll never run into that case, I'd do it as above (with the abort()). A good C compiler can realize the test is constant and simply generate one arm of the conditions anyway. If I think it might possibly be used on a 64-bit int system, I'd go ahead and worry about what exception to raise (probably PyTypeError, but ...).
Damn ! I was so sure that 'int' could only be 16 or 32 bits then I found this page : http://www.doc.ic.ac.uk/lab/secondyear/cstyle/node20.html Well thank you for your suggestion, I'll adopt it ! Regards,
participants (6)
-
Bruce Southey
-
Francesc Altet
-
Jochen Küpper
-
Nicolas Pernetty
-
Scott David Daniels
-
Todd Miller