Host introspection: Difference between revisions

From Rosetta Code
Content added Content deleted
(added ruby)
Line 169: Line 169:
=={{header|Python}}==
=={{header|Python}}==
<python>>>> import sys, math
<python>>>> import sys, math
>>> int(round(math.log(sys.maxint,2)+1))
>>> int(round(math.log(sys.maxint,2)+1)) # this only works in Python 2.x
32
>>> import struct
>>> struct.calcsize('i') * 8
32
32
>>> sys.byteorder
>>> sys.byteorder

Revision as of 06:44, 1 January 2009

Task
Host introspection
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

<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; </ada> Sample output on a Pentium machine:

Word size 32
Endianness LOW_ORDER_FIRST

ALGOL 68

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))

Output (Intel i686):

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

On older CPUs the results would vary:

ALGOL 68R ALGOL 68RS
~
bits per char:   6
bits per int:   24
chars per int:   4
ICL 2900
bits per char:   8
bits per int:   32
chars per int:   4
Multics
bits per char:   6
bits per int:   36
chars per int:   6

C

<c>#include <stdio.h>

  1. include <stddef.h> /* for size_t */
  2. 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;

}</c>

On POSIX-compatible systems, the following also tests the endianness (this makes use of the fact that network order is big endian): <c>

  1. include <stdio.h>
  2. include <arpa/inet.h>

int main() {

 if (htonl(1) == 1)
   printf("big endian\n");
 else
   printf("little endian\n");

} </c>

D

<d> import std.stdio, std.system;

void main() {

 writefln("word size = ", size_t.sizeof * 8);
 writefln(endian == Endian.LittleEndian ? "little" : "big", " endian");

} </d>

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" ;

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

Works with: Fortran version 90 and later
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

Haskell

import Data.Bits

main = print $ bitSize (undefined :: Int) -- print word size

Dunno about endianness

J

Method A:

   ":&> (|: 32 64 ;"0 big`little) {"_1~ 2 2 #: 16b_e0 + a. i. 0 { 3!:1 ''  
32
little

Method B:

   ((4*#) ,:&": little`big {::~ '7'={.) {: 3!:3 ] 33 b.~_1
32
little

Java

<java>System.out.println("word size: "+System.getProperty("sun.arch.data.model")); System.out.println("endianness: "+System.getProperty("sun.cpu.endian"));</java>

OCaml

<ocaml>Printf.printf "%d\n" Sys.word_size; (* Print word size *) Printf.printf "%s\n" Sys.os_type; (* Print operating system *)</ocaml> Dunno about endianness

Python

<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' >>> </python>

Ruby

irb(main):001:0> 42.size * 8
=> 32
irb(main):002:0> if [1].pack('i')[0] != 0
irb(main):003:1>   'little endian'
irb(main):004:1> else
irb(main):005:1*   'big endian'
irb(main):006:1> end
=> "little endian"