1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

compiler: add support for $if x32, x64, big_endian, little_endian

This commit is contained in:
Delyan Angelov
2019-11-15 15:14:28 +02:00
committed by Alexander Medvednikov
parent 751a89ccc8
commit e577b40743
9 changed files with 144 additions and 22 deletions

View File

@@ -4,8 +4,6 @@
module runtime
import os
//$if linux {
fn C.sysconf(name int) i64
//}
@@ -15,18 +13,32 @@ fn C.GetCurrentProcessorNumber() u32
//}
pub fn nr_cpus() int {
$if linux {
return int(C.sysconf(C._SC_NPROCESSORS_ONLN))
}
$if mac {
return int(C.sysconf(C._SC_NPROCESSORS_ONLN))
}
$if windows {
mut nr := int(C.GetCurrentProcessorNumber())
if nr == 0 {
nr = os.getenv('NUMBER_OF_PROCESSORS').int()
}
return nr
return nr_cpus_win()
}
return 1
return nr_cpus_nix()
}
pub fn is_32bit() bool {
mut x := false
$if x32 { x = true }
return x
}
pub fn is_64bit() bool {
mut x := false
$if x64 { x = true }
return x
}
pub fn is_little_endian() bool {
mut x := false
$if little_endian { x = true }
return x
}
pub fn is_big_endian() bool {
mut x := false
$if big_endian { x = true }
return x
}

View File

@@ -0,0 +1,16 @@
module runtime
fn nr_cpus_nix() int {
$if linux {
return int(C.sysconf(C._SC_NPROCESSORS_ONLN))
}
$if mac {
return int(C.sysconf(C._SC_NPROCESSORS_ONLN))
}
return 1
}
fn nr_cpus_win() int {
eprintln('nr_cpus_win should be callable only for windows')
return 1
}

View File

@@ -4,3 +4,31 @@ fn test_nr_cpus() {
nr_cpus := runtime.nr_cpus()
assert nr_cpus > 0
}
fn test_is_32bit(){
x := runtime.is_32bit().str()
assert x == 'true' || x == 'false'
}
fn test_is_64bit(){
x := runtime.is_64bit().str()
assert x == 'true' || x == 'false'
}
fn test_is_little_endian(){
x := runtime.is_little_endian().str()
assert x == 'true' || x == 'false'
}
fn test_is_big_endian(){
x := runtime.is_big_endian().str()
assert x == 'true' || x == 'false'
}
fn test_is_big_endian_different_than_is_little_endian(){
assert runtime.is_big_endian() != runtime.is_little_endian()
}
fn test_is_32bit_different_than_is_64bit(){
assert runtime.is_32bit() != runtime.is_64bit()
}

View File

@@ -1 +1,16 @@
module runtime
import os
fn nr_cpus_win() int {
mut nr := int(C.GetCurrentProcessorNumber())
if nr == 0 {
nr = os.getenv('NUMBER_OF_PROCESSORS').int()
}
return nr
}
fn nr_cpus_nix() int {
eprintln('nr_cpus_nix should be callable only for nix platforms')
return 1
}