Skip to contentSkip to conversation

PATCH: itest - an integer compare function

U-Boot-Users Robin Gilks 2 participants 2 messages Jan 6, 2004 – Feb 6, 2004

lore
Share & tools
lore
#0 lore raw

Changelog:
Introduce a new command to be used in conditional expressions (such as hush 'if') to test the value of a memory location.
Supports -eq, -ne, -gt, -lt, -ge, -le
Allows scalars or memory contents (value prefixed by '*') Makes use of command data width expression of .l, .wm .b for 4, 2 or 1 byte wide comparisons
e.g.
if itest.l *40000 -eq 12345678; then bootm 41000; else bootm 80000; fi

Patch is against u-boot-1.0.0

Note: I'm still concerned that the CONFIG_COMMANDS mask has run out of bits - maybe time for a different mechanism as I'm sure the bit I've used has gone by now!!

Signature · 12 lines
Robin Gilks
Senior Design Engineer          Phone: (+64)(3) 357 1569
Tait Electronics                Fax  :  (+64)(3) 359 4632
PO Box 1645 Christchurch        Email : robin.gilks at tait.co.nz
New Zealand
-------------- next part --------------
A non-text attachment was scrubbed...
Name: u-boot-itest.patch
Type: text/x-diff
Size: 5219 bytes
Desc: not available
Url : http://lists.denx.de/pipermail/u-boot/attachments/20040106/447446f1/attachment.patch 
#1 lore raw

Dear Robin,

Quoted 9 lines · in message <[email protected]> you wrote:

Introduce a new command to be used in conditional expressions (such as hush 'if') to test the value of a memory location.
Supports -eq, -ne, -gt, -lt, -ge, -le
Allows scalars or memory contents (value prefixed by '*') Makes use of command data width expression of .l, .wm .b for 4, 2 or 1 byte wide comparisons
e.g.
if itest.l *40000 -eq 12345678; then bootm 41000; else bootm 80000; fi

Sorry, but I don;t want to add the code as is.

May I suggest you rewrite it a bit?

Quoted 34 lines
(quoted diff) +34 -0
+	switch (op) {
+	case EQ:
+		return (l == r);
+	case NE:
+		return (l != r);
+	case LT:
+		return (l < r);
+	case GT:
+		return (l > r);
+	case LE:
+		return (l <= r);
+	case GE:
+		return (l >= r);
+	}
+
+	return (FALSE);
+}
+
+
+int binary_test(char *op, char *arg1, char *arg2, int w)
+{
+
+	if (op[2] == 't') {
+		switch (op[1]) {
+		case 'l':
+			return (arithcomp(arg1, arg2, LT, w));	/* -lt */
+		case 'g':
+			return (arithcomp(arg1, arg2, GT, w));	/* -gt */
+		}
+	} else if (op[1] == 'e') {
+		switch (op[2]) {
+		case 'q':
+			return (arithcomp(arg1, arg2, EQ, w));	/* -eq */
+		}

Review

Quoted 10 lines
(quoted diff) +10 -0
+	} else if (op[2] == 'e') {
+		switch (op[1]) {
+		case 'n':
+			return (arithcomp(arg1, arg2, NE, w));	/* -ne */
+		case 'g':
+			return (arithcomp(arg1, arg2, GE, w));	/* -ge */
+		case 'l':
+			return (arithcomp(arg1, arg2, LE, w));	/* -le */
+		}
+	}

Instead of all the nested tests we could use an array with command names and table lookup - which would also allow to use operators like '<' or ">=", and I also expect the total memory footprint to be even smaller.

Also, instead of just returning FALSE we should at least print an error message for invalid input / operators.

Finally, maybe you even want to add string compare :-)

Best regards,

Wolfgang Denk

Signature · 5 lines
See us @ Embedded World, Nuremberg, Feb 17 - 19,  Hall 12.0 Booth 440
Phone: (+49)-8142-4596-87  Fax: (+49)-8142-4596-88  Email: wd at denx.de
"We have the right to survive!"
"Not be killing others."
	-- Deela and Kirk, "Wink of An Eye", stardate 5710.5