Cosmopolitan Libc: build-once run-anywhere C library

Last modified on December 28, 2020

Cosmopolitan makes C a manufacture-as quickly as dart-anywhere language, similar to
Java, except it does not require interpreters or digital machines be
put in beforehand. Cosmo offers the a similar portability benefits as
high-stage languages like Prance and Rust, nonetheless it fully does not create a bizarre
language and likewise you may wish to presumably merely no longer have to configure a CI map to fabricate separate
binaries for each and each working map. What Cosmopolitan focuses on is
fixing C by decoupling it from platforms, so it might moreover be fairly to make use of
for writing diminutive unix purposes which might be merely distributed to a needed
broader viewers.

Getting Started

Assuming you dangle GCC on Linux, then all you will have are the 5
additional information which may presumably be linked under:

# manufacture simple c program on say line
echo '
  major() {
    printf("whats up worldn");
  }
'>whats up.c

# dart gcc compiler in freestanding mode
gcc -g -Os -static -fno-pie -mno-crimson-zone -nostdlib -nostdinc -o whats up.com whats up.c 
  -Wl,--oformat=binary -Wl,--gc-sections -Wl,-z,max-page-measurement=0x1000 
  -Wl,-T,ape.lds -contain cosmopolitan.h crt.o ape.o cosmopolitan.a

# ~40kb static binary (may moreover be ~16kb w/ MODE=diminutive)
./whats up.com

The above say fixes GCC so it outputs moveable binaries that may
dart on each Linux distro furthermore to to Mac OS X, Windows NT,
FreeBSD, and OpenBSD too. For diminutive print on how this works, please study
the αcτµαlly pδrταblε εxεcµταblε weblog put up. This
current binary format can be elective: ragged ELF binaries may moreover be
compiled too by putting off the -Wl,--oformat=binary flag.

Your program may also boot on naked steel too. In numerous phrases, you may wish to presumably presumably dangle
written a original textbook C program, and due to Cosmopolitan's
low-stage linker magic, you may wish to presumably presumably dangle efficiently created your luxuriate in working
map which occurs to dart on the entire novel ones as efficiently. Now
that's one thing no particular person's carried out sooner than.

Mailing Record

Please be part of
the Cosmopolitan
Cosmonauts
Google Neighborhood!

Efficiency

Cosmopolitan has been optimized by hand for attractive effectivity on
trendy desktops and servers. In comparability with glibc, it's indispensable to look information from
Cosmopolitan to be virtually as snappy, nonetheless with an advise of a magnitude
tinier code measurement. In comparability with Musl or Newlib, you may wish to presumably search information from that
Cosmopolitan will on the whole lag needed quicker, whereas having roughly the
linked code measurement, if no longer tinier.

In the case of the most important libc goal, memcpy(),
Cosmopolitan outperformed each numerous originate supply library examined. The
chart under reveals how snappy reminiscence is transferred depending on the
measurement of the copy. Since it's log scale, every grid sq. represents a
2x distinction in effectivity. What makes Cosmopolitan so snappy right here is
it makes make use of of makes make use of of a number of numerous reminiscence copying techniques. For diminutive
sizes it makes make use of of an oblique division with overlapping strikes; for medium
sizes it makes make use of of simd vectors, and for giant copies it makes make use of of nontemporal
hints which stop cache thrash. Other libraries repeatedly fall transient
due to they make use of a one-measurement-fits-all strategy. Let's assume, Newlib
goes 10x slower for the optimum block measurement (half of L1 cache) due to it
frequently does nontemporal strikes.


memcpy() performance for varying n values

Trickle-Down Efficiency

Performing the right on benchmarks is no longer satisfactory. Cosmopolitan additionally makes make use of of
a second strategy that the above benchmark does not measure, which we
identify "trickle-down effectivity". For an instance of how that works,
look after in concepts the next basic reality about C which is steadily misplaced sight of.
Exterior goal calls such as a result of the next:

memcpy(foo, bar, n);

Are roughly equal to the next meeting, which leads
compilers to purchase that nearly all cpu voice is clobbered:

asm unstable("identify memcpy"
             : "=a"(rax), "=D"(rdi), "=S"(rsi), "=d"(rdx)
             : "1"(foo), "2"(bar), "3"(n)
             : "rcx", "r8", "r9", "r10", "r11", "reminiscence", "cc",
               "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6");

In numerous phrases the compiler assumes that, in calling the goal,
fifteen separate registers and all reminiscence will be overwritten. Conception
the Machine V
ABI
for additional diminutive print. This may moreover be problematic for
usually-known as features corresponding to memcpy, because it inhibits many
optimizations and it tosses a wrench throughout the compiler register
allocation algorithm, thus inflicting stack spillage which additional
degrades effectivity whereas bloating the output binary measurement.

So what Cosmopolitan does for memcpy() and loads of numerous
usually-known as core library leaf features, is defining a simple
macro wrapper, which tells the compiler the gorgeous subset of the abi
that's genuinely major, e.g.

#define memcpy(DEST, SRC, N) ({       
  void *Dest=(DEST);                
  void *Src=(SRC);                  
  size_t Measurement=(N);                  
  asm("identify memcpy"                   
      : "=m"(*(char[Size][Size])(Dest))  
      : "D"(Dest), "S"(Src), "d"(n),  
        "m"(*(char[Size][Size])(Src))    
      : "rcx", "xmm3", "xmm4", "cc"); 
    Dest;                             
  })

What this implies, is that Cosmopolitan memcpy() is no longer merely snappy, it
additionally makes unrelated code throughout the features that identify it quicker too as
a aspect-discontinue. When this strategy was first carried out for memcpy()
alone, most of the features throughout the Cosmopolitan codebase had their
generated code measurement decreased by a 3rd.

For an instance of 1 such goal, look after in concepts strlcpy,
which is the BSD come of saying strcpy:

/Copies string, the BSD come.
 @param d is buffer which needn't be initialized
 @param s is a NUL-terminated string
 @param n is byte talent of d
 @return strlen(s)
 @level to d and s can't overlap
 @level to we grasp memccpy()
 */
size_t strlcpy(char *d, const char *s, size_t n) {
  size_t slen, loyal;
  slen=strlen(s);
  if (n) {
    loyal=MIN(n - 1, slen);
    memcpy(d, s, loyal);
    d[actual]=' ';
  }
  return slen;
}

If we compile our strlcpy goal, then here is the
meeting code that the compiler outputs:

/ compiled with ragged libc
strlcpy:
	push	%rbp
	mov	%rsp,%rbp
	push	%r14
	mov	%rsi,%r14
	push	%r13
	mov	%rdi,%r13
	mov	%rsi,%rdi
	push	%r12
	push	%rbx
	mov	%rdx,%rbx
	identify	strlen
	mov	%rax,%r12
	take a look at	%rbx,%rbx
	jne	1f
	pop	%rbx
	mov	%r12,%rax
	po

Read More

Similar Products:

    None Found

Recent Content