Grow like a flower forever – Chirag Patel


Grow like a flower forever – Chirag Patel

Take care of your health. Yes. Why not. But, how?

Some simple tips:
1. Eat whenever you want to (I am not kidding).
2. Eat whatever you like (Have I gone mad?).
3. Eat enjoying every bite and chew as many times as you can (now, you are talking).
4. Stop eating when your stomach says you are full (sure, you are not fool).
5. Do light aerobics for 15 minutes everyday.
6. Take as much slow and as much deep as possible breaths, atleast 5 such breathes everyday.

And, enjoy your healthy life.

Advertisements

Color a file type with VIM


I am using gVim 8.0 for Windows. You can copy log.vim to C:\Program Files (x86)\Vim\vim80\syntax folder. You can add following lines to C:\Program Files (x86)\Vim\vim80\filetype.vim file (usually in alphabetical order listing):

” Log
au BufNewFile,BufRead *.log setf log

———- content of log.vim —————–
” Vim syntax file
” Language: Log file
” Maintainer: Chirag Patel
” Latest Revision: 08 August 2017

if exists(“b:current_syntax”)
finish
endif

:syn keyword logError CRI Error Exception FAILED Failed failed error aborted
:syn keyword logWarning WRN warning Warning Stopped
:syn keyword logInfo INF
:syn keyword logDebug DBG

hi def logError guibg=red guifg=white
hi def logWarning guibg=yellow guifg=black

double precision rounding


Uses my_double_greater_check from http://rutmandal.info/double-precision-comparison-in-cc/

double my_double_round( double val, int multiplier )
{
         double val_adj = val * multiplier;
         if ( my_double_greater_check( val_adj floor( val_adj ), 0.0 ) > 0 )
                      val = round( val_adj ) / multiplier;
         return val;
}

round and floor are C math library <math.h> functions.

E.g.

double rnd_val = my_double_round( 0.000998, 1000);

rnd_val will be 0.001

double precision comparision


Checking double precision value for limits is tricky.

Say,

double x = 1.0;
if ( x == 1.0 ) do_something();

 

This comparison can fail as double precision value when converted to hexadecimal (what computer can do only) representation, looses some least significant bits or positions beyond double precision digits. So, in above example if ( x == 0.9999999 ) might have worked depending on compiler and/or CPU. Therefore, in programming, we create functions to check such limits to approximate values.

 

// Used for double type comparasion
typedef union mydouble_ {
    double dbl_val;
    unsigned long un_u32_val[2];
    unsigned char un_u8_val[8];
} mydouble;

 

// Checks double value against close enough limits i.e. min <= val <= max
// Return: -1 => val < min
// Return: 0 => min <= val <= max
// Return: 1 => val > max
int my_double_limit_check( double val, double min, double max )
{
    mydouble dbl;
    mydouble cmp;
    int ret = 0;
 
    if ( val < min )
    {
        dbl.dbl_val = val;
        cmp.dbl_val = min;
        if ( dbl.un_u32_val[0] != cmp.un_u32_val[0] )
            ret = -1;
        else if ( dbl.un_u8_val[4] != cmp.un_u8_val[4] )
            ret = -1;
    }
 
    if ( val > max )
    {
        dbl.dbl_val = val;
        cmp.dbl_val = max;
        if ( dbl.un_u32_val[0] != cmp.un_u32_val[0] )
            ret = 1;
        else if ( dbl.un_u8_val[4] != cmp.un_u8_val[4] )
            ret = 1;
    }
 
    return ret;
}
 
// Checks double value against close enough less value i.e. val < min
// Return: -1 => val < min
// Return: 0 => val >= min
int my_double_less_check( double val, double min )
{
    mydouble dbl;
    mydouble cmp;
    int ret = 0;
 
    if ( val < min )
    {
        dbl.dbl_val = val;
        cmp.dbl_val = min;
        if ( dbl.un_u32_val[0] != cmp.un_u32_val[0] )
            ret = -1;
        else if ( dbl.un_u8_val[4] != cmp.un_u8_val[4] )
            ret = -1;
    }
 
    return ret;
}
 
// Checks double value against close enough greater value i.e. val > max
// Return: 1 => val > max
// Return: 0 => val <= max
int my_double_greater_check( double val, double max )
{
    myouble dbl;
    myouble cmp;
    int ret = 0;
 
    if ( val > max )
    {
        dbl.dbl_val = val;
        cmp.dbl_val = max;
        if ( dbl.un_u32_val[0] != cmp.un_u32_val[0] )
            ret = 1;
        else if ( dbl.un_u8_val[4] != cmp.un_u8_val[4] )
            ret = 1;
    }
 
    return ret;
}
 
// Checks double value against close enough equal value i.e. val == eql
// Return: 0 => val == eql
// Retrun: 1 => val != eql
int my_double_equal_check( double val, double eql )
{
    mydouble dbl;
    mydouble cmp;
    int ret = 0;
 
    if ( val != eql )
    {
        dbl.dbl_val = val;
        cmp.dbl_val = eql;
        if ( dbl.un_u32_val[0] != cmp.un_u32_val[0] )
            ret = 1;
        else if ( dbl.un_u8_val[4] != cmp.un_u8_val[4] )
            ret = 1;
    }
 
    return ret;
}

Memory thrashing


Memory Thrashing compiled by Chirag Patel December 27, 2008
(Ref: Embedded Systems Design June 2008)

Memory threshing is a typical problem that goes unnotices while programming time-critical systems. Translation Look-aside Buffers (TLB) are used for data and instruction cache. There are two main cache-replacement schemes: 1) Least Frequently Used (LFU) and 2) Least Recently Used (LRU). Memory accesses require address translations in modern systems. So, when a page table is is found in an on-chip TLB (a TLB hit), the lookup normally does not do translation. On TLB miss, the processor must look again and must calculate offset to find a byte physically.

When a system has multiple concurrent accesses, operating system schedules time slices for those processes. If we consider 32-entry LRU TLB and 6 processes each using different page memory, at least 12 pages are active at any given time (instruction + data). If every process uses double nested procedures (or jumps between 3 pages) in each time slice, there are 36 active pages. If operating system time slices sequentially, by the time 6th process is reached, 30 pages are accessed. By the end of 6th process time slice, 36 page accesses should have passed. So, the cache manager will discard first 4 accesses (LRU algorithm). When the time comes for the first process, it will have TLB misses for those first 4 page accesses. As the processor continues, it keeps discarding the pages that the next process in sequence will need. This memory thrashing greatly degrades system performance.

To avoid thrashing:
– Long unused variables should be declared absolutely rather than relatively.
– Macros should be expanded.
– Avoid nesting procedure calls whenever possible.
– Minimize number of concurrent tasks.
– Don’t use jumps larger than page size unless absolutely necessary.

VIM configuration


Custom syntax with vim Chirag Patel August 10, 2015

On Linux, install vim if you do not have.
On Windows, install gVim (I tried 7.4).

Create vjn.vim file similar to following example.

Vim syntax file
Language: config file
Maintainer: Chirag Patel

if exists(b:current_syntax)
finish
endif

:syn keyword error ERROR
:syn keyword warn WARNING

:syn keyword user chirag vrund svara parul
:syn keyword admin digant vikas hina

hi def error guibg=red guifg=white
hi def warn guibg=yellow guifg=black

hi def user guifg=tomato1
hi def admin guifg=deep pink

Check vim74/rgb.txt file to find color definitions.
Add vjn.vim to vim74/syntax folder.
Edit vim74/filetype.vim file and add following lines to alphabetical order of file types.

Config
au BufNewFile,BufRead *.vjn setf vjn

You are all set to have fun and have interesting configuration file looks.

Also, do not forget to add following to your vim settings (_vimrc or .vimrc) for more fun.

colorscheme slate
set guifont=Courier_New:h10:cANSI
set autoindent
set ts=4
set softtabstop=4
set shiftwidth=4
set incsearch
set hlsearch
set nowrap
set noexpandtab
set showmatch
filetype on
syntax on
set ruler
set cindent
set noswapfile
set mat=5