[clean-list] Sub-arrays How do I manipulate an array pointer?

John van Groningen johnvg at cs.ru.nl
Fri Jun 2 17:45:10 MEST 2006


Matthew Bromberg wrote:
>I am not sure how to proceed concerning manipulating pointers to arrays in
>clean.
>In particular I need to pass to the C code, not the start of an unboxed
>array, but rather some
>offset from the beginning,

That is not possible with the current implementation of ccall. However
it is possible to add the offset using abc instructions just before
the ccall instruction. For example:

daxpy_oy :: !Int !Real !{#Real} !Int !{#Real} !Int !Int !*Real -> *Real;
daxpy_oy nlen alpha x x_d y y_o y_d s = code {
	| y   at offset 1 on the A stack
	| y_o at offset 4 on the B stack
	push_b 4		| y_o
	push_a_b 1		| y

	pushI 3
	push_b 2
	shiftl%
	addI
	push_b_a 0
	pop_b 2

	update_a 0 2	| y+y_o<<3
	pop_a 1

	| remove y_0
	update_b 3 4
	update_b 2 3
	update_b 1 2
	update_b 0 1
	pop_b 1

	ccall daxpy "IRAIAI:V:R"
}

daxpy_oxy :: !Int !Real !{#Real} !Int !Int !{#Real} !Int !Int !*Real -> *Real;
daxpy_oxy nlen alpha x x_o x_d y y_o y_d s = code {
	| x   at offset 0 on the A stack
	| y   at offset 1 on the A stack
	| x_o at offset 3 on the B stack
	| y_o at offset 5 on the B stack

	push_b 3		| x_o
	push_a_b 0		| x

	pushI 3
	push_b 2
	shiftl%
	addI
	push_b_a 0
	pop_b 2

	update_a 0 1	| x+x_o<<3
	pop_a 1

	push_b 6		| y_o
	push_a_b 1		| y

	pushI 3
	push_b 2
	shiftl%
	addI
	push_b_a 0
	pop_b 2

	update_a 0 2	| y+y_o<<3
	pop_a 1

	| remove x_o and y_o
	update_b 4 5
	update_b 2 4
	update_b 1 3
	update_b 0 2
	pop_b 2

	ccall daxpy "IRAIAI:V:R"
}

Start :: (!{#Real},!{#Real},!Real);
Start
	# st = 0.0;
	# r1 = { 1.0,  2.0,  3.0 , 4.0};
//	# st = daxpy   4 2.0 {0.5, -0.5, 1.0, -1.0} 1 r1 1 st;
	# st = daxpy_oy 2 2.0 {0.5, -0.5, 1.0, -1.0} 1 r1 1 1 st;
//	# st = daxpy_oxy 2 2.0 {0.5, -0.5, 1.0, -1.0} 1 1 r1 1 1 st;
	# r2 = {11.0, 12.0, 13.0, 14.0};
//	#! st = daxpy 4 3.0 {0.5, -0.5, 1.0, -1.0} 1 r2 1 st;
	#! st = daxpy_oxy 2 3.0 {0.5, -0.5, 1.0, -1.0} 1 1 r2 1 1 st;
	= (r1,r2,st);

The ccall could be extended to make this possible without all the abc
instructions.

>or even better to just store the subarray, as an
>array pointing
>to the middle of another array,  if that were possible.

This is not possible for arrays that are allocated in the heap of Clean.

>I notice in the htoclean documentation that there is a mention of treating C
>pointers as integers, but I have no idea how to coerce an array of unboxed
>doubles to an integer representing a pointer.  Is there a simple way to do
>this?  In Haskell it is possible to add offsets to foreign pointers for
>example.

Pointers that point to memory allocated outside the Clean heap (for example
by malloc in C) can be stored as an Int in Clean. Offsets can be added to
those integers.

Kind regards,

John van Groningen


More information about the clean-list mailing list