Main Include and Self-testsBuild Setup, Porting, and Debugging Tools Data Types and Predefined Strings Macros Self Tests Required DefinesThe required defines are: CPU bits, CPU type, and OS type. You will find a TEMPLATE file in the make/ directory of the source. Unless you are porting the libraries to a new platform, these will already be set and usable with the `build` script. CPU_64BIT or CPU_32BIT - CPU bitness Either CPU_32BIT or CPU_64BIT must be defined, not both. CPU_TYPE - CPU type The CPU type is set by defining CPU_TYPE to be one of the following. Note that valid values of CPU_TYPE are from 0 to COSM_CPU_TYPE_MAX inclusive. The array COSM_CPU_TYPES of CPU names will also be of use to programmers, see below.
OS_TYPE - OS type The OS is set by defining OS_TYPE to be one of the following. Note that valid values of OS_TYPE are from 0 to COSM_OS_TYPE_MAX inclusive. The array COSM_OS_TYPES of OS names will also be of use to programmers, see below.
Optional Defines
Data TypesCosm defines the following portable data types: Note that the 32, 16, and 8-bit types can be used normally, however the 64-bit and 128-bit types must use special functions (os_math) for math, and special macros (_COSM_SET and _COSM_EQ) for setting constants to maintain portability. Integer Types u8 Unsigned 8-bit integer. s8 Signed 8-bit integer. u16 Unsigned 16-bit integer. s16 Signed 16-bit integer. u32 Unsigned 32-bit integer. s32 Signed 32-bit integer. u64 Unsigned 64-bit integer. s64 Signed 64-bit integer. u128 Unsigned 128-bit integer. s128 Signed 128-bit integer. Floating Point Types f32 32-bit floating point value, ~7 significant digits. f64 64-bit floating point value, ~16 significant digits. f128 Longest floating point value available up to 128-bits. ~16-34 significant digits depending on the system. Make no assumptions about the size of f128. Not only the CPU, but the compiler and the system libraries must support f128 types for it to work at all. This is for hardcore math, those who need it know what it is, and if you don't then you probably only need to use f32. Text Types ascii ASCII type. Cosm apps never use char, not even in CPU/OS. utf8 Unicode UTF-8 type for strings. For user interaction. utf8char Single Unicode codepoint, 32-bit. Since UTF-8 is fully compatable with ASCII, you can feed any utf8 function a normal ascii string with a simple typecast to eliminate the compiler warning. It's very important to understand that "utf8 string[128];" is 128 bytes for storing UTF-8, not 128 unicode characters. Some reading about Unicode and UTF-8 is required before using many of the functions in os_io and interfacing with your operating syetem's GUI API. Time Type cosmtime Time value. Times are signed s128's of type cosmtime, 64b.64b format based 0 = 00:00:00 UTC, Jan 1, 2000 AD. This gives a Range of +/- 2.923E11 years and a resolution of 5.421E-20 seconds. This allows times from bang to crunch - assuming a closed universe (if it's not closed, we have bigger problems). It's also useful for timing the half lives of particles like neutral sigma baryons. For all math on cosmtime values, CosmS128* functions must be used. If all you need is the seconds for timestamps, that is always a s64 in cosmtime.hi, and the fraction of a second is a u64 in cosmtime.lo - both still then require CosmSave to be used. You should never be stuffing data back into a cosmtime except with CosmU128Load, since once you discard the precision it is gone. Defined Strings and ThingsStringscputypes.h defines the string arrays COSM_CPU_TYPES and COSM_OS_TYPES. These are mostly for debugging output, since the CPU and OS are not relivant in most cases. Example const ascii * cpu_types[] = COSM_CPU_TYPES; const ascii * os_types[] = COSM_OS_TYPES; CosmPrint( "CPU = %.20s, OS = %.20s\n", cpu_types[( CPU_TYPE > COSM_CPU_TYPE_MAX ) ? 0 : CPU_TYPE], os_types[( OS_TYPE > COSM_OS_TYPE_MAX ) ? 0 : OS_TYPE] ); EndianCOSM_ENDIAN_CURRENT, COSM_ENDIAN_BIG, and COSM_ENDIAN_LITTLE are defined. COSM_ENDIAN_CURRENT is whichever one the machine is running in at the time. This allows you to check the endian at compile time or at run time. In almost all cases compile time checking leads to faster code, but for statistics/reporting you will need the run time value. Example /* Compile time checking */ #if ( COSM_ENDIAN_CURRENT == COSM_ENDIAN_BIG ) /* do the big thing */ #else /* do the little thing */ #endif /* Runtime checking */ if ( COSM_ENDIAN_CURRENT == COSM_ENDIAN_BIG ) { /* do the big thing */ } else { /* do the little thing */ } MiscCOSM_PASS, COSM_FAIL, and NULL are defined. On a CPU_32BIT system the compiler directly provides a fake 64-bit type. This will generally be faster then using C code for 64-bit operations. But for specific code you may get better performance by #ifdef testing CPU_32BIT or CPU_64BIT and writing specific code for each. _COSM_SETDescriptionSets or initializes a 64-bit or 128-bit integer (signed or unsigned) to a constant value. 64-bit Types The first argument is the variable to be set, followed by the value in hex (without the '0x' prefix), in groups of 32 bits, 2 groups total. Exampleu64 a; s64 b; a = 0x4FF4642312345678LL; /* a is now 0x4FF4642312345678 */ b = 0x49FDC23887654321LL; /* b is now 0x49FDC23887654321 */ 128-bit Types The first argument is the variable to be set, followed by the value in hex (without the '0x' prefix), in groups of 32 bits, 4 groups total. Exampleu128 a; s128 b; _COSM_SET128( a, 0123456789ABCDEF, FEDCBA9876543210 ); /* a is now 0x0123456789ABCDEFFEDCBA9876543210 */ _COSM_SET128( b, 32507DFFDAF85A34, 7AF51C3445A54391 ); /* b is now 0x32507DFFDAF85A347AF51C3445A54391 */ _COSM_EQDescriptionCompares a 64-bit or 128-bit integer (signed or unsigned) variable to a constant. Returns non-zero if equal, and 0 if unequal. 64-bit Types The first argument is the variable to be compared to a constant, followed by the constant to compare it to in hex (without the '0x' prefix), in groups of 32 bits, 2 groups total. Exampleu64 a; s64 b; a = 0x4FF4642312345678LL; if ( ( a == 0x4FF4642312345678LL ) ) { /* equal */ } b = 0x49FDC23887654321LL; if ( ( b == 0x49FDC23887654321LL ) ) { /* equal */ } 128-bit Types The first argument is the variable to be compared to a constant, followed by the constant to compare it to in hex (without the '0x' prefix), in groups of 32 bits, 4 groups total. Exampleu128 a; s128 b; _COSM_SET128( a, 0123456789ABCDEF, FEDCBA9876543210 ); if ( _COSM_EQ128( a, 0123456789ABCDEF, FEDCBA9876543210 ) ) { /* equal */ } _COSM_SET128( b, 32507DFFDAF85A34, 7AF51C3445A54391 ); if ( _COSM_EQ128( a, 32507DFFDAF85A34, 7AF51C3445A54391 ) ) { /* equal */ } CosmTestSyntax#include "cosm.h" s32 CosmTest( s32 * failed_module, s32 * failed_test, s32 module_num ); DescriptionIf module_num is 0, then test all Cosm functions and return a negative number corresponding to the module that failed. failed_module and failed_test will be set to the corresponding failed functions. Otherwise test only the module module_num. See example below for useable of COSM_TEST_MODULE_MAX and __cosm_test_modules, defined in cosm.h. Return ValuesCOSM_PASS on success, or a negative number corresponding to the module that failed. ErrorsA negative number will be returned corresponding to the module/test that failed. Examples32 error; s32 error2; CosmPrint( "\nRunning system tests... " ); if ( CosmTest( &error, &error2, 0 ) != COSM_PASS ) { CosmPrint( "Test failure in module %.16s %i.\n", __cosm_test_modules[( -error > COSM_TEST_MODULE_MAX ) ? 0 : -error].name, error2 ); CosmProcessEnd( error ); } CosmPrint( "all passed.\n" );
© Copyright Mithral Communications & Design Inc.
1995-2024.
All rights reserved.
Mithral® and Cosm® are trademarks of
Mithral Communications & Design Inc.
|