Host introspection: Difference between revisions
No edit summary |
m (Fixed lang tags.) |
||
Line 4: | Line 4: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
<lang ada> |
<lang ada>with Ada.Text_IO; use Ada.Text_IO; |
||
with Ada.Text_IO; use Ada.Text_IO; |
|||
with System; use System; |
with System; use System; |
||
Line 12: | Line 11: | ||
Put_Line ("Word size" & Integer'Image (Word_Size)); |
Put_Line ("Word size" & Integer'Image (Word_Size)); |
||
Put_Line ("Endianness " & Bit_Order'Image (Default_Bit_Order)); |
Put_Line ("Endianness " & Bit_Order'Image (Default_Bit_Order)); |
||
end Host_Introspection; |
end Host_Introspection;</lang> |
||
⚫ | |||
Sample output on a Pentium machine: |
Sample output on a Pentium machine: |
||
<pre> |
<pre> |
||
Line 20: | Line 18: | ||
</pre> |
</pre> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
⚫ | |||
<pre> |
|||
⚫ | |||
INT bits per char = ENTIER (ln(max abs char+1)/ln(max abs bit+1)); |
INT bits per char = ENTIER (ln(max abs char+1)/ln(max abs bit+1)); |
||
INT bits per int = ENTIER (1+ln(max int+1.0)/ln(max abs bit+1)); |
INT bits per int = ENTIER (1+ln(max int+1.0)/ln(max abs bit+1)); |
||
Line 44: | Line 41: | ||
int byte order +:= REPR(abcdi OVER (max abs bit+1) ** shift MOD (max abs char+1)) |
int byte order +:= REPR(abcdi OVER (max abs bit+1) ** shift MOD (max abs char+1)) |
||
OD; |
OD; |
||
printf(($"int byte order: "g,", Hex:",16r8dl$,int byte order, BIN abcdi)) |
printf(($"int byte order: "g,", Hex:",16r8dl$,int byte order, BIN abcdi))</lang> |
||
</pre> |
|||
Output (Intel i686): |
Output (Intel i686): |
||
⚫ | |||
<pre> |
|||
⚫ | |||
bits per char: 8 |
bits per char: 8 |
||
bits per int: 32 |
bits per int: 32 |
||
chars per int: 4 |
chars per int: 4 |
||
bits width: 32 |
bits width: 32 |
||
int byte order: ABCD, Hex:44434241 |
int byte order: ABCD, Hex:44434241</lang> |
||
</pre> |
|||
On older CPUs the results would vary: |
On older CPUs the results would vary: |
||
{|border="1" align="center" |
{|border="1" align="center" |
||
Line 60: | Line 54: | ||
|| ALGOL 68RS |
|| ALGOL 68RS |
||
|- |
|- |
||
⚫ | |||
|| ~ <pre> |
|||
⚫ | |||
bits per int: 24 |
bits per int: 24 |
||
chars per int: 4</ |
chars per int: 4</lang> |
||
|| ICL 2900 < |
|| ICL 2900 <lang algol68>bits per char: 8 |
||
bits per char: 8 |
|||
bits per int: 32 |
bits per int: 32 |
||
chars per int: 4</ |
chars per int: 4</lang> |
||
|| Multics < |
|| Multics <lang algol68>bits per char: 6 |
||
bits per char: 6 |
|||
bits per int: 36 |
bits per int: 36 |
||
chars per int: 6</ |
chars per int: 6</lang> |
||
|} |
|} |
||
Line 90: | Line 81: | ||
On POSIX-compatible systems, the following also tests the endianness (this makes use of the fact that network order is big endian): |
On POSIX-compatible systems, the following also tests the endianness (this makes use of the fact that network order is big endian): |
||
<lang c> |
<lang c>#include <stdio.h> |
||
#include <stdio.h> |
|||
#include <arpa/inet.h> |
#include <arpa/inet.h> |
||
Line 100: | Line 90: | ||
else |
else |
||
printf("little endian\n"); |
printf("little endian\n"); |
||
⚫ | |||
} |
|||
⚫ | |||
=={{header|D}}== |
=={{header|D}}== |
||
<lang d> |
<lang d>import std.stdio, std.system; |
||
import std.stdio, std.system; |
|||
void main() { |
void main() { |
||
writefln("word size = ", size_t.sizeof * 8); |
writefln("word size = ", size_t.sizeof * 8); |
||
writefln(endian == Endian.LittleEndian ? "little" : "big", " endian"); |
writefln(endian == Endian.LittleEndian ? "little" : "big", " endian"); |
||
⚫ | |||
} |
|||
⚫ | |||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
To find the word size: |
To find the word size: |
||
<lang erlang> |
<lang erlang>1> erlang:system_info(wordsize). |
||
⚫ | |||
1> erlang:system_info(wordsize). |
|||
4 |
|||
</lang> |
|||
In the case of endianness, Erlang's bit syntax by default has a 'native' option which lets you use what is supported natively. As such, there is no function to find endianness. However, one could write one by using bit syntax, setting endianness and then comparing to the native format: |
In the case of endianness, Erlang's bit syntax by default has a 'native' option which lets you use what is supported natively. As such, there is no function to find endianness. However, one could write one by using bit syntax, setting endianness and then comparing to the native format: |
||
<lang erlang> |
<lang erlang>1> <<1:4/native-unit:8>>. |
||
1> <<1:4/native-unit:8>>. |
|||
<<1,0,0,0>> |
<<1,0,0,0>> |
||
2> <<1:4/big-unit:8>> |
2> <<1:4/big-unit:8>> |
||
<<0,0,0,1>> |
<<0,0,0,1>> |
||
3> <<1:4/little-unit:8>>. |
3> <<1:4/little-unit:8>>. |
||
<<1,0,0,0>> |
<<1,0,0,0>></lang> |
||
</lang> |
|||
And so the following function would output endiannes: |
And so the following function would output endiannes: |
||
<lang erlang>endianness() when <<1:4/native-unit:8>> =:= <<1:4/big-unit:8>> -> big; |
|||
<lang erlang> |
|||
endianness() |
endianness() -> little.</lang> |
||
endianness() -> little. |
|||
</lang> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
<lang factor>USING: alien.c-types io layouts ; |
|||
"Word size: " write cell 8 * . |
|||
"Endianness: " write little-endian? "little" "big" ? print</lang> |
|||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
<lang forth>: endian |
|||
cr 1 cells . ." address units per cell" |
|||
s" ADDRESS-UNIT-BITS" environment? if cr . ." bits per address unit" then |
|||
cr 1 here ! here c@ if ." little" else ." big" then ." endian" ;</lang> |
|||
This relies on '''c@''' being a byte fetch (4 chars = 1 cells). Although it is on most architectures, ANS Forth only guarantees that 1 chars <= 1 cells. Some Forths like OpenFirmware have explicitly sized fetches, like b@. |
This relies on '''c@''' being a byte fetch (4 chars = 1 cells). Although it is on most architectures, ANS Forth only guarantees that 1 chars <= 1 cells. Some Forths like OpenFirmware have explicitly sized fetches, like b@. |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{works with|Fortran|90 and later}} |
{{works with|Fortran|90 and later}} |
||
<lang fortran>INTEGER, PARAMETER :: i8 = SELECTED_INT_KIND(2) |
|||
INTEGER, PARAMETER :: i16 = SELECTED_INT_KIND(4) |
|||
INTEGER(i8) :: a(2) |
|||
INTEGER(i16) :: b |
|||
WRITE(*,*) bit_size(1) ! number of bits in the default integer type |
|||
! which may (or may not!) equal the word size |
|||
b = Z'1234' ! Hexadecimal assignment |
|||
a = (TRANSFER(b, a)) ! Split a 16 bit number into two 8 bit numbers |
|||
IF (a(1) == Z'12') THEN ! where did the most significant 8 bits end up |
|||
WRITE(*,*) "Big Endian" |
|||
ELSE |
|||
WRITE(*,*) "Little Endian" |
|||
END IF</lang> |
|||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
⚫ | |||
<pre> |
|||
⚫ | |||
main = print $ bitSize (undefined :: Int) -- print word size |
main = print $ bitSize (undefined :: Int) -- print word size</lang> |
||
</pre> |
|||
Dunno about endianness |
Dunno about endianness |
||
Line 179: | Line 158: | ||
'''Method A''': |
'''Method A''': |
||
":&> (|: 32 64 ;"0 big`little) {"_1~ 2 2 #: 16b_e0 + a. i. 0 { 3!:1 <nowiki>''</nowiki> |
<lang j> ":&> (|: 32 64 ;"0 big`little) {"_1~ 2 2 #: 16b_e0 + a. i. 0 { 3!:1 <nowiki>''</nowiki> |
||
32 |
|||
little</lang> |
|||
'''Method B''': |
'''Method B''': |
||
((4*#) ,:&": little`big {::~ '7'={.) {: 3!:3 ] 33 b.~_1 |
<lang j> ((4*#) ,:&": little`big {::~ '7'={.) {: 3!:3 ] 33 b.~_1 |
||
32 |
|||
little</lang> |
|||
=={{header|Java}}== |
=={{header|Java}}== |
||
<lang java>System.out.println("word size: "+System.getProperty("sun.arch.data.model")); |
<lang java>System.out.println("word size: "+System.getProperty("sun.arch.data.model")); |
||
Line 248: | Line 227: | ||
>>> socket.gethostname() |
>>> socket.gethostname() |
||
'PADDY3118-RESTING' |
'PADDY3118-RESTING' |
||
>>> |
>>></lang> |
||
=={{header|R}}== |
=={{header|R}}== |
||
Word size |
Word size |
||
⚫ | |||
<lang R> |
|||
⚫ | |||
# or |
# or |
||
object.size(0L) # e.g. 32 bytes |
object.size(0L) # e.g. 32 bytes</lang> |
||
</lang> |
|||
Endianness |
Endianness |
||
⚫ | |||
<lang R> |
|||
⚫ | |||
</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
<lang ruby> |
<lang ruby>word_size = 42.size * 8 |
||
byte = [1].pack('i')[0] |
|||
byte = byte.ord if RUBY_VERSION >= "1.9" |
|||
byte_order = (byte == 0 ? 'big' : 'little') + ' endian'</lang> |
|||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
Line 287: | Line 262: | ||
=={{header|Slate}}== |
=={{header|Slate}}== |
||
<lang slate>inform: 'Endianness: ' ; Platform current endianness. |
|||
<lang slate> |
|||
inform: ' |
inform: 'Word Size: ' ; (Platform current bytesPerWord * 8) printString.</lang> |
||
inform: 'Word Size: ' ; (Platform current bytesPerWord * 8) printString. |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 312: | Line 285: | ||
=={{header|TI-89 BASIC}}== |
=={{header|TI-89 BASIC}}== |
||
<lang ti89b>Disp "32-bit big-endian"</lang> |
|||
{{omit from|E}} |
{{omit from|E}} |
Revision as of 17:29, 20 November 2009
You are encouraged to solve this task according to the task description, using any language you may know.
Print the word size and endianness of the host machine.
Ada
<lang ada>with Ada.Text_IO; use Ada.Text_IO; with System; use System;
procedure Host_Introspection is begin
Put_Line ("Word size" & Integer'Image (Word_Size)); Put_Line ("Endianness " & Bit_Order'Image (Default_Bit_Order));
end Host_Introspection;</lang> Sample output on a Pentium machine:
Word size 32 Endianness LOW_ORDER_FIRST
ALGOL 68
<lang algol68>INT max abs bit = ABS(BIN 1 SHL 1)-1; INT bits per char = ENTIER (ln(max abs char+1)/ln(max abs bit+1)); INT bits per int = ENTIER (1+ln(max int+1.0)/ln(max abs bit+1));
printf(($"states per bit: "dl$,max abs bit+1)); printf(($"bits per char: "z-dl$,bits per char)); printf(($"bits per int: "z-dl$,bits per int)); printf(($"chars per int: "z-dl$,bits per int OVER bits per char));
printf(($"bits width: "z-dl$, bits width));
STRING abcds = "ABCD"; FILE abcdf; INT abcdi;
INT errno := open(abcdf, "abcd.dat",stand back channel); put(abcdf,abcds); # output alphabetically # reset(abcdf); get bin(abcdf,abcdi); # input in word byte order # STRING int byte order := ""; FOR shift FROM 0 BY bits per char TO bits per int - bits per char DO
int byte order +:= REPR(abcdi OVER (max abs bit+1) ** shift MOD (max abs char+1))
OD; printf(($"int byte order: "g,", Hex:",16r8dl$,int byte order, BIN abcdi))</lang> Output (Intel i686): <lang algol68>states per bit: 2 bits per char: 8 bits per int: 32 chars per int: 4 bits width: 32 int byte order: ABCD, Hex:44434241</lang> On older CPUs the results would vary:
ALGOL 68R | ALGOL 68RS | |
~ <lang algol68>bits per char: 6
bits per int: 24 chars per int: 4</lang> |
ICL 2900 <lang algol68>bits per char: 8
bits per int: 32 chars per int: 4</lang> |
Multics <lang algol68>bits per char: 6
bits per int: 36 chars per int: 6</lang> |
C
<lang c>#include <stdio.h>
- include <stddef.h> /* for size_t */
- include <limits.h> /* for CHAR_BIT */
int main() {
int one = 1; printf("word size = %d\n", CHAR_BIT * sizeof(size_t)); /* best bet: size_t typically is exactly one word */ if (*(char *)&one) /* if the least significant bit is located in the lowest-address byte */ printf("little endian\n"); else printf("big endian\n"); return 0;
}</lang>
On POSIX-compatible systems, the following also tests the endianness (this makes use of the fact that network order is big endian): <lang c>#include <stdio.h>
- include <arpa/inet.h>
int main() {
if (htonl(1) == 1) printf("big endian\n"); else printf("little endian\n");
}</lang>
D
<lang d>import std.stdio, std.system;
void main() {
writefln("word size = ", size_t.sizeof * 8); writefln(endian == Endian.LittleEndian ? "little" : "big", " endian");
}</lang>
Erlang
To find the word size: <lang erlang>1> erlang:system_info(wordsize). 4</lang>
In the case of endianness, Erlang's bit syntax by default has a 'native' option which lets you use what is supported natively. As such, there is no function to find endianness. However, one could write one by using bit syntax, setting endianness and then comparing to the native format:
<lang erlang>1> <<1:4/native-unit:8>>. <<1,0,0,0>> 2> <<1:4/big-unit:8>> <<0,0,0,1>> 3> <<1:4/little-unit:8>>. <<1,0,0,0>></lang>
And so the following function would output endiannes:
<lang erlang>endianness() when <<1:4/native-unit:8>> =:= <<1:4/big-unit:8>> -> big; endianness() -> little.</lang>
Factor
<lang factor>USING: alien.c-types io layouts ; "Word size: " write cell 8 * . "Endianness: " write little-endian? "little" "big" ? print</lang>
Forth
<lang forth>: endian
cr 1 cells . ." address units per cell" s" ADDRESS-UNIT-BITS" environment? if cr . ." bits per address unit" then cr 1 here ! here c@ if ." little" else ." big" then ." endian" ;</lang>
This relies on c@ being a byte fetch (4 chars = 1 cells). Although it is on most architectures, ANS Forth only guarantees that 1 chars <= 1 cells. Some Forths like OpenFirmware have explicitly sized fetches, like b@.
Fortran
<lang fortran>INTEGER, PARAMETER :: i8 = SELECTED_INT_KIND(2) INTEGER, PARAMETER :: i16 = SELECTED_INT_KIND(4) INTEGER(i8) :: a(2) INTEGER(i16) :: b
WRITE(*,*) bit_size(1) ! number of bits in the default integer type
! which may (or may not!) equal the word size
b = Z'1234' ! Hexadecimal assignment a = (TRANSFER(b, a)) ! Split a 16 bit number into two 8 bit numbers
IF (a(1) == Z'12') THEN ! where did the most significant 8 bits end up
WRITE(*,*) "Big Endian"
ELSE
WRITE(*,*) "Little Endian"
END IF</lang>
Haskell
<lang haskell>import Data.Bits
main = print $ bitSize (undefined :: Int) -- print word size</lang> Dunno about endianness
J
Method A:
<lang j> ":&> (|: 32 64 ;"0 big`little) {"_1~ 2 2 #: 16b_e0 + a. i. 0 { 3!:1 '' 32 little</lang>
Method B:
<lang j> ((4*#) ,:&": little`big {::~ '7'={.) {: 3!:3 ] 33 b.~_1 32 little</lang>
Java
<lang java>System.out.println("word size: "+System.getProperty("sun.arch.data.model")); System.out.println("endianness: "+System.getProperty("sun.cpu.endian"));</lang>
Modula-3
<lang modula3>MODULE Host EXPORTS Main;
IMPORT IO, Fmt, Word, Swap;
BEGIN
IO.Put("Word Size: " & Fmt.Int(Word.Size) & "\n"); IF Swap.endian = Swap.Endian.Big THEN IO.Put("Endianness: Big\n"); ELSE IO.Put("Endianness: Little\n"); END;
END Host.</lang>
Output (on an x86):
Word Size: 32 Endianness: Little
Objective-C
<lang objc>NSLog(@"operating system name: %@", [[NSProcessInfo processInfo] operatingSystemName]); NSLog(@"operating system version: %@", [[NSProcessInfo processInfo] operatingSystemVersionString]); //Mac OS X 10.5+: NSLog(@"number of bytes of physical memory: %llu", [[NSProcessInfo processInfo] physicalMemory]); NSLog(@"number of processors: %u", [[NSProcessInfo processInfo] processorCount]);</lang>
Dunno about word size and endianness.
OCaml
<lang ocaml>Printf.printf "%d\n" Sys.word_size; (* Print word size *) Printf.printf "%s\n" Sys.os_type; (* Print operating system *)</lang> Dunno about endianness
PowerShell
<lang powershell>Write-Host Word Size: ((Get-WMIObject Win32_Processor).DataWidth) Write-Host -NoNewLine "Endianness: " if ([BitConverter]::IsLittleEndian) {
Write-Host Little-Endian
} else {
Write-Host Big-Endian
}</lang> Note that endianness is essentially a moot point with PowerShell, as there is only a Windows implementation currently and current Windows versions don't run on big-endian systems. But in theory this check should work.
Python
<lang python>>>> import sys, math >>> int(round(math.log(sys.maxint,2)+1)) # this only works in Python 2.x 32 >>> import struct >>> struct.calcsize('i') * 8 32 >>> sys.byteorder little >>> import socket >>> socket.gethostname() 'PADDY3118-RESTING' >>></lang>
R
Word size <lang R>8 * .Machine$sizeof.long # e.g. 32
- or
object.size(0L) # e.g. 32 bytes</lang> Endianness <lang R>.Platform$endian # e.g. "little"</lang>
Ruby
<lang ruby>word_size = 42.size * 8
byte = [1].pack('i')[0] byte = byte.ord if RUBY_VERSION >= "1.9" byte_order = (byte == 0 ? 'big' : 'little') + ' endian'</lang>
Scheme
<lang scheme>(define host-info
(begin (display "Endianness: ") (display (machine-byte-order)) (newline) (display "Word Size: ") (display (if (fixnum? (expt 2 33)) 64 32)) (newline)))</lang>
Output:
Endianness: little-endian Word Size: 32
Slate
<lang slate>inform: 'Endianness: ' ; Platform current endianness. inform: 'Word Size: ' ; (Platform current bytesPerWord * 8) printString.</lang> Output:
Endianness: LittleEndian Word Size: 32
Tcl
This is very straightforward in Tcl. The global array tcl_platform
contains these values. In an interactive tclsh
:
<lang tcl>% parray tcl_platform
tcl_platform(byteOrder) = littleEndian
tcl_platform(machine) = intel
tcl_platform(os) = Windows NT
tcl_platform(osVersion) = 5.1
tcl_platform(platform) = windows
tcl_platform(pointerSize) = 4
tcl_platform(threaded) = 1
tcl_platform(user) = glennj
tcl_platform(wordSize) = 4</lang>
TI-89 BASIC
<lang ti89b>Disp "32-bit big-endian"</lang>