FP to binary converter - deals with negative numbers

A General Discussion forum for TI calculators

Moderator: MaxCoderz Staff

Post Reply
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

FP to binary converter - deals with negative numbers

Post by King Harold »

check it out, it is probably poorly written (of course, I wrote it..) but it works, I've tested it.

It is not really optimized for anything, but it could well be faster than TI's convOp1 (especially because this isn't a bcall so it doesn't have the huge overhead)

returns C on error (exponent < 0), Z on positive, NZ on negative
input: HL pointing to the signbyte of the FP
output: HL is the converted number (Signed!)

note: works with higher than 9999 as well. The time it takes depends on the number of digits in decimal. Does NOT work with negative values overflowing into the 7th bit of H (less than -32767), and of course it doesn't work with values which don't fit into a 16 bit number at all. For positive numbers it is actually unsigned, so it will look negative for numbers higher than 32767.

Code: Select all

ConvFP:
.module convFP
	push hl
	call {+}
	pop de
	ld a,(de)
	bit 7,a
	ret z
	set 7,h
	ret
+:	inc hl
	ld a,(hl)
	sub $80
	ret c		;return if exp is < 0
	ld d,a	;store undivided exp
	rra	;div by 2
	ld c,a
	ld a,0
	rl a	;a is 0 (exp is even) or 1 (exp is odd)
	inc c	;+1
	ld b,0
	add hl,bc
	ex de,hl
	ld b,h	;b is exp
	ld hl,0
	inc b
	ld c,b
	;hl = zero, de = pointer to first byte, a contains the 'parity' bit
	or a
	jr z,MSNibble
LSNibble:
	ld a,(de)
	and $0F
	push de
	ld d,0
	ld e,a
	push bc
	ld a,c
	sub b
	inc a
	ld b,a
	call MultDE10pow
	pop bc
	add hl,de
	pop de
	djnz MSNibble
	or a
	ret
MSNibble:
	ld a,(de)
	and $F0
	rra
	rra
	rra
	rra
	push de
	ld d,0
	ld e,a
	push bc
	ld a,c
	sub b
	inc a
	ld b,a
	call MultDE10pow
	pop bc
	add hl,de
	pop de
	dec de
	djnz LSNibble
	or a
	ret
MultDE10pow:
	djnz {+}
	ret
+:	push hl
	ex de,hl
-:	add hl,hl	;x2
	push hl
	add hl,hl	;x4
	add hl,hl	;x8
	pop de
	add hl,de	;x10
	djnz {-}
	ex de,hl
	pop hl
	ret
.endmodule
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

come on people, I want to know what you think.
Is this actually useful?
No sloppy code?
Bugs even?

(also, that forward jumping djnz may be refreshing for some people who think of djnz as a loop instruction)
leofox
Calc Master
Posts: 1064
Joined: Fri 17 Dec, 2004 3:22 pm
Location: Probably playing DDR
Contact:

Post by leofox »

I could definitely use this on the gravlab UI (a GLUI of sorts :D), convOP1 sucks because it doesnt go above 9999.
Image
Image
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

Didn't you need 8.16 fixed point numbers?
I guess you could mod this routine to produce 8.8 fixed numbers, but you're not going to get a 24 bit number out of it..
leofox
Calc Master
Posts: 1064
Joined: Fri 17 Dec, 2004 3:22 pm
Location: Probably playing DDR
Contact:

Post by leofox »

Nah, only 16 bits are useful anyway, the rest is mostly used as guard digits of sorts.
Image
Image
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

then add something to DE (say 4) and change the "DE time 10 to the power of B" routine so that it can use negative powers (repeatedly dividing by 10 won't be very fast though) - and probably something else as well because a big change usually results in 1 or more bugs.

you can do this yourself can't you?
leofox
Calc Master
Posts: 1064
Joined: Fri 17 Dec, 2004 3:22 pm
Location: Probably playing DDR
Contact:

Post by leofox »

Becuase the Grav engine uses a nonstandard way of signing, it may be harder to add input of negative numbers.

But as long as this is the same as ConvOp1, this will definitely be useful.
Image
Image
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

well I wouldn't say it's the same, but it definitely converts a floating point number to a 16bit fixed point number although it's 16.0
also it doesn't necessarily convert OP1, it starts at HL so (HL) should be the signbyte. You could point HL to OP2 or OP3 or even list elements or to a floating point number somewhere else..
Post Reply