This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,3 @@
Print the [[wp:Word_size#Word_size_choice|word size]] and [[wp:Endianness|endianness]] of the host machine.
See also: [[Variable size/Get]]

View file

@ -0,0 +1,2 @@
---
note: Programming environment operations

View file

@ -0,0 +1,24 @@
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))

View file

@ -0,0 +1,8 @@
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;

View file

@ -0,0 +1,7 @@
DIM P% 8
!P% = -1
I% = 0 : REPEAT I% += 1 : UNTIL P%?I%=0
PRINT "Word size = " ; I% " bytes"
!P% = 1
IF P%?0 = 1 THEN PRINT "Little-endian"
IF P%?(I%-1) = 1 THEN PRINT "Big-endian"

View file

@ -0,0 +1,3 @@
main :
{ "Word size: " << msize 3 shl %d << " bits" cr <<
"Endianness: " << { endian } { "little" } { "big" } ifte cr << }

View file

@ -0,0 +1,9 @@
static void Main()
{
Console.WriteLine("Word size = {0} bytes,",sizeof(int));
if (BitConverter.IsLittleEndian)
Console.WriteLine("Little-endian.");
else
Console.WriteLine("Big-endian.");
}

View file

@ -0,0 +1,22 @@
#include <stdio.h>
#include <stddef.h> /* for size_t */
#include <limits.h> /* for CHAR_BIT */
int main() {
int one = 1;
/*
* Best bet: size_t typically is exactly one word.
*/
printf("word size = %d bits\n", (int)(CHAR_BIT * sizeof(size_t)));
/*
* Check if the least significant bit is located
* in the lowest-address byte.
*/
if (*(char *)&one)
printf("little endian\n");
else
printf("big endian\n");
return 0;
}

View file

@ -0,0 +1,10 @@
#include <stdio.h>
#include <arpa/inet.h>
int main()
{
if (htonl(1) == 1)
printf("big endian\n");
else
printf("little endian\n");
}

View file

@ -0,0 +1,2 @@
(println "word size: " (System/getProperty "sun.arch.data.model"))
(println "endianness: " (System/getProperty "sun.cpu.endian"))

View file

@ -0,0 +1 @@
(machine-type) ;; => "X86-64" on SBCL here

View file

@ -0,0 +1,6 @@
import std.stdio, std.system;
void main() {
writefln("word size = ", size_t.sizeof * 8);
writefln(endian == Endian.LittleEndian ? "little" : "big", " endian");
}

View file

@ -0,0 +1,10 @@
program HostIntrospection ;
{$APPTYPE CONSOLE}
uses SysUtils;
begin
Writeln('word size: ', SizeOf(Integer));
Writeln('endianness: little endian'); // Windows is always little endian
end.

View file

@ -0,0 +1,2 @@
1> erlang:system_info(wordsize).
4

View file

@ -0,0 +1,6 @@
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>>

View file

@ -0,0 +1,2 @@
endianness() when <<1:4/native-unit:8>> =:= <<1:4/big-unit:8>> -> big;
endianness() -> little.

View file

@ -0,0 +1,3 @@
USING: alien.c-types alien.data io layouts ;
"Word size: " write cell 8 * .
"Endianness: " write little-endian? "little" "big" ? print

View file

@ -0,0 +1,4 @@
: 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" ;

View file

@ -0,0 +1,13 @@
integer :: i
character(len=1) :: c(20)
equivalence (c, i)
WRITE(*,*) bit_size(1) ! number of bits in the default integer type
! which may (or may not!) equal the word size
i = 1
IF (ichar(c(1)) == 0) THEN
WRITE(*,*) "Big Endian"
ELSE
WRITE(*,*) "Little Endian"
END IF

View file

@ -0,0 +1,38 @@
package main
import (
"fmt"
"io/ioutil"
"strings"
"unsafe"
)
func main() {
// inspect an int variable to determine endianness
x := 1
if *(*byte)(unsafe.Pointer(&x)) == 1 {
fmt.Println("little endian")
} else {
fmt.Println("big endian")
}
// inspect cpuinfo to determine word size (unix-like os only)
c, err := ioutil.ReadFile("/proc/cpuinfo")
if err != nil {
fmt.Println(err)
return
}
ls := strings.Split(string(c), "\n")
for _, l := range ls {
if strings.HasPrefix(l, "flags") {
for _, f := range strings.Fields(l) {
if f == "lm" { // "long mode"
fmt.Println("64 bit word size")
return
}
}
fmt.Println("32 bit word size")
return
}
}
fmt.Println("cpuinfo flags not found")
}

View file

@ -0,0 +1,17 @@
package main
import (
"debug/elf"
"fmt"
"os"
)
func main() {
f, err := elf.Open(os.Args[0])
if err != nil {
fmt.Println(" ", err)
return
}
fmt.Println(f.FileHeader.ByteOrder)
f.Close()
}

View file

@ -0,0 +1,2 @@
println "word size: ${System.getProperty('sun.arch.data.model')}"
println "endianness: ${System.getProperty('sun.cpu.endian')}"

View file

@ -0,0 +1,8 @@
import Data.Bits
import ADNS.Endian -- http://hackage.haskell.org/package/hsdns
main = do
putStrLn $ "Word size: " ++ bitsize
putStrLn $ "Endianness: " ++ show endian
where
bitsize = show $ bitSize (undefined :: Int)

View file

@ -0,0 +1,2 @@
IF64 {32 64
64

View file

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

View file

@ -0,0 +1,8 @@
import java.nio.ByteOrder;
public class ShowByteOrder {
public static void main(String[] args) {
// Print "BIG_ENDIAN" or "LITTLE_ENDIAN".
System.out.println(ByteOrder.nativeOrder());
}
}

View file

@ -0,0 +1,2 @@
System.out.println("word size: "+System.getProperty("sun.arch.data.model"));
System.out.println("endianness: "+System.getProperty("sun.cpu.endian"));

View file

@ -0,0 +1,17 @@
function [endian]=endian()
fid=tmpfile();
fwrite(fid,1:8,'uint8');
fseek(fid,0,'bof');
t=fread(fid,8,'int8');
i8=sprintf('%02X',t);
fseek(fid,0,'bof');
t=fread(fid,4,'int16');
i16=sprintf('%04X',t);
fclose(fid);
if strcmp(i8,i16) endian='big';
else endian='little';
end;

View file

@ -0,0 +1,2 @@
If[$ByteOrdering > 0, Print["Big endian"], Print["Little endian" ]]
$SystemWordLength "bits"

View file

@ -0,0 +1,12 @@
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.

View file

@ -0,0 +1,2 @@
use Config;
print "UV size: $Config{uvsize}, byte order: $Config{byteorder}\n";

View file

@ -0,0 +1,9 @@
use 5.010;
use Config;
my ($size, $order, $end) = @Config{qw(uvsize byteorder)};
given ($order) {
when (join '', sort split '') { $end = 'little' }
when (join '', reverse sort split '') { $end = 'big' }
default { $end = 'mixed' }
}
say "UV size: $size, byte order: $order ($end-endian)";

View file

@ -0,0 +1,12 @@
(in (cmd) # Inspect ELF header
(rd 4) # Skip "7F" and 'E', 'L' and 'F'
(prinl
(case (rd 1) # Get EI_CLASS byte
(1 "32 bits")
(2 "64 bits")
(T "Bad EI_CLASS") ) )
(prinl
(case (rd 1) # Get EI_DATA byte
(1 "Little endian")
(2 "Big endian")
(T "Bad EI_DATA") ) ) )

View file

@ -0,0 +1,12 @@
>>> 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'
>>>

View file

@ -0,0 +1,3 @@
8 * .Machine$sizeof.long # e.g. 32
# or
object.size(0L) # e.g. 32 bytes

View file

@ -0,0 +1 @@
.Platform$endian # e.g. "little"

View file

@ -0,0 +1,6 @@
/*REXX program to examine which operating system that REXX is running under. */
parse source opSys howInvoked pathName
/*where opSys will indicate which operating system REXX is running under, and */
/*from that, one could make assumptions what the wordsize is, etc. */

View file

@ -0,0 +1,4 @@
#lang racket/base
(printf "Word size: ~a\n" (system-type 'word))
(printf "Endianness: ~a\n" (if (system-big-endian?) 'big 'little))

View file

@ -0,0 +1,10 @@
# We assume that a Fixnum occupies one machine word.
# Fixnum#size returns bytes (1 byte = 8 bits).
word_size = 42.size * 8
puts "Word size: #{word_size} bits"
# Array#pack knows the native byte order. We pack 1 as a 16-bit integer,
# then unpack bytes: [0, 1] is big endian, [1, 0] is little endian.
bytes = [1].pack('S').unpack('C*')
byte_order = (bytes[0] == 0 ? 'big' : 'little') + ' endian'
puts "Byte order: #{byte_order}"

View file

@ -0,0 +1,8 @@
(define host-info
(begin
(display "Endianness: ")
(display (machine-byte-order))
(newline)
(display "Word Size: ")
(display (if (fixnum? (expt 2 33)) 64 32))
(newline)))

View file

@ -0,0 +1,10 @@
% 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