okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
9258e4086
main
1// metal/include/metal_compat/cuda_fp16.h - the half-precision surface the dsv41 sources use, on a Mac. 2// 3// The engine touches exactly these (counted by compiling src/dsv41/*.cu against this directory, M5 Max 2026-10-23): 4// __half 16, __half2 11, __half_raw 3, __half_as_ushort 8, 5// __float2half_rn 17, __half2float 18, __half22float2 6, 6// the round-to-nearest float helpers (__fmul_rn, __fadd_rn, __fmaf_rn, __frcp_rn, __fsqrt_rn), 7// and the plain half/half2 typedefs ggml-common.h uses. 8// On Apple Silicon half is IEEE binary16 too, so the storage is a uint16_t and the conversions are the standard 9// round-to-nearest-even ones - the same bits CUDA produces. 10#pragma once 11 12#include "vector_types.h" 13 14#include <cstdint> 15#include <cstring> 16 17struct __half_raw { 18uint16_t x ; 19__half_raw ()= default ; 20 constexpr__half_raw (uint16_t bits ) :x (bits ) {} 21}; 22 23struct __half { 24uint16_t x ; 25__half ()= default ; 26__half (const __half_raw & r ) :x (r .x ) {} 27operator __half_raw ()const {return __half_raw {x }; } 28}; 29 30struct __half2 { 31uint16_t x ,y ; 32}; 33 34typedef __half half ; 35typedef __half2 half2 ; 36 37static inline uint16_t strata_f32_to_f16_bits (float f ) { 38uint32_t u ; 39std ::memcpy (& u ,& f ,sizeof u ); 40const uint32_t sign = (u >>16 )& 0x8000u ; 41const uint32_t mag = u & 0x7fffffffu ; 42if (mag >=0x7f800000u )return (uint16_t ) (sign | (mag > 0x7f800000u ?0x7e00u :0x7c00u )); 43if (mag >=0x477ff000u )return (uint16_t ) (sign |0x7c00u ); 44const uint32_t r = (mag + 0x0fffu + ((mag >>13 )& 1u )) >>13 ; 45if (r >=0x7c00u )return (uint16_t ) (sign |0x7c00u ); 46return (uint16_t ) (sign |r ); 47} 48 49static inline float strata_f16_bits_to_f32 (uint16_t h ) { 50const uint32_t sign = (uint32_t ) (h & 0x8000u ) <<16 ; 51const uint32_t e = (h >>10 )& 0x1fu ,m = h & 0x3ffu ; 52uint32_t bits ; 53if (e == 0 ) { 54if (m == 0 )bits = sign ; 55else { 56const float v = (float )m * (1.0f /16777216.0f ); 57return sign ?- v :v ; 58 } 59 }else if (e == 31 ) { 60bits = sign |0x7f800000u | (m <<13 ); 61 }else { 62bits = sign | ((e - 15 + 127 ) <<23 ) | (m <<13 ); 63 } 64float out ; 65std ::memcpy (& out ,& bits ,sizeof out ); 66return out ; 67} 68 69static inline __half __float2half_rn (float f ) {__half h ;h .x = strata_f32_to_f16_bits (f );return h ; } 70static inline __half __float2half (float f ) {return __float2half_rn (f ); } 71static inline float __half2float (__half h ) {return strata_f16_bits_to_f32 (h .x ); } 72static inline uint16_t __half_as_ushort (__half h ) {return h .x ; } 73static inline __half __ushort_as_half (uint16_t u ) {__half h ;h .x = u ;return h ; } 74static inline float2 __half22float2 (__half2 h ) {return make_float2 (strata_f16_bits_to_f32 (h .x ),strata_f16_bits_to_f32 (h .y )); } 75static inline __half2 __floats2half2_rn (float a ,float b ) { 76__half2 h ; 77h .x = strata_f32_to_f16_bits (a ); 78h .y = strata_f32_to_f16_bits (b ); 79return h ; 80} 81 82// round-to-nearest float helpers: the FP ops here are IEEE and un-fused, which is what these mean 83static inline float __fmul_rn (float a ,float b ) {return a * b ; } 84static inline float __fadd_rn (float a ,float b ) {return a + b ; } 85static inline float __fmaf_rn (float a ,float b ,float c ) {return a * b + c ; } 86static inline float __frcp_rn (float a ) {return 1.0f /a ; } 87static inline float __fsqrt_rn (float a ) {return __builtin_sqrtf (a ); } 88 89// the half2 pack/unpack and arithmetic helpers the trellis and vision paths use 90static inline __half2 __halves2half2 (__half lo ,__half hi ) {__half2 h ;h .x = lo .x ;h .y = hi .x ;return h ; } 91static inline __half2 __half2half2 (__half v ) {__half2 h ;h .x = v .x ;h .y = v .x ;return h ; } 92static inline __half __low2half (__half2 h ) {__half o ;o .x = h .x ;return o ; } 93static inline __half __high2half (__half2 h ) {__half o ;o .x = h .y ;return o ; } 94static inline float __low2float (__half2 h ) {return strata_f16_bits_to_f32 (h .x ); } 95static inline float __high2float (__half2 h ) {return strata_f16_bits_to_f32 (h .y ); } 96static inline __half2 __hadd2 (__half2 a ,__half2 b ) {return __floats2half2_rn (__half2float (__low2half (a ))+ __half2float (__low2half (b )),__half2float (__high2half (a ))+ __half2float (__high2half (b ))); } 97static inline __half2 __hmul2 (__half2 a ,__half2 b ) {return __floats2half2_rn (__half2float (__low2half (a ))* __half2float (__low2half (b )),__half2float (__high2half (a ))* __half2float (__high2half (b ))); } 98static inline __half2 __hfma2 (__half2 a ,__half2 b ,__half2 c ) {return __floats2half2_rn (__half2float (__low2half (a ))* __half2float (__low2half (b ))+ __half2float (__low2half (c )),__half2float (__high2half (a ))* __half2float (__high2half (b ))+ __half2float (__high2half (c ))); } 99static inline __half2 __hneg2 (__half2 a ) {__half2 h ;h .x = a .x ^0x8000u ;h .y = a .y ^0x8000u ;return h ; }