Sunday, January 17, 2016

#C: fast string to hex string

following this post, i have also added a method to convert a char* buffer to a char* buffer which holds the hex values of each input character.

e.g.:
HelloWorld -> 48656c6c6f576f726c64

a branchless method with a lookup table is used again. it works for any type of data (e.g. UTF-16, UTF-8, etc.) as long as sizeof(char) is 1 byte, as is in the standard.

the cfg2 C library now includes the function cfg_char_to_hex(), which can be found here.
it's effectively the reverse of cfg_hex_to_char().

--

Thursday, October 22, 2015

#Functors in Java

i've added a Github repository for a a very simple Functor (function object) implementation in SE 7.

--

Monday, October 12, 2015

#C: fast parsing of hex strings to char buffers

the C library for parsing INI file syntax (cfg2) that i'm hobby-maintaining has recently been improved quite a lot. supports sections, the parser should be much better now, improved API and so on.

one of the features i've added is to be able to store HEX strings of binary data in the text file that can be optionally parsed into memory char buffers.

for example:
some_key=48656c6c6f77576f726c64

where the HEX string equals "HelloWorld".

problems at hand:
1) detect bad length
2) detect bad characters
3) support case-insensitive input

of course, most programmers can immediately start writing a solution for this type of parser and solve all 3 problems, but the question is - how to write this to be as efficient as possible?

solving 1) is quite easy. since we are working with 8 bit characters, we are going to have 256 values per character or a maximum of 0xFF (255). but instead of parsing bad characters we are strict and require the HEX string length to be divisible by 2:
if (len % 2)
    goto error;
one would approach solving 2) with "if, then, else" branches to see if a character in the HEX string is in the 0-9,A-F range of the ASCII table.

3) can be solved with toupper()/tolower()

but instead, a lookup table can be used to solve both 2) and 3) in a single pass. this lookup table could have 256 indexes, where most are filled with zero, while the valid indexes are filled with values of interested.

the table can be seen in this source file - named "hex_to_char_lookup".

so, when we first traverse the example HEX string we reach a couple of characters "48".
the first one "4" - this character has an ASCII decimal value of 52. the lookup table has mapped this value at index 52 as 5. the reason for 5 instead of 4 is that i wanted the table to look pretty, filled with zeroes and thus i increased each value by one so that "0" is mapped to 1, "1" is mapped to 2 and so on. this can be removed for a small optimization [1], but then one has to fill the table with values such as -1.

"8" which is mapped to ASCII index 56 in the table and a value 9 is returned.

if we reach a bad character, 0 will be returned in which case the program should return an error with the bad character position in the HEX string.

at some point in the example string, "c" will be reached and here we could have a case-sensitivity issue, but the lookup table has mapped both the ASCII "c" and "C" to return the same value which is 13 or the 12th index in hexadecimal, plus 1.
0 1 2 3 4 5 6 7 8 9 A B C D E F
                        ^ (12 + 1 = 13)
now, to obtain the resulting C char we have to form a hexadecimal value combining the two decimal values, as follows:
first = table[52]; // 5
second = table[56]; // 9
if (!first || !second)
    goto error;
first--; second--; // 4, 8; possible optimization [1]
unsigned char result = first * 16 + second; // 72 = "H"
and then the result can be written to the output char buffer.

that's all!
TMK, this parsing method would work for any string encoding UTF-8, UTF-16, etc. as long as sizeof(char) == 1, which should be very much true as it's in the ISO C standard.
 --

Wednesday, February 4, 2015

#libmodplugw

here is a small (wrapper / extension) library that can be used to manipulate libmodplug decoded buffers in terms of rows and patterns with sample accuracy:

https://github.com/neolit123/libmodplugw

--

Friday, December 19, 2014

#AS3 KeyboardHandler

(i've posted this as a comment in Starling bug report from a user)

for performance crucial sections such as keyboard interaction i would not use anything but callbacks.
the flash event system, and the alternatives such as signals are nice for higher level management, but they can't really beat callbacks for performance. what Starling does with the enhanced events is also fast, but here is my point.
the virtual machine and the garbage collector already provide you with the flexibility of one of the core features - everything is an Object including Function.
"functors" or function pointers is something that even languages like C++ don't handle that well, so you really should take advantage of this low level, yet still high level feature.
since the only way in flash to see if a key is pressed is via the KeyboardEvent, use that and build a callback system on top of it. only maintain the stage events and propagate manually not display objects but rather objects of interest.
check this concept i wrote quickly:
  • KeyboardHandler is a state machine that tracks keyboard isDown states for a keyCode and calls your registered callback.
  • it uses the native stage
  • it's super fast
  • it's worker safe
  • you can stop it at any time using "enabled = false"
  • it does call event.preventDefault(), event.stopImmediatePropagation(), but you can mod that yourself or simply call "enabled = false" when needed.
  • once a key is down it no longer registers as down until it up, this forces you to write your key logic smart.
  • if you don't track a key state yourself (which would be faster) at any time you can call supportedKeyIsDown(...) to see if a key is currently down. this allows easy checks for key combinations e.g ALT + SHIFT + LEFT.

Friday, September 13, 2013

#fast IEEE 754 double to int32 @ music-dsp

at the music-dsp mailing list there was a short discussion about a "double -> int" rounding hack present in the JUCE library. i've posted an alternative solution that does the same without IEE 754 arithmetic. i'm sure similar solutions can be found in libraries and for CPU's that don't have a floating point co-processor unit. code is for c99.

http://music.columbia.edu/pipermail/music-dsp/2013-September/071572.html

can be summarized into:
1) separate double into mantissa, exponent, sign bit
2) depending on exponent value shift the mantissa left or right by the difference of the exponent and the amount of bits in the mantissa
3) truncate and receive a 32bit integer, while applying the original double's sign

--

Tuesday, September 10, 2013

#flash cs6 compiler bug

i have to work with tools like the one mentioned in the title. here is a bug that only happens if you assign code to a frame in the (main) timeline. target version is 12.0.0.481 and could be fixed in later interations.

you can try the following:
// Array can be used here instead, for 'list'
var list:Vector.<String> = Vector.<String>(["hello", "world"]);
trace(list); // hello,world
var vlen:uint = list.length;
trace(vlen); // 2
const clen:uint = vlen; // this works but 'vlen' is zero at this point
trace(clen); // 0
const len:uint = list.length; // list is a null object reference

results in:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Untitled_fla::MainTimeline()

what happens here is that the as3 compiler tries to play smart and traverses for 'const' declarations, and once it finds 'len' it decided to assign a value to the address where it will be stored in the 'constant pool' and then to be referenced by bytecode. but since 'list' is not a 'const' declaration itself the object is simply not constructed yet and 'list' is a null reference.


same does not happen if:

- 'const list:...' is used, but one may not want that
- if the same code is written in an external document class instead on the timeline

if this is by design it may cause compatibility issues with class written code and simply confuse developers. after all, constant declarations after variable declarations should make perfect sense. in general, i wouldn't recommend writing anything more important on timeline frames, but use classes with ENTER_FRAME listeners or attempt to use addFrameScript() on MovieClip instances instead.
--

Monday, May 6, 2013

#robotic arm flashbacks

i remember programming this machine with an apple 2 clone, when i was quite young. it was a educational robotic arm i had access to in a laboratory in the mid 80's.

 

it had pretty good mobility, small error margin and it could lift up a kilo or so. here is a sheet photo where you can see some Tesla ICs and a voltage regulator:
image

--

Thursday, February 21, 2013

#android 4.2.2 and RSA encryption

i was doing some testing recently with the "Android Debug Bridge" ADB, but the device was refusing APK upload. it appears that the recent JB 4.2.2 has a security update:
http://android-developers.blogspot.com/2013/02/security-enhancements-in-jelly-bean.html

which means that you will need to update your Android SDK and copy / use the adb.exe (and all dependencies from "/platform-tools"), which should be version >= 1.0.31 as of this point.

--

Friday, February 8, 2013

#bulls and cows: open source game in ANSI C

here is an open source "bulls and cows" game written in ANSI C:
download

pre-compiled win32 executable included.

i wrote that after a simple challenge proposed by some of my friends, and now apparently they even play it from time to time.

if you are a C programmer, try guessing out why the 0xDA special case happens and how volatile it is.
--