project_files/hwc/rtl/pmath.c
author koda
Wed, 07 Nov 2012 18:04:27 +0000
branchwebgl
changeset 7983 02f36c3e7f6c
child 8047 25a4daa6473c
permissions -rw-r--r--
add xymeng's rtl port
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     1
#include "pmath.h"
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     2
#include <math.h>
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     3
#include <stdlib.h>
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     4
#include <stdint.h>
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     5
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     6
/*
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     7
 * power raises base to the power power.
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     8
 * This is equivalent to exp(power*ln(base)). Therefore base should be non-negative.
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     9
 */
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    10
float fpcrtl_power(float base, float exponent)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    11
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    12
    return exp(exponent * log(base));
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    13
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    14
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    15
/* Currently the games only uses sign of an integer */
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    16
int fpcrtl_signi(int x)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    17
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    18
    if(x > 0){
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    19
        return 1;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    20
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    21
    else if(x < 0){
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    22
        return -1;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    23
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    24
    else{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    25
        return 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    26
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    27
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    28
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    29
float fpcrtl_csc(float x)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    30
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    31
    return 1 / sin(x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    32
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    33
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    34
float __attribute__((overloadable)) fpcrtl_abs(float x)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    35
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    36
    return fabs(x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    37
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    38
double __attribute__((overloadable)) fpcrtl_abs(double x)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    39
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    40
    return fabs(x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    41
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    42
int __attribute__((overloadable)) fpcrtl_abs(int x)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    43
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    44
    return abs(x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    45
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    46
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    47
int64_t __attribute__((overloadable)) fpcrtl_abs(int64_t x)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    48
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    49
    return x < 0 ? -x : x;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    50
}