Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2017-04

Platform Dependent Integer Types

13 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

African Wild Dog

Which integer types have their size dependent on platform? E.g. in Delphi, LongInt can 32 or 64 bits depending on the platform. The documentation says "every platform has a ”native” integer size, depending on whether the platform is 8-bit, 16-bit, 32-bit or 64-bit. e.g. On AVR this is 8-bit. ", but it is not clear about which integer types are "natives".

Regards

Reply by email

Mattias Gaertner
to African Wild Dog
Which integer types have their size dependent on platform? E.g. in Delphi, LongInt can 32 or 64 bits depending on the platform.

Why do you think that Delphi's longint has 64bits anywhere? Delphi's NativeInt is 32 or 64bit depending on platform.

The documentation says "every platform has a ”native” integer size, depending on whether the platform is 8-bit, 16-bit, 32-bit or 64-bit. e.g. On AVR this is 8-bit. ", but it is not clear about which integer types are "natives".

Most of FPC's integer types defined via IFDFEs. See fpc/rtl/inc/systemh.inc
You can use Lazarus' Find Declaration on any type and see the definition.
For example:

{$ifdef CPU64}
  SizeInt = Int64;
  SizeUInt = QWord;
  PtrInt = Int64;
  PtrUInt = QWord;
  ValSInt = int64;
  ValUInt = qword;
  CodePointer = Pointer;
  CodePtrInt = PtrInt;
  CodePtrUInt = PtrUInt;
{$endif CPU64}
{$ifdef CPU32}
  SizeInt = Longint;
  SizeUInt = DWord;
  PtrInt = Longint;
  PtrUInt = DWord;
  ValSInt = Longint;
  ValUInt = Cardinal;
  CodePointer = Pointer;
  CodePtrInt = PtrInt;
  CodePtrUInt = PtrUInt;
{$endif CPU32}
  { NativeInt and NativeUInt are Delphi compatibility types. Even though Delphi
    has IntPtr and UIntPtr, the Delphi documentation for NativeInt states that
    'The size of NativeInt is equivalent to the size of the pointer on the
    current platform'. Because of the misleading names, these types shouldn't be
    used in the FPC RTL. Note that on i8086 their size changes between 16-bit
    and 32-bit according to the memory model, so they're not really a 'native
    int' type there at all. }
  NativeInt  = PtrInt;
  NativeUInt = PtrUInt;

Mattias

Reply by email

Marco van de Voort
to African Wild Dog: Which integer types have their size dependent on platform? E.g. in Delphi, LongInt can 32…

In our previous episode, African Wild Dog said:

Which integer types have their size dependent on platform? E.g. in Delphi, LongInt can 32 or 64 bits depending on the platform.

In Delphi they retroactively equated longint to C long, being 32-bit on 64-bits windows and 64-bit on Linux. The Delphi Linux compiler is btw a different compiler than windows.

On FPC it is always 32-bit. Ptrint and ptruint scale with pointer size, and integer depends on compilation mode, 16 or 32-bit.

For the C compiler that FPC corresponds with (usually gcc), there are types that correspond with C types in unit ctypes.

Maybe some of the new ultra small targets like AVR make exceptions, but those are the general rules.

On AVR this is 8-bit. ", but it is not clear about which integer types are "natives".

The typical pascal solution is to use subranges and hope the compiler will optimize it.

Reply by email

African Wild Dog
to Marco van de Voort

2017-04-07 11:15 GMT-03:00 Marco van de Voort :

Show quoted text 12 lines
In our previous episode, African Wild Dog said: > Which integer types have their size dependent on platform? > E.g. in Delphi, LongInt can 32 or 64 bits depending on the platform. In Delphi they retroactively equated longint to C long, being 32-bit on 64-bits windows and 64-bit on Linux. The Delphi Linux compiler is btw a different compiler than windows. On FPC it is always 32-bit. Ptrint and ptruint scale with pointer size, and integer depends on compilation mode, 16 or 32-bit.

So, is the size of "Integer" type dependent on the compilation mode? (Delphi is always 32 bits)

Reply by email

African Wild Dog
to Mattias Gaertner: On Thu, 6 Apr 2017 18:25:40 -0300 Why do you think that Delphi's longint has 64bits…

2017-04-06 18:50 GMT-03:00 Mattias Gaertner :

Why do you think that Delphi's longint has 64bits anywhere? Delphi's NativeInt is 32 or 64bit depending on platform.

http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Internal_Data_Formats_(Delphi)#Platform-Independent_Signed_Integer_Types .
As Marco have pointed out, in Windows the Delphi's LongInt type is always 32-bits. On other platforms is platform dependent.

Show quoted text 52 lines
> The documentation says "every platform has a ”native” integer size, > depending on whether the platform is 8-bit, 16-bit, 32-bit or 64-bit. e.g. > On AVR this is 8-bit. ", but it is not clear about which integer types are > "natives". Most of FPC's integer types defined via IFDFEs. See fpc/rtl/inc/systemh.inc You can use Lazarus' Find Declaration on any type and see the definition. For example: {$ifdef CPU64} SizeInt = Int64; SizeUInt = QWord; PtrInt = Int64; PtrUInt = QWord; ValSInt = int64; ValUInt = qword; CodePointer = Pointer; CodePtrInt = PtrInt; CodePtrUInt = PtrUInt; {$endif CPU64} {$ifdef CPU32} SizeInt = Longint; SizeUInt = DWord; PtrInt = Longint; PtrUInt = DWord; ValSInt = Longint; ValUInt = Cardinal; CodePointer = Pointer; CodePtrInt = PtrInt; CodePtrUInt = PtrUInt; {$endif CPU32} { NativeInt and NativeUInt are Delphi compatibility types. Even though Delphi has IntPtr and UIntPtr, the Delphi documentation for NativeInt states that 'The size of NativeInt is equivalent to the size of the pointer on the current platform'. Because of the misleading names, these types shouldn't be used in the FPC RTL. Note that on i8086 their size changes between 16-bit and 32-bit according to the memory model, so they're not really a 'native int' type there at all. }

Still not clear for me. Are NativeInt and NativeUInt provided for Delphi compatibility, right? So, if i need an integer type which depends on platform/fpc compilation mode, which one should i use?

Reply by email

Marco van de Voort
to African Wild Dog: 2017-04-07 11:15 GMT-03:00 Marco van de Voort : So, is the size of "Integer" type…

In our previous episode, African Wild Dog said:

> On FPC it is always 32-bit. Ptrint and ptruint scale with pointer size, and > integer depends on compilation mode, 16 or 32-bit. > So, is the size of "Integer" type dependent on the compilation mode?

Yes.

(Delphi is always 32 bits)

In objfpc and delphi modes it is 32-bit, in more TP oriented modes it is 16-bit as it was in TP.

Reply by email

Michael Van Canneyt
to African Wild Dog: 2017-04-07 11:15 GMT-03:00 Marco van de Voort : So, is the size of "Integer" type…
African Wild Dog wrote 16 lines
2017-04-07 11:15 GMT-03:00 Marco van de Voort : > In our previous episode, African Wild Dog said: >> Which integer types have their size dependent on platform? >> E.g. in Delphi, LongInt can 32 or 64 bits depending on the platform. > > In Delphi they retroactively equated longint to C long, being 32-bit on > 64-bits windows and 64-bit on Linux. The Delphi Linux compiler is btw a > different compiler than windows. > > On FPC it is always 32-bit. Ptrint and ptruint scale with pointer size, and > integer depends on compilation mode, 16 or 32-bit. > > So, is the size of "Integer" type dependent on the compilation mode? (Delphi is always 32 bits)

Yes. It is 16 bit in fpc/tp mode, 32 bit in Delphi/ObjFPC and MacPas.

Michael.

Reply by email

Michael Van Canneyt
to African Wild Dog: 2017-04-06 18:50 GMT-03:00 Mattias Gaertner :…
African Wild Dog wrote 10 lines
2017-04-06 18:50 GMT-03:00 Mattias Gaertner : > Why do you think that Delphi's longint has 64bits anywhere? > Delphi's NativeInt is 32 or 64bit depending on platform. > > http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Internal_Data_Formats_(Delphi)#Platform-Independent_Signed_Integer_Types . As Marco have pointed out, in Windows the Delphi's LongInt type is always 32-bits. On other platforms is platform dependent.

In short: It is platform dependent in Delphi.

Still not clear for me. Are NativeInt and NativeUInt provided for Delphi compatibility, right? So, if i need an integer type which depends on platform/fpc compilation mode, which one should i use?

NativeInt and NativeUInt are indeed provided for Delphi compatibility.

Which integer type to chose : That depends.
What do you want to achieve exactly ?

Michael.

Reply by email

Marco van de Voort
to African Wild Dog: 2017-04-06 18:50 GMT-03:00 Mattias Gaertner :…

In our previous episode, African Wild Dog said:

Show quoted text 5 lines
> http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Internal_Data_Formats_(Delphi)#Platform-Independent_Signed_Integer_Types . As Marco have pointed out, in Windows the Delphi's LongInt type is always 32-bits. On other platforms is platform dependent.

Note that this is a very recent development. It started with the iOS 64-bit arm compiler a while back, and now since weeks their new Linux offering.

Both however are a different compiler than the regular.

Still not clear for me. Are NativeInt and NativeUInt provided for Delphi compatibility, right? So, if i need an integer type which depends on platform/fpc compilation mode, which one should i use?

For what purpose? There are only ones that scale with pointer size, and then any of the native(u)int, ptr(u)int or int(u)ptr types are ok.

Reply by email

African Wild Dog
to Michael Van Canneyt: In short: It is platform dependent in Delphi. NativeInt and NativeUInt are indeed…

2017-04-07 12:10 GMT-03:00 Michael Van Canneyt :

Show quoted text 7 lines
NativeInt and NativeUInt are indeed provided for Delphi compatibility. Which integer type to chose : That depends. What do you want to achieve exactly ?

In Delphi, if i want an integer type which depends on the target platform (32 or 64 bits) i can use the NativeInt and NativeUInt types. What is the FPC standard type for this? PrtInt and PtrUInt? (I usually mix both delphi mode and objfpc mode units in the same project).

Reply by email

Michael Van Canneyt
to African Wild Dog
African Wild Dog wrote 13 lines
2017-04-07 12:10 GMT-03:00 Michael Van Canneyt : > > NativeInt and NativeUInt are indeed provided for Delphi compatibility. > > Which integer type to chose : That depends. What do you want to achieve > exactly ? > > In Delphi, if i want an integer type which depends on the target platform (32 or 64 bits) i can use the NativeInt and NativeUInt types. What is the FPC standard type for this? PrtInt and PtrUInt? (I usually mix both delphi mode and objfpc mode units in the same project).

You can use PrtInt and PtrUInt for this or NativeInt and NativeUINT, they should be equivalent.

The only 'dubious' case may be the 8086 msdos case, which is somewhat confused about what exactly constitutes a pointer... There you may be better off with NativeInt and NativeUINT.

Michael.

Reply by email

African Wild Dog
to Michael Van Canneyt

2017-04-07 12:33 GMT-03:00 Michael Van Canneyt :

Show quoted text 28 lines
On Fri, 7 Apr 2017, African Wild Dog wrote: 2017-04-07 12:10 GMT-03:00 Michael Van Canneyt : > > >> NativeInt and NativeUInt are indeed provided for Delphi compatibility. >> >> Which integer type to chose : That depends. What do you want to achieve >> exactly ? >> >> >> In Delphi, if i want an integer type which depends on the target platform > (32 or 64 bits) i can use the NativeInt and NativeUInt types. What is the > FPC standard type for this? PrtInt and PtrUInt? (I usually mix both delphi > mode and objfpc mode units in the same project). > You can use PrtInt and PtrUInt for this or NativeInt and NativeUINT, they should be equivalent. The only 'dubious' case may be the 8086 msdos case, which is somewhat confused about what exactly constitutes a pointer... There you may be better off with NativeInt and NativeUINT.

Thank you for the clarification.

Best regards

Reply by email

Florian Klämpfl
to Marco van de Voort: In our previous episode, African Wild Dog said: In Delphi they retroactively equated…
Marco van de Voort wrote 15 lines
In our previous episode, African Wild Dog said: > Which integer types have their size dependent on platform? > E.g. in Delphi, LongInt can 32 or 64 bits depending on the platform. In Delphi they retroactively equated longint to C long, being 32-bit on 64-bits windows and 64-bit on Linux. The Delphi Linux compiler is btw a different compiler than windows. On FPC it is always 32-bit. Ptrint and ptruint scale with pointer size, and integer depends on compilation mode, 16 or 32-bit. For the C compiler that FPC corresponds with (usually gcc), there are types that correspond with C types in unit ctypes. Maybe some of the new ultra small targets like AVR make exceptions,

No. The only difference is that the smallest integer operation is 8 bit: On a 32 bit target, <byte>*<byte> is evaluated as a longint, on avr, it is evaluated as 8 bit operation.

However, we introduced ALUSInt/ALUUInt, it is an int type which scales with the ALU size of the CPU as PtrInt/PtrUInt are larger than the ALU on AVR.

but those are the general rules.

Reply by email