aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvincent.del.medico <vincent.del.medico@gmail.com>2011-06-11 10:03:17 +0000
committervincent.del.medico <vincent.del.medico@gmail.com>2011-06-11 10:03:17 +0000
commit495b0a23adc3bf06195174a464b7b31dabbaa967 (patch)
tree2e77d45d1b67981a315ae6e179b4877981663c85
parentc59675818b54f267574c79b79f59bb5f23edbdd9 (diff)
downloadlibfixmath-495b0a23adc3bf06195174a464b7b31dabbaa967.tar.gz
Added a fix16_sqr32 function, which computes sqrt with reduced 64-bit operations (and without any 64-bit operations by setting FIXMATH_NO_64BIT)
-rw-r--r--libfixmath/Makefile67
-rw-r--r--libfixmath/fix16_sqrt.c31
2 files changed, 50 insertions, 48 deletions
diff --git a/libfixmath/Makefile b/libfixmath/Makefile
index b284590..febc21c 100644
--- a/libfixmath/Makefile
+++ b/libfixmath/Makefile
@@ -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)
+
diff --git a/libfixmath/fix16_sqrt.c b/libfixmath/fix16_sqrt.c
index 06df310..ca8474d 100644
--- a/libfixmath/fix16_sqrt.c
+++ b/libfixmath/fix16_sqrt.c
@@ -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);