Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

OPTIMIZED ENTERPRISE ANONIX

Name: Anonymous 2011-07-07 0:31

#ifndef OMG_OPTIMIZED_CTYPE_H_
#define OMG_OPTIMIZED_CTYPE_H_

/**
 *                        __ _   _  ____  _   _ _ _  _
 *                       /  |  \| |/ __ \|  \| | | \/ /
 *                      /   |   \ | |  | |   \ | |\  /
 *                     /  . | |\  | |__| | |\  | |/  \
 *                    /__/|_|_| \_|\____/|_| \_|_|_/\_\
 *       
 *                       OPTIMIZED ANONIX INTERFACES
 *
 *  -------------------------------------------------------------------------
 *
 *  Optimized branchless ctype facilities using bitwise operations. Assumes a
 *  fixed ASCII (ANSI_X3.110-1983) code page. Functions are ISO/IEC 9899:1999
 *  (C99) compliant. Requires an ISO/IEC 9899:1999 (C99) compliant compiler.
 * 
 *  Version: 1.666alpha
 *  Bug Reports: strawberryshake [at] 1337 [dot] jp
 */
 
inline int isblank(int c) { return (c == ' ') | (c == '\t') | (c == '\v'); }
inline int iscntrl(int c) { return (c <= 0x1F) | (c == 0x7F); }
inline int isdigit(int c) { return (((unsigned int)c) - ((unsigned int)'0')) <= ((unsigned int)('9' - '0')); }
inline int isgraph(int c) { return (((unsigned int)c) - ((unsigned int)'!')) <= ((unsigned int)('~' - '!')); }
inline int islower(int c) { return (((unsigned int)c) - ((unsigned int)'a')) <= ((unsigned int)('z' - 'a')); }
inline int isprint(int c) { return (((unsigned int)c) - ((unsigned int)' ')) <= ((unsigned int)('~' - ' ')); }
inline int isupper(int c) { return (((unsigned int)c) - ((unsigned int)'A')) <= ((unsigned int)('Z' - 'A')); }
inline int isalpha(int c) { return ::hrc::islower(c) | ::hrc::isupper(c); }
inline int isalnum(int c) { return ::hrc::islower(c) | ::hrc::isupper(c) | ::hrc::isdigit(c); }
inline int ispunct(int c) { return ~(::hrc::islower(c) | ::hrc::isupper(c) | ::hrc::isdigit(c)); }
inline int isspace(int c) { return ::hrc::isblank(c) | (c == '\f') | (c == '\n') | (c == '\r'); }

inline int isxdigit(int c) {
    return ::hrc::isdigit(c) |
        ((((unsigned int)c) - ((unsigned int)'a')) <= ((unsigned int)('f' - 'a'))) |
        ((((unsigned int)c) - ((unsigned int)'A')) <= ((unsigned int)('F' - 'A')));
}

inline int tolower(int c) {
    unsigned int upper = (unsigned int)::hrc::isupper(c);
    return (int)((((~upper) + 1) & ((unsigned int)(c - 'A' + 'a'))) | ((~(upper - 1)) & ((unsigned int)c)));
}

inline int toupper(int c) {
    unsigned int lower = (unsigned int)::hrc::islower(c);
    return (int)((((~lower) + 1) & ((unsigned int)(c - 'a' + 'A'))) | ((~(lower - 1)) & ((unsigned int)c)));
}

#endif

Name: Anonymous 2011-07-07 0:37

Sorry, off to a rough start. In my haste, forgot the proper tags. Rather amateurish of a long-time /prog/rider, don't you agree? I also left in some C++ stuff. I'm actually using this in one of my private C++ libraries, but I'm placing this in the public domain with the exception that there is absolutely no warranty.

#ifndef OMG_OPTIMIZED_CTYPE_H_
#define OMG_OPTIMIZED_CTYPE_H_

/**
 *                        __ _   _  ____  _   _ _ _  _
 *                       /  |  \| |/ __ \|  \| | | \/ /
 *                      /   |   \ | |  | |   \ | |\  /
 *                     /  . | |\  | |__| | |\  | |/  \
 *                    /__/|_|_| \_|\____/|_| \_|_|_/\_\
 *       
 *                       OPTIMIZED ANONIX INTERFACES
 *
 *  -------------------------------------------------------------------------
 *
 *  Optimized branchless ctype facilities using bitwise operations. Assumes a
 *  fixed ASCII (ANSI_X3.110-1983) code page. Functions are ISO/IEC 9899:1999
 *  (C99) compliant. Requires an ISO/IEC 9899:1999 (C99) compliant compiler.
 * 
 *  Version: 1.666alpha2
 *  Bug Reports: strawberryshake [at] 1337 [dot] jp
 */
 
inline int isblank(int c) { return (c == ' ') | (c == '\t') | (c == '\v'); }
inline int iscntrl(int c) { return (c <= 0x1F) | (c == 0x7F); }
inline int isdigit(int c) { return (((unsigned int)c) - ((unsigned int)'0')) <= ((unsigned int)('9' - '0')); }
inline int isgraph(int c) { return (((unsigned int)c) - ((unsigned int)'!')) <= ((unsigned int)('~' - '!')); }
inline int islower(int c) { return (((unsigned int)c) - ((unsigned int)'a')) <= ((unsigned int)('z' - 'a')); }
inline int isprint(int c) { return (((unsigned int)c) - ((unsigned int)' ')) <= ((unsigned int)('~' - ' ')); }
inline int isupper(int c) { return (((unsigned int)c) - ((unsigned int)'A')) <= ((unsigned int)('Z' - 'A')); }
inline int isalpha(int c) { return islower(c) | isupper(c); }
inline int isalnum(int c) { return islower(c) | isupper(c) | isdigit(c); }
inline int ispunct(int c) { return ~(islower(c) | isupper(c) | isdigit(c)); }
inline int isspace(int c) { return isblank(c) | (c == '\f') | (c == '\n') | (c == '\r'); }

inline int isxdigit(int c) {
    return isdigit(c) |
        ((((unsigned int)c) - ((unsigned int)'a')) <= ((unsigned int)('f' - 'a'))) |
        ((((unsigned int)c) - ((unsigned int)'A')) <= ((unsigned int)('F' - 'A')));
}

inline int tolower(int c) {
    unsigned int upper = (unsigned int)isupper(c);
    return (int)((((~upper) + 1) & ((unsigned int)(c - 'A' + 'a'))) | ((~(upper - 1)) & ((unsigned int)c)));
}

inline int toupper(int c) {
    unsigned int lower = (unsigned int)islower(c);
    return (int)((((~lower) + 1) & ((unsigned int)(c - 'a' + 'A'))) | ((~(lower - 1)) & ((unsigned int)c)));
}

#endif

Name: Anonymous 2011-07-07 0:49

Are you wizard?

Name: Anonymous 2011-07-07 0:54

These are all very nice, but aren't these the sort of functions that should be no-brainers? I mean, is all of this really necessary?

Name: Anonymous 2011-07-07 1:02

>>4
If they were no brainers, how come CUDDERfan didn't come up with them in his ctype.h implementation?

Name: Anonymous 2011-07-07 1:17

>>5
Because nobody needs these functions who couldn't write them in about 5 seconds. Nobody thinks about this shit.

Also, isn't using integers about 3 bytes of wasted space? Who taught you C?

Name: Anonymous 2011-07-07 1:27

>>2
I honestly don't see what is so special about this. Anybody could do that in like 5 minutes.

Name: Anonymous 2011-07-07 1:29

Wow, 1337.jp is a resolvable domain.

Name: Anonymous 2011-07-07 1:33

>>6
I know C. You apparently do not. You need to use int so that you can represent all C language characters plus EOF, which typically has a value of -1. Since in C89/C99, char could be less than 8-bits in size, there was no reliable way to guarantee being able to store EOF without first expanding to a larger type. In the new C1x and C++0x standards, char must be at least 8-bits in size, capable of representing all possible UTF-8 octets, meaning you really do need to use int.

Furthermore, when operating on such variables, it's usually best to work with variables that are the same size as the target architecture's machine word. On x86, it's often faster to use 32-bit or 64-bit integers and 8-bit integers in ALU operations.

That's why the functions in ctype.h in the ISO/IEC C standards specifications use int as well. In fact, C99 added stdint.h with types like int_fast16_t, int_fast32_t and int_fast64_t, and so on.

However, I have a feeling IHBT by you.

Name: Anonymous 2011-07-07 1:38

>>9
Indeed, good sir. Although I did learn a fair bit from it, so thanks.

Name: Anonymous 2011-07-07 1:45

>>7
Yes, admittedly, there's nothing really special, but apparently you missed the threads a month or two back with a different ctype.h implementation that was slower. Hence, you have failed to grasp the significance of this being a pseudo-inside joke.

Name: Anonymous 2011-07-07 1:51

>>11
No, I saw it. It was filled with ternary boolean statements that were pointless.

Believe me, I was perplexed then too.

Name: Anonymous 2011-07-07 4:11

OPTIMIZED
ENTERPRISE
Nice oxymoron.

Name: Anonymous 2011-07-07 4:17

Where's printf realisation?

Name: Anonymous 2011-07-07 5:17

>>14
I'm working on it. I actually need to write my own vsnprintf and vsnwprintf soon, along with non-standard vsnu16printf, and vsnu32printf implementations. The default printf in most Standard C libraries, including glibc/eglibc, are terribly slow, locking global mutexes to obtain access to global locale tables, causing cache misses, as well as allocating temporary buffers on the heap, even when such allocations are small and could be safely done on the stack in most cases. If you really need locale specific stuff, much better use the thread-safe POSIX-2008.1 locale variants of functions with the _l suffix.

I just finished my own <clocale>, <cstring>, <cwchar>[code], [code]<cuchar>, <cctype>, <cwctype> headers with POSIX-2008.1 conformance support on Linux, Windows, OS X, and Playstation 3. In most cases, I'm just importing the Standard C library functions into our project's namespace with using declarations, providing implementations for missing functions where needed, as well as rewriting ANY and ALL functions that touch the global locale to not do so which also entailed writing string functions for UTF-8, UTF-16, and UTF-32.

I'm laying the foundations for our next-gen multi-platform engine code-base.

I have an XBox 360 dev kit too, but we've decided to drop support for the 360 going forward as they're planning on announcing new shit next year and we'll probably get in early as partners. PS3 will probably still be Sony's primary console until 2014, given Sony's financial troubles, and the costs involved in launching a new platform.

Name: Anonymous 2011-07-07 7:09

>>15
I have an XBox 360 dev kit too, but we've decided to drop support for the 360 going forward as they're planning on announcing new shit next year
But I thought Kinect was the future!

Name: Anonymous 2011-07-07 7:49

>>16
It is! But if you haven't noticed, Microsoft just released a Kinect SDK for Windows, is merging Windows Live for Games and the XBox Live Marketplace, and there are rumors of packaging an XBox 360 emulator with Windows 8 to ship sometime next year. Kinect will probably be reborn on their next console. After all, there's an endless supply of casuals and gullible parents who just can't wait for the next Kinect title!

Name: Anonymous 2011-07-07 8:43

>>17
Not really. Kinect sold a lot of units but not a lot of software. Besides the pack-in game and that dancing game what sold a lot? What is the killer app for the system? And I doubt it's that atrocious Star Wars game that they showed at E3. The Wii is successful because the motion controls work reasonably well in some games and you can still have traditional games with the control scheme like the 3 Mario games on the system for example. For Kinect it seems like it's only some cool tech demos done by hobbyists and shitty games.

And I don't have a lot of faith in that Xbox 360 emulator they're allegedly working on seeing how half-assed the Xbox emulator on the 360 is. I want my Jet Set Radio Future at a constant 60 FPS DAMN IT!

Name: Anonymous 2011-07-07 8:56

>>18
I was being sarcastic in my last point.

Name: Anonymous 2011-07-07 9:50

>>19
It was too subtle for me with all the analysts claiming it was a huge success. Well of course it will be a huge initial success if you spend $500 million on marketing.

Name: Anonymous 2011-07-07 10:46

>>15
going forward
Enterprise confirmed.

This reminds me of EASTL.

Branchless code isn't always better than branchful code.

Does the string processing in a game really need to be optimized? I would think content decoding, 3D math, data structures, memory management, AI and shaders would be the kind of things that are important.

Name: Anonymous 2011-07-09 9:28

inlines do not work this way.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List