Click here to Skip to main content
15,883,843 members
Articles / Desktop Programming / MFC

Tiny C Runtime Library

Rate me:
Please Sign up or sign in to vote.
4.86/5 (60 votes)
25 Mar 20079 min read 332K   5.6K   123  
Reduce code bloat for those simple utility programs by using a streamlined C runtime - now with Unicode support!
// math.cpp

// math functions

// 08/20/05 (mv)

#include <windows.h>
#include "libct.h"

BEGIN_EXTERN_C

static int lastrand;
int _fltused = 0;

int abs(int n)
{
	return (n>0)?n:-n;
}

void srand(unsigned int seed)
{
	lastrand = seed;
}

int rand()
{
	return (((lastrand = lastrand * 214013L + 2531011L) >> 16) & 0x7FFF);
}

__declspec(naked) void _CIacos()
{
	__asm
	{
		fld		st(0)
		fld		st(0)
		fmul
		fld1
		fsubr
		fsqrt
		fxch
		fpatan
		ret
	}
}

__declspec(naked) void _ftol2()
{
	__asm
	{
		push        ebp
		mov         ebp,esp
		sub         esp,20h
		and         esp,0FFFFFFF0h
		fld         st(0)
		fst         dword ptr [esp+18h]
		fistp       qword ptr [esp+10h]
		fild        qword ptr [esp+10h]
		mov         edx,dword ptr [esp+18h]
		mov         eax,dword ptr [esp+10h]
		test        eax,eax
		je          integer_QnaN_or_zero
	arg_is_not_integer_QnaN:
		fsubp       st(1),st
		test        edx,edx
		jns         positive
		fstp        dword ptr [esp]
		mov         ecx,dword ptr [esp]
		xor         ecx,80000000h
		add         ecx,7FFFFFFFh
		adc         eax,0
		mov         edx,dword ptr [esp+14h]
		adc         edx,0
		jmp         localexit
	positive:
		fstp        dword ptr [esp]
		mov         ecx,dword ptr [esp]
		add         ecx,7FFFFFFFh
		sbb         eax,0
		mov         edx,dword ptr [esp+14h]
		sbb         edx,0
		jmp         localexit
	integer_QnaN_or_zero:
		mov         edx,dword ptr [esp+14h]
		test        edx,7FFFFFFFh
		jne         arg_is_not_integer_QnaN
		fstp        dword ptr [esp+18h]
		fstp        dword ptr [esp+18h]
	localexit:
  		leave
  		ret
	}
}

END_EXTERN_C

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
Mike_V is currently a student at UCLA.

After a few years on the Dark Side, he reformed and now chants "Death to VB." His computer-related interests include C++, C#, and ASP.NET (in C#, of course). He writes operating systems in C++ and assembler as a hobby.

Comments and Discussions