Skip to content

Commit

Permalink
clone
Browse files Browse the repository at this point in the history
  • Loading branch information
yuichiis committed May 19, 2024
1 parent 689c0d2 commit 0ed8dd5
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 63 deletions.
70 changes: 52 additions & 18 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,69 @@ on:
- main

jobs:
no_ext_tests:
runs-on: ubuntu-22.04
tests:
runs-on: ${{ matrix.os }}

strategy:
fail-fast: true
matrix:
php: ['8.1','8.2','8.3']

include:
- name: macOS
os: macos-latest
php: '8.3'
- name: PHP8.1
os: ubuntu-latest
php: '8.1'
- name: PHP8.2
os: ubuntu-latest
php: '8.2'
- name: PHP8.3
os: ubuntu-latest
php: '8.3'
- name: Windows
os: windows-latest
php: '8.3'

steps:
- name: Setup PHP ${{ matrix.php }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
# PHP Extras
coverage: none
tools: composer, phpunit:10.5, phpstan
#ini-values: "memory_limit=512M"
extensions: ffi

- name: Checkout codes
uses: "actions/checkout@v4"

#- name: Composer
# uses: php-actions/composer@v6
# with:
# php_version: ${{ matrix.php }}
# php_extensions: ffi

- name: Composer
uses: php-actions/composer@v6
with:
php_version: ${{ matrix.php }}
php_extensions: ffi
run: composer update

#- name: PHP Static Analysys
# uses: php-actions/phpstan@v3
# with:
# php_version: ${{ matrix.php }}
# path: src/

- name: PHP Static Analysys
uses: php-actions/phpstan@v3
with:
php_version: ${{ matrix.php }}
path: src/
run: phpstan

#- name: PHPUnit Tests
# uses: php-actions/phpunit@v3
# with:
# configuration: tests/phpunit.xml
# version: 10.5
# php_version: ${{ matrix.php }}
# php_extensions: ffi

- name: PHPUnit Tests
uses: php-actions/phpunit@v3
with:
configuration: tests/phpunit.xml
version: 10.5
php_version: ${{ matrix.php }}
php_extensions: ffi
run: phpunit -c tests

5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ Requirements

How to setup
============

Install using composer.

```shell
$ composer require rindow/rindow-math-buffer-ffi


```
20 changes: 17 additions & 3 deletions src/Buffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use InvalidArgumentException;
use OutOfRangeException;
use LogicException;
use RuntimeException;
use FFI;

class complex_t {
Expand Down Expand Up @@ -66,10 +67,18 @@ class Buffer implements LinearBuffer

public function __construct(int $size, int $dtype)
{
if(self::$ffi===null) {
$code = file_get_contents(__DIR__.'/buffer.h');
if ($size <= 0) {
throw new InvalidArgumentException("Size must be positive");
}

if (self::$ffi === null) {
$code = @file_get_contents(__DIR__ . '/buffer.h');
if ($code === false) {
throw new RuntimeException("Unable to read buffer.h file");
}
self::$ffi = FFI::cdef($code);
}

if(!isset(self::$typeString[$dtype])) {
throw new InvalidArgumentException("Invalid data type");
}
Expand Down Expand Up @@ -103,7 +112,7 @@ protected function assertOffsetIsInt(string $method, mixed $offset) : void
protected function isComplex(int $dtype=null) : bool
{
$dtype = $dtype ?? $this->dtype;
return $dtype==NDArray::complex64||$dtype==NDArray::complex128;
return $dtype === NDArray::complex64 || $dtype === NDArray::complex128;
}

public function dtype() : int
Expand Down Expand Up @@ -186,4 +195,9 @@ public function load(string $string) : void
}
FFI::memcpy($this->data,$string,$byte);
}

public function __clone()
{
$this->data = clone $this->data;
}
}
88 changes: 88 additions & 0 deletions src/BufferNoHeader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
namespace Rindow\Math\Buffer\FFI;

use FFI;
use InvalidArgumentException;
use Interop\Polite\Math\Matrix\NDArray;

class BufferNoHeader extends Buffer
{
public function __construct(int $size, int $dtype)
{
if(parent::$ffi===null) {
parent::$ffi = FFI::cdef('');
}
if(!isset(parent::$typeString[$dtype])) {
throw new InvalidArgumentException("Invalid data type");
}
$limitsize = intdiv(parent::MAX_BYTES,parent::$valueSize[$dtype]);
if($size>=$limitsize) {
throw new InvalidArgumentException("Data size is too large.");
}
$this->size = $size;
$this->dtype = $dtype;
switch($dtype) {
case NDArray::complex64: {
$declaration = parent::$typeString[NDArray::float32];
$size *= 2;
break;
}
case NDArray::complex128: {
$declaration = parent::$typeString[NDArray::float64];
$size *= 2;
break;
}
default: {
$declaration = parent::$typeString[$dtype];
break;
}
}
$this->data = parent::$ffi->new("{$declaration}[{$size}]");
}

public function addr(int $offset) : FFI\CData
{
if($this->isComplex()) {
$offset *= 2;
}
return FFI::addr($this->data[$offset]);
}

public function offsetGet(mixed $offset): mixed
{
$this->assertOffset('offsetGet',$offset);
if($this->isComplex()) {
$offset *= 2;
$real = $this->data[$offset];
$imag = $this->data[$offset+1];
$value = (object)['real'=>$real,'imag'=>$imag];
} else {
$value = $this->data[$offset];
}
if($this->dtype===NDArray::bool) {
$value = $value ? true : false;
}
return $value;
}

public function offsetSet(mixed $offset, mixed $value): void
{
$this->assertOffset('offsetSet',$offset);
if($this->isComplex()) {
if(is_array($value)) {
[$real,$imag] = $value;
} elseif(is_object($value)) {
$real = $value->real;
$imag = $value->imag;
} else {
$type = gettype($value);
throw new InvalidArgumentException("Cannot convert to complex number.: ".$type);
}
$offset *= 2;
$this->data[$offset] = $real;
$this->data[$offset+1] = $imag;
} else {
$this->data[$offset] = $value;
}
}
}
Loading

0 comments on commit 0ed8dd5

Please sign in to comment.