ObjFW  Check-in [f4313d070a]

Overview
Comment:Reduce ObjC method calls in OFFloatVector and OFFloatMatrix.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: f4313d070a1278d8d79fc14bc319a51e0cc97f9b76320e03603345ea880f53be
User & Date: js on 2011-06-13 14:08:58
Other Links: manifest | tags
Context
2011-06-13
21:28
Fix a typo in README. check-in: 31ad00b702 user: js tags: trunk
14:08
Reduce ObjC method calls in OFFloatVector and OFFloatMatrix. check-in: f4313d070a user: js tags: trunk
03:47
Fix -[conformsToProtocol:] for the old GNU runtime. check-in: a1769f0105 user: js tags: trunk
Changes

Modified src/OFFloatMatrix.h from [40205a6dae] to [8345b9957a].

21
22
23
24
25
26
27

28
29
30
31
32
33
34
@class OFFloatVector;

/**
 * \brief A class for storing and manipulating matrices of floats.
 */
@interface OFFloatMatrix: OFObject <OFCopying>
{

	size_t rows, columns;
	float *data;
}

/**
 * \brief Creates a new matrix with the specified dimension.
 *







>







21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@class OFFloatVector;

/**
 * \brief A class for storing and manipulating matrices of floats.
 */
@interface OFFloatMatrix: OFObject <OFCopying>
{
@public
	size_t rows, columns;
	float *data;
}

/**
 * \brief Creates a new matrix with the specified dimension.
 *

Modified src/OFFloatMatrix.m from [a7338c48b5] to [d37cee00b5].

12
13
14
15
16
17
18

19
20
21
22
23
24
25
26
27

28
29
30
31
32
33
34
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"


#include <string.h>
#include <math.h>

#import "OFFloatMatrix.h"
#import "OFFloatVector.h"
#import "OFString.h"

#import "OFInvalidArgumentException.h"
#import "OFNotImplementedException.h"

#import "OFOutOfRangeException.h"

#import "macros.h"

@implementation OFFloatMatrix
+ matrixWithRows: (size_t)rows
	 columns: (size_t)columns







>









>







12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#include <stdlib.h>
#include <string.h>
#include <math.h>

#import "OFFloatMatrix.h"
#import "OFFloatVector.h"
#import "OFString.h"

#import "OFInvalidArgumentException.h"
#import "OFNotImplementedException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"

#import "macros.h"

@implementation OFFloatMatrix
+ matrixWithRows: (size_t)rows
	 columns: (size_t)columns
65
66
67
68
69
70
71
72

73
74
75
76


77
78
79
80
81
82
83
84
{
	self = [super init];

	@try {
		rows = rows_;
		columns = columns_;

		if (SIZE_MAX / rows < columns)

			@throw [OFOutOfRangeException
			    newWithClass: isa];

		data = [self allocMemoryForNItems: rows * columns


					 withSize: sizeof(float)];

		memset(data, 0, rows * columns * sizeof(float));

		if (rows == columns) {
			size_t i;

			for (i = 0; i < rows * columns; i += rows + 1)







|
>



|
>
>
|







67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
{
	self = [super init];

	@try {
		rows = rows_;
		columns = columns_;

		if (SIZE_MAX / rows < columns ||
		    SIZE_MAX / rows * columns < sizeof(float))
			@throw [OFOutOfRangeException
			    newWithClass: isa];

		if ((data = malloc(rows * columns * sizeof(float))) == NULL)
			@throw [OFOutOfMemoryException
			     newWithClass: isa
			    requestedSize: rows * columns * sizeof(float)];

		memset(data, 0, rows * columns * sizeof(float));

		if (rows == columns) {
			size_t i;

			for (i = 0; i < rows * columns; i += rows + 1)
115
116
117
118
119
120
121
122

123
124
125
126


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141







142
143
144
145
146
147
148

	@try {
		size_t i;

		rows = rows_;
		columns = columns_;

		if (SIZE_MAX / rows < columns)

			@throw [OFOutOfRangeException
			    newWithClass: isa];

		data = [self allocMemoryForNItems: rows * columns


					 withSize: sizeof(float)];

		for (i = 0; i < rows; i++) {
			size_t j;

			for (j = i; j < rows * columns; j += rows)
				data[j] = (float)va_arg(arguments, double);
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}








- (void)setValue: (float)value
	  forRow: (size_t)row
	  column: (size_t)column
{
	if (row >= rows || column >= columns)
		@throw [OFOutOfRangeException newWithClass: isa];







|
>
|
<

|
>
>
|














>
>
>
>
>
>
>







120
121
122
123
124
125
126
127
128
129

130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162

	@try {
		size_t i;

		rows = rows_;
		columns = columns_;

		if (SIZE_MAX / rows < columns ||
		    SIZE_MAX / rows * columns < sizeof(float))
			@throw [OFOutOfRangeException newWithClass: isa];


		if ((data = malloc(rows * columns * sizeof(float))) == NULL)
			@throw [OFOutOfMemoryException
			     newWithClass: isa
			    requestedSize: rows * columns * sizeof(float)];

		for (i = 0; i < rows; i++) {
			size_t j;

			for (j = i; j < rows * columns; j += rows)
				data[j] = (float)va_arg(arguments, double);
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	free(data);

	[super dealloc];
}

- (void)setValue: (float)value
	  forRow: (size_t)row
	  column: (size_t)column
{
	if (row >= rows || column >= columns)
		@throw [OFOutOfRangeException newWithClass: isa];
310
311
312
313
314
315
316
317


318
319
320
321
322
323
324
325
	float *newData;
	size_t i, base1, base2;

	if (rows != matrix->columns)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	newData = [self allocMemoryForNItems: matrix->rows * columns


				    withSize: sizeof(float)];

	base1 = 0;
	base2 = 0;

	for (i = 0; i < columns; i++) {
		size_t base3 = base2;
		size_t j;







|
>
>
|







324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
	float *newData;
	size_t i, base1, base2;

	if (rows != matrix->columns)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	if ((newData = malloc(matrix->rows * columns * sizeof(float))) == NULL)
		@throw [OFOutOfMemoryException
		     newWithClass: isa
		    requestedSize: matrix->rows * columns * sizeof(float)];

	base1 = 0;
	base2 = 0;

	for (i = 0; i < columns; i++) {
		size_t base3 = base2;
		size_t j;
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357





358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
			base3++;
		}

		base1 += rows;
		base2 += matrix->rows;
	}

	[self freeMemory: data];
	data = newData;

	rows = matrix->rows;
}

- (void)transpose
{
	float *newData = [self allocMemoryForNItems: rows * columns
					   withSize: sizeof(float)];
	size_t i, k;






	rows ^= columns;
	columns ^= rows;
	rows ^= columns;

	for (i = k = 0; i < rows; i++) {
		size_t j;

		for (j = i; j < rows * columns; j += rows)
			newData[j] = data[k++];
	}

	[self freeMemory: data];
	data = newData;
}

- (void)translateWithVector: (OFFloatVector*)vector
{
	OFFloatMatrix *translation;
	float *cArray;







|







|
<

>
>
>
>
>












|







356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371

372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
			base3++;
		}

		base1 += rows;
		base2 += matrix->rows;
	}

	free(data);
	data = newData;

	rows = matrix->rows;
}

- (void)transpose
{
	float *newData;

	size_t i, k;

	if ((newData = malloc(rows * columns * sizeof(float))) == NULL)
		@throw [OFOutOfMemoryException newWithClass: isa
					      requestedSize: rows * columns *
							     sizeof(float)];

	rows ^= columns;
	columns ^= rows;
	rows ^= columns;

	for (i = k = 0; i < rows; i++) {
		size_t j;

		for (j = i; j < rows * columns; j += rows)
			newData[j] = data[k++];
	}

	free(data);
	data = newData;
}

- (void)translateWithVector: (OFFloatVector*)vector
{
	OFFloatMatrix *translation;
	float *cArray;

Modified src/OFFloatVector.m from [5ca73ec320] to [b49ac3419e].

12
13
14
15
16
17
18

19
20
21
22
23
24
25
26
27

28
29
30
31


32






33
34
35
36
37
38
39
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"


#include <string.h>
#include <math.h>

#import "OFFloatVector.h"
#import "OFFloatMatrix.h"
#import "OFString.h"

#import "OFInvalidArgumentException.h"
#import "OFNotImplementedException.h"

#import "OFOutOfRangeException.h"

#import "macros.h"



@implementation OFFloatVector






+ vectorWithDimension: (size_t)dimension
{
	return [[[self alloc] initWithDimension: dimension] autorelease];
}

+ vectorWithDimensionAndData: (size_t)dimension, ...
{







>









>




>
>

>
>
>
>
>
>







12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#include <stdlib.h>
#include <string.h>
#include <math.h>

#import "OFFloatVector.h"
#import "OFFloatMatrix.h"
#import "OFString.h"

#import "OFInvalidArgumentException.h"
#import "OFNotImplementedException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"

#import "macros.h"

static Class floatMatrix = Nil;

@implementation OFFloatVector
+ (void)initialize
{
	if (self == [OFFloatVector class])
		floatMatrix = [OFFloatMatrix class];
}

+ vectorWithDimension: (size_t)dimension
{
	return [[[self alloc] initWithDimension: dimension] autorelease];
}

+ vectorWithDimensionAndData: (size_t)dimension, ...
{
59
60
61
62
63
64
65


66



67
68
69
70
71
72
73
74
- initWithDimension: (size_t)dimension_
{
	self = [super init];

	@try {
		dimension = dimension_;



		data = [self allocMemoryForNItems: dimension



					 withSize: sizeof(float)];

		memset(data, 0, dimension * sizeof(float));
	} @catch (id e) {
		[self release];
		@throw e;
	}








>
>
|
>
>
>
|







69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
- initWithDimension: (size_t)dimension_
{
	self = [super init];

	@try {
		dimension = dimension_;

		if (SIZE_MAX / dimension < sizeof(float))
			@throw [OFOutOfRangeException newWithClass: isa];

		if ((data = malloc(dimension * sizeof(float))) == NULL)
			@throw [OFOutOfMemoryException
			     newWithClass: isa
			    requestedSize: dimension * sizeof(float)];

		memset(data, 0, dimension * sizeof(float));
	} @catch (id e) {
		[self release];
		@throw e;
	}

94
95
96
97
98
99
100


101



102
103
104
105
106
107
108
109
110
111
112







113
114
115
116
117
118
119
	self = [super init];

	@try {
		size_t i;

		dimension = dimension_;



		data = [self allocMemoryForNItems: dimension



					 withSize: sizeof(float)];

		for (i = 0; i < dimension; i++)
			data[i] = (float)va_arg(arguments, double);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}








- (void)setValue: (float)value
	 atIndex: (size_t)index
{
	if (index >= dimension)
		@throw [OFOutOfRangeException newWithClass: isa];








>
>
|
>
>
>
|










>
>
>
>
>
>
>







109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
	self = [super init];

	@try {
		size_t i;

		dimension = dimension_;

		if (SIZE_MAX / dimension < sizeof(float))
			@throw [OFOutOfRangeException newWithClass: isa];

		if ((data = malloc(dimension * sizeof(float))) == NULL)
			@throw [OFOutOfMemoryException
			     newWithClass: isa
			    requestedSize: dimension * sizeof(float)];

		for (i = 0; i < dimension; i++)
			data[i] = (float)va_arg(arguments, double);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	free(data);

	[super dealloc];
}

- (void)setValue: (float)value
	 atIndex: (size_t)index
{
	if (index >= dimension)
		@throw [OFOutOfRangeException newWithClass: isa];

317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334


335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355

	for (i = 0; i < dimension; i++)
		data[i] /= magnitude;
}

- (void)multiplyWithMatrix: (OFFloatMatrix*)matrix
{
	size_t rows, columns;
	float *cArray, *newData;
	size_t i, j, k;

	columns = [matrix columns];

	if (dimension != columns)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	rows = [matrix rows];


	cArray = [matrix cArray];

	newData = [self allocMemoryForNItems: rows
				    withSize: sizeof(float)];
	memset(newData, 0, rows * sizeof(float));

	for (i = j = k = 0; i < rows * columns; i++) {
		newData[j] += cArray[i] * data[k];

		if (++j == rows) {
			k++;
			j = 0;
		}
	}

	[self freeMemory: data];
	data = newData;

	dimension = rows;
}
@end







<
|


<
|
<



|
>
>
|

<
<
|

|
|

|





|


|


344
345
346
347
348
349
350

351
352
353

354

355
356
357
358
359
360
361
362


363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379

	for (i = 0; i < dimension; i++)
		data[i] /= magnitude;
}

- (void)multiplyWithMatrix: (OFFloatMatrix*)matrix
{

	float *newData;
	size_t i, j, k;


	if (matrix->isa != floatMatrix || dimension != matrix->columns)

		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	if ((newData = malloc(matrix->rows * sizeof(float))) == NULL)
		@throw [OFOutOfMemoryException
		     newWithClass: isa
		    requestedSize: matrix->rows * sizeof(float)];



	memset(newData, 0, matrix->rows * sizeof(float));

	for (i = j = k = 0; i < matrix->rows * matrix->columns; i++) {
		newData[j] += matrix->data[i] * data[k];

		if (++j == matrix->rows) {
			k++;
			j = 0;
		}
	}

	free(data);
	data = newData;

	dimension = matrix->rows;
}
@end

Modified src/OFObject.m from [251a843844] to [1d0de2f3be].

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#define PRE_IVAR_ALIGN ((sizeof(struct pre_ivar) + \
	(__BIGGEST_ALIGNMENT__ - 1)) & ~(__BIGGEST_ALIGNMENT__ - 1))
#define PRE_IVAR ((struct pre_ivar*)(void*)((char*)self - PRE_IVAR_ALIGN))

static struct {
	Class isa;
} alloc_failed_exception;
static Class autoreleasepool = Nil;

static SEL cxx_construct = NULL;
static SEL cxx_destruct = NULL;

size_t of_pagesize;

#ifdef NEED_OBJC_SYNC_INIT







|







83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#define PRE_IVAR_ALIGN ((sizeof(struct pre_ivar) + \
	(__BIGGEST_ALIGNMENT__ - 1)) & ~(__BIGGEST_ALIGNMENT__ - 1))
#define PRE_IVAR ((struct pre_ivar*)(void*)((char*)self - PRE_IVAR_ALIGN))

static struct {
	Class isa;
} alloc_failed_exception;
static Class autoreleasePool = Nil;

static SEL cxx_construct = NULL;
static SEL cxx_destruct = NULL;

size_t of_pagesize;

#ifdef NEED_OBJC_SYNC_INIT
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800

- autorelease
{
	/*
	 * Cache OFAutoreleasePool since class lookups are expensive with the
	 * GNU runtime.
	 */
	if (autoreleasepool == Nil)
		autoreleasepool = [OFAutoreleasePool class];

	[autoreleasepool addObject: self];

	return self;
}

- (void)dealloc
{
	Class class;







|
|

|







783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800

- autorelease
{
	/*
	 * Cache OFAutoreleasePool since class lookups are expensive with the
	 * GNU runtime.
	 */
	if (autoreleasePool == Nil)
		autoreleasePool = [OFAutoreleasePool class];

	[autoreleasePool addObject: self];

	return self;
}

- (void)dealloc
{
	Class class;