Add casts to avoid warnings about comparison between signed and unsigned

This fixes warnings with gcc.
Merge pull request #1509 from mstorsjo/fix-warnings-cast

Add casts to avoid warnings about comparison between signed and unsigned