Added a fix16_sqr32 function, which computes sqrt with reduced 64-bit operations (and without any 64-bit operations by setting FIXMATH_NO_64BIT)
This commit is contained in:
parent
c59675818b
commit
495b0a23ad
|
@ -1,48 +1,19 @@
|
|||
#Project settings
|
||||
PROJECT = libfixmath
|
||||
LIB =
|
||||
SRC = .
|
||||
INC =
|
||||
|
||||
#Compiler settings
|
||||
CPP = gcc
|
||||
CC = gcc
|
||||
AS = gcc
|
||||
LD = gcc
|
||||
AR = ar
|
||||
CPP_FLAGS = -O2 $(INC) -Wall -Wextra -c
|
||||
CC_FLAGS = -O2 $(INC) -Wall -Wextra -c
|
||||
AS_FLAGS = $(CC_FLAGS) -D_ASSEMBLER_
|
||||
LD_FLAGS = -Wall
|
||||
|
||||
# Find all source files
|
||||
SRC_CPP = $(foreach dir, $(SRC), $(wildcard $(dir)/*.cpp))
|
||||
SRC_C = $(foreach dir, $(SRC), $(wildcard $(dir)/*.c))
|
||||
SRC_S = $(foreach dir, $(SRC), $(wildcard $(dir)/*.S))
|
||||
OBJ_CPP = $(patsubst %.cpp, %.o, $(SRC_CPP))
|
||||
OBJ_C = $(patsubst %.c, %.o, $(SRC_C))
|
||||
OBJ_S = $(patsubst %.S, %.o, $(SRC_S))
|
||||
OBJ = $(OBJ_CPP) $(OBJ_C) $(OBJ_S)
|
||||
|
||||
# Compile rules.
|
||||
.PHONY : all
|
||||
all: $(PROJECT).a
|
||||
|
||||
$(PROJECT).a: $(OBJ)
|
||||
$(AR) rcs $(PROJECT).a $(OBJ)
|
||||
|
||||
$(OBJ_CPP) : %.o : %.cpp
|
||||
$(CPP) $(CPP_FLAGS) -o $@ $<
|
||||
|
||||
$(OBJ_C) : %.o : %.c
|
||||
$(CC) $(CC_FLAGS) -o $@ $<
|
||||
|
||||
$(OBJ_S) : %.o : %.S
|
||||
$(AS) $(AS_FLAGS) -o $@ $<
|
||||
|
||||
|
||||
|
||||
# Clean rules
|
||||
.PHONY : clean
|
||||
clean:
|
||||
rm -f $(PROJECT).a $(OBJ)
|
||||
CC = gcc
|
||||
SRC = $(wildcard *.c)
|
||||
OBJ = $(SRC:.c=.o)
|
||||
BIN = test
|
||||
|
||||
LDFLAGS += -lm
|
||||
CFLAGS += -O2 -DFIXMATH_NO_CACHE -W -Wall
|
||||
|
||||
all : $(BIN)
|
||||
|
||||
$(BIN) : $(OBJ)
|
||||
$(CC) $(LDFLAGS) $^ -o $@
|
||||
|
||||
.c.o :
|
||||
$(CC) $(CFLAGS) $< -c
|
||||
|
||||
clean:
|
||||
rm -rf *.o *~ $(BIN)
|
||||
|
||||
|
|
|
@ -7,7 +7,38 @@ fix16_t _fix16_sqrt_cache_index[4096] = { 0 };
|
|||
fix16_t _fix16_sqrt_cache_value[4096] = { 0 };
|
||||
#endif
|
||||
|
||||
fix16_t fix16_sqrt32(fix16_t inValue)
|
||||
{
|
||||
fix16_t retval = 0;
|
||||
fix16_t x = inValue;
|
||||
|
||||
/* Take only Leading 1 bit */
|
||||
x |= (x >> 1);
|
||||
x |= (x >> 2);
|
||||
x |= (x >> 4);
|
||||
x |= (x >> 8);
|
||||
x |= (x >> 16);
|
||||
x = x & ~(x >> 1);
|
||||
|
||||
/* Avoid useless loops */
|
||||
if (x & 0xFF000000)
|
||||
{
|
||||
x = 0x00800000;
|
||||
}
|
||||
/* Condition for numbers less than 1.0 */
|
||||
if ( !(inValue>>16) ) x = 0x00008000;
|
||||
|
||||
while(x)
|
||||
{
|
||||
retval |= x;
|
||||
if ( (uint32_t)fix16_mul(retval,retval) > (uint32_t)inValue)
|
||||
{
|
||||
retval &= ~x;
|
||||
}
|
||||
x >>= 1;
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
fix16_t fix16_sqrt(fix16_t inValue) {
|
||||
int neg = (inValue < 0);
|
||||
|
|
Loading…
Reference in New Issue