February 26, 2011 Re: Interfacing with c and platform dependent sizes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Wey | On 26.02.2011 17:06, Mike Wey wrote:
> On 02/26/2011 11:49 AM, Jacob Carlborg wrote:
>> On 2011-02-26 01:28, simendsjo wrote:
>>> C is not my strong side, so I'm having some problems wrapping some code.
>>>
>>> I found a couple of sources on this:
>>> 1) http://www.digitalmars.com/d/2.0/htomodule.html
>>> 2) http://www.digitalmars.com/d/2.0/interfaceToC.html
>>>
>>> 1)
>>> C's long is the same as D's int.
>>> long long is long
>>>
>>> 2)
>>> C 32bit's long long is D's long, C 64 bits long is D's long.
>>>
>>> So.. A long in C is the same as the platform size? And long long doesn't
>>> exist in 64 bit?
>>
>> In general you can follow the table at
>> http://www.digitalmars.com/d/2.0/htomodule.html
>>
>> But when it comes to "long" in C you have to be careful. On 32bit
>> platforms a C "long" will be 32bit long. But on 64bit platforms it
>> depends of what data model is used. To simplify things:
>>
>> * On Windows 64bit a C "long" will be 32bit long
>> * On Posix 64bit a C "long" will be 64bit long.
>>
>> What you can do is to create an alias called "c_long" and "c_ulong",
>> something like this:
>>
>> version (D_LP64)
>> {
>> version (Windows)
>> {
>> alias int c_long;
>> alias uint c_ulong;
>> }
>>
>> else
>> {
>> alias long c_long;
>> alias ulong c_ulong;
>> }
>> }
>>
>> else
>> {
>> alias int c_long;
>> alias uint c_ulong;
>> }
>>
>> To read more about data models:
>> http://en.wikipedia.org/wiki/64-bit#Specific_C-language_data_models
>>
>
> You can also import core.stdc.config witch defines both c_long and c_ulong.
>
Thanks for all the answers. Is something like this correct?
version(X86_64)
{
version(Windows)
{
version = LLP64;
}
else version(Posix)
{
version = LP64;
}
else
{
static assert(0);
}
version(LLP64)
{
alias short c_short;
alias ushort c_ushort;
alias int c_int;
alias uint c_uint;
alias int c_long;
alias uint c_ulong;
alias long c_longlong;
alias ulong c_ulonglong;
alias ulong c_size_t;
}
else version(LP64)
{
alias short c_short;
alias ushort c_ushort;
alias int c_int;
alias uint c_uint;
alias long c_long;
alias ulong c_ulong;
alias long c_longlong;
alias ulong c_ulonglong;
alias ulong c_size_t;
}
else version(ILP64)
{
alias short c_short;
alias ushort c_ushort;
alias long c_int;
alias ulong c_uint;
alias long c_long;
alias ulong c_ulong;
alias long c_longlong;
alias ulong c_ulonglong;
alias ulong c_size_t;
}
else version(SILP64)
{
alias long c_short;
alias ulong c_ushort;
alias long c_int;
alias ulong c_uint;
alias long c_long;
alias ulong c_ulong;
alias long c_longlong;
alias ulong c_ulonglong;
alias ulong c_size_t;
}
else
{
static assert(0);
}
}
else version(X86)
{
alias short c_short;
alias ushort c_ushort;
alias int c_int;
alias uint c_uint;
alias int c_long;
alias uint c_ulong;
alias long c_longlong;
alias ulong c_ulonglong;
alias uint c_size_t;
}
else
{
static assert(0);
}
|
February 26, 2011 Re: Interfacing with c and platform dependent sizes | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | On 02/26/2011 05:58 PM, simendsjo wrote: > On 26.02.2011 17:06, Mike Wey wrote: >> On 02/26/2011 11:49 AM, Jacob Carlborg wrote: >>> On 2011-02-26 01:28, simendsjo wrote: >>>> C is not my strong side, so I'm having some problems wrapping some >>>> code. >>>> >>>> I found a couple of sources on this: >>>> 1) http://www.digitalmars.com/d/2.0/htomodule.html >>>> 2) http://www.digitalmars.com/d/2.0/interfaceToC.html >>>> >>>> 1) >>>> C's long is the same as D's int. >>>> long long is long >>>> >>>> 2) >>>> C 32bit's long long is D's long, C 64 bits long is D's long. >>>> >>>> So.. A long in C is the same as the platform size? And long long >>>> doesn't >>>> exist in 64 bit? >>> >>> In general you can follow the table at >>> http://www.digitalmars.com/d/2.0/htomodule.html >>> >>> But when it comes to "long" in C you have to be careful. On 32bit >>> platforms a C "long" will be 32bit long. But on 64bit platforms it >>> depends of what data model is used. To simplify things: >>> >>> * On Windows 64bit a C "long" will be 32bit long >>> * On Posix 64bit a C "long" will be 64bit long. >>> >>> What you can do is to create an alias called "c_long" and "c_ulong", >>> something like this: >>> >>> version (D_LP64) >>> { >>> version (Windows) >>> { >>> alias int c_long; >>> alias uint c_ulong; >>> } >>> >>> else >>> { >>> alias long c_long; >>> alias ulong c_ulong; >>> } >>> } >>> >>> else >>> { >>> alias int c_long; >>> alias uint c_ulong; >>> } >>> >>> To read more about data models: >>> http://en.wikipedia.org/wiki/64-bit#Specific_C-language_data_models >>> >> >> You can also import core.stdc.config witch defines both c_long and >> c_ulong. >> > Thanks for all the answers. Is something like this correct? > > version(X86_64) > { > version(Windows) > { > version = LLP64; > } > else version(Posix) > { > version = LP64; > } > else > { > static assert(0); > } > > version(LLP64) > { > alias short c_short; > alias ushort c_ushort; > alias int c_int; > alias uint c_uint; > alias int c_long; > alias uint c_ulong; > alias long c_longlong; > alias ulong c_ulonglong; > alias ulong c_size_t; > } > else version(LP64) > { > alias short c_short; > alias ushort c_ushort; > alias int c_int; > alias uint c_uint; > alias long c_long; > alias ulong c_ulong; > alias long c_longlong; > alias ulong c_ulonglong; > alias ulong c_size_t; > } > else version(ILP64) > { > alias short c_short; > alias ushort c_ushort; > alias long c_int; > alias ulong c_uint; > alias long c_long; > alias ulong c_ulong; > alias long c_longlong; > alias ulong c_ulonglong; > alias ulong c_size_t; > } > else version(SILP64) > { > alias long c_short; > alias ulong c_ushort; > alias long c_int; > alias ulong c_uint; > alias long c_long; > alias ulong c_ulong; > alias long c_longlong; > alias ulong c_ulonglong; > alias ulong c_size_t; > } > else > { > static assert(0); > } > } > else version(X86) > { > alias short c_short; > alias ushort c_ushort; > alias int c_int; > alias uint c_uint; > alias int c_long; > alias uint c_ulong; > alias long c_longlong; > alias ulong c_ulonglong; > alias uint c_size_t; > } > else > { > static assert(0); > } > The aliases look correct, but i don't think you'll need ILP64 or SILP64 any time soon. -- Mike Wey |
February 27, 2011 Re: Interfacing with c and platform dependent sizes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On 2/25/2011 7:24 PM, Steven Schveighoffer wrote:
> BTW, I think long long is a gnu extension, it's not standard C (I don't
> think long long exists in Visual C for instance).
I'm pretty sure it's standard as of C99 (though not yet for C++; that's coming with C++0x). MSVC does indeed support it.
|
February 27, 2011 Re: Interfacing with c and platform dependent sizes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Wey | On 2011-02-26 17:06, Mike Wey wrote: > On 02/26/2011 11:49 AM, Jacob Carlborg wrote: >> On 2011-02-26 01:28, simendsjo wrote: >>> C is not my strong side, so I'm having some problems wrapping some code. >>> >>> I found a couple of sources on this: >>> 1) http://www.digitalmars.com/d/2.0/htomodule.html >>> 2) http://www.digitalmars.com/d/2.0/interfaceToC.html >>> >>> 1) >>> C's long is the same as D's int. >>> long long is long >>> >>> 2) >>> C 32bit's long long is D's long, C 64 bits long is D's long. >>> >>> So.. A long in C is the same as the platform size? And long long doesn't >>> exist in 64 bit? >> >> In general you can follow the table at >> http://www.digitalmars.com/d/2.0/htomodule.html >> >> But when it comes to "long" in C you have to be careful. On 32bit >> platforms a C "long" will be 32bit long. But on 64bit platforms it >> depends of what data model is used. To simplify things: >> >> * On Windows 64bit a C "long" will be 32bit long >> * On Posix 64bit a C "long" will be 64bit long. >> >> What you can do is to create an alias called "c_long" and "c_ulong", >> something like this: >> >> version (D_LP64) >> { >> version (Windows) >> { >> alias int c_long; >> alias uint c_ulong; >> } >> >> else >> { >> alias long c_long; >> alias ulong c_ulong; >> } >> } >> >> else >> { >> alias int c_long; >> alias uint c_ulong; >> } >> >> To read more about data models: >> http://en.wikipedia.org/wiki/64-bit#Specific_C-language_data_models >> > > You can also import core.stdc.config witch defines both c_long and c_ulong. Yeah, I always forgets that module in druntime, even though it's based on Tango where I do remember it. -- /Jacob Carlborg |
February 27, 2011 Re: Interfacing with c and platform dependent sizes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Wey | On 2011-02-26 23:02, Mike Wey wrote: > On 02/26/2011 05:58 PM, simendsjo wrote: >> On 26.02.2011 17:06, Mike Wey wrote: >>> On 02/26/2011 11:49 AM, Jacob Carlborg wrote: >>>> On 2011-02-26 01:28, simendsjo wrote: >>>>> C is not my strong side, so I'm having some problems wrapping some >>>>> code. >>>>> >>>>> I found a couple of sources on this: >>>>> 1) http://www.digitalmars.com/d/2.0/htomodule.html >>>>> 2) http://www.digitalmars.com/d/2.0/interfaceToC.html >>>>> >>>>> 1) >>>>> C's long is the same as D's int. >>>>> long long is long >>>>> >>>>> 2) >>>>> C 32bit's long long is D's long, C 64 bits long is D's long. >>>>> >>>>> So.. A long in C is the same as the platform size? And long long >>>>> doesn't >>>>> exist in 64 bit? >>>> >>>> In general you can follow the table at >>>> http://www.digitalmars.com/d/2.0/htomodule.html >>>> >>>> But when it comes to "long" in C you have to be careful. On 32bit >>>> platforms a C "long" will be 32bit long. But on 64bit platforms it >>>> depends of what data model is used. To simplify things: >>>> >>>> * On Windows 64bit a C "long" will be 32bit long >>>> * On Posix 64bit a C "long" will be 64bit long. >>>> >>>> What you can do is to create an alias called "c_long" and "c_ulong", >>>> something like this: >>>> >>>> version (D_LP64) >>>> { >>>> version (Windows) >>>> { >>>> alias int c_long; >>>> alias uint c_ulong; >>>> } >>>> >>>> else >>>> { >>>> alias long c_long; >>>> alias ulong c_ulong; >>>> } >>>> } >>>> >>>> else >>>> { >>>> alias int c_long; >>>> alias uint c_ulong; >>>> } >>>> >>>> To read more about data models: >>>> http://en.wikipedia.org/wiki/64-bit#Specific_C-language_data_models >>>> >>> >>> You can also import core.stdc.config witch defines both c_long and >>> c_ulong. >>> >> Thanks for all the answers. Is something like this correct? >> >> version(X86_64) >> { >> version(Windows) >> { >> version = LLP64; >> } >> else version(Posix) >> { >> version = LP64; >> } >> else >> { >> static assert(0); >> } >> >> version(LLP64) >> { >> alias short c_short; >> alias ushort c_ushort; >> alias int c_int; >> alias uint c_uint; >> alias int c_long; >> alias uint c_ulong; >> alias long c_longlong; >> alias ulong c_ulonglong; >> alias ulong c_size_t; >> } >> else version(LP64) >> { >> alias short c_short; >> alias ushort c_ushort; >> alias int c_int; >> alias uint c_uint; >> alias long c_long; >> alias ulong c_ulong; >> alias long c_longlong; >> alias ulong c_ulonglong; >> alias ulong c_size_t; >> } >> else version(ILP64) >> { >> alias short c_short; >> alias ushort c_ushort; >> alias long c_int; >> alias ulong c_uint; >> alias long c_long; >> alias ulong c_ulong; >> alias long c_longlong; >> alias ulong c_ulonglong; >> alias ulong c_size_t; >> } >> else version(SILP64) >> { >> alias long c_short; >> alias ulong c_ushort; >> alias long c_int; >> alias ulong c_uint; >> alias long c_long; >> alias ulong c_ulong; >> alias long c_longlong; >> alias ulong c_ulonglong; >> alias ulong c_size_t; >> } >> else >> { >> static assert(0); >> } >> } >> else version(X86) >> { >> alias short c_short; >> alias ushort c_ushort; >> alias int c_int; >> alias uint c_uint; >> alias int c_long; >> alias uint c_ulong; >> alias long c_longlong; >> alias ulong c_ulonglong; >> alias uint c_size_t; >> } >> else >> { >> static assert(0); >> } >> > > The aliases look correct, but i don't think you'll need ILP64 or SILP64 > any time soon. I agree. -- /Jacob Carlborg |
February 27, 2011 Re: Interfacing with c and platform dependent sizes | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | On 2011-02-26 17:58, simendsjo wrote: > On 26.02.2011 17:06, Mike Wey wrote: >> On 02/26/2011 11:49 AM, Jacob Carlborg wrote: >>> On 2011-02-26 01:28, simendsjo wrote: >>>> C is not my strong side, so I'm having some problems wrapping some >>>> code. >>>> >>>> I found a couple of sources on this: >>>> 1) http://www.digitalmars.com/d/2.0/htomodule.html >>>> 2) http://www.digitalmars.com/d/2.0/interfaceToC.html >>>> >>>> 1) >>>> C's long is the same as D's int. >>>> long long is long >>>> >>>> 2) >>>> C 32bit's long long is D's long, C 64 bits long is D's long. >>>> >>>> So.. A long in C is the same as the platform size? And long long >>>> doesn't >>>> exist in 64 bit? >>> >>> In general you can follow the table at >>> http://www.digitalmars.com/d/2.0/htomodule.html >>> >>> But when it comes to "long" in C you have to be careful. On 32bit >>> platforms a C "long" will be 32bit long. But on 64bit platforms it >>> depends of what data model is used. To simplify things: >>> >>> * On Windows 64bit a C "long" will be 32bit long >>> * On Posix 64bit a C "long" will be 64bit long. >>> >>> What you can do is to create an alias called "c_long" and "c_ulong", >>> something like this: >>> >>> version (D_LP64) >>> { >>> version (Windows) >>> { >>> alias int c_long; >>> alias uint c_ulong; >>> } >>> >>> else >>> { >>> alias long c_long; >>> alias ulong c_ulong; >>> } >>> } >>> >>> else >>> { >>> alias int c_long; >>> alias uint c_ulong; >>> } >>> >>> To read more about data models: >>> http://en.wikipedia.org/wiki/64-bit#Specific_C-language_data_models >>> >> >> You can also import core.stdc.config witch defines both c_long and >> c_ulong. >> > Thanks for all the answers. Is something like this correct? > > version(X86_64) > { > version(Windows) > { > version = LLP64; > } > else version(Posix) > { > version = LP64; > } > else > { > static assert(0); > } > > version(LLP64) > { > alias short c_short; > alias ushort c_ushort; > alias int c_int; > alias uint c_uint; > alias int c_long; > alias uint c_ulong; > alias long c_longlong; > alias ulong c_ulonglong; > alias ulong c_size_t; > } > else version(LP64) > { > alias short c_short; > alias ushort c_ushort; > alias int c_int; > alias uint c_uint; > alias long c_long; > alias ulong c_ulong; > alias long c_longlong; > alias ulong c_ulonglong; > alias ulong c_size_t; > } > else version(ILP64) > { > alias short c_short; > alias ushort c_ushort; > alias long c_int; > alias ulong c_uint; > alias long c_long; > alias ulong c_ulong; > alias long c_longlong; > alias ulong c_ulonglong; > alias ulong c_size_t; > } > else version(SILP64) > { > alias long c_short; > alias ulong c_ushort; > alias long c_int; > alias ulong c_uint; > alias long c_long; > alias ulong c_ulong; > alias long c_longlong; > alias ulong c_ulonglong; > alias ulong c_size_t; > } > else > { > static assert(0); > } > } > else version(X86) > { > alias short c_short; > alias ushort c_ushort; > alias int c_int; > alias uint c_uint; > alias int c_long; > alias uint c_ulong; > alias long c_longlong; > alias ulong c_ulonglong; > alias uint c_size_t; > } > else > { > static assert(0); > } > I suggest you use core.stdc.config instead, I forgot it existed. BTW size_t already exists in D (defined in the object module) and it will be the same as size_t in C. -- /Jacob Carlborg |
February 27, 2011 Re: Interfacing with c and platform dependent sizes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bekenn | On Sat, 26 Feb 2011 22:24:52 -0500, Bekenn <leaveme@alone.com> wrote:
> On 2/25/2011 7:24 PM, Steven Schveighoffer wrote:
>> BTW, I think long long is a gnu extension, it's not standard C (I don't
>> think long long exists in Visual C for instance).
>
> I'm pretty sure it's standard as of C99 (though not yet for C++; that's coming with C++0x). MSVC does indeed support it.
OK, I was not aware, my C book is from college, and I went to college in 94.
I always thought in Windows you had to use something like __int64.
-Steve
|
February 27, 2011 Re: Interfacing with c and platform dependent sizes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On 27.02.2011 11:43, Jacob Carlborg wrote:
> On 2011-02-26 17:58, simendsjo wrote:
>> On 26.02.2011 17:06, Mike Wey wrote:
>>> On 02/26/2011 11:49 AM, Jacob Carlborg wrote:
>>>> On 2011-02-26 01:28, simendsjo wrote:
>>>>> C is not my strong side, so I'm having some problems wrapping some
>>>>> code.
>>>>>
>>>>> I found a couple of sources on this:
>>>>> 1) http://www.digitalmars.com/d/2.0/htomodule.html
>>>>> 2) http://www.digitalmars.com/d/2.0/interfaceToC.html
>>>>>
>>>>> 1)
>>>>> C's long is the same as D's int.
>>>>> long long is long
>>>>>
>>>>> 2)
>>>>> C 32bit's long long is D's long, C 64 bits long is D's long.
>>>>>
>>>>> So.. A long in C is the same as the platform size? And long long
>>>>> doesn't
>>>>> exist in 64 bit?
>>>>
>>>> In general you can follow the table at
>>>> http://www.digitalmars.com/d/2.0/htomodule.html
>>>>
>>>> But when it comes to "long" in C you have to be careful. On 32bit
>>>> platforms a C "long" will be 32bit long. But on 64bit platforms it
>>>> depends of what data model is used. To simplify things:
>>>>
>>>> * On Windows 64bit a C "long" will be 32bit long
>>>> * On Posix 64bit a C "long" will be 64bit long.
>>>>
>>>> What you can do is to create an alias called "c_long" and "c_ulong",
>>>> something like this:
>>>>
>>>> version (D_LP64)
>>>> {
>>>> version (Windows)
>>>> {
>>>> alias int c_long;
>>>> alias uint c_ulong;
>>>> }
>>>>
>>>> else
>>>> {
>>>> alias long c_long;
>>>> alias ulong c_ulong;
>>>> }
>>>> }
>>>>
>>>> else
>>>> {
>>>> alias int c_long;
>>>> alias uint c_ulong;
>>>> }
>>>>
>>>> To read more about data models:
>>>> http://en.wikipedia.org/wiki/64-bit#Specific_C-language_data_models
>>>>
>>>
>>> You can also import core.stdc.config witch defines both c_long and
>>> c_ulong.
>>>
>> Thanks for all the answers. Is something like this correct?
>>
>> version(X86_64)
>> {
>> version(Windows)
>> {
>> version = LLP64;
>> }
>> else version(Posix)
>> {
>> version = LP64;
>> }
>> else
>> {
>> static assert(0);
>> }
>>
>> version(LLP64)
>> {
>> alias short c_short;
>> alias ushort c_ushort;
>> alias int c_int;
>> alias uint c_uint;
>> alias int c_long;
>> alias uint c_ulong;
>> alias long c_longlong;
>> alias ulong c_ulonglong;
>> alias ulong c_size_t;
>> }
>> else version(LP64)
>> {
>> alias short c_short;
>> alias ushort c_ushort;
>> alias int c_int;
>> alias uint c_uint;
>> alias long c_long;
>> alias ulong c_ulong;
>> alias long c_longlong;
>> alias ulong c_ulonglong;
>> alias ulong c_size_t;
>> }
>> else version(ILP64)
>> {
>> alias short c_short;
>> alias ushort c_ushort;
>> alias long c_int;
>> alias ulong c_uint;
>> alias long c_long;
>> alias ulong c_ulong;
>> alias long c_longlong;
>> alias ulong c_ulonglong;
>> alias ulong c_size_t;
>> }
>> else version(SILP64)
>> {
>> alias long c_short;
>> alias ulong c_ushort;
>> alias long c_int;
>> alias ulong c_uint;
>> alias long c_long;
>> alias ulong c_ulong;
>> alias long c_longlong;
>> alias ulong c_ulonglong;
>> alias ulong c_size_t;
>> }
>> else
>> {
>> static assert(0);
>> }
>> }
>> else version(X86)
>> {
>> alias short c_short;
>> alias ushort c_ushort;
>> alias int c_int;
>> alias uint c_uint;
>> alias int c_long;
>> alias uint c_ulong;
>> alias long c_longlong;
>> alias ulong c_ulonglong;
>> alias uint c_size_t;
>> }
>> else
>> {
>> static assert(0);
>> }
>>
>
> I suggest you use core.stdc.config instead, I forgot it existed. BTW
> size_t already exists in D (defined in the object module) and it will be
> the same as size_t in C.
>
Ok. Thanks.
|
February 27, 2011 Re: Interfacing with c and platform dependent sizes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Sunday 27 February 2011 05:41:49 Steven Schveighoffer wrote:
> On Sat, 26 Feb 2011 22:24:52 -0500, Bekenn <leaveme@alone.com> wrote:
> > On 2/25/2011 7:24 PM, Steven Schveighoffer wrote:
> >> BTW, I think long long is a gnu extension, it's not standard C (I don't think long long exists in Visual C for instance).
> >
> > I'm pretty sure it's standard as of C99 (though not yet for C++; that's coming with C++0x). MSVC does indeed support it.
>
> OK, I was not aware, my C book is from college, and I went to college in 94.
>
> I always thought in Windows you had to use something like __int64.
That would be the smart thing to do, but long long does exist. IHMO, if you don't care about the size of an integral type in C/C++, you use int. Otherwise, you use a type that specifices it's size and is _guaranteed_ to be that size on all systems. Using types like long and long long is just asking for it. Fortunately, D specifies the size of its types, so that isn't a problem.
- Jonathan M Davis
|
Copyright © 1999-2021 by the D Language Foundation