<commit_before>#include "nonsimple.h"

#include <stdint.h>
#include <bitbox.h>
#include <stdlib.h> // abs
#include <string.h> // memset

uint8_t tile_map[TILE_MAP_MEMORY] CCM_MEMORY;
uint8_t tile_translator[16] CCM_MEMORY;
//uint16_t tile_properties[256] CCM_MEMORY;
uint8_t tile_draw[16][16][8] CCM_MEMORY;
int16_t tile_map_x CCM_MEMORY, tile_map_y CCM_MEMORY;
uint16_t tile_map_width CCM_MEMORY, tile_map_height CCM_MEMORY;
// tile_map_width * tile_map_height <= TILE_MAP_MEMORY
// tile_map_x < tile_map_width - 320
// tile_map_y < tile_map_height - 240
uint16_t palette[16] CCM_MEMORY; 

// break sprites up into 16x16 tiles:
uint8_t sprite_frame[16][8][16][8]; // 16 sprites, 8 frames, 16x16 pixels...

struct object object[MAX_OBJECTS] CCM_MEMORY;
uint8_t first_free_object CCM_MEMORY;
uint8_t first_used_object CCM_MEMORY;
uint8_t object_count CCM_MEMORY; 

uint8_t draw_order[MAX_OBJECTS] CCM_MEMORY;
uint8_t drawing_count CCM_MEMORY; 
uint8_t first_drawing_index CCM_MEMORY; 
uint8_t last_drawing_index CCM_MEMORY; 


static inline void swap_draw_order_k_kminus1(int k);

void graph_frame() 
{
    // insertion sort all the drawing objects on the map
    for (int j=0; j<drawing_count-1; ++j)
    {
//        for (int k=j+1; k > 0 && ((draw_order[k-1].iy > draw_order[k].iy) ||    
//            ((draw_order[k-1].iy == draw_order[k].iy) && 
//             (draw_order[k-1].index_z&240 > draw_order[k].index_z&240)) ); --k)
        for (int k=j+1; k > 0 && ((object[draw_order[k-1]].iy > object[draw_order[k]].iy) ||    
            ((object[draw_order[k-1]].iy == object[draw_order[k]].iy) && 
             (object[draw_order[k-1]].z > object[draw_order[k]].z)) ); --k)
        {
            swap_draw_order_k_kminus1(k);
        }
    }
    
    first_drawing_index = 0;
    last_drawing_index = 0;
}
// --------------------------------------------------------------

void graph_line() 
{
    if (vga_odd)
        return;
    if (tile_map_x % 16)
    {
        uint16_t *dst = draw_buffer;
        int tile_j = tile_map_y + vga_line;
        int index = (tile_j/16)*(tile_map_width) + tile_map_x / 16;
        tile_j %= 16;
        uint8_t *tile = &tile_map[index/2];
        int odd = (index % 2);

        // draw the first tile (it's somewhat off-screen)
        uint8_t trans;
        if (odd)
        {
            trans = tile_translator[((*tile++)>>4)&15];
            odd = 0;
        }
        else
        {
            trans = tile_translator[(*tile)&15];
            odd = 1;
        }
        uint8_t *tile_color = &tile_draw[trans][tile_j][(tile_map_x%16)/2];
        if (tile_map_x % 2)
        {
            for (int l=(tile_map_x%16)/2; l<7; ++l)
            {
                *dst++ = palette[(*tile_color++)>>4];
                *dst++ = palette[(*tile_color)&15];
            }
            *dst++ = palette[(*tile_color)>>4];
        }
        else
        {
            for (int l=(tile_map_x%16)/2; l<8; ++l)
            {
                *dst++ = palette[(*tile_color)&15];
                *dst++ = palette[(*tile_color++)>>4];
            }
        }

        // draw 19 un-broken tiles:
        for (int k=0; k<19; ++k)
        {
            // translate the tile into what tile it should be drawn as:
            if (odd)
            {
                trans = tile_translator[((*tile++)>>4)&15];
                odd = 0;
            }
            else
            {
                trans = tile_translator[(*tile)&15];
                odd = 1;
            }

            tile_color = &tile_draw[trans][tile_j][0];
            for (int l=0; l<8; ++l)
            {
                *dst++ = palette[(*tile_color)&15];
                *dst++ = palette[(*tile_color++)>>4];
            }
        }

        // draw 22'nd broken tile:
        if (odd)
        {
            trans = tile_translator[((*tile++)>>4)&15];
            odd = 0;
        }
        else
        {
            trans = tile_translator[(*tile)&15];
            odd = 1;
        }
        tile_color = &tile_draw[trans][tile_j][0];
        
        if (tile_map_x % 2)
        {
            *dst++ = palette[(*tile_color)&15];
            for (int l=0; l<(tile_map_x%16)/2; ++l)
            {
                *dst++ = palette[(*tile_color++)>>4];
                *dst++ = palette[(*tile_color)&15];
            }
        }
        else
        {
            for (int l=0; l<(tile_map_x%16)/2; ++l)
            {
                *dst++ = palette[(*tile_color)&15];
                *dst++ = palette[(*tile_color++)>>4];
            }
        }
    }
    else // tile_map_x puts a tile exactly on the edge of the screen
    {
        uint16_t *dst = draw_buffer;
        int tile_j = tile_map_y + vga_line;
        int index = (tile_j/16)*(tile_map_width) + tile_map_x / 16;
        tile_j %= 16;
        uint8_t *tile = &tile_map[index/2];
        int odd = (index % 2);

        // draw 20 un-broken tiles:
        for (int k=0; k<20; ++k)
        {
            // translate the tile into what tile it should be drawn as:
            uint8_t trans;
            if (odd)
            {
                trans = tile_translator[((*tile++)>>4)&15];
                odd = 0;
            }
            else
            {
                trans = tile_translator[(*tile)&15];
                odd = 1;
            }

            uint8_t *tile_color = &tile_draw[trans][tile_j][0];
            for (int l=0; l<8; ++l)
            {
                *dst++ = palette[(*tile_color)&15];
                *dst++ = palette[(*tile_color++)>>4];
            }
        }
    }<commit_msg>optimizations for tile drawing<commit_after>#include "nonsimple.h"

#include <stdint.h>
#include <bitbox.h>
#include <stdlib.h> // abs
#include <string.h> // memset

uint8_t tile_map[TILE_MAP_MEMORY] CCM_MEMORY;
uint8_t tile_translator[16] CCM_MEMORY;
//uint16_t tile_properties[256] CCM_MEMORY;
uint8_t tile_draw[16][16][8] CCM_MEMORY;
int16_t tile_map_x CCM_MEMORY, tile_map_y CCM_MEMORY;
uint16_t tile_map_width CCM_MEMORY, tile_map_height CCM_MEMORY;
// tile_map_width * tile_map_height <= TILE_MAP_MEMORY
// tile_map_x < tile_map_width - 320
// tile_map_y < tile_map_height - 240
uint16_t palette[16] CCM_MEMORY; 

// break sprites up into 16x16 tiles:
uint8_t sprite_frame[16][8][16][8]; // 16 sprites, 8 frames, 16x16 pixels...

struct object object[MAX_OBJECTS] CCM_MEMORY;
uint8_t first_free_object CCM_MEMORY;
uint8_t first_used_object CCM_MEMORY;
uint8_t object_count CCM_MEMORY; 

uint8_t draw_order[MAX_OBJECTS] CCM_MEMORY;
uint8_t drawing_count CCM_MEMORY; 
uint8_t first_drawing_index CCM_MEMORY; 
uint8_t last_drawing_index CCM_MEMORY; 


static inline void swap_draw_order_k_kminus1(int k);

void graph_frame() 
{
    // insertion sort all the drawing objects on the map
    for (int j=0; j<drawing_count-1; ++j)
    {
//        for (int k=j+1; k > 0 && ((draw_order[k-1].iy > draw_order[k].iy) ||    
//            ((draw_order[k-1].iy == draw_order[k].iy) && 
//             (draw_order[k-1].index_z&240 > draw_order[k].index_z&240)) ); --k)
        for (int k=j+1; k > 0 && ((object[draw_order[k-1]].iy > object[draw_order[k]].iy) ||    
            ((object[draw_order[k-1]].iy == object[draw_order[k]].iy) && 
             (object[draw_order[k-1]].z > object[draw_order[k]].z)) ); --k)
        {
            swap_draw_order_k_kminus1(k);
        }
    }
    
    first_drawing_index = 0;
    last_drawing_index = 0;
}
// --------------------------------------------------------------

void graph_line() 
{
    if (vga_odd)
        return;
    if (tile_map_x % 16)
    {
        uint16_t *dst = draw_buffer;
        int tile_j = tile_map_y + vga_line;
        int index = (tile_j/16)*(tile_map_width) + tile_map_x / 16;
        tile_j %= 16;
        uint8_t *tile = &tile_map[index/2];
        if (index % 2)
        {
            // draw the first tile (it's somewhat off-screen)
            uint8_t trans = tile_translator[((*tile)>>4)]; // first one is odd
                
            uint8_t *tile_color = &tile_draw[trans][tile_j][(tile_map_x%16)/2] - 1;
            if (tile_map_x % 2)
            {
                ++tile_color;
                for (int l=(tile_map_x%16)/2; l<7; ++l)
                {
                    *dst++ = palette[(*tile_color)>>4];
                    *dst++ = palette[(*(++tile_color))&15];
                }
                *dst++ = palette[(*tile_color)>>4];
            }
            else
            {
                for (int l=(tile_map_x%16)/2; l<8; ++l)
                {
                    *dst++ = palette[(*(++tile_color))&15];
                    *dst++ = palette[(*tile_color)>>4];
                }
            }

            // draw 19 un-broken tiles (tile 2 to tile 21)
            for (int k=0; k<19/2; ++k)
            {
                // translate the tile into what tile it should be drawn as:
                // first one is even (not odd)
                trans = tile_translator[(*(++tile))&15];
                tile_color = &tile_draw[trans][tile_j][0] - 1;
                for (int l=0; l<8; ++l)
                {
                    *dst++ = palette[(*(++tile_color))&15];
                    *dst++ = palette[(*tile_color)>>4];
                }

                // second one is odd
                trans = tile_translator[((*tile)>>4)];
                tile_color = &tile_draw[trans][tile_j][0] - 1;
                for (int l=0; l<8; ++l)
                {
                    *dst++ = palette[(*(++tile_color))&15];
                    *dst++ = palette[(*tile_color)>>4];
                }
            }
            // 21st tile is even, but still unbroken
            trans = tile_translator[(*(++tile))&15];
            tile_color = &tile_draw[trans][tile_j][0] - 1;
            for (int l=0; l<8; ++l)
            {
                *dst++ = palette[(*(++tile_color))&15];
                *dst++ = palette[(*tile_color)>>4];
            }

            // draw 22'nd broken tile is odd
            trans = tile_translator[((*tile)>>4)&15];
            tile_color = &tile_draw[trans][tile_j][0]-1; 
            if (tile_map_x % 2)
            {
                *dst++ = palette[(*(++tile_color))&15];
                for (int l=0; l<(tile_map_x%16)/2; ++l)
                {
                    *dst++ = palette[(*tile_color)>>4];
                    *dst++ = palette[(*(++tile_color))&15];
                }
            }
            else
            {
                for (int l=0; l<(tile_map_x%16)/2; ++l)
                {
                    *dst++ = palette[(*(++tile_color))&15];
                    *dst++ = palette[(*tile_color)>>4];
                }
            }
        }
        else 
        {
            // draw the first tile (it's somewhat off-screen)
            uint8_t trans = tile_translator[(*tile)&15]; // first one is even
            uint8_t *tile_color = &tile_draw[trans][tile_j][(tile_map_x%16)/2] - 1;
            if (tile_map_x % 2)
            {
                ++tile_color;
                for (int l=(tile_map_x%16)/2; l<7; ++l)
                {
                    *dst++ = palette[(*tile_color)>>4];
                    *dst++ = palette[(*(++tile_color))&15];
                }
                *dst++ = palette[(*tile_color)>>4];
            }
            else
            {
                for (int l=(tile_map_x%16)/2; l<8; ++l)
                {
                    *dst++ = palette[(*(++tile_color))&15];
                    *dst++ = palette[(*tile_color)>>4];
                }
            }

            // draw 19 un-broken tiles (tile 2 to tile 21)
            for (int k=0; k<19/2; ++k)
            {
                // translate the tile into what tile it should be drawn as:
                // first one is odd
                trans = tile_translator[((*tile)>>4)];
                tile_color = &tile_draw[trans][tile_j][0] - 1;
                for (int l=0; l<8; ++l)
                {
                    *dst++ = palette[(*(++tile_color))&15];
                    *dst++ = palette[(*tile_color)>>4];
                }
                
                // second one is even
                trans = tile_translator[(*(++tile))&15];
                tile_color = &tile_draw[trans][tile_j][0] - 1;
                for (int l=0; l<8; ++l)
                {
                    *dst++ = palette[(*(++tile_color))&15];
                    *dst++ = palette[(*tile_color)>>4];
                }
            }
            // 21st tile is odd, but still unbroken
            trans = tile_translator[((*tile)>>4)];
            tile_color = &tile_draw[trans][tile_j][0] - 1;
            for (int l=0; l<8; ++l)
            {
                *dst++ = palette[(*(++tile_color))&15];
                *dst++ = palette[(*tile_color)>>4];
            }

            // draw 22'nd broken tile is even
            trans = tile_translator[(*(++tile))&15];
            tile_color = &tile_draw[trans][tile_j][0]-1; 
            if (tile_map_x % 2)
            {
                *dst++ = palette[(*(++tile_color))&15];
                for (int l=0; l<(tile_map_x%16)/2; ++l)
                {
                    *dst++ = palette[(*tile_color)>>4];
                    *dst++ = palette[(*(++tile_color))&15];
                }
            }
            else
            {
                for (int l=0; l<(tile_map_x%16)/2; ++l)
                {
                    *dst++ = palette[(*(++tile_color))&15];
                    *dst++ = palette[(*tile_color)>>4];
                }
            }
        }
//        int odd = (index % 2);
//
//        // draw the first tile (it's somewhat off-screen)
//        uint8_t trans;
//        if (odd)
//        {
//            trans = tile_translator[((*tile)>>4)];
//            odd = 0;
//        }
//        else
//        {
//            trans = tile_translator[(*tile)&15];
//            odd = 1;
//        }
//        uint8_t *tile_color = &tile_draw[trans][tile_j][(tile_map_x%16)/2] - 1;
//        if (tile_map_x % 2)
//        {
//            ++tile_color;
//            for (int l=(tile_map_x%16)/2; l<7; ++l)
//            {
//                *dst++ = palette[(*tile_color)>>4];
//                *dst++ = palette[(*(++tile_color))&15];
//            }
//            *dst++ = palette[(*tile_color)>>4];
//        }
//        else
//        {
//            for (int l=(tile_map_x%16)/2; l<8; ++l)
//            {
//                *dst++ = palette[(*(++tile_color))&15];
//                *dst++ = palette[(*tile_color)>>4];
//            }
//        }
//
//        // draw 19 un-broken tiles:
//        for (int k=0; k<19; ++k)
//        {
//            // translate the tile into what tile it should be drawn as:
//            if (odd)
//            {
//                trans = tile_translator[((*tile)>>4)];
//                odd = 0;
//            }
//            else
//            {
//                trans = tile_translator[(*(++tile))&15];
//                odd = 1;
//            }
//
//            tile_color = &tile_draw[trans][tile_j][0] - 1;
//            for (int l=0; l<8; ++l)
//            {
//                *dst++ = palette[(*(++tile_color))&15];
//                *dst++ = palette[(*tile_color)>>4];
//            }
//        }
//
//        // draw 22'nd broken tile:
//        if (odd)
//        {
//            trans = tile_translator[((*tile)>>4)&15];
//            odd = 0;
//        }
//        else
//        {
//            trans = tile_translator[(*(++tile))&15];
//            odd = 1;
//        }
//        tile_color = &tile_draw[trans][tile_j][0]-1;
//        
//        if (tile_map_x % 2)
//        {
//            *dst++ = palette[(*(++tile_color))&15];
//            for (int l=0; l<(tile_map_x%16)/2; ++l)
//            {
//                *dst++ = palette[(*tile_color)>>4];
//                *dst++ = palette[(*(++tile_color))&15];
//            }
//        }
//        else
//        {
//            for (int l=0; l<(tile_map_x%16)/2; ++l)
//            {
//                *dst++ = palette[(*(++tile_color))&15];
//                *dst++ = palette[(*tile_color)>>4];
//            }
//        }
    }
    else // tile_map_x puts a tile exactly on the edge of the screen
    {
        uint16_t *dst = draw_buffer;
        int tile_j = tile_map_y + vga_line;
        int index = (tile_j/16)*(tile_map_width) + tile_map_x / 16;
        tile_j %= 16;
        uint8_t *tile = &tile_map[index/2]-1;
        if (index % 2)
        {
            ++tile;
            for (int k=0; k<10; ++k)
            {
                // translate the tile into what tile it should be drawn as:
                uint8_t trans = tile_translator[((*tile)>>4)];

                uint8_t *tile_color = &tile_draw[trans][tile_j][0] - 1;
                for (int l=0; l<8; ++l)
                {
                    *dst++ = palette[(*(++tile_color))&15];
                    *dst++ = palette[(*tile_color)>>4];
                }
                    
                trans = tile_translator[(*(++tile))&15];
                tile_color = &tile_draw[trans][tile_j][0] - 1;
                for (int l=0; l<8; ++l)
                {
                    *dst++ = palette[(*(++tile_color))&15];
                    *dst++ = palette[(*tile_color)>>4];
                }
            }
        }
        else // not odd
        {
            for (int k=0; k<10; ++k)
            {
                // translate the tile into what tile it should be drawn as:
                uint8_t trans = tile_translator[(*(++tile))&15];

                uint8_t *tile_color = &tile_draw[trans][tile_j][0] - 1;
                for (int l=0; l<8; ++l)
                {
                    *dst++ = palette[(*(++tile_color))&15];
                    *dst++ = palette[(*tile_color)>>4];
                }
                
                // translate the tile into what tile it should be drawn as:
                trans = tile_translator[((*tile)>>4)];

                tile_color = &tile_draw[trans][tile_j][0] - 1;
                for (int l=0; l<8; ++l)
                {
                    *dst++ = palette[(*(++tile_color))&15];
                    *dst++ = palette[(*tile_color)>>4];
                }
            }

        }

        // draw 20 un-broken tiles:
        // int odd = (index % 2);
        //for (int k=0; k<20; ++k)
        //{
        //    // translate the tile into what tile it should be drawn as:
        //    uint8_t trans;
        //    if (odd)
        //    {
        //        trans = tile_translator[((*tile++)>>4)&15];
        //        odd = 0;
        //    }
        //    else
        //    {
        //        trans = tile_translator[(*tile)&15];
        //        odd = 1;
        //    }

        //    uint8_t *tile_color = &tile_draw[trans][tile_j][0];
        //    for (int l=0; l<8; ++l)
        //    {
        //        *dst++ = palette[(*tile_color)&15];
        //        *dst++ = palette[(*tile_color++)>>4];
        //    }
        //}
    }
{'fraction_non_alphanumeric': 0.16852507530518418, 'fraction_numerical': 0.03165460022195212, 'mean_word_length': 1.3455627169062965, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 121, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2941413', 'n_tokens_mistral': 6828, 'n_tokens_neox': 6246, 'n_words': 1184}