#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "osp.h"
 
    
/* function prototype */
void osp_atomic_inc(int32_t *pVar)
{
    if (pVar == NULL)
    {
        return;
    }

    if (((uint64_t)pVar & 3) != 0)
    {
        (*pVar)++;
    }
    else
    {
        __sync_add_and_fetch(pVar, 1);
    }
}

void osp_atomic_dec(int32_t*pVar)
{
    if (pVar == NULL)
    {
        return;
    }

    if (((uint64_t)pVar & 3) != 0)
    {
        (*pVar)--;
    }
    else
    {

        __sync_sub_and_fetch(pVar, 1);
    }
}

int32_t osp_atomic_get(int32_t*pVar)
{
    if (pVar == NULL)
    {
        return 0;
    }

    if (((uint64_t)pVar & 3) != 0)
    {
        return *pVar;
    }
    else
    {

        return __sync_sub_and_fetch(pVar, 0);
    }
}

void osp_atomic_set(int32_t *pVar, int32_t val)
{  

 
    atomic_set((OSP_atomic_t *)pVar,val);
}

 void osp_atomic64_inc(int64_t *pVar)
{
    if (pVar == NULL)
    {
        return;
    }
    
    if (((uint64_t)pVar & 7) != 0)
    {
        (*pVar)++;
    }
    else
    {
 
        __sync_add_and_fetch(pVar, 1);
    }
}

void osp_atomic64_dec(int64_t*pVar)
{
    if (pVar == NULL)
    {
        return;
    }
    
    if (((uint64_t)pVar & 7) != 0)
    {
        (*pVar)--;
    }
    else
    { 
        __sync_sub_and_fetch(pVar, 1);
    }
}

int64_t osp_atomic64_get(int64_t*pVar)
{
    if (((uint64_t)pVar & 7) != 0)
    {
        return *pVar;
    }
    else
    {
 
        return __sync_or_and_fetch(pVar, 0);
    }
}

void osp_atomic64_set(int64_t*pVar, int64_t val)
{  
    if (((uint64_t)pVar & 7) != 0)
    {
        *pVar = val;
    }
    else
    {
 
        __sync_lock_test_and_set(pVar, val);
    }
} 