gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > [devKitARM] needing logarithm function

#134511 - spacy51 - Fri Jul 13, 2007 2:49 pm

Hi there!

I am currently working on an OGG Vorbis player for my NDS.

Unfortunately, there is a problem when calculating the frequency of my timer in the sound output. I #included <math.h> and used either log() or log10(), but the linker(!) throws the following error:
Quote:
1>d:/Dokumente/Development/nds/ogg/source/main.c(131): undefined reference to `log'


I guess it is related to the DS not having floating point support. Fortunately, I neither need floating point parameters nor return values of the log() function. A plain integer-only function would do for my purposes. If you know of a workaround to set the TIMER0_DATA to specific frequency without using logarithm let me know.



Here's the related source code:
Code:
void initTimer(int freq_kHz)
{
   //disable the timer before changing parameters
   TIMER0_CR = 0;
   TIMER0_DATA = TIMER_FREQ_1024( log((freq_kHz / 1000)) / log(2) ); // 1 - 1Hz, 2 - 2Hz, 3 - 4Hz
   TIMER0_CR = TIMER_ENABLE | TIMER_DIV_1024 | TIMER_IRQ_REQ;
   irqSet(IRQ_TIMER0, timerHandler);
   irqEnable(IRQ_TIMER0);
}

#134514 - simonjhall - Fri Jul 13, 2007 2:53 pm

Are you linking with -lm?
_________________
Big thanks to everyone who donated for Quake2

#134515 - spacy51 - Fri Jul 13, 2007 3:02 pm

I am using the arm9 template makefile of the latest devKitPro. I don't see a -lm option there. What does it do?

EDIT: Oh, now I know what you meant. I had to add the "-lm" option and I guess it means "Link with math library".
Thanks, it works now.

Code:
#---------------------------------------------------------------------------------
.SUFFIXES:
#---------------------------------------------------------------------------------

ifeq ($(strip $(DEVKITARM)),)
$(error "Please set DEVKITARM in your environment. export DEVKITARM=<path to>devkitARM")
endif

include $(DEVKITARM)/ds_rules

#---------------------------------------------------------------------------------
# TARGET is the name of the output
# BUILD is the directory where object files & intermediate files will be placed
# SOURCES is a list of directories containing source code
# DATA is a list of directories containing data files
# INCLUDES is a list of directories containing header files
#---------------------------------------------------------------------------------
TARGET      :=   arm9
BUILD      :=   build
SOURCES      :=   source tremor
DATA      :=   data
INCLUDES   :=   include

#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
ARCH   :=   -mthumb -mthumb-interwork

CFLAGS   :=   -g -Wall -O2\
      -march=armv5te -mtune=arm946e-s \
      -fomit-frame-pointer -ffast-math \
      $(ARCH)

CFLAGS   +=   $(INCLUDE) -DARM9
CXXFLAGS   := $(CFLAGS) -fno-rtti -fno-exceptions

ASFLAGS   :=   -g -march=armv5te -mtune=arm946e-s
LDFLAGS   =   -specs=ds_arm9.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)

#---------------------------------------------------------------------------------
# any extra libraries we wish to link with the project
#---------------------------------------------------------------------------------
LIBS   := -lnds9


#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
# include and lib
#---------------------------------------------------------------------------------
LIBDIRS   :=   $(LIBNDS)

#---------------------------------------------------------------------------------
# no real need to edit anything past this point unless you need to add additional
# rules for different file extensions
#---------------------------------------------------------------------------------
ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------

export OUTPUT   :=   $(CURDIR)/$(TARGET)

export VPATH   :=   $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
         $(foreach dir,$(DATA),$(CURDIR)/$(dir))

export DEPSDIR   :=   $(CURDIR)/$(BUILD)

CFILES      :=   $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES   :=   $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
SFILES      :=   $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
BINFILES   :=   $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))

#---------------------------------------------------------------------------------
# use CXX for linking C++ projects, CC for standard C
#---------------------------------------------------------------------------------
ifeq ($(strip $(CPPFILES)),)
#---------------------------------------------------------------------------------
   export LD   :=   $(CC)
#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
   export LD   :=   $(CXX)
#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------

export OFILES   :=   $(addsuffix .o,$(BINFILES)) \
         $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)

export INCLUDE   :=   $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
         $(foreach dir,$(LIBDIRS),-I$(dir)/include) \
         -I$(CURDIR)/$(BUILD)

export LIBPATHS   :=   $(foreach dir,$(LIBDIRS),-L$(dir)/lib)

.PHONY: $(BUILD) clean all

#---------------------------------------------------------------------------------
all: $(BUILD)

$(BUILD):
   @[ -d $@ ] || mkdir -p $@
   @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile

#---------------------------------------------------------------------------------
clean:
   @echo clean ...
   @rm -fr $(BUILD) $(TARGET).elf $(TARGET).nds $(TARGET).arm9 $(TARGET).ds.gba

run:
   wmb -data $(OUTPUT).nds


#---------------------------------------------------------------------------------
else

DEPENDS   :=   $(OFILES:.o=.d)

#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
$(OUTPUT).ds.gba   :    $(OUTPUT).nds
$(OUTPUT).nds   :    $(OUTPUT).arm9
$(OUTPUT).arm9   :   $(OUTPUT).elf
$(OUTPUT).elf   :   $(OFILES)

#---------------------------------------------------------------------------------
%.bin.o   :   %.bin
#---------------------------------------------------------------------------------
   @echo $(notdir $<)
   @$(bin2o)


-include $(DEPENDS)

#---------------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------------

#134545 - tepples - Fri Jul 13, 2007 8:53 pm

spacy51 wrote:
Fortunately, I neither need floating point parameters nor return values of the log() function. A plain integer-only function would do for my purposes. If you know of a workaround to set the TIMER0_DATA to specific frequency without using logarithm let me know.

How about making a lookup table?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#134551 - wintermute - Fri Jul 13, 2007 9:46 pm

spacy51 wrote:
Hi there!

I am currently working on an OGG Vorbis player for my NDS.

Unfortunately, there is a problem when calculating the frequency of my timer in the sound output. I #included <math.h> and used either log() or log10(), but the linker(!) throws the following error:
Quote:
1>d:/Dokumente/Development/nds/ogg/source/main.c(131): undefined reference to `log'



Why are you actually using log anyway?

This code makes no sense to me at all, where did you get this from.

Code:

   TIMER0_DATA = TIMER_FREQ_1024( log((freq_kHz / 1000)) / log(2) ); // 1 - 1Hz, 2 - 2Hz, 3 - 4Hz



The TIMER_FREQ_1024 macro takes a frequency in Hz and converts it into a timer count value which produces that frequency for a div 1024 timer.
_________________
devkitPro - professional toolchains at amateur prices
devkitPro IRC support
Personal Blog

#134556 - kusma - Fri Jul 13, 2007 10:23 pm

log(x) / log(2) for integers can be reimplemented with the CLZ instruction on the ARM9, or a LUT on the ARM7.

#134559 - wintermute - Fri Jul 13, 2007 10:46 pm

kusma wrote:
log(x) / log(2) for integers can be reimplemented with the CLZ instruction on the ARM9, or a LUT on the ARM7.


But why?

Why would you use that for setting a DS timer frequency?

Did anyone look at his code and try to infer what he was trying to do?
_________________
devkitPro - professional toolchains at amateur prices
devkitPro IRC support
Personal Blog

#134567 - kusma - Sat Jul 14, 2007 2:54 am

wintermute wrote:
But why?

Because I prefer answering the question rather than telling him what's wrong with it based on assumptions. (Please, no old quotes. And yes, you probably are right about the need for the function. ;) )

#134603 - LiraNuna - Sat Jul 14, 2007 5:18 pm

Quote:
The TIMER_FREQ_1024 macro takes a frequency in Hz and converts it into a timer count value which produces that frequency for a div 1024 timer.


Code:
TIMER0_DATA = TIMER_FREQ_1024(freq_kHz * 1000);


?!
_________________
Private property.
Violators will be shot, survivors will be shot again.