diff --git a/lib/linguist/languages.yml b/lib/linguist/languages.yml index f3b08bca..740ef607 100644 --- a/lib/linguist/languages.yml +++ b/lib/linguist/languages.yml @@ -273,6 +273,7 @@ C++: - .hh - .hpp - .hxx + - .inl - .tcc - .tpp diff --git a/lib/linguist/samples.json b/lib/linguist/samples.json index d997844f..7eb5d9e2 100644 --- a/lib/linguist/samples.json +++ b/lib/linguist/samples.json @@ -60,7 +60,8 @@ ".cc", ".cpp", ".h", - ".hpp" + ".hpp", + ".inl" ], "Ceylon": [ ".ceylon" @@ -616,8 +617,8 @@ ".gemrc" ] }, - "tokens_total": 530100, - "languages_total": 652, + "tokens_total": 531394, + "languages_total": 653, "tokens": { "ABAP": { "*/**": 1, @@ -9767,18 +9768,18 @@ "C++": { "class": 40, "Bar": 2, - "{": 581, + "{": 629, "protected": 4, "char": 127, "*name": 6, - ";": 2471, + ";": 2564, "public": 33, - "void": 225, + "void": 226, "hello": 2, - "(": 2729, - ")": 2731, - "}": 581, - "//": 278, + "(": 2853, + ")": 2855, + "}": 629, + "//": 292, "///": 843, "mainpage": 1, "C": 6, @@ -9791,22 +9792,22 @@ "in": 165, "Raspberry": 6, "Pi": 5, - "This": 18, - "is": 100, - "a": 156, + "This": 19, + "is": 102, + "a": 157, "RPi": 17, ".": 16, "It": 7, "provides": 3, "access": 17, - "to": 253, + "to": 254, "GPIO": 87, "and": 118, "other": 17, "IO": 2, "functions": 19, "on": 55, - "the": 537, + "the": 541, "chip": 9, "allowing": 3, "pins": 40, @@ -9819,7 +9820,7 @@ "can": 21, "control": 17, "interface": 9, - "with": 32, + "with": 33, "various": 4, "external": 3, "devices.": 1, @@ -9842,14 +9843,14 @@ "polling": 1, "interrupts": 1, "are": 36, - "not": 26, - "+": 60, + "not": 29, + "+": 70, "compatible": 1, "installs": 1, "header": 7, "file": 31, "non": 2, - "-": 349, + "-": 360, "shared": 2, "any": 23, "Linux": 2, @@ -9858,16 +9859,16 @@ "but": 5, "clearly": 1, "no": 7, - "use": 34, + "use": 37, "except": 2, "or": 44, "another": 1, "The": 50, "version": 38, - "of": 211, + "of": 215, "package": 1, - "that": 33, - "this": 52, + "that": 36, + "this": 55, "documentation": 3, "refers": 1, "be": 35, @@ -9952,7 +9953,7 @@ "must": 6, "friends": 2, "make": 6, - "sure": 3, + "sure": 6, "disable": 2, "bcm2835_gpio_cler_len": 1, "after": 18, @@ -9980,14 +9981,14 @@ "xx": 2, "./configure": 1, "sudo": 2, - "check": 3, + "check": 4, "endcode": 2, "Physical": 21, "Addresses": 6, "bcm2835_peri_read": 3, "bcm2835_peri_write": 3, "bcm2835_peri_set_bits": 2, - "low": 2, + "low": 5, "level": 10, "peripheral": 14, "register": 17, @@ -10019,7 +10020,7 @@ "Ennnnnn": 1, "available": 6, "nnnnnn.": 1, - "base": 4, + "base": 6, "registers": 12, "following": 2, "externals": 1, @@ -10133,7 +10134,7 @@ "subject": 1, "paging": 1, "swapping": 2, - "while": 12, + "while": 13, "does": 4, "things": 1, "besides": 1, @@ -10153,7 +10154,7 @@ "guarantee": 1, "bcm2835_delay": 5, "bcm2835_delayMicroseconds": 6, - "return": 164, + "return": 221, "exactly": 2, "requested.": 1, "fact": 2, @@ -10166,7 +10167,7 @@ "longer": 1, "delay": 9, "times": 2, - "than": 5, + "than": 6, "one": 73, "asked": 1, "for.": 1, @@ -10182,7 +10183,7 @@ "sched_param": 1, "sp": 4, "memset": 3, - "&": 149, + "&": 161, "sizeof": 15, "sp.sched_priority": 1, "sched_get_priority_max": 1, @@ -10190,7 +10191,7 @@ "sched_setscheduler": 1, "mlockall": 1, "MCL_CURRENT": 1, - "|": 14, + "|": 19, "MCL_FUTURE": 1, "Open": 2, "Source": 2, @@ -10198,7 +10199,7 @@ "GPL": 2, "appropriate": 7, "option": 1, - "if": 306, + "if": 316, "want": 5, "share": 2, "source": 12, @@ -10252,7 +10253,7 @@ "sar": 1, "Ortiz": 1, "Document": 1, - "testing": 1, + "testing": 2, "Functions": 1, "bcm2835_gpio_ren": 3, "bcm2835_gpio_fen": 3, @@ -10301,15 +10302,15 @@ "mallocs": 1, "frees": 1, "found": 1, - "calling": 8, + "calling": 9, "nanosleep": 7, "takes": 1, "least": 2, "us.": 1, - "need": 3, + "need": 6, "link": 3, "version.": 1, - "s": 18, + "s": 24, "doc": 1, "Also": 1, "added": 2, @@ -10368,7 +10369,7 @@ "Changes": 1, "timer": 2, "counter": 1, - "instead": 1, + "instead": 4, "clock_gettime": 1, "improved": 1, "accuracy.": 1, @@ -10418,11 +10419,11 @@ "completing.": 1, "Patched": 1, "p": 6, - "[": 274, + "[": 276, "atched": 1, "his": 1, "submitted": 1, - "high": 1, + "high": 5, "load": 1, "processes.": 1, "Updated": 1, @@ -10447,9 +10448,9 @@ "DIRECTLY": 1, "USE": 1, "LISTS": 1, - "#ifndef": 27, + "#ifndef": 28, "BCM2835_H": 3, - "#define": 341, + "#define": 342, "#include": 121, "": 2, "defgroup": 7, @@ -10469,7 +10470,7 @@ "clock": 21, "core_clk": 1, "BCM2835_CORE_CLK_HZ": 1, - "<": 247, + "<": 250, "Base": 17, "Address": 10, "BCM2835_PERI_BASE": 9, @@ -10524,7 +10525,7 @@ "BCM2835_GPFSEL1": 1, "BCM2835_GPFSEL2": 1, "BCM2835_GPFSEL3": 1, - "c": 62, + "c": 72, "BCM2835_GPFSEL4": 1, "BCM2835_GPFSEL5": 1, "BCM2835_GPSET0": 1, @@ -10569,7 +10570,7 @@ "brief": 12, "bcm2835PortFunction": 1, "Port": 1, - "function": 18, + "function": 19, "select": 9, "modes": 1, "bcm2835_gpio_fsel": 2, @@ -10645,7 +10646,7 @@ "BCM2835_PAD_GROUP_GPIO_46_53": 1, "Numbers": 1, "Here": 1, - "we": 4, + "we": 10, "terms": 4, "numbers.": 1, "These": 6, @@ -10747,7 +10748,7 @@ "Full": 1, "BCM2835_SPI0_CS_RXR": 1, "RXR": 3, - "needs": 3, + "needs": 4, "Reading": 1, "full": 9, "BCM2835_SPI0_CS_TXD": 1, @@ -10917,7 +10918,7 @@ "BCM2835_I2C_REASON_ERROR_CLKT": 1, "BCM2835_I2C_REASON_ERROR_DATA": 1, "sent": 1, - "/": 14, + "/": 15, "BCM2835_ST_CS": 1, "Control/Status": 1, "BCM2835_ST_CLO": 1, @@ -10957,8 +10958,8 @@ "BCM2835_PWM0_REPEATFF": 1, "BCM2835_PWM0_SERIAL": 1, "BCM2835_PWM0_ENABLE": 1, - "x": 48, - "#endif": 89, + "x": 86, + "#endif": 98, "#ifdef": 19, "__cplusplus": 12, "init": 2, @@ -10985,8 +10986,8 @@ "case": 34, "errors.": 1, "successful": 2, - "else": 48, - "int": 161, + "else": 50, + "int": 192, "Close": 1, "deallocating": 1, "allocated": 2, @@ -10999,14 +11000,14 @@ "out": 5, "what": 2, "would": 2, - "do": 9, + "do": 13, "rather": 2, "causes": 1, "normal": 1, "operation.": 2, "Call": 2, "param": 72, - "]": 273, + "]": 275, "level.": 3, "uint8_t": 43, "lowlevel": 2, @@ -11234,7 +11235,7 @@ "placed": 1, "rbuf.": 1, "rbuf": 3, - "long": 11, + "long": 14, "tbuf": 4, "Buffer": 10, "send.": 5, @@ -11279,7 +11280,7 @@ "nothing": 1, "driver": 1, "const": 170, - "*": 161, + "*": 177, "receive.": 2, "received.": 2, "Allows": 2, @@ -11325,7 +11326,7 @@ "": 4, "": 4, "": 2, - "namespace": 31, + "namespace": 32, "std": 52, "DEFAULT_DELIMITER": 1, "CsvStreamer": 5, @@ -11374,12 +11375,12 @@ "leading/trailing": 1, "spaces": 3, "trimmed": 1, - "bool": 104, + "bool": 105, "Like": 1, "specify": 1, "trim": 2, "keep": 1, - "float": 8, + "float": 74, "double": 25, "writeln": 1, "Flushes": 1, @@ -11536,7 +11537,7 @@ "*rr": 1, "*zero": 1, "n": 28, - "i": 47, + "i": 83, "BN_CTX_start": 1, "BN_CTX_get": 8, "EC_GROUP_get_order": 1, @@ -11602,11 +11603,11 @@ "nBitsR": 3, "BN_num_bits": 2, "nBitsS": 3, - "&&": 23, + "&&": 24, "nRecId": 4, "<4;>": 1, "keyRec": 5, - "1": 2, + "1": 4, "GetPubKey": 5, "break": 34, "BN_bn2bin": 2, @@ -11670,7 +11671,7 @@ "vchSecret": 1, "GetPrivKey": 1, "SetPubKey": 1, - "Sign": 1, + "Sign": 2, "LIBCANIH": 2, "": 1, "": 1, @@ -11678,7 +11679,7 @@ "//#define": 1, "DEBUG": 5, "dout": 2, - "#else": 25, + "#else": 31, "cerr": 1, "libcanister": 2, "//the": 8, @@ -11737,7 +11738,7 @@ "canister*": 1, "parent": 1, "//internal": 1, - "id": 1, + "id": 4, "//use": 1, "own.": 1, "newline": 2, @@ -11801,14 +11802,14 @@ "avoid": 1, "uncaching": 1, "//really": 1, - "just": 1, + "just": 2, "internally": 1, "harm.": 1, "cacheclean": 1, "dFlush": 1, "Q_OS_LINUX": 2, "": 1, - "#if": 44, + "#if": 52, "QT_VERSION": 1, "QT_VERSION_CHECK": 1, "#error": 9, @@ -11844,6 +11845,141 @@ "phantom.execute": 1, "app.exec": 1, "phantom.returnValue": 1, + "__OG_MATH_INL__": 2, + "og": 1, + "OG_INLINE": 41, + "Math": 41, + "Abs": 1, + "MASK_SIGNED": 2, + "y": 16, + "Fabs": 1, + "f": 104, + "uInt": 1, + "*pf": 1, + "reinterpret_cast": 8, + "": 1, + "pf": 1, + "fabsf": 1, + "Round": 1, + "floorf": 2, + "Floor": 1, + "Ceil": 1, + "ceilf": 1, + "Ftoi": 1, + "@todo": 1, + "note": 1, + "sse": 1, + "cvttss2si": 2, + "OG_ASM_MSVC": 4, + "defined": 23, + "OG_FTOI_USE_SSE": 2, + "SysInfo": 2, + "cpu.general.SSE": 2, + "__asm": 8, + "eax": 5, + "mov": 6, + "fld": 4, + "fistp": 3, + "//__asm": 3, + "O_o": 3, + "#elif": 7, + "OG_ASM_GNU": 4, + "__asm__": 4, + "__volatile__": 4, + "cast": 7, + "why": 3, + "did": 3, + "static_cast": 11, + "": 3, + "FtoiFast": 2, + "Ftol": 1, + "": 1, + "Fmod": 1, + "numerator": 2, + "denominator": 2, + "fmodf": 1, + "Modf": 2, + "modff": 2, + "Sqrt": 2, + "sqrtf": 2, + "InvSqrt": 1, + "OG_ASSERT": 4, + "RSqrt": 1, + "g": 2, + "*reinterpret_cast": 3, + "guess": 1, + "f375a86": 1, + "": 1, + "Newtons": 1, + "calculation": 1, + "Log": 1, + "logf": 3, + "Log2": 1, + "INV_LN_2": 1, + "Log10": 1, + "INV_LN_10": 1, + "Pow": 1, + "exp": 2, + "powf": 1, + "Exp": 1, + "expf": 1, + "IsPowerOfTwo": 4, + "faster": 3, + "two": 2, + "known": 1, + "methods": 2, + "moved": 1, + "beginning": 1, + "HigherPowerOfTwo": 4, + "LowerPowerOfTwo": 2, + "FloorPowerOfTwo": 1, + "CeilPowerOfTwo": 1, + "ClosestPowerOfTwo": 1, + "Digits": 1, + "digits": 6, + "step": 3, + "Sin": 2, + "sinf": 1, + "ASin": 1, + "<=>": 2, + "0f": 2, + "HALF_PI": 2, + "asinf": 1, + "Cos": 2, + "cosf": 1, + "ACos": 1, + "PI": 1, + "acosf": 1, + "Tan": 1, + "tanf": 1, + "ATan": 2, + "atanf": 1, + "f1": 2, + "f2": 2, + "atan2f": 1, + "SinCos": 1, + "sometimes": 1, + "assembler": 1, + "waaayy": 1, + "_asm": 1, + "fsincos": 1, + "ecx": 2, + "edx": 2, + "fstp": 2, + "dword": 2, + "ptr": 2, + "asm": 1, + "Deg2Rad": 1, + "DEG_TO_RAD": 1, + "Rad2Deg": 1, + "RAD_TO_DEG": 1, + "Square": 1, + "v": 10, + "Cube": 1, + "Sec2Ms": 1, + "sec": 2, + "Ms2Sec": 1, + "ms": 2, "NINJA_METRICS_H_": 3, "int64_t.": 1, "Metrics": 2, @@ -11908,7 +12044,6 @@ "Seconds": 1, "call.": 1, "Elapsed": 1, - "static_cast": 8, "": 1, "primary": 1, "metrics.": 1, @@ -12084,7 +12219,6 @@ "u": 9, "*name_": 1, "assign": 3, - "reinterpret_cast": 7, "temp": 2, "SWIG": 2, "QSCICOMMAND_H": 2, @@ -12097,7 +12231,6 @@ "represents": 1, "editor": 1, "command": 9, - "two": 1, "keys": 3, "bound": 4, "Methods": 1, @@ -12445,7 +12578,6 @@ "drawing": 4, "actually": 1, "sized.": 1, - "methods": 1, "area": 5, "draw": 1, "text.": 3, @@ -12506,7 +12638,6 @@ "expected_length": 4, "ASSERT": 17, "overflow": 1, - "digits": 3, "c0_": 64, "d": 8, "HexValue": 2, @@ -12524,7 +12655,6 @@ "COLON": 2, "SEMICOLON": 2, "CONDITIONAL": 2, - "f": 5, "LBRACK": 2, "RBRACK": 2, "LBRACE": 2, @@ -12604,7 +12734,6 @@ "IsCarriageReturn": 2, "IsLineFeed": 2, "fall": 2, - "v": 3, "xxx": 1, "immediately": 1, "octal": 1, @@ -12617,7 +12746,6 @@ "E": 3, "l": 1, "w": 1, - "y": 13, "keyword": 1, "Unescaped": 1, "in_character_class": 2, @@ -12688,7 +12816,6 @@ "": 6, "kASCIISize": 1, "ConvertToUtf16": 2, - "*reinterpret_cast": 1, "": 2, "kUC16Size": 2, "is_ascii": 3, @@ -12914,7 +13041,6 @@ "r.double_value": 3, "r.uint64_t_value": 1, "HeapNumber": 1, - "cast": 1, "set_value": 1, "InitializeOncePerProcessImpl": 3, "SetUp": 4, @@ -12935,7 +13061,6 @@ "ExternalReference": 1, "CallOnce": 1, "V8_V8_H_": 3, - "defined": 21, "GOOGLE3": 2, "NDEBUG": 4, "both": 1, @@ -13148,7 +13273,6 @@ "CYTHON_INLINE": 68, "__GNUC__": 5, "__inline__": 1, - "#elif": 3, "__inline": 1, "__STDC_VERSION__": 2, "L": 1, @@ -55241,7 +55365,7 @@ "Brightscript": 579, "C": 59053, "C#": 278, - "C++": 31181, + "C++": 32475, "Ceylon": 50, "Cirru": 244, "Clojure": 510, @@ -55404,7 +55528,7 @@ "Brightscript": 1, "C": 29, "C#": 2, - "C++": 27, + "C++": 28, "Ceylon": 1, "Cirru": 9, "Clojure": 7, @@ -55549,5 +55673,5 @@ "YAML": 2, "Zephir": 2 }, - "md5": "c03f5780c3430c9d636060acee77293a" + "md5": "5532e7021cf7ff94922c6d7fcec9e8bf" } \ No newline at end of file diff --git a/samples/C++/Math.inl b/samples/C++/Math.inl new file mode 100644 index 00000000..194370a3 --- /dev/null +++ b/samples/C++/Math.inl @@ -0,0 +1,530 @@ +/* +=========================================================================== +The Open Game Libraries. +Copyright (C) 2007-2010 Lusito Software + +Author: Santo Pfingsten (TTK-Bandit) +Purpose: Math namespace +----------------------------------------- + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any damages +arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it +freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + +3. This notice may not be removed or altered from any source distribution. +=========================================================================== +*/ + +#ifndef __OG_MATH_INL__ +#define __OG_MATH_INL__ + +namespace og { + +/* +============================================================================== + + Math + +============================================================================== +*/ + +/* +================ +Math::Abs +================ +*/ +OG_INLINE int Math::Abs( int i ) { +#if 1 + if ( i & 0x80000000 ) + return 0x80000000 - (i & MASK_SIGNED); + return i; +#else + int y = x >> 31; + return ( ( x ^ y ) - y ); +#endif +} + +/* +================ +Math::Fabs +================ +*/ +OG_INLINE float Math::Fabs( float f ) { +#if 1 + uInt *pf = reinterpret_cast(&f); + *(pf) &= MASK_SIGNED; + return f; +#else + return fabsf( f ); +#endif +} + +/* +================ +Math::Round +================ +*/ +OG_INLINE float Math::Round( float f ) { + return floorf( f + 0.5f ); +} + +/* +================ +Math::Floor +================ +*/ +OG_INLINE float Math::Floor( float f ) { + return floorf( f ); +} + +/* +================ +Math::Ceil +================ +*/ +OG_INLINE float Math::Ceil( float f ) { + return ceilf( f ); +} + +/* +================ +Math::Ftoi + +ok since this is SSE, why should the other ftoi be the faster one ? +and: we might need to add a check for SSE extensions.. +because sse isn't *really* faster (I actually read that GCC does not handle +SSE extensions perfectly. I'll find the link and send it to you when you're online) +================ +*/ +OG_INLINE int Math::Ftoi( float f ) { + //! @todo needs testing + // note: sse function cvttss2si +#if OG_ASM_MSVC + int i; +#if defined(OG_FTOI_USE_SSE) + if( SysInfo::cpu.general.SSE ) { + __asm cvttss2si eax, f + __asm mov i, eax + return i; + } else +#endif + { + __asm fld f + __asm fistp i + //__asm mov eax, i // do we need this ? O_o + } + return i; +#elif OG_ASM_GNU + int i; +#if defined(OG_FTOI_USE_SSE) + if( SysInfo::cpu.general.SSE ) { + __asm__ __volatile__( "cvttss2si %1 \n\t" + : "=m" (i) + : "m" (f) + ); + } else +#endif + { + __asm__ __volatile__( "flds %1 \n\t" + "fistpl %0 \n\t" + : "=m" (i) + : "m" (f) + ); + } + return i; +#else + // we use c++ cast instead of c cast (not sure why id did that) + return static_cast(f); +#endif +} + +/* +================ +Math::FtoiFast +================ +*/ +OG_INLINE int Math::FtoiFast( float f ) { +#if OG_ASM_MSVC + int i; + __asm fld f + __asm fistp i + //__asm mov eax, i // do we need this ? O_o + return i; +#elif OG_ASM_GNU + int i; + __asm__ __volatile__( "flds %1 \n\t" + "fistpl %0 \n\t" + : "=m" (i) + : "m" (f) + ); + return i; +#else + // we use c++ cast instead of c cast (not sure why id did that) + return static_cast(f); +#endif +} + +/* +================ +Math::Ftol +================ +*/ +OG_INLINE long Math::Ftol( float f ) { +#if OG_ASM_MSVC + long i; + __asm fld f + __asm fistp i + //__asm mov eax, i // do we need this ? O_o + return i; +#elif OG_ASM_GNU + long i; + __asm__ __volatile__( "flds %1 \n\t" + "fistpl %0 \n\t" + : "=m" (i) + : "m" (f) + ); + return i; +#else + // we use c++ cast instead of c cast (not sure why id did that) + return static_cast(f); +#endif +} + +/* +================ +Math::Sign +================ +*/ +OG_INLINE float Math::Sign( float f ) { + if ( f > 0.0f ) + return 1.0f; + if ( f < 0.0f ) + return -1.0f; + return 0.0f; +} + +/* +================ +Math::Fmod +================ +*/ +OG_INLINE float Math::Fmod( float numerator, float denominator ) { + return fmodf( numerator, denominator ); +} + +/* +================ +Math::Modf +================ +*/ +OG_INLINE float Math::Modf( float f, float& i ) { + return modff( f, &i ); +} +OG_INLINE float Math::Modf( float f ) { + float i; + return modff( f, &i ); +} + +/* +================ +Math::Sqrt +================ +*/ +OG_INLINE float Math::Sqrt( float f ) { + return sqrtf( f ); +} + +/* +================ +Math::InvSqrt + +Cannot be 0.0f +================ +*/ +OG_INLINE float Math::InvSqrt( float f ) { + OG_ASSERT( f != 0.0f ); + return 1.0f / sqrtf( f ); +} + +/* +================ +Math::RSqrt + +Can be 0.0f +================ +*/ +OG_INLINE float Math::RSqrt( float f ) { + float g = 0.5f * f; + int i = *reinterpret_cast(&f); + + // do a guess + i = 0x5f375a86 - ( i>>1 ); + f = *reinterpret_cast(&i); + + // Newtons calculation + f = f * ( 1.5f - g * f * f ); + return f; +} + +/* +================ +Math::Log/Log2/Log10 + +Log of 0 is bad. +I've also heard you're not really +supposed to do log of negatives, yet +they work fine. +================ +*/ +OG_INLINE float Math::Log( float f ) { + OG_ASSERT( f != 0.0f ); + return logf( f ); +} +OG_INLINE float Math::Log2( float f ) { + OG_ASSERT( f != 0.0f ); + return INV_LN_2 * logf( f ); +} +OG_INLINE float Math::Log10( float f ) { + OG_ASSERT( f != 0.0f ); + return INV_LN_10 * logf( f ); +} + +/* +================ +Math::Pow +================ +*/ +OG_INLINE float Math::Pow( float base, float exp ) { + return powf( base, exp ); +} + +/* +================ +Math::Exp +================ +*/ +OG_INLINE float Math::Exp( float f ) { + return expf( f ); +} + +/* +================ +Math::IsPowerOfTwo +================ +*/ +OG_INLINE bool Math::IsPowerOfTwo( int x ) { + // This is the faster of the two known methods + // with the x > 0 check moved to the beginning + return x > 0 && ( x & ( x - 1 ) ) == 0; +} + +/* +================ +Math::HigherPowerOfTwo +================ +*/ +OG_INLINE int Math::HigherPowerOfTwo( int x ) { + x--; + x |= x >> 1; + x |= x >> 2; + x |= x >> 4; + x |= x >> 8; + x |= x >> 16; + return x + 1; +} + +/* +================ +Math::LowerPowerOfTwo +================ +*/ +OG_INLINE int Math::LowerPowerOfTwo( int x ) { + return HigherPowerOfTwo( x ) >> 1; +} + +/* +================ +Math::FloorPowerOfTwo +================ +*/ +OG_INLINE int Math::FloorPowerOfTwo( int x ) { + return IsPowerOfTwo( x ) ? x : LowerPowerOfTwo( x ); +} + +/* +================ +Math::CeilPowerOfTwo +================ +*/ +OG_INLINE int Math::CeilPowerOfTwo( int x ) { + return IsPowerOfTwo( x ) ? x : HigherPowerOfTwo( x ); +} + +/* +================ +Math::ClosestPowerOfTwo +================ +*/ +OG_INLINE int Math::ClosestPowerOfTwo( int x ) { + if ( IsPowerOfTwo( x ) ) + return x; + int high = HigherPowerOfTwo( x ); + int low = high >> 1; + return ((high-x) < (x-low)) ? high : low; +} + +/* +================ +Math::Digits +================ +*/ +OG_INLINE int Math::Digits( int x ) { + int digits = 1; + int step = 10; + while (step <= x) { + digits++; + step *= 10; + } + return digits; +} + +/* +================ +Math::Sin/ASin +================ +*/ +OG_INLINE float Math::Sin( float f ) { + return sinf( f ); +} +OG_INLINE float Math::ASin( float f ) { + if ( f <= -1.0f ) + return -HALF_PI; + if ( f >= 1.0f ) + return HALF_PI; + return asinf( f ); +} + +/* +================ +Math::Cos/ACos +================ +*/ +OG_INLINE float Math::Cos( float f ) { + return cosf( f ); +} +OG_INLINE float Math::ACos( float f ) { + if ( f <= -1.0f ) + return PI; + if ( f >= 1.0f ) + return 0.0f; + return acosf( f ); +} + +/* +================ +Math::Tan/ATan +================ +*/ +OG_INLINE float Math::Tan( float f ) { + return tanf( f ); +} +OG_INLINE float Math::ATan( float f ) { + return atanf( f ); +} +OG_INLINE float Math::ATan( float f1, float f2 ) { + return atan2f( f1, f2 ); +} + +/* +================ +Math::SinCos +================ +*/ +OG_INLINE void Math::SinCos( float f, float &s, float &c ) { +#if OG_ASM_MSVC + // sometimes assembler is just waaayy faster + _asm { + fld f + fsincos + mov ecx, c + mov edx, s + fstp dword ptr [ecx] + fstp dword ptr [edx] + } +#elif OG_ASM_GNU + asm ("fsincos" : "=t" (c), "=u" (s) : "0" (f)); +#else + s = Sin(f); + c = Sqrt( 1.0f - s * s ); // faster than calling Cos(f) +#endif +} + +/* +================ +Math::Deg2Rad +================ +*/ +OG_INLINE float Math::Deg2Rad( float f ) { + return f * DEG_TO_RAD; +} + +/* +================ +Math::Rad2Deg +================ +*/ +OG_INLINE float Math::Rad2Deg( float f ) { + return f * RAD_TO_DEG; +} + +/* +================ +Math::Square +================ +*/ +OG_INLINE float Math::Square( float v ) { + return v * v; +} + +/* +================ +Math::Cube +================ +*/ +OG_INLINE float Math::Cube( float v ) { + return v * v * v; +} + +/* +================ +Math::Sec2Ms +================ +*/ +OG_INLINE int Math::Sec2Ms( int sec ) { + return sec * 1000; +} + +/* +================ +Math::Ms2Sec +================ +*/ +OG_INLINE int Math::Ms2Sec( int ms ) { + return FtoiFast( ms * 0.001f ); +} + +} + +#endif