Difference between revisions of "Word"

From Lazarus wiki
Jump to navigationJump to search
(rearrange, mention Integer, (supposedly) add new external links)
m (→‎FPC: fix spelling mistake, update date)
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{Word}}
 
{{Word}}
  
A ''word'' is the processor’s native data unit.
+
A '''word''' is the processor’s native data unit.
 
Modern consumer processors have a word width of [[64 bit|64 bits]].
 
Modern consumer processors have a word width of [[64 bit|64 bits]].
  
Line 16: Line 16:
  
 
=== FPC ===
 
=== FPC ===
Contrary to the data type’s name, in [[FPC]] a <syntaxhighlight lang="pascal" inline>word</syntaxhighlight> is defined as a subrange data type <syntaxhighlight lang="pascal" inline>0..65535</syntaxhighlight>.
+
For source compatibility reasons, [[FPC]] defines <syntaxhighlight lang="pascal" inline>word</syntaxhighlight> in the same way as Turbo Pascal and Delphi: the subrange data type <syntaxhighlight lang="pascal" inline>0..65535</syntaxhighlight>.
 
The <syntaxhighlight lang="delphi" inline>high</syntaxhighlight> value <syntaxhighlight lang="pascal" inline>65535</syntaxhighlight> is <math>2^{16}-1</math>.
 
The <syntaxhighlight lang="delphi" inline>high</syntaxhighlight> value <syntaxhighlight lang="pascal" inline>65535</syntaxhighlight> is <math>2^{16}-1</math>.
 
Thus a {{Doc|package=RTL|unit=system|identifier=word|text=<syntaxhighlight lang="pascal" inline>system.word</syntaxhighlight>}} occupies two bytes of space.
 
Thus a {{Doc|package=RTL|unit=system|identifier=word|text=<syntaxhighlight lang="pascal" inline>system.word</syntaxhighlight>}} occupies two bytes of space.
 
Subrange data types are stored in a quantity that serves best the goals of performance and memory efficiency.
 
Subrange data types are stored in a quantity that serves best the goals of performance and memory efficiency.
  
This deviation from the normal usage of the term ''word'' in computer science cannot be explained.<!-- Explain it, if you know why. I don't wanna spread rumors. -->
+
The processor’s native word size, as defined above, corresponds to different types depending on the purpose you want to use it for:
Instead, one has to use the data type {{Doc|package=RTL|unit=system|identifier=ptruint|text=<syntaxhighlight lang="pascal" inline>system.ptrUInt</syntaxhighlight>}} if the processor’s native size is desired.
+
* the (as of 2022 still undocumented) {{Doc|package=RTL|unit=system|identifier=alusint|text=<syntaxhighlight lang="pascal" inline>system.ALUSint</syntaxhighlight>}} and {{Doc|package=RTL|unit=system|identifier=aluuint|text=<syntaxhighlight lang="pascal" inline>system.ALUUint</syntaxhighlight>}} types correspond to the native word size used by the processor’s ALU (arithmetic and logical unit), as defined at the beginning of this page. In general, this type should not be used in high level code. Instead, choose a data type based on the values it should be able to represent, as this is safer and more portable. It is the compiler’s job to generate optimal code.
This data type has the same properties as a word according to the normal definition, but is more cryptic and unreadable (try pronouncing ptruint).
+
* {{Doc|package=RTL|unit=system|identifier=codeptruint|text=<syntaxhighlight lang="pascal" inline>system.CodePtrUInt</syntaxhighlight>}} corresponds to the size of pointers to code, such as the address of a [[Procedure|procedure]]. This can be different from a pointer to data, e.&#8239;g. on targets that support multiple [[DOS#Supported memory models|memory models]].
 +
* {{Doc|package=RTL|unit=system|identifier=ptruint|text=<syntaxhighlight lang="pascal" inline>system.PtrUInt</syntaxhighlight>}} corresponds to the size of pointers to data.
  
In FPC a [[Smallint|<syntaxhighlight lang="pascal" inline>smallInt</syntaxhighlight>]] has the same size as a <syntaxhighlight lang="pascal" inline>word</syntaxhighlight>.
+
On many platforms, all of these types have the same size, but it is not the case everywhere.
 +
 
 +
In FPC a [[Smallint|<syntaxhighlight lang="pascal" inline>smallInt</syntaxhighlight>]] has the same size as a <syntaxhighlight lang="pascal" inline>word</syntaxhighlight>, but is signed.
  
 
{{Data types}}
 
{{Data types}}

Latest revision as of 12:17, 8 September 2022

Deutsch (de) English (en) suomi (fi) français (fr) русский (ru)

A word is the processor’s native data unit. Modern consumer processors have a word width of 64 bits.

Data type

Most run-time libraries provide the native data type of a processor as the Pascal data type word. It is a subset of all whole numbers (non-negative integers) that can be represented by the processor’s natural data unit size.

On a 64-bit architecture this means a word is an integer within the range [math]\displaystyle{ [0,~2^{64}-1] }[/math]. On a 32-bit architecture a word will be an integer in the range [math]\displaystyle{ [0,~2^{32}-1] }[/math], and so on, respectively.

In GNU Pascal a word is just an alias for cardinal, which has the same properties regarding possible values.

If a signed integer having the processor’s native size is wanted, the data type integer provides this functionality.

FPC

For source compatibility reasons, FPC defines word in the same way as Turbo Pascal and Delphi: the subrange data type 0..65535. The high value 65535 is [math]\displaystyle{ 2^{16}-1 }[/math]. Thus a system.word occupies two bytes of space. Subrange data types are stored in a quantity that serves best the goals of performance and memory efficiency.

The processor’s native word size, as defined above, corresponds to different types depending on the purpose you want to use it for:

  • the (as of 2022 still undocumented) system.ALUSint and system.ALUUint types correspond to the native word size used by the processor’s ALU (arithmetic and logical unit), as defined at the beginning of this page. In general, this type should not be used in high level code. Instead, choose a data type based on the values it should be able to represent, as this is safer and more portable. It is the compiler’s job to generate optimal code.
  • system.CodePtrUInt corresponds to the size of pointers to code, such as the address of a procedure. This can be different from a pointer to data, e. g. on targets that support multiple memory models.
  • system.PtrUInt corresponds to the size of pointers to data.

On many platforms, all of these types have the same size, but it is not the case everywhere.

In FPC a smallInt has the same size as a word, but is signed.


navigation bar: data types
simple data types

boolean byte cardinal char currency double dword extended int8 int16 int32 int64 integer longint real shortint single smallint pointer qword word

complex data types

array class object record set string shortstring