Skip to content
This repository has been archived by the owner on Sep 18, 2023. It is now read-only.

Commit

Permalink
Move load_ufloat/sfloat to its own file
Browse files Browse the repository at this point in the history
Moves load_ufloat and load_sfloat from vr-format to its own file
called vr-small-float. That way the same code can be reused for
converting a half float to a double.
  • Loading branch information
bpeel committed Feb 19, 2019
1 parent 08280e6 commit 9b5b8bf
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 37 deletions.
1 change: 1 addition & 0 deletions Android.mk
Expand Up @@ -44,6 +44,7 @@ VKRUNNER_SRC_FILES := vkrunner/vr-allocate-store.c \
vkrunner/vr-requirements.c \
vkrunner/vr-result.c \
vkrunner/vr-script.c \
vkrunner/vr-small-float.c \
vkrunner/vr-source.c \
vkrunner/vr-stream.c \
vkrunner/vr-strtof.c \
Expand Down
2 changes: 2 additions & 0 deletions vkrunner/CMakeLists.txt
Expand Up @@ -52,6 +52,8 @@ set(VKRUNNER_SOURCE_FILES
vr-result.c
vr-script.c
vr-script-private.h
vr-small-float.c
vr-small-float.h
vr-source-private.h
vr-source.c
vr-stream.c
Expand Down
42 changes: 5 additions & 37 deletions vkrunner/vr-format.c
Expand Up @@ -30,9 +30,9 @@

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

#include "vr-format-table.h"
#include "vr-small-float.h"

const struct vr_format *
vr_format_lookup_by_name(const char *name)
Expand Down Expand Up @@ -112,39 +112,6 @@ sign_extend(uint32_t part, int bits)
return part;
}

static double
load_ufloat(uint32_t part,
int e_bits,
int m_bits)
{
int e_max = UINT32_MAX >> (32 - e_bits);
int e = (part >> m_bits) & e_max;
int m = part & (UINT32_MAX >> (32 - m_bits));

if (e == e_max)
return m == 0 ? INFINITY : NAN;

if (e == 0)
e = 1;
else
m += 1 << m_bits;

return ldexp(m / (double) (1 << m_bits), e - (e_max >> 1));
}

static double
load_sfloat(uint32_t part,
int e_bits,
int m_bits)
{
double res = load_ufloat(part, e_bits, m_bits);

if (res != NAN && (part & (1 << (e_bits + m_bits))))
res = -res;

return res;
}

static double
load_packed_part(uint32_t part,
int bits,
Expand All @@ -168,9 +135,9 @@ load_packed_part(uint32_t part,
case VR_FORMAT_MODE_UFLOAT:
switch (bits) {
case 10:
return load_ufloat(part, 5, 5);
return vr_small_float_load_unsigned(part, 5, 5);
case 11:
return load_ufloat(part, 5, 6);
return vr_small_float_load_unsigned(part, 5, 6);
default:
vr_fatal("unknown bit size in packed UFLOAT format");
}
Expand Down Expand Up @@ -273,7 +240,8 @@ load_part(int bits,
case VR_FORMAT_MODE_SFLOAT:
switch (bits) {
case 16:
return load_sfloat(*(uint16_t *) fb, 5, 10);
return vr_small_float_load_signed(*(uint16_t *) fb,
5, 10);
case 32:
return *(float *) fb;
case 64:
Expand Down
62 changes: 62 additions & 0 deletions vkrunner/vr-small-float.c
@@ -0,0 +1,62 @@
/*
* vkrunner
*
* Copyright (C) 2018, 2019 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

#include "config.h"

#include "vr-small-float.h"

double
vr_small_float_load_unsigned(uint32_t part,
int e_bits,
int m_bits)
{
int e_max = UINT32_MAX >> (32 - e_bits);
int e = (part >> m_bits) & e_max;
int m = part & (UINT32_MAX >> (32 - m_bits));

if (e == e_max)
return m == 0 ? INFINITY : NAN;

if (e == 0)
e = 1;
else
m += 1 << m_bits;

return ldexp(m / (double) (1 << m_bits), e - (e_max >> 1));
}

double
vr_small_float_load_signed(uint32_t part,
int e_bits,
int m_bits)
{
double res = vr_small_float_load_unsigned(part, e_bits, m_bits);

if (res != NAN && (part & (1 << (e_bits + m_bits))))
res = -res;

return res;
}

45 changes: 45 additions & 0 deletions vkrunner/vr-small-float.h
@@ -0,0 +1,45 @@
/*
* vkrunner
*
* Copyright (C) 2019 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

#ifndef VR_SMALL_FLOAT_H
#define VR_SMALL_FLOAT_H

#include "vr-small-float.h"

#include <stdint.h>
#include <math.h>
#include <inttypes.h>

double
vr_small_float_load_unsigned(uint32_t part,
int e_bits,
int m_bits);

double
vr_small_float_load_signed(uint32_t part,
int e_bits,
int m_bits);

#endif /* VR_SMALL_FLOAT_H */

0 comments on commit 9b5b8bf

Please sign in to comment.